From 8c2381bafbd3a86b3ad5d9dca11933439048a5a3 Mon Sep 17 00:00:00 2001 From: Swedz Date: Sun, 27 Oct 2024 03:01:26 -0400 Subject: [PATCH] Add support for recipe conditions to have icons --- .../compat/viewer/usage/MachineCategory.java | 37 +++++++++++++++---- .../kubejs/recipe/CustomConditionEventJS.java | 7 +++- .../AdjacentBlockProcessCondition.java | 6 +++ .../condition/BiomeProcessCondition.java | 7 ++++ .../condition/CustomProcessCondition.java | 25 ++++++++++--- .../condition/DimensionProcessCondition.java | 7 ++++ .../condition/MachineProcessCondition.java | 5 +++ 7 files changed, 80 insertions(+), 14 deletions(-) diff --git a/src/client/java/aztech/modern_industrialization/compat/viewer/usage/MachineCategory.java b/src/client/java/aztech/modern_industrialization/compat/viewer/usage/MachineCategory.java index 05f5c0e0d..f08838c90 100644 --- a/src/client/java/aztech/modern_industrialization/compat/viewer/usage/MachineCategory.java +++ b/src/client/java/aztech/modern_industrialization/compat/viewer/usage/MachineCategory.java @@ -52,10 +52,10 @@ import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Style; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.RecipeHolder; import net.minecraft.world.item.crafting.RecipeManager; import net.minecraft.world.item.crafting.RecipeType; -import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.block.ComposterBlock; public class MachineCategory extends ViewerCategory> { @@ -258,15 +258,36 @@ public void buildWidgets(RecipeHolder recipeHolder, WidgetList wi // Conditions boolean conditionsRequired = recipe.conditions.size() > 0; if (steelHatchRequired || upgradeEuRequired > 0 || conditionsRequired) { - ItemLike displayedItem; + List displayedItems = new ArrayList<>(); if (steelHatchRequired) { - displayedItem = BuiltInRegistries.ITEM.get(MI.id("steel_item_input_hatch")); - } else if (conditionsRequired) { - displayedItem = MIItem.WRENCH; - } else { - displayedItem = MIItem.BASIC_UPGRADE; + displayedItems.add(BuiltInRegistries.ITEM.get(MI.id("steel_item_input_hatch")).getDefaultInstance()); + } + if (upgradeEuRequired > 0) { + displayedItems.add(MIItem.BASIC_UPGRADE.stack()); + } + for (var condition : recipe.conditions) { + ItemStack displayedItem = condition.icon(); + if (!displayedItem.isEmpty()) { + displayedItems.add(displayedItem); + } + } + if (displayedItems.isEmpty()) { + displayedItems.add(MIItem.WRENCH.stack()); } - widgets.item(width / 2f - 3, 3.75, 10.8, 10.8, displayedItem); + + double x = width / 2f - 3; + double y = 3.75; + double wh = 10.8; + widgets.drawable(graphics -> { + int itemIndex = (int) ((System.currentTimeMillis() / 1500L) % displayedItems.size()); + ItemStack displayedItem = displayedItems.get(itemIndex).copyWithCount(1); + + graphics.pose().pushPose(); + graphics.pose().translate(x, y, 0); + graphics.pose().scale((float) wh / 16, (float) wh / 16, 1); + graphics.renderFakeItem(displayedItem, 0, 0); + graphics.pose().popPose(); + }); } // Tooltips List tooltips = new ArrayList<>(); diff --git a/src/main/java/aztech/modern_industrialization/compat/kubejs/recipe/CustomConditionEventJS.java b/src/main/java/aztech/modern_industrialization/compat/kubejs/recipe/CustomConditionEventJS.java index ef73a26ed..9cad1acb7 100644 --- a/src/main/java/aztech/modern_industrialization/compat/kubejs/recipe/CustomConditionEventJS.java +++ b/src/main/java/aztech/modern_industrialization/compat/kubejs/recipe/CustomConditionEventJS.java @@ -29,9 +29,14 @@ import dev.latvian.mods.kubejs.event.KubeEvent; import java.util.function.BiPredicate; import net.minecraft.network.chat.Component; +import net.minecraft.world.item.ItemStack; public class CustomConditionEventJS implements KubeEvent { + public void register(String id, BiPredicate predicate, ItemStack icon, Component... description) { + CustomProcessCondition.register(id, predicate, icon, description); + } + public void register(String id, BiPredicate predicate, Component... description) { - CustomProcessCondition.register(id, predicate, description); + register(id, predicate, ItemStack.EMPTY, description); } } diff --git a/src/main/java/aztech/modern_industrialization/machines/recipe/condition/AdjacentBlockProcessCondition.java b/src/main/java/aztech/modern_industrialization/machines/recipe/condition/AdjacentBlockProcessCondition.java index 5b5780bd1..5d4e8581b 100644 --- a/src/main/java/aztech/modern_industrialization/machines/recipe/condition/AdjacentBlockProcessCondition.java +++ b/src/main/java/aztech/modern_industrialization/machines/recipe/condition/AdjacentBlockProcessCondition.java @@ -36,6 +36,7 @@ import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.util.StringRepresentable; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.Block; public record AdjacentBlockProcessCondition(Block block, RelativePosition relativePosition) implements MachineProcessCondition { @@ -76,6 +77,11 @@ public void appendDescription(List list) { list.add(text.text(block.getName())); } + @Override + public ItemStack icon() { + return block.asItem().getDefaultInstance(); + } + @Override public MapCodec codec() { return CODEC; diff --git a/src/main/java/aztech/modern_industrialization/machines/recipe/condition/BiomeProcessCondition.java b/src/main/java/aztech/modern_industrialization/machines/recipe/condition/BiomeProcessCondition.java index 515b3f44d..fd9194dbf 100644 --- a/src/main/java/aztech/modern_industrialization/machines/recipe/condition/BiomeProcessCondition.java +++ b/src/main/java/aztech/modern_industrialization/machines/recipe/condition/BiomeProcessCondition.java @@ -32,6 +32,8 @@ import net.minecraft.network.chat.Component; import net.minecraft.network.codec.StreamCodec; import net.minecraft.resources.ResourceKey; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; import net.minecraft.world.level.biome.Biome; public record BiomeProcessCondition(ResourceKey biome) implements MachineProcessCondition { @@ -56,6 +58,11 @@ public void appendDescription(List list) { list.add(MIText.RequiresBiome.text(biomeComponent)); } + @Override + public ItemStack icon() { + return Items.OAK_SAPLING.getDefaultInstance(); + } + @Override public MapCodec codec() { return CODEC; diff --git a/src/main/java/aztech/modern_industrialization/machines/recipe/condition/CustomProcessCondition.java b/src/main/java/aztech/modern_industrialization/machines/recipe/condition/CustomProcessCondition.java index 579e9182c..081f57670 100644 --- a/src/main/java/aztech/modern_industrialization/machines/recipe/condition/CustomProcessCondition.java +++ b/src/main/java/aztech/modern_industrialization/machines/recipe/condition/CustomProcessCondition.java @@ -39,6 +39,7 @@ import net.minecraft.network.chat.ComponentSerialization; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; +import net.minecraft.world.item.ItemStack; public class CustomProcessCondition implements MachineProcessCondition { static final Map definitions = new HashMap<>(); @@ -49,8 +50,10 @@ public static void onReload() { KubeJSProxy.instance.fireCustomConditionEvent(); } - public static void register(String id, BiPredicate predicate, Component... description) { + public static void register(String id, BiPredicate predicate, ItemStack icon, + Component... description) { Objects.requireNonNull(predicate); + Objects.requireNonNull(icon); if (definitions.containsKey(id)) { throw new IllegalArgumentException("Duplicate custom process condition definition: " + id); @@ -60,12 +63,12 @@ public static void register(String id, BiPredicate predicate, - List description) { + ItemStack icon, List description) { } private static MapCodec makeCodec(boolean syncToClient) { @@ -73,15 +76,19 @@ private static MapCodec makeCodec(boolean syncToClient) g -> g .group( Codec.STRING.fieldOf("custom_id").forGetter(c -> c.id), + ItemStack.OPTIONAL_CODEC.fieldOf("icon").forGetter(c -> c.icon), ComponentSerialization.CODEC.listOf().optionalFieldOf("description") .forGetter(c -> syncToClient ? Optional.of(c.description) : Optional.empty())) - .apply(g, (id, desc) -> desc.map(d -> new CustomProcessCondition(id, d)).orElseGet(() -> new CustomProcessCondition(id)))); + .apply(g, (id, icon, desc) -> desc.map(d -> new CustomProcessCondition(id, icon, d)) + .orElseGet(() -> new CustomProcessCondition(id)))); } static final MapCodec CODEC = Codec.STRING.fieldOf("custom_id").xmap(CustomProcessCondition::new, c -> c.id); static final StreamCodec STREAM_CODEC = StreamCodec.composite( ByteBufCodecs.STRING_UTF8, c -> c.id, + ItemStack.OPTIONAL_STREAM_CODEC, + c -> c.icon, ComponentSerialization.TRUSTED_STREAM_CODEC.apply(ByteBufCodecs.list()), c -> c.description, CustomProcessCondition::new); @@ -92,15 +99,18 @@ public CustomProcessCondition(String id) { throw new IllegalArgumentException("Unknown custom process condition definition: " + id); } this.id = id; + this.icon = definition.icon; this.description = definition.description; } - public CustomProcessCondition(String id, List description) { + public CustomProcessCondition(String id, ItemStack icon, List description) { this.id = id; + this.icon = icon; this.description = description; } private final String id; + private final ItemStack icon; private final List description; @Override @@ -113,6 +123,11 @@ public void appendDescription(List list) { list.addAll(description); } + @Override + public ItemStack icon() { + return icon; + } + @Override public MapCodec codec() { return CODEC; diff --git a/src/main/java/aztech/modern_industrialization/machines/recipe/condition/DimensionProcessCondition.java b/src/main/java/aztech/modern_industrialization/machines/recipe/condition/DimensionProcessCondition.java index 751547510..017fbd9b0 100644 --- a/src/main/java/aztech/modern_industrialization/machines/recipe/condition/DimensionProcessCondition.java +++ b/src/main/java/aztech/modern_industrialization/machines/recipe/condition/DimensionProcessCondition.java @@ -23,6 +23,7 @@ */ package aztech.modern_industrialization.machines.recipe.condition; +import aztech.modern_industrialization.MIItem; import aztech.modern_industrialization.MIText; import aztech.modern_industrialization.machines.recipe.MachineRecipe; import com.mojang.serialization.MapCodec; @@ -32,6 +33,7 @@ import net.minecraft.network.chat.Component; import net.minecraft.network.codec.StreamCodec; import net.minecraft.resources.ResourceKey; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; public record DimensionProcessCondition(ResourceKey dimension) implements MachineProcessCondition { @@ -56,6 +58,11 @@ public void appendDescription(List list) { list.add(MIText.RequiresDimension.text(dimComponent)); } + @Override + public ItemStack icon() { + return MIItem.SINGULARITY.asItem().getDefaultInstance(); + } + @Override public MapCodec codec() { return CODEC; diff --git a/src/main/java/aztech/modern_industrialization/machines/recipe/condition/MachineProcessCondition.java b/src/main/java/aztech/modern_industrialization/machines/recipe/condition/MachineProcessCondition.java index dedb41be0..32e7de1b3 100644 --- a/src/main/java/aztech/modern_industrialization/machines/recipe/condition/MachineProcessCondition.java +++ b/src/main/java/aztech/modern_industrialization/machines/recipe/condition/MachineProcessCondition.java @@ -35,6 +35,7 @@ import net.minecraft.network.codec.StreamCodec; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.item.ItemStack; public interface MachineProcessCondition { Codec CODEC = ResourceLocation.CODEC @@ -60,6 +61,10 @@ public interface MachineProcessCondition { void appendDescription(List list); + default ItemStack icon() { + return ItemStack.EMPTY; + } + MapCodec codec(); StreamCodec streamCodec();