Skip to content

Commit

Permalink
Fixed fluid replacements (probably)
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jul 13, 2024
1 parent 63e32bf commit ae98b03
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 146 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -29,9 +32,4 @@ public boolean matches(Context cx, FluidIngredient in, boolean exact) {

return false;
}

@Override
public String toString() {
return ingredient.toString();
}
}
14 changes: 13 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
}
}
47 changes: 46 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
}
}
18 changes: 18 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -49,6 +51,7 @@ public interface ItemStackKJS extends
ToStringJS,
Replaceable,
MutableDataComponentHolderKJS,
ItemMatch,
RegistryObjectKJS<Item> {
default ItemStack kjs$self() {
return (ItemStack) this;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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 {
}

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/java/dev/latvian/mods/kubejs/recipe/match/ItemMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
}
}
Loading

0 comments on commit ae98b03

Please sign in to comment.