diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/match/FluidIngredientMatch.java b/src/main/java/dev/latvian/mods/kubejs/core/FluidIngredientKJS.java similarity index 50% rename from src/main/java/dev/latvian/mods/kubejs/recipe/match/FluidIngredientMatch.java rename to src/main/java/dev/latvian/mods/kubejs/core/FluidIngredientKJS.java index 3211c7928..527082df4 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/match/FluidIngredientMatch.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/FluidIngredientKJS.java @@ -1,24 +1,27 @@ -package dev.latvian.mods.kubejs.recipe.match; +package dev.latvian.mods.kubejs.core; import dev.latvian.mods.kubejs.error.KubeRuntimeException; +import dev.latvian.mods.kubejs.recipe.match.FluidMatch; import dev.latvian.mods.rhino.Context; +import dev.latvian.mods.rhino.util.RemapPrefixForJS; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.crafting.FluidIngredient; -public record FluidIngredientMatch(FluidIngredient ingredient) implements FluidMatch { +@RemapPrefixForJS("kjs$") +public interface FluidIngredientKJS extends FluidMatch { @Override - public boolean matches(Context cx, FluidStack s, boolean exact) { - return !s.isEmpty() && ingredient.test(s); + default boolean matches(Context cx, FluidStack s, boolean exact) { + return !s.isEmpty() && ((FluidIngredient) this).test(s); } @Override - public boolean matches(Context cx, FluidIngredient in, boolean exact) { + default boolean matches(Context cx, FluidIngredient in, boolean exact) { if (in == FluidIngredient.empty()) { return false; } try { - for (var stack : ingredient.getStacks()) { + for (var stack : ((FluidIngredient) this).getStacks()) { if (in.test(stack)) { return true; } @@ -29,9 +32,4 @@ public boolean matches(Context cx, FluidIngredient in, boolean exact) { return false; } - - @Override - public String toString() { - return ingredient.toString(); - } } 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 f5ab5a6a9..1c1b0c6c3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java @@ -2,13 +2,15 @@ import dev.latvian.mods.kubejs.fluid.FluidLike; import dev.latvian.mods.kubejs.fluid.FluidWrapper; +import dev.latvian.mods.kubejs.recipe.match.FluidMatch; import dev.latvian.mods.kubejs.recipe.match.Replaceable; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.world.level.material.Fluid; import net.neoforged.neoforge.fluids.FluidStack; +import net.neoforged.neoforge.fluids.crafting.FluidIngredient; -public interface FluidStackKJS extends Replaceable, FluidLike { +public interface FluidStackKJS extends Replaceable, FluidLike, FluidMatch { default FluidStack kjs$self() { return (FluidStack) (Object) this; } @@ -45,4 +47,14 @@ default Object replaceThisWith(Context cx, Object with) { return this; } + + @Override + default boolean matches(Context cx, FluidStack s, boolean exact) { + return kjs$self().getFluid() == s.getFluid(); + } + + @Override + default boolean matches(Context cx, FluidIngredient ingredient, boolean exact) { + return ingredient.test(kjs$self()); + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java index 2756a8c22..971cf8579 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java @@ -1,10 +1,13 @@ package dev.latvian.mods.kubejs.core; import com.mojang.serialization.Codec; +import dev.latvian.mods.kubejs.bindings.IngredientWrapper; import dev.latvian.mods.kubejs.bindings.SizedIngredientWrapper; +import dev.latvian.mods.kubejs.error.KubeRuntimeException; import dev.latvian.mods.kubejs.ingredient.WildcardIngredient; import dev.latvian.mods.kubejs.item.ItemPredicate; import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; +import dev.latvian.mods.kubejs.recipe.match.ItemMatch; import dev.latvian.mods.kubejs.recipe.match.Replaceable; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.kubejs.util.WithCodec; @@ -18,7 +21,7 @@ import net.neoforged.neoforge.common.crafting.SizedIngredient; @RemapPrefixForJS("kjs$") -public interface IngredientKJS extends ItemPredicate, Replaceable, WithCodec { +public interface IngredientKJS extends ItemPredicate, Replaceable, WithCodec, ItemMatch { default Ingredient kjs$self() { throw new NoMixinException(); } @@ -78,4 +81,46 @@ default Object replaceThisWith(Context cx, Object with) { return this; } + + @Override + default boolean matches(Context cx, ItemStack item, boolean exact) { + if (item.isEmpty()) { + return false; + } else if (exact) { + var stacks = kjs$getStacks(); + return stacks.size() == 1 && ItemStack.isSameItemSameComponents(stacks.getFirst(), item); + } else { + return test(item); + } + } + + @Override + default boolean matches(Context cx, Ingredient in, boolean exact) { + if (in == Ingredient.EMPTY) { + return false; + } + + if (exact) { + var t1 = IngredientWrapper.tagKeyOf(kjs$self()); + var t2 = IngredientWrapper.tagKeyOf(in); + + if (t1 != null && t2 != null) { + return t1 == t2; + } else { + return equals(in); + } + } + + try { + for (var stack : in.getItems()) { + if (test(stack)) { + return true; + } + } + } catch (Exception ex) { + throw new KubeRuntimeException("Failed to test ingredient " + in, ex); + } + + return false; + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java index 6d46ed134..c6a353590 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java @@ -6,6 +6,7 @@ import dev.latvian.mods.kubejs.item.ChancedItem; import dev.latvian.mods.kubejs.item.ItemStackJS; import dev.latvian.mods.kubejs.level.BlockContainerJS; +import dev.latvian.mods.kubejs.recipe.match.ItemMatch; import dev.latvian.mods.kubejs.recipe.match.Replaceable; import dev.latvian.mods.kubejs.util.ID; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; @@ -33,6 +34,7 @@ import net.minecraft.world.item.enchantment.Enchantment; import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.item.enchantment.ItemEnchantments; +import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import org.jetbrains.annotations.Nullable; @@ -49,6 +51,7 @@ public interface ItemStackKJS extends ToStringJS, Replaceable, MutableDataComponentHolderKJS, + ItemMatch, RegistryObjectKJS { default ItemStack kjs$self() { return (ItemStack) this; @@ -275,4 +278,19 @@ default Object replaceThisWith(Context cx, Object with) { return this; } + + @Override + default boolean matches(Context cx, ItemStack s, boolean exact) { + return kjs$self().getItem() == s.getItem(); + } + + @Override + default boolean matches(Context cx, Ingredient in, boolean exact) { + return in.test(kjs$self()); + } + + @Override + default boolean matches(Context cx, ItemLike itemLike, boolean exact) { + return kjs$self().getItem() == itemLike.asItem(); + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/SizedFluidIngredientKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/SizedFluidIngredientKJS.java new file mode 100644 index 000000000..b7b03e488 --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/core/SizedFluidIngredientKJS.java @@ -0,0 +1,39 @@ +package dev.latvian.mods.kubejs.core; + +import dev.latvian.mods.kubejs.fluid.FluidWrapper; +import dev.latvian.mods.kubejs.recipe.match.FluidMatch; +import dev.latvian.mods.kubejs.recipe.match.Replaceable; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; +import dev.latvian.mods.rhino.Context; +import dev.latvian.mods.rhino.util.RemapPrefixForJS; +import net.neoforged.neoforge.fluids.FluidStack; +import net.neoforged.neoforge.fluids.crafting.FluidIngredient; +import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient; + +@RemapPrefixForJS("kjs$") +public interface SizedFluidIngredientKJS extends Replaceable, FluidMatch { + default SizedFluidIngredient kjs$self() { + return (SizedFluidIngredient) (Object) this; + } + + @Override + default Object replaceThisWith(Context cx, Object with) { + var ingredient = FluidWrapper.wrapIngredient(RegistryAccessContainer.of(cx), with); + + if (!ingredient.equals(kjs$self().ingredient())) { + return new SizedFluidIngredient(ingredient, kjs$self().amount()); + } + + return this; + } + + @Override + default boolean matches(Context cx, FluidStack s, boolean exact) { + return ((FluidMatch) kjs$self().ingredient()).matches(cx, s, exact); + } + + @Override + default boolean matches(Context cx, FluidIngredient in, boolean exact) { + return ((FluidMatch) kjs$self().ingredient()).matches(cx, in, exact); + } +} diff --git a/src/main/java/dev/latvian/mods/kubejs/core/SizedIngredientKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/SizedIngredientKJS.java index 18f90159f..ecf79c5cb 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/SizedIngredientKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/SizedIngredientKJS.java @@ -1,13 +1,15 @@ package dev.latvian.mods.kubejs.core; import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; +import dev.latvian.mods.kubejs.recipe.match.ItemMatch; import dev.latvian.mods.kubejs.recipe.match.Replaceable; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Ingredient; import net.neoforged.neoforge.common.crafting.SizedIngredient; -public interface SizedIngredientKJS extends Replaceable, IngredientSupplierKJS { +public interface SizedIngredientKJS extends Replaceable, IngredientSupplierKJS, ItemMatch { default SizedIngredient kjs$self() { return (SizedIngredient) (Object) this; } @@ -27,4 +29,14 @@ default Object replaceThisWith(Context cx, Object with) { default Ingredient kjs$asIngredient() { return kjs$self().ingredient(); } + + @Override + default boolean matches(Context cx, ItemStack item, boolean exact) { + return kjs$self().ingredient().matches(cx, item, exact); + } + + @Override + default boolean matches(Context cx, Ingredient in, boolean exact) { + return kjs$self().ingredient().matches(cx, in, exact); + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/FluidIngredientMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/FluidIngredientMixin.java new file mode 100644 index 000000000..e98d43094 --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/FluidIngredientMixin.java @@ -0,0 +1,11 @@ +package dev.latvian.mods.kubejs.core.mixin; + +import dev.latvian.mods.kubejs.core.FluidIngredientKJS; +import dev.latvian.mods.rhino.util.RemapPrefixForJS; +import net.neoforged.neoforge.fluids.crafting.FluidIngredient; +import org.spongepowered.asm.mixin.Mixin; + +@Mixin(value = FluidIngredient.class, remap = false) +@RemapPrefixForJS("kjs$") +public abstract class FluidIngredientMixin implements FluidIngredientKJS { +} diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/SizedFluidIngredientMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/SizedFluidIngredientMixin.java new file mode 100644 index 000000000..e8ffd9bb1 --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/SizedFluidIngredientMixin.java @@ -0,0 +1,11 @@ +package dev.latvian.mods.kubejs.core.mixin; + +import dev.latvian.mods.kubejs.core.SizedFluidIngredientKJS; +import dev.latvian.mods.rhino.util.RemapPrefixForJS; +import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient; +import org.spongepowered.asm.mixin.Mixin; + +@Mixin(value = SizedFluidIngredient.class, remap = false) +@RemapPrefixForJS("kjs$") +public abstract class SizedFluidIngredientMixin implements SizedFluidIngredientKJS { +} diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/match/IngredientMatch.java b/src/main/java/dev/latvian/mods/kubejs/recipe/match/IngredientMatch.java deleted file mode 100644 index 4aff0ce79..000000000 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/match/IngredientMatch.java +++ /dev/null @@ -1,56 +0,0 @@ -package dev.latvian.mods.kubejs.recipe.match; - -import dev.latvian.mods.kubejs.bindings.IngredientWrapper; -import dev.latvian.mods.kubejs.error.KubeRuntimeException; -import dev.latvian.mods.rhino.Context; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.crafting.Ingredient; - -public record IngredientMatch(Ingredient ingredient) implements ItemMatch { - @Override - public boolean matches(Context cx, ItemStack item, boolean exact) { - if (item.isEmpty()) { - return false; - } else if (exact) { - var stacks = ingredient.kjs$getStacks(); - return stacks.size() == 1 && ItemStack.isSameItemSameComponents(stacks.getFirst(), item); - } else { - return ingredient.test(item); - } - } - - @Override - public boolean matches(Context cx, Ingredient in, boolean exact) { - if (in == Ingredient.EMPTY) { - return false; - } - - if (exact) { - var t1 = IngredientWrapper.tagKeyOf(ingredient); - var t2 = IngredientWrapper.tagKeyOf(in); - - if (t1 != null && t2 != null) { - return t1 == t2; - } else { - return ingredient.equals(in); - } - } - - try { - for (var stack : in.getItems()) { - if (ingredient.test(stack)) { - return true; - } - } - } catch (Exception ex) { - throw new KubeRuntimeException("Failed to test ingredient " + in, ex); - } - - return false; - } - - @Override - public String toString() { - return ingredient.toString(); - } -} diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/match/ItemMatch.java b/src/main/java/dev/latvian/mods/kubejs/recipe/match/ItemMatch.java index a2b69770f..f7e479309 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/match/ItemMatch.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/match/ItemMatch.java @@ -5,22 +5,12 @@ import net.minecraft.world.item.Items; import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.level.ItemLike; -import net.minecraft.world.level.block.Block; public interface ItemMatch extends ReplacementMatch { boolean matches(Context cx, ItemStack item, boolean exact); boolean matches(Context cx, Ingredient in, boolean exact); - /** - * @deprecated This method is fully contained in {@link #matches(Context, ItemLike, boolean)}. - */ - @Deprecated(forRemoval = true) - default boolean matches(Context cx, Block block, boolean exact) { - var item = block.asItem(); - return item != Items.AIR && matches(cx, item.getDefaultInstance(), exact); - } - default boolean matches(Context cx, ItemLike itemLike, boolean exact) { var item = itemLike.asItem(); return item != Items.AIR && matches(cx, item.getDefaultInstance(), exact); diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatch.java b/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatch.java index 0f7a2c47c..25fbd0924 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatch.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatch.java @@ -3,8 +3,6 @@ import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; -import net.neoforged.neoforge.fluids.FluidStack; -import net.neoforged.neoforge.fluids.crafting.FluidIngredient; public interface ReplacementMatch { ReplacementMatch NONE = new ReplacementMatch() { @@ -19,20 +17,9 @@ static ReplacementMatch wrap(Context cx, Object o) { return NONE; } else if (o instanceof ReplacementMatch m) { return m; - } else if (o instanceof FluidIngredient fi) { - return new FluidIngredientMatch(fi); - } else if (o instanceof FluidStack fs) { - return new SingleFluidMatch(fs); + } else { + var in = IngredientJS.wrap(RegistryAccessContainer.of(cx), o); + return in.isEmpty() ? NONE : in; } - - var in = IngredientJS.wrap(RegistryAccessContainer.of(cx), o); - - if (in.isEmpty()) { - return NONE; - } else if (in.getItems().length == 1) { - return new SingleItemMatch(in.getItems()[0]); - } - - return new IngredientMatch(in); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/match/SingleFluidMatch.java b/src/main/java/dev/latvian/mods/kubejs/recipe/match/SingleFluidMatch.java deleted file mode 100644 index 3d0b40c33..000000000 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/match/SingleFluidMatch.java +++ /dev/null @@ -1,22 +0,0 @@ -package dev.latvian.mods.kubejs.recipe.match; - -import dev.latvian.mods.rhino.Context; -import net.neoforged.neoforge.fluids.FluidStack; -import net.neoforged.neoforge.fluids.crafting.FluidIngredient; - -public record SingleFluidMatch(FluidStack stack) implements FluidMatch { - @Override - public boolean matches(Context cx, FluidStack s, boolean exact) { - return stack.getFluid() == s.getFluid(); - } - - @Override - public boolean matches(Context cx, FluidIngredient ingredient, boolean exact) { - return ingredient.test(stack); - } - - @Override - public String toString() { - return stack.getFluid().kjs$getId(); - } -} diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/match/SingleItemMatch.java b/src/main/java/dev/latvian/mods/kubejs/recipe/match/SingleItemMatch.java deleted file mode 100644 index e2ba7c60e..000000000 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/match/SingleItemMatch.java +++ /dev/null @@ -1,28 +0,0 @@ -package dev.latvian.mods.kubejs.recipe.match; - -import dev.latvian.mods.rhino.Context; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.crafting.Ingredient; -import net.minecraft.world.level.ItemLike; - -public record SingleItemMatch(ItemStack stack) implements ItemMatch { - @Override - public boolean matches(Context cx, ItemStack s, boolean exact) { - return stack.getItem() == s.getItem(); - } - - @Override - public boolean matches(Context cx, Ingredient in, boolean exact) { - return in.test(stack); - } - - @Override - public boolean matches(Context cx, ItemLike itemLike, boolean exact) { - return stack.getItem() == itemLike.asItem(); - } - - @Override - public String toString() { - return stack.getItem().kjs$getId(); - } -} diff --git a/src/main/resources/kubejs.mixins.json b/src/main/resources/kubejs.mixins.json index 69d178345..14e1417ec 100644 --- a/src/main/resources/kubejs.mixins.json +++ b/src/main/resources/kubejs.mixins.json @@ -22,6 +22,7 @@ "EntityMixin", "EntityTypeMixin", "FireworkRocketEntityMixin", + "FluidIngredientMixin", "FluidMixin", "FluidStackMixin", "GameRulesMixin", @@ -50,6 +51,7 @@ "ResourceLocationMixin", "ServerLevelMixin", "ServerPlayerMixin", + "SizedFluidIngredientMixin", "SizedIngredientMixin", "StringRepresentableMixin", "StringTagMixin",