Skip to content

Commit

Permalink
Rework tab and tier creation to be delayed
Browse files Browse the repository at this point in the history
Signed-off-by: TheSilkMiner <thesilkminer@outlook.com>
  • Loading branch information
TheSilkMiner committed May 31, 2022
1 parent 443d10a commit d62f2bd
Show file tree
Hide file tree
Showing 24 changed files with 312 additions and 194 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.blamejared.contenttweaker.core.api;

import com.blamejared.contenttweaker.core.api.action.ContentTweakerAction;
import com.blamejared.crafttweaker.api.CraftTweakerAPI;
import com.google.common.base.Suppliers;

import java.util.Objects;
import java.util.ServiceLoader;
import java.util.function.Supplier;

Expand All @@ -14,6 +17,31 @@ public static ApiBridge get() {
return BRIDGE.get();
}

public static void apply(final ContentTweakerAction action) {
try {
CraftTweakerAPI.apply(action);
} catch (final UnsupportedOperationException e) {
if (e.getMessage() == null || !e.getMessage().contains("outside of a script run")) {
throw e;
}

// ContentTweaker does not support staged action as of now, which we require for some things like creative
// tab editing and modification and tool tiers... essentially everything that cannot be determined at script
// execution time. While we wait to find a proper solution in CraftTweaker, we manually log and apply the
// action
// TODO("Discuss this internally and find how to best approach staged actions as they are now required")
try {
if (!action.validate(CraftTweakerAPI.LOGGER)) {
return;
}
CraftTweakerAPI.LOGGER.info("ContentTweaker Staged: %s".formatted(Objects.requireNonNull(action.describe())));
action.apply();
} catch (final Exception ex) {
CraftTweakerAPI.LOGGER.error("Unable to execute a ContentTweaker staged action due to an error", ex);
}
}
}

private static ApiBridge find() {
return ServiceLoader.load(ApiBridge.class).findFirst().orElseThrow();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.blamejared.contenttweaker.core.api.object;

import com.blamejared.contenttweaker.core.api.ContentTweakerConstants;
import com.blamejared.contenttweaker.core.api.zen.ContentTweakerZenConstants;
import com.blamejared.crafttweaker.api.annotation.ZenRegister;
import org.openzen.zencode.java.ZenCodeType;

@ZenCodeType.Name(ContentTweakerZenConstants.RT_PACKAGE + ".Factory")
@ZenRegister
@ZenRegister(loaders = ContentTweakerConstants.CONTENT_LOADER_ID)
public interface ObjectFactory<T> {
ObjectType<T> type();
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
package com.blamejared.contenttweaker.vanilla.api.action.tab;

import com.blamejared.contenttweaker.core.api.action.ContentTweakerAction;
import com.blamejared.contenttweaker.vanilla.api.util.ContentTweakerCreativeTab;
import com.blamejared.contenttweaker.vanilla.api.zen.object.ItemReference;
import com.blamejared.contenttweaker.vanilla.api.zen.util.CreativeTab;
import org.apache.logging.log4j.Logger;
import com.blamejared.contenttweaker.vanilla.api.zen.util.CreativeTabReference;

import java.util.Objects;
import java.util.function.Consumer;

public final class SetCreativeTabIconAction implements ContentTweakerAction {
private final CreativeTab tab;
private final CreativeTabReference tab;
private final ItemReference icon;
private final Consumer<ItemReference> iconSetter;

private SetCreativeTabIconAction(final CreativeTab tab, final ItemReference icon) {
private SetCreativeTabIconAction(final CreativeTabReference tab, final ItemReference icon, final Consumer<ItemReference> iconSetter) {
this.tab = tab;
this.icon = icon;
this.iconSetter = iconSetter;
}

public static SetCreativeTabIconAction of(final CreativeTab tab, final ItemReference icon) {
return new SetCreativeTabIconAction(Objects.requireNonNull(tab), Objects.requireNonNull(icon));
public static SetCreativeTabIconAction of(final CreativeTabReference tab, final ItemReference icon, final Consumer<ItemReference> iconSetter) {
return new SetCreativeTabIconAction(Objects.requireNonNull(tab), Objects.requireNonNull(icon), Objects.requireNonNull(iconSetter));
}

@Override
public void apply() {
((ContentTweakerCreativeTab) this.tab.unwrap()).setDisplayItem(this.icon);
this.iconSetter.accept(this.icon);
}

@Override
public String describe() {
return "Setting icon of tab %s to %s".formatted(this.tab, this.icon.id());
}

@Override
public boolean validate(final Logger logger) {
if (!(this.tab.unwrap() instanceof ContentTweakerCreativeTab)) {
logger.error("Creative tab " + this.tab + " is not customizable");
return false;
}
return true;
return "Setting icon of tab %s to %s".formatted(this.tab.id(), this.icon.id());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.blamejared.contenttweaker.vanilla.api.action.tier;

import com.blamejared.contenttweaker.core.api.action.ContentTweakerAction;
import com.blamejared.contenttweaker.vanilla.api.zen.util.TierReference;

import java.util.Objects;

public final class CreateTierAction implements ContentTweakerAction {
private final TierReference reference;
private final Runnable creationAction;

private CreateTierAction(final TierReference reference, final Runnable creationAction) {
this.reference = reference;
this.creationAction = creationAction;
}

public static CreateTierAction of(final TierReference reference, final Runnable creationAction) {
return new CreateTierAction(Objects.requireNonNull(reference), Objects.requireNonNull(creationAction));
}

@Override
public void apply() {
this.creationAction.run();
}

@Override
public String describe() {
return "Creating a new tier named %s%s".formatted(this.reference.name(), this.reference.level() < 0? "" : " with level " + this.reference.level());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public final class AxeToolItemBuilder extends ToolItemBuilder<AxeToolItemBuilder> {
private static final class TotallyNotAnAxe extends AxeItem {
TotallyNotAnAxe(final ToolData data, final Supplier<Properties> properties) {
super(data.tier(), data.baseAttackDamage(), data.attackSpeed(), properties.get());
super(data.tier().get(), data.baseAttackDamage(), data.attackSpeed(), properties.get());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public final class HoeToolItemBuilder extends ToolItemBuilder<HoeToolItemBuilder> {
private static final class TotallyNotAHoe extends HoeItem {
TotallyNotAHoe(final ToolData data, final Supplier<Properties> properties) {
super(data.tier(), (int) data.baseAttackDamage(), data.attackSpeed(), properties.get());
super(data.tier().get(), (int) data.baseAttackDamage(), data.attackSpeed(), properties.get());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.blamejared.contenttweaker.vanilla.api.zen.object.property.FoodItemProperties;
import com.blamejared.contenttweaker.vanilla.api.zen.object.property.ItemProperties;
import com.blamejared.contenttweaker.vanilla.api.zen.object.property.StandardItemProperties;
import com.blamejared.contenttweaker.vanilla.api.zen.util.CreativeTab;
import com.blamejared.contenttweaker.vanilla.api.zen.util.CreativeTabReference;
import com.blamejared.crafttweaker.api.CraftTweakerAPI;
import com.blamejared.crafttweaker.api.annotation.ZenRegister;
import com.blamejared.crafttweaker.api.util.GenericUtil;
Expand All @@ -34,7 +34,7 @@ public abstract class ItemBuilder<T extends ItemBuilder<T>> {
private Integer maxStackSize;
private Integer maxDamage;
private ItemReference remainder;
private CreativeTab group;
private CreativeTabReference group;
private Rarity rarity;
private Boolean fireResistance;

Expand Down Expand Up @@ -115,7 +115,7 @@ public T craftRemainder(final ItemReference remainder) {
}

@ZenCodeType.Method("tab")
public T tab(final CreativeTab tab) {
public T tab(final CreativeTabReference tab) {
this.group = tab;
return this.self();
}
Expand Down Expand Up @@ -245,7 +245,7 @@ private void apply(
final Integer durability,
final Integer stack,
final ItemReference remainder,
final CreativeTab tab,
final CreativeTabReference tab,
final Rarity rarity,
final Boolean fire,
final Integer nutrition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public final class PickaxeToolItemBuilder extends ToolItemBuilder<PickaxeToolItemBuilder> {
private static final class TotallyNotAPickaxe extends PickaxeItem {
TotallyNotAPickaxe(final ToolData data, final Supplier<Properties> properties) {
super(data.tier(), (int) data.baseAttackDamage(), data.attackSpeed(), properties.get());
super(data.tier().get(), (int) data.baseAttackDamage(), data.attackSpeed(), properties.get());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public final class ShovelToolItemBuilder extends ToolItemBuilder<ShovelToolItemBuilder> {
private static final class TotallyNotAShovel extends ShovelItem {
TotallyNotAShovel(final ToolData data, final Supplier<Properties> properties) {
super(data.tier(), data.baseAttackDamage(), data.attackSpeed(), properties.get());
super(data.tier().get(), data.baseAttackDamage(), data.attackSpeed(), properties.get());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import com.blamejared.contenttweaker.vanilla.api.resource.PathHelper;
import com.blamejared.contenttweaker.vanilla.api.zen.ContentTweakerVanillaConstants;
import com.blamejared.contenttweaker.vanilla.api.zen.object.ItemReference;
import com.blamejared.contenttweaker.vanilla.api.zen.util.TierReference;
import com.blamejared.contenttweaker.vanilla.object.VanillaObjectTypes;
import com.blamejared.crafttweaker.api.annotation.ZenRegister;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.SwordItem;
import net.minecraft.world.item.Tier;
import org.openzen.zencode.java.ZenCodeType;

import java.util.Objects;
Expand All @@ -28,7 +28,7 @@
public final class SwordItemBuilder extends ItemBuilder<SwordItemBuilder> {
private Integer attackDamageBase;
private Float attackDamageSpeed;
private Tier tier;
private TierReference tier;

public SwordItemBuilder(final BiFunction<ObjectHolder<? extends Item>, Consumer<ResourceManager>, ItemReference> registrationManager) {
super(registrationManager);
Expand All @@ -50,7 +50,7 @@ public SwordItemBuilder attackSpeed(final float attackSpeed) {
}

@ZenCodeType.Method("tier")
public SwordItemBuilder tier(final Tier tier) {
public SwordItemBuilder tier(final TierReference tier) {
this.tier = Objects.requireNonNull(tier);
return this;
}
Expand All @@ -66,7 +66,7 @@ public ObjectHolder<? extends Item> create(final ResourceLocation name, final Su
if (this.attackDamageSpeed == null) {
throw new IllegalStateException("Unable to create a sword item without attack speed");
}
return ObjectHolder.of(VanillaObjectTypes.ITEM, name, () -> new SwordItem(this.tier, this.attackDamageBase, this.attackDamageSpeed, builtProperties.get()));
return ObjectHolder.of(VanillaObjectTypes.ITEM, name, () -> new SwordItem(this.tier.unwrap(), this.attackDamageBase, this.attackDamageSpeed, builtProperties.get()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.blamejared.contenttweaker.core.api.resource.ResourceManager;
import com.blamejared.contenttweaker.vanilla.api.zen.ContentTweakerVanillaConstants;
import com.blamejared.contenttweaker.vanilla.api.zen.object.ItemReference;
import com.blamejared.contenttweaker.vanilla.api.zen.util.TierReference;
import com.blamejared.crafttweaker.api.annotation.ZenRegister;
import com.blamejared.crafttweaker.api.util.GenericUtil;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -20,11 +21,11 @@
@ZenCodeType.Name(ContentTweakerVanillaConstants.ITEM_BUILDER_PACKAGE + ".ToolBuilder")
@ZenRegister(loaders = ContentTweakerConstants.CONTENT_LOADER_ID)
public abstract class ToolItemBuilder<T extends ToolItemBuilder<T>> extends ItemBuilder<T> {
protected record ToolData(Tier tier, float baseAttackDamage, float attackSpeed) {}
protected record ToolData(Supplier<Tier> tier, float baseAttackDamage, float attackSpeed) {}

private Float attackDamageBase;
private Float attackDamageSpeed;
private Tier tier;
private TierReference tier;

protected ToolItemBuilder(final BiFunction<ObjectHolder<? extends Item>, Consumer<ResourceManager>, ItemReference> registrationManager) {
super(registrationManager);
Expand All @@ -46,7 +47,7 @@ public T attackSpeed(final float attackSpeed) {
}

@ZenCodeType.Method("tier")
public T tier(final Tier tier) {
public T tier(final TierReference tier) {
this.tier = Objects.requireNonNull(tier);
return this.self();
}
Expand All @@ -62,7 +63,7 @@ public final ObjectHolder<? extends Item> create(final ResourceLocation name, fi
if (this.attackDamageSpeed == null) {
throw new IllegalStateException("Unable to create a tool item without attack speed");
}
return this.createTool(name, new ToolData(this.tier, this.attackDamageBase, this.attackDamageSpeed), builtProperties);
return this.createTool(name, new ToolData(this.tier::unwrap, this.attackDamageBase, this.attackDamageSpeed), builtProperties);
}

public abstract ObjectHolder<? extends Item> createTool(final ResourceLocation name, final ToolData toolData, final Supplier<Item.Properties> builtProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.blamejared.contenttweaker.core.api.ContentTweakerConstants;
import com.blamejared.contenttweaker.vanilla.api.zen.ContentTweakerVanillaConstants;
import com.blamejared.contenttweaker.vanilla.api.zen.object.ItemReference;
import com.blamejared.contenttweaker.vanilla.api.zen.util.CreativeTab;
import com.blamejared.contenttweaker.vanilla.api.zen.util.CreativeTabReference;
import com.blamejared.contenttweaker.vanilla.mixin.CreativeModeTabAccessor;
import com.blamejared.crafttweaker.api.annotation.ZenRegister;
import net.minecraft.core.Registry;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Rarity;
import org.openzen.zencode.java.ZenCodeType;

Expand Down Expand Up @@ -37,8 +39,13 @@ public ItemReference craftingRemainingItem() {
}

@ZenCodeType.Getter("category")
public CreativeTab category() {
return CreativeTab.wrap(this.resolve().getItemCategory());
@ZenCodeType.Nullable
public CreativeTabReference category() {
final CreativeModeTab tab = this.resolve().getItemCategory();
if (tab == null) {
return null;
}
return CreativeTabReference.of(((CreativeModeTabAccessor) tab).contenttweaker$langId(), () -> tab);
}

@ZenCodeType.Getter("rarity")
Expand Down

This file was deleted.

0 comments on commit d62f2bd

Please sign in to comment.