From 849393fe2ed50474e7b8c1bf1cef670a3f09530b Mon Sep 17 00:00:00 2001 From: LatvianModder Date: Fri, 14 Jun 2024 00:03:44 +0300 Subject: [PATCH] Some more component changes, re-added fluid stack and ingredient components, fixed item builder bug with DataComponents --- .../mods/kubejs/BuiltinKubeJSPlugin.java | 16 +-- .../kubejs/bindings/DataComponentWrapper.java | 9 ++ .../latvian/mods/kubejs/core/FluidKJS.java | 18 ++- .../mods/kubejs/core/FluidStackKJS.java | 12 +- .../latvian/mods/kubejs/fluid/FluidLike.java | 13 ++- .../mods/kubejs/fluid/FluidWrapper.java | 90 ++++++++++++++- .../latvian/mods/kubejs/item/ItemBuilder.java | 24 ++-- .../latvian/mods/kubejs/item/ItemStackJS.java | 39 +++---- .../mods/kubejs/recipe/KubeRecipe.java | 4 +- .../recipe/component/BlockComponent.java | 7 +- .../recipe/component/BlockStateComponent.java | 7 +- .../recipe/component/FluidComponents.java | 106 ------------------ .../component/FluidIngredientComponent.java | 59 ++++++++++ .../recipe/component/FluidStackComponent.java | 58 ++++++++++ .../recipe/component/IngredientComponent.java | 2 +- .../recipe/component/ItemStackComponent.java | 4 +- .../recipe/component/ListRecipeComponent.java | 15 +-- .../recipe/component/MapRecipeComponent.java | 15 +-- .../recipe/component/OrRecipeComponent.java | 10 +- .../recipe/component/RecipeComponent.java | 24 +--- .../component/RecipeComponentBuilder.java | 15 +-- .../component/RecipeComponentValue.java | 8 +- .../component/RecipeComponentWithParent.java | 9 +- .../component/SizedIngredientComponent.java | 2 +- src/main/resources/architectury.common.json | 12 ++ 25 files changed, 328 insertions(+), 250 deletions(-) delete mode 100644 src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidComponents.java create mode 100644 src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidIngredientComponent.java create mode 100644 src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidStackComponent.java diff --git a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java index b593e4253..e9bcfb4bc 100644 --- a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java +++ b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java @@ -92,6 +92,8 @@ import dev.latvian.mods.kubejs.recipe.component.BooleanComponent; import dev.latvian.mods.kubejs.recipe.component.CharacterComponent; import dev.latvian.mods.kubejs.recipe.component.EnumComponent; +import dev.latvian.mods.kubejs.recipe.component.FluidIngredientComponent; +import dev.latvian.mods.kubejs.recipe.component.FluidStackComponent; import dev.latvian.mods.kubejs.recipe.component.IngredientComponent; import dev.latvian.mods.kubejs.recipe.component.ItemStackComponent; import dev.latvian.mods.kubejs.recipe.component.MapRecipeComponent; @@ -149,6 +151,7 @@ import net.minecraft.core.Vec3i; import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentPatch; +import net.minecraft.core.component.DataComponentType; import net.minecraft.core.particles.ParticleOptions; import net.minecraft.core.registries.Registries; import net.minecraft.nbt.CollectionTag; @@ -189,6 +192,7 @@ import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.common.crafting.SizedIngredient; import net.neoforged.neoforge.fluids.FluidStack; +import net.neoforged.neoforge.fluids.crafting.FluidIngredient; import org.joml.Matrix3f; import org.joml.Matrix4f; import org.joml.Quaternionf; @@ -477,6 +481,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) { registry.register(CollectionTag.class, (from, target) -> NBTUtils.isTagCollection(from), NBTUtils::toTagCollection); registry.register(ListTag.class, (from, target) -> NBTUtils.isTagCollection(from), NBTUtils::toTagList); registry.register(Tag.class, NBTUtils::toTag); + registry.register(DataComponentType.class, DataComponentWrapper::wrapType); registry.register(DataComponentMap.class, DataComponentWrapper::mapOf); registry.register(DataComponentPatch.class, DataComponentWrapper::patchOf); @@ -511,6 +516,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) { registry.register(BlockStatePredicate.class, BlockStatePredicate::of); registry.register(RuleTest.class, BlockStatePredicate::ruleTestOf); registry.register(FluidStack.class, FluidWrapper::wrap); + registry.register(FluidIngredient.class, FluidWrapper::wrapIngredient); registry.register(RecipeFilter.class, RecipeFilter::of); registry.register(SlotFilter.class, SlotFilter::wrap); registry.register(Tier.class, ItemBuilder::toolTierOf); @@ -574,14 +580,8 @@ public void registerRecipeComponents(RecipeComponentFactoryRegistry registry) { registry.register("item_stack", ItemStackComponent.ITEM_STACK); registry.register("strict_item_stack", ItemStackComponent.STRICT_ITEM_STACK); - // event.register("inputFluid", FluidComponents.INPUT); - // event.register("inputFluidArray", FluidComponents.INPUT_ARRAY); - // event.register("inputFluidOrItem", FluidComponents.INPUT_OR_ITEM); - // event.register("inputFluidOrItemArray", FluidComponents.INPUT_OR_ITEM_ARRAY); - // event.register("outputFluid", FluidComponents.OUTPUT); - // event.register("outputFluidArray", FluidComponents.OUTPUT_ARRAY); - // event.register("outputFluidOrItem", FluidComponents.OUTPUT_OR_ITEM); - // event.register("outputFluidOrItemArray", FluidComponents.OUTPUT_OR_ITEM_ARRAY); + registry.register("fluid_stack", FluidStackComponent.FLUID_STACK); + registry.register("fluid_ingredient", FluidIngredientComponent.FLUID_INGREDIENT); registry.register("block", BlockComponent.BLOCK); diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java index 0e0c6ada7..136361fa0 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java @@ -8,6 +8,7 @@ import com.mojang.serialization.DynamicOps; import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.kubejs.util.Cast; +import dev.latvian.mods.kubejs.util.ID; import dev.latvian.mods.rhino.Context; import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentPatch; @@ -25,6 +26,14 @@ public interface DataComponentWrapper { Dynamic2CommandExceptionType ERROR_MALFORMED_COMPONENT = new Dynamic2CommandExceptionType((object, object2) -> Component.translatableEscape("arguments.item.component.malformed", object, object2)); SimpleCommandExceptionType ERROR_EXPECTED_COMPONENT = new SimpleCommandExceptionType(Component.translatable("arguments.item.component.expected")); + static DataComponentType wrapType(Object object) { + if (object instanceof DataComponentType) { + return (DataComponentType) object; + } + + return BuiltInRegistries.DATA_COMPONENT_TYPE.get(ID.mc(object)); + } + static DataComponentMap readMap(DynamicOps registryOps, StringReader reader) throws CommandSyntaxException { reader.skipWhitespace(); DataComponentMap.Builder builder = null; diff --git a/src/main/java/dev/latvian/mods/kubejs/core/FluidKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/FluidKJS.java index c7bc0f230..e04133ec5 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/FluidKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/FluidKJS.java @@ -1,12 +1,16 @@ package dev.latvian.mods.kubejs.core; +import dev.latvian.mods.kubejs.fluid.FluidLike; import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.RemapPrefixForJS; import net.minecraft.world.level.material.Fluid; +import net.minecraft.world.level.material.Fluids; +import net.neoforged.neoforge.fluids.FluidType; @RemapPrefixForJS("kjs$") -public interface FluidKJS extends RegistryObjectKJS { - default Fluid kjs$self() { +public interface FluidKJS extends RegistryObjectKJS, FluidLike { + @Override + default Fluid kjs$getFluid() { return (Fluid) this; } @@ -14,4 +18,14 @@ public interface FluidKJS extends RegistryObjectKJS { default RegistryInfo kjs$getKubeRegistry() { return RegistryInfo.FLUID; } + + @Override + default int kjs$getAmount() { + return FluidType.BUCKET_VOLUME; + } + + @Override + default boolean kjs$isEmpty() { + return kjs$getFluid().isSame(Fluids.EMPTY); + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java index 2818a3ae5..3e4fbf6e0 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java @@ -5,6 +5,7 @@ import dev.latvian.mods.kubejs.recipe.OutputReplacement; import dev.latvian.mods.kubejs.recipe.ReplacementMatch; import dev.latvian.mods.rhino.Context; +import net.minecraft.world.level.material.Fluid; import net.neoforged.neoforge.fluids.FluidStack; public interface FluidStackKJS extends OutputReplacement, FluidLike { @@ -13,7 +14,7 @@ public interface FluidStackKJS extends OutputReplacement, FluidLike { } @Override - default long kjs$getAmount() { + default int kjs$getAmount() { return kjs$self().getAmount(); } @@ -23,8 +24,13 @@ public interface FluidStackKJS extends OutputReplacement, FluidLike { } @Override - default FluidLike kjs$copy(long amount) { - return (FluidLike) (Object) kjs$self().copyWithAmount((int) amount); + default Fluid kjs$getFluid() { + return kjs$self().getFluid(); + } + + @Override + default FluidLike kjs$copy(int amount) { + return (FluidLike) (Object) kjs$self().copyWithAmount(amount); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidLike.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidLike.java index ef2f1e32f..b14ac7bed 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidLike.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidLike.java @@ -2,20 +2,23 @@ import dev.latvian.mods.kubejs.recipe.ReplacementMatch; import dev.latvian.mods.rhino.util.RemapPrefixForJS; +import net.minecraft.world.level.material.Fluid; @RemapPrefixForJS("kjs$") public interface FluidLike extends ReplacementMatch { - long kjs$getAmount(); + int kjs$getAmount(); + + Fluid kjs$getFluid(); default boolean kjs$isEmpty() { - return kjs$getAmount() <= 0L; + return kjs$getAmount() <= 0; } - default FluidLike kjs$copy(long amount) { + default FluidLike kjs$copy(int amount) { return this; } - default boolean matches(FluidLike other) { - return equals(other); + default boolean contains(FluidLike other) { + return kjs$getFluid().isSame(other.kjs$getFluid()); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidWrapper.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidWrapper.java index 0f949efb1..d72bd4640 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidWrapper.java @@ -1,23 +1,58 @@ package dev.latvian.mods.kubejs.fluid; +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.serialization.DynamicOps; +import dev.latvian.mods.kubejs.bindings.DataComponentWrapper; import dev.latvian.mods.kubejs.registry.RegistryInfo; +import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.type.TypeInfo; import net.minecraft.core.component.DataComponentMap; +import net.minecraft.nbt.Tag; import net.minecraft.resources.ResourceLocation; +import net.minecraft.util.Mth; import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.Fluids; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.FluidType; +import net.neoforged.neoforge.fluids.crafting.EmptyFluidIngredient; +import net.neoforged.neoforge.fluids.crafting.FluidIngredient; import java.util.ArrayList; import java.util.List; public interface FluidWrapper { TypeInfo TYPE_INFO = TypeInfo.of(FluidStack.class); + TypeInfo INGREDIENT_TYPE_INFO = TypeInfo.of(FluidIngredient.class); static FluidStack wrap(Context cx, Object o) { - return FluidStack.EMPTY; // FIXME + if (o == null || o == FluidStack.EMPTY || o == Fluids.EMPTY || o == EmptyFluidIngredient.INSTANCE) { + return FluidStack.EMPTY; + } else if (o instanceof FluidStack stack) { + return stack; + } else if (o instanceof Fluid fluid) { + return new FluidStack(fluid, FluidType.BUCKET_VOLUME); + } else if (o instanceof FluidIngredient in) { + return in.hasNoFluids() ? FluidStack.EMPTY : in.getStacks()[0]; + } else { + return ofString(((KubeJSContext) cx).getNbtOps(), o.toString()); + } + } + + static FluidIngredient wrapIngredient(Context cx, Object o) { + if (o == null || o == FluidStack.EMPTY || o == Fluids.EMPTY || o == EmptyFluidIngredient.INSTANCE) { + return EmptyFluidIngredient.INSTANCE; + } else if (o instanceof FluidStack stack) { + return FluidIngredient.of(stack); + } else if (o instanceof Fluid fluid) { + return FluidIngredient.of(fluid); + } else if (o instanceof FluidIngredient in) { + return in; + } else { + var stack = wrap(cx, o); + return stack.isEmpty() ? EmptyFluidIngredient.INSTANCE : FluidIngredient.of(stack); + } } static FluidStack of(FluidStack o) { @@ -81,4 +116,57 @@ static boolean exists(ResourceLocation id) { static ResourceLocation getId(Fluid fluid) { return RegistryInfo.FLUID.getId(fluid); } + + static FluidStack ofString(DynamicOps registryOps, String s) { + if (s.isEmpty() || s.equals("-") || s.equals("air") || s.equals("minecraft:air")) { + return FluidStack.EMPTY; + } else { + try { + var reader = new StringReader(s); + reader.skipWhitespace(); + + if (!reader.canRead()) { + return FluidStack.EMPTY; + } + + return read(registryOps, new StringReader(s)); + } catch (CommandSyntaxException ex) { + throw new RuntimeException(ex); + } + } + } + + static FluidStack read(DynamicOps registryOps, StringReader reader) throws CommandSyntaxException { + if (!reader.canRead()) { + return FluidStack.EMPTY; + } + + if (reader.peek() == '-') { + return FluidStack.EMPTY; + } + + int amount = 1; + + if (reader.canRead() && StringReader.isAllowedNumber(reader.peek())) { + amount = Mth.ceil(reader.readDouble()); + reader.skipWhitespace(); + reader.expect('x'); + reader.skipWhitespace(); + + if (amount < 1) { + throw new IllegalArgumentException("Item count smaller than 1 is not allowed!"); + } + } + + var fluidId = ResourceLocation.read(reader); + var fluidStack = new FluidStack(RegistryInfo.FLUID.getValue(fluidId), amount); + + var next = reader.canRead() ? reader.peek() : 0; + + if (next == '[' || next == '{') { + fluidStack.applyComponents(DataComponentWrapper.readPatch(registryOps, reader)); + } + + return fluidStack; + } } \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java index 3327f0e18..ff4404662 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java @@ -1,6 +1,7 @@ package dev.latvian.mods.kubejs.item; import com.google.gson.JsonObject; +import dev.latvian.mods.kubejs.bindings.DataComponentWrapper; import dev.latvian.mods.kubejs.bindings.ItemWrapper; import dev.latvian.mods.kubejs.color.Color; import dev.latvian.mods.kubejs.generator.AssetJsonGenerator; @@ -66,7 +67,7 @@ public static Tier toolTierOf(Object o) { return ItemBuilder.TOOL_TIERS.getOrDefault(ID.kjsString(asString), Tiers.IRON); } - public transient Map, Object> components; + public transient Map components; public transient int maxStackSize; public transient int maxDamage; public transient int burnTime; @@ -100,12 +101,12 @@ public static Tier toolTierOf(Object o) { public ItemBuilder(ResourceLocation i) { super(i); - maxStackSize = 64; + maxStackSize = -1; maxDamage = 0; burnTime = 0; containerItem = null; subtypes = null; - rarity = Rarity.COMMON; + rarity = null; glow = false; tooltip = new ArrayList<>(); textureJson = new JsonObject(); @@ -323,7 +324,6 @@ public ItemBuilder fireResistant(boolean isFireResistant) { return this; } - @Info("Makes the item fire resistant like netherite tools.") public ItemBuilder fireResistant() { return fireResistant(true); @@ -332,19 +332,27 @@ public ItemBuilder fireResistant() { public Item.Properties createItemProperties() { var properties = new KubeJSItemProperties(this); - if (components != null) { + if (components != null && !components.isEmpty()) { for (var entry : components.entrySet()) { - properties.component((DataComponentType) entry.getKey(), entry.getValue()); + var type = DataComponentWrapper.wrapType(entry.getKey()); + + if (type != null) { + properties.component((DataComponentType) type, entry.getValue()); + } else { + ConsoleJS.STARTUP.error("Component '" + entry.getKey() + "' not found for item " + id); + } } } if (maxDamage > 0) { properties.durability(maxDamage); - } else { + } else if (maxStackSize != -1) { properties.stacksTo(maxStackSize); } - properties.rarity(rarity); + if (rarity != null) { + properties.rarity(rarity); + } var item = containerItem == null ? Items.AIR : ItemWrapper.getItem(containerItem); diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ItemStackJS.java b/src/main/java/dev/latvian/mods/kubejs/item/ItemStackJS.java index 36393e581..2b6301160 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ItemStackJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ItemStackJS.java @@ -178,25 +178,6 @@ static ItemStack wrap(Context cx, @Nullable Object o) { return ItemStack.EMPTY; } - static ItemStack ofString(DynamicOps registryOps, String s) { - if (s.isEmpty() || s.equals("-") || s.equals("air") || s.equals("minecraft:air")) { - return ItemStack.EMPTY; - } else { - try { - var reader = new StringReader(s); - reader.skipWhitespace(); - - if (!reader.canRead()) { - return ItemStack.EMPTY; - } - - return read(registryOps, new StringReader(s)); - } catch (CommandSyntaxException ex) { - throw new RuntimeException(ex); - } - } - } - static Item getRawItem(Context cx, @Nullable Object o) { if (o == null) { return Items.AIR; @@ -215,6 +196,7 @@ static Item getRawItem(Context cx, @Nullable Object o) { } // Use ItemStackJS.of(object) + static ItemStack resultFromRecipeJson(DynamicOps registryOps, @Nullable JsonElement json) { if (json == null || json.isJsonNull()) { return ItemStack.EMPTY; @@ -243,6 +225,25 @@ static boolean isItemStackLike(Object from) { return from instanceof ItemStack; } + static ItemStack ofString(DynamicOps registryOps, String s) { + if (s.isEmpty() || s.equals("-") || s.equals("air") || s.equals("minecraft:air")) { + return ItemStack.EMPTY; + } else { + try { + var reader = new StringReader(s); + reader.skipWhitespace(); + + if (!reader.canRead()) { + return ItemStack.EMPTY; + } + + return read(registryOps, new StringReader(s)); + } catch (CommandSyntaxException ex) { + throw new RuntimeException(ex); + } + } + } + static ItemStack read(DynamicOps registryOps, StringReader reader) throws CommandSyntaxException { if (!reader.canRead()) { return ItemStack.EMPTY; diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java b/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java index 63a668cb7..33ec45b80 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java @@ -326,7 +326,7 @@ public final RecipeComponentValue[] outputValues() { @Override public boolean hasInput(Context cx, ReplacementMatch match) { for (var v : inputValues()) { - if (v.isInput(this, match)) { + if (v.matches(this, match)) { return true; } } @@ -352,7 +352,7 @@ public boolean replaceInput(Context cx, ReplacementMatch match, InputReplacement @Override public boolean hasOutput(Context cx, ReplacementMatch match) { for (var v : outputValues()) { - if (v.isOutput(this, match)) { + if (v.matches(this, match)) { return true; } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/BlockComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/BlockComponent.java index 2cc56f42a..ae2f840b6 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/BlockComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/BlockComponent.java @@ -38,12 +38,7 @@ public Block wrap(Context cx, KubeRecipe recipe, Object from) { } @Override - public boolean isInput(KubeRecipe recipe, Block value, ReplacementMatch match) { - return match instanceof BlockStatePredicate m2 && m2.testBlock(value); - } - - @Override - public boolean isOutput(KubeRecipe recipe, Block value, ReplacementMatch match) { + public boolean matches(KubeRecipe recipe, Block value, ReplacementMatch match) { return match instanceof BlockStatePredicate m2 && m2.testBlock(value); } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/BlockStateComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/BlockStateComponent.java index abbafa7c8..7af14fc98 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/BlockStateComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/BlockStateComponent.java @@ -55,12 +55,7 @@ public BlockState wrap(Context cx, KubeRecipe recipe, Object from) { } @Override - public boolean isInput(KubeRecipe recipe, BlockState value, ReplacementMatch match) { - return match instanceof BlockStatePredicate m2 && m2.test(value); - } - - @Override - public boolean isOutput(KubeRecipe recipe, BlockState value, ReplacementMatch match) { + public boolean matches(KubeRecipe recipe, BlockState value, ReplacementMatch match) { return match instanceof BlockStatePredicate m2 && m2.test(value); } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidComponents.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidComponents.java deleted file mode 100644 index 8aed651d3..000000000 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidComponents.java +++ /dev/null @@ -1,106 +0,0 @@ -package dev.latvian.mods.kubejs.recipe.component; - -public interface FluidComponents { - /* FIXME - RecipeComponent INPUT = new RecipeComponent<>() { - @Override - public ComponentRole role() { - return ComponentRole.INPUT; - } - - @Override - public Class componentClass() { - return InputFluid.class; - } - - @Override - public JsonElement write(RecipeJS recipe, InputFluid value) { - return recipe.writeInputFluid(value); - } - - @Override - public InputFluid read(RecipeJS recipe, Object from) { - return recipe.readInputFluid(from); - } - - @Override - public boolean hasPriority(RecipeJS recipe, Object from) { - return recipe.inputFluidHasPriority(from); - } - - @Override - public boolean isInput(RecipeJS recipe, InputFluid value, ReplacementMatch match) { - return match instanceof FluidLike m && value.matches(m); - } - - @Override - public String checkEmpty(RecipeKey key, InputFluid value) { - if (value.kjs$isEmpty()) { - return "Input fluid '" + key.name + "' can't be empty!"; - } - - return ""; - } - - @Override - public String toString() { - return "input_fluid"; - } - }; - - RecipeComponent INPUT_ARRAY = INPUT.asArray(); - RecipeComponent> INPUT_OR_ITEM = INPUT.or(ItemComponents.INPUT); - RecipeComponent[]> INPUT_OR_ITEM_ARRAY = INPUT_OR_ITEM.asArray(); - - RecipeComponent OUTPUT = new RecipeComponent<>() { - @Override - public ComponentRole role() { - return ComponentRole.OUTPUT; - } - - @Override - public Class componentClass() { - return OutputFluid.class; - } - - @Override - @Nullable - public JsonElement write(RecipeJS recipe, OutputFluid value) { - return recipe.writeOutputFluid(value); - } - - @Override - public OutputFluid read(RecipeJS recipe, Object from) { - return recipe.readOutputFluid(from); - } - - @Override - public boolean hasPriority(RecipeJS recipe, Object from) { - return recipe.outputFluidHasPriority(from); - } - - @Override - public boolean isOutput(RecipeJS recipe, OutputFluid value, ReplacementMatch match) { - return match instanceof FluidLike m && value.matches(m); - } - - @Override - public String checkEmpty(RecipeKey key, OutputFluid value) { - if (value.kjs$isEmpty()) { - return "Output fluid '" + key.name + "' can't be empty!"; - } - - return ""; - } - - @Override - public String toString() { - return "output_fluid"; - } - }; - - RecipeComponent OUTPUT_ARRAY = OUTPUT.asArray(); - RecipeComponent> OUTPUT_OR_ITEM = OUTPUT.or(ItemComponents.OUTPUT); - RecipeComponent[]> OUTPUT_OR_ITEM_ARRAY = OUTPUT_OR_ITEM.asArray(); - */ -} diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidIngredientComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidIngredientComponent.java new file mode 100644 index 000000000..81691fe61 --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidIngredientComponent.java @@ -0,0 +1,59 @@ +package dev.latvian.mods.kubejs.recipe.component; + +import com.mojang.serialization.Codec; +import dev.architectury.fluid.FluidStack; +import dev.latvian.mods.kubejs.fluid.FluidLike; +import dev.latvian.mods.kubejs.fluid.FluidWrapper; +import dev.latvian.mods.kubejs.recipe.KubeRecipe; +import dev.latvian.mods.kubejs.recipe.RecipeKey; +import dev.latvian.mods.kubejs.recipe.ReplacementMatch; +import dev.latvian.mods.kubejs.recipe.schema.RecipeSchema; +import dev.latvian.mods.rhino.Context; +import dev.latvian.mods.rhino.type.TypeInfo; +import net.minecraft.world.level.material.Fluid; +import net.neoforged.neoforge.fluids.crafting.FluidIngredient; +import org.jetbrains.annotations.Nullable; + +public class FluidIngredientComponent implements RecipeComponent { + public static final FluidIngredientComponent FLUID_INGREDIENT = new FluidIngredientComponent(); + + @Override + public Codec codec() { + return FluidIngredient.CODEC; + } + + @Override + public TypeInfo typeInfo() { + return FluidWrapper.INGREDIENT_TYPE_INFO; + } + + @Override + public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) { + return from instanceof FluidIngredient || from instanceof FluidStack || from instanceof Fluid; + } + + @Override + public boolean matches(KubeRecipe recipe, FluidIngredient value, ReplacementMatch match) { + return match instanceof FluidLike m && m.contains((FluidLike) value); + } + + @Override + public String checkEmpty(RecipeKey key, FluidIngredient value) { + if (value.isEmpty()) { + return "FluidIngredient '" + key.name + "' can't be empty!"; + } + + return ""; + } + + @Override + @Nullable + public String createUniqueId(FluidIngredient value) { + return value == null || value.isEmpty() || value.hasNoFluids() ? null : RecipeSchema.normalizeId(value.getStacks()[0].getFluid().kjs$getId()).replace('/', '_'); + } + + @Override + public String toString() { + return "fluid_stack"; + } +} diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidStackComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidStackComponent.java new file mode 100644 index 000000000..1b7ccaff9 --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/FluidStackComponent.java @@ -0,0 +1,58 @@ +package dev.latvian.mods.kubejs.recipe.component; + +import com.mojang.serialization.Codec; +import dev.architectury.fluid.FluidStack; +import dev.latvian.mods.kubejs.fluid.FluidLike; +import dev.latvian.mods.kubejs.fluid.FluidWrapper; +import dev.latvian.mods.kubejs.recipe.KubeRecipe; +import dev.latvian.mods.kubejs.recipe.RecipeKey; +import dev.latvian.mods.kubejs.recipe.ReplacementMatch; +import dev.latvian.mods.kubejs.recipe.schema.RecipeSchema; +import dev.latvian.mods.rhino.Context; +import dev.latvian.mods.rhino.type.TypeInfo; +import net.minecraft.world.level.material.Fluid; +import org.jetbrains.annotations.Nullable; + +public class FluidStackComponent implements RecipeComponent { + public static final FluidStackComponent FLUID_STACK = new FluidStackComponent(); + + @Override + public Codec codec() { + return FluidStack.CODEC; + } + + @Override + public TypeInfo typeInfo() { + return FluidWrapper.TYPE_INFO; + } + + @Override + public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) { + return from instanceof FluidStack || from instanceof Fluid; + } + + @Override + public boolean matches(KubeRecipe recipe, FluidStack value, ReplacementMatch match) { + return match instanceof FluidLike m && m.contains((FluidLike) (Object) value); + } + + @Override + public String checkEmpty(RecipeKey key, FluidStack value) { + if (value.isEmpty()) { + return "FluidStack '" + key.name + "' can't be empty!"; + } + + return ""; + } + + @Override + @Nullable + public String createUniqueId(FluidStack value) { + return value == null || value.isEmpty() ? null : RecipeSchema.normalizeId(value.getFluid().kjs$getId()).replace('/', '_'); + } + + @Override + public String toString() { + return "fluid_stack"; + } +} diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/IngredientComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/IngredientComponent.java index 09202bd56..576f7729c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/IngredientComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/IngredientComponent.java @@ -72,7 +72,7 @@ public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) { } @Override - public boolean isInput(KubeRecipe recipe, Ingredient value, ReplacementMatch match) { + public boolean matches(KubeRecipe recipe, Ingredient value, ReplacementMatch match) { return match instanceof ItemMatch m && m.contains(value); } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/ItemStackComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/ItemStackComponent.java index d67fd4383..5b0d83bda 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/ItemStackComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/ItemStackComponent.java @@ -40,7 +40,7 @@ public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) { } @Override - public boolean isOutput(KubeRecipe recipe, ItemStack value, ReplacementMatch match) { + public boolean matches(KubeRecipe recipe, ItemStack value, ReplacementMatch match) { return match instanceof ItemMatch m && !value.isEmpty() && m.contains(value); } @@ -61,6 +61,6 @@ public String createUniqueId(ItemStack value) { @Override public String toString() { - return "item_stack"; + return name; } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/ListRecipeComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/ListRecipeComponent.java index 23a88f130..163c608ea 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/ListRecipeComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/ListRecipeComponent.java @@ -99,9 +99,9 @@ public List wrap(Context cx, KubeRecipe recipe, Object from) { } @Override - public boolean isInput(KubeRecipe recipe, List value, ReplacementMatch match) { + public boolean matches(KubeRecipe recipe, List value, ReplacementMatch match) { for (var v : value) { - if (component.isInput(recipe, v, match)) { + if (component.matches(recipe, v, match)) { return true; } } @@ -130,17 +130,6 @@ public List replaceInput(Context cx, KubeRecipe recipe, List original, Rep return arr; } - @Override - public boolean isOutput(KubeRecipe recipe, List value, ReplacementMatch match) { - for (var v : value) { - if (component.isOutput(recipe, v, match)) { - return true; - } - } - - return false; - } - @Override public List replaceOutput(Context cx, KubeRecipe recipe, List original, ReplacementMatch match, OutputReplacement with) { var arr = original; diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/MapRecipeComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/MapRecipeComponent.java index e6670e01c..1bcd77bb4 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/MapRecipeComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/MapRecipeComponent.java @@ -86,9 +86,9 @@ public String checkEmpty(RecipeKey> key, TinyMap value) { } @Override - public boolean isInput(KubeRecipe recipe, TinyMap value, ReplacementMatch match) { + public boolean matches(KubeRecipe recipe, TinyMap value, ReplacementMatch match) { for (var entry : value.entries()) { - if (component.isInput(recipe, entry.value(), match)) { + if (component.matches(recipe, entry.value(), match)) { return true; } } @@ -115,17 +115,6 @@ public TinyMap replaceInput(Context cx, KubeRecipe recipe, TinyMap o return map; } - @Override - public boolean isOutput(KubeRecipe recipe, TinyMap value, ReplacementMatch match) { - for (var entry : value.entries()) { - if (component.isOutput(recipe, entry.value(), match)) { - return true; - } - } - - return false; - } - @Override public TinyMap replaceOutput(Context cx, KubeRecipe recipe, TinyMap original, ReplacementMatch match, OutputReplacement with) { var map = original; diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/OrRecipeComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/OrRecipeComponent.java index 5e321b80d..a6e053484 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/OrRecipeComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/OrRecipeComponent.java @@ -48,9 +48,9 @@ public Either wrap(Context cx, KubeRecipe recipe, Object from) { } @Override - public boolean isInput(KubeRecipe recipe, Either value, ReplacementMatch match) { + public boolean matches(KubeRecipe recipe, Either value, ReplacementMatch match) { var l = value.left(); - return l.isPresent() ? high.isInput(recipe, l.get(), match) : low.isInput(recipe, value.right().get(), match); + return l.isPresent() ? high.matches(recipe, l.get(), match) : low.matches(recipe, value.right().get(), match); } @Override @@ -66,12 +66,6 @@ public Either replaceInput(Context cx, KubeRecipe recipe, Either ori } } - @Override - public boolean isOutput(KubeRecipe recipe, Either value, ReplacementMatch match) { - var l = value.left(); - return l.isPresent() ? high.isOutput(recipe, l.get(), match) : low.isOutput(recipe, value.right().get(), match); - } - @Override public Either replaceOutput(Context cx, KubeRecipe recipe, Either original, ReplacementMatch match, OutputReplacement with) { var l = original.left(); diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponent.java index b54a02708..5ad8d3eb4 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponent.java @@ -170,37 +170,21 @@ default boolean hasPriority(Context cx, KubeRecipe recipe, Object from) { } /** - * Returns true if the given value is considered a valid input for this component - * that matches the given replacement match. - * * @param recipe The recipe object used for context * @param value The value to check * @param match The replacement match to check against - * @return Whether the given value is a matched input for this component + * @return true if the given value matches the given replacement match. */ - default boolean isInput(KubeRecipe recipe, T value, ReplacementMatch match) { + default boolean matches(KubeRecipe recipe, T value, ReplacementMatch match) { return false; } default T replaceInput(Context cx, KubeRecipe recipe, T original, ReplacementMatch match, InputReplacement with) { - return original instanceof InputReplacement r && isInput(recipe, original, match) ? wrap(cx, recipe, with.replaceInput(cx, recipe, match, r)) : original; - } - - /** - * Returns true if the given value is considered a valid output for this component - * that matches the given replacement match. - * - * @param recipe The recipe object used for context - * @param value The value to check - * @param match The replacement match to check against - * @return Whether the given value is a matched output for this component - */ - default boolean isOutput(KubeRecipe recipe, T value, ReplacementMatch match) { - return false; + return original instanceof InputReplacement r && matches(recipe, original, match) ? wrap(cx, recipe, with.replaceInput(cx, recipe, match, r)) : original; } default T replaceOutput(Context cx, KubeRecipe recipe, T original, ReplacementMatch match, OutputReplacement with) { - return original instanceof OutputReplacement r && isOutput(recipe, original, match) ? wrap(cx, recipe, with.replaceOutput(cx, recipe, match, r)) : original; + return original instanceof OutputReplacement r && matches(recipe, original, match) ? wrap(cx, recipe, with.replaceOutput(cx, recipe, match, r)) : original; } /** diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentBuilder.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentBuilder.java index fa5d02678..732b2750a 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentBuilder.java @@ -128,9 +128,9 @@ public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) { } @Override - public boolean isInput(KubeRecipe recipe, RecipeComponentBuilderMap value, ReplacementMatch match) { + public boolean matches(KubeRecipe recipe, RecipeComponentBuilderMap value, ReplacementMatch match) { for (var e : value.holders) { - if (e.isInput(recipe, match)) { + if (e.matches(recipe, match)) { return true; } } @@ -149,17 +149,6 @@ public RecipeComponentBuilderMap replaceInput(Context cx, KubeRecipe recipe, Rec return original; } - @Override - public boolean isOutput(KubeRecipe recipe, RecipeComponentBuilderMap value, ReplacementMatch match) { - for (var e : value.holders) { - if (e.isOutput(recipe, match)) { - return true; - } - } - - return false; - } - @Override public RecipeComponentBuilderMap replaceOutput(Context cx, KubeRecipe recipe, RecipeComponentBuilderMap original, ReplacementMatch match, OutputReplacement with) { for (var e : original.holders) { diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentValue.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentValue.java index 07eb778a1..c90c4df40 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentValue.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentValue.java @@ -33,8 +33,8 @@ public RecipeComponentValue copy() { return copy; } - public boolean isInput(KubeRecipe recipe, ReplacementMatch match) { - return value != null && key.role.isInput() && key.component.isInput(recipe, value, match); + public boolean matches(KubeRecipe recipe, ReplacementMatch match) { + return value != null && key.role.isInput() && key.component.matches(recipe, value, match); } public boolean replaceInput(Context cx, KubeRecipe recipe, ReplacementMatch match, InputReplacement with) { @@ -53,10 +53,6 @@ public boolean replaceInput(Context cx, KubeRecipe recipe, ReplacementMatch matc return false; } - public boolean isOutput(KubeRecipe recipe, ReplacementMatch match) { - return value != null && key.role.isOutput() && key.component.isOutput(recipe, value, match); - } - public boolean replaceOutput(Context cx, KubeRecipe recipe, ReplacementMatch match, OutputReplacement with) { if (!key.role.isOutput()) { return false; diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentWithParent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentWithParent.java index 4d307f245..849c2167e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentWithParent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RecipeComponentWithParent.java @@ -41,8 +41,8 @@ default boolean hasPriority(Context cx, KubeRecipe recipe, Object from) { } @Override - default boolean isInput(KubeRecipe recipe, T value, ReplacementMatch match) { - return parentComponent().isInput(recipe, value, match); + default boolean matches(KubeRecipe recipe, T value, ReplacementMatch match) { + return parentComponent().matches(recipe, value, match); } @Override @@ -50,11 +50,6 @@ default T replaceInput(Context cx, KubeRecipe recipe, T original, ReplacementMat return parentComponent().replaceInput(cx, recipe, original, match, with); } - @Override - default boolean isOutput(KubeRecipe recipe, T value, ReplacementMatch match) { - return parentComponent().isOutput(recipe, value, match); - } - @Override default T replaceOutput(Context cx, KubeRecipe recipe, T original, ReplacementMatch match, OutputReplacement with) { return parentComponent().replaceOutput(cx, recipe, original, match, with); diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/SizedIngredientComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/SizedIngredientComponent.java index ca732ef98..fc5099dc2 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/SizedIngredientComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/SizedIngredientComponent.java @@ -41,7 +41,7 @@ public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) { } @Override - public boolean isInput(KubeRecipe recipe, SizedIngredient value, ReplacementMatch match) { + public boolean matches(KubeRecipe recipe, SizedIngredient value, ReplacementMatch match) { return match instanceof ItemMatch m && m.contains(value.ingredient()); } diff --git a/src/main/resources/architectury.common.json b/src/main/resources/architectury.common.json index b0fe19f23..724730c31 100644 --- a/src/main/resources/architectury.common.json +++ b/src/main/resources/architectury.common.json @@ -71,6 +71,18 @@ ], "net/minecraft/class_3505": [ "dev/latvian/mods/kubejs/core/TagManagerKJS" + ], + "net/minecraft/class_3611": [ + "dev/latvian/mods/kubejs/core/FluidKJS" + ], + "net/minecraft/class_3917": [ + "dev/latvian/mods/kubejs/core/MenuTypeKJS" + ], + "net/minecraft/class_1299": [ + "dev/latvian/mods/kubejs/core/EntityTypeKJS" + ], + "net/minecraft/class_1887": [ + "dev/latvian/mods/kubejs/core/EnchantmentKJS" ] } } \ No newline at end of file