From da603d57e8361840f3db1dafac78bfc3dc8b74fa Mon Sep 17 00:00:00 2001 From: kross <135918757+krossgg@users.noreply.github.com> Date: Wed, 6 Nov 2024 05:30:06 -0500 Subject: [PATCH] Filter Fix (#2265) --- .../gtceu/api/cover/filter/Filter.java | 4 -- .../gtceu/api/cover/filter/FluidFilter.java | 6 --- .../gtceu/api/cover/filter/ItemFilter.java | 6 --- .../api/cover/filter/SimpleFluidFilter.java | 43 +++++++++---------- .../api/cover/filter/SimpleItemFilter.java | 43 +++++++++++-------- .../gtceu/api/cover/filter/TagFilter.java | 21 ++++++--- .../api/cover/filter/TagFluidFilter.java | 24 ++++------- .../gtceu/api/cover/filter/TagItemFilter.java | 25 ++++------- .../gtceu/data/tag/GTDataComponents.java | 9 ++-- 9 files changed, 78 insertions(+), 103 deletions(-) diff --git a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/Filter.java b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/Filter.java index a97ab05f7c..aed276996e 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/Filter.java +++ b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/Filter.java @@ -2,8 +2,6 @@ import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup; -import net.minecraft.nbt.CompoundTag; - import java.util.function.Consumer; import java.util.function.Predicate; @@ -16,8 +14,6 @@ public interface Filter> extends Predicate { WidgetGroup openConfigurator(int x, int y); - CompoundTag saveFilter(); - void setOnUpdated(Consumer onUpdated); default boolean isBlackList() { diff --git a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/FluidFilter.java b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/FluidFilter.java index c63cf2a1c8..d1d6d0f11e 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/FluidFilter.java +++ b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/FluidFilter.java @@ -2,7 +2,6 @@ import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup; -import net.minecraft.nbt.CompoundTag; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.ItemLike; import net.neoforged.neoforge.fluids.FluidStack; @@ -63,11 +62,6 @@ public WidgetGroup openConfigurator(int x, int y) { throw new NotImplementedException("Not available for empty fluid filter"); } - @Override - public CompoundTag saveFilter() { - throw new NotImplementedException("Not available for empty fluid filter"); - } - @Override public void setOnUpdated(Consumer onUpdated) { throw new NotImplementedException("Not available for empty fluid filter"); diff --git a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/ItemFilter.java b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/ItemFilter.java index 8c59307724..59d2efc846 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/ItemFilter.java +++ b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/ItemFilter.java @@ -2,7 +2,6 @@ import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup; -import net.minecraft.nbt.CompoundTag; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.ItemLike; @@ -62,11 +61,6 @@ public WidgetGroup openConfigurator(int x, int y) { throw new NotImplementedException("Not available for empty item filter"); } - @Override - public CompoundTag saveFilter() { - throw new NotImplementedException("Not available for empty item filter"); - } - @Override public void setOnUpdated(Consumer onUpdated) { throw new NotImplementedException("Not available for empty item filter"); diff --git a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/SimpleFluidFilter.java b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/SimpleFluidFilter.java index c6a38a7440..ed9f022175 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/SimpleFluidFilter.java +++ b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/SimpleFluidFilter.java @@ -6,13 +6,9 @@ import com.gregtechceu.gtceu.api.transfer.fluid.CustomFluidTank; import com.gregtechceu.gtceu.data.tag.GTDataComponents; -import com.lowdragmc.lowdraglib.Platform; import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup; import net.minecraft.MethodsReturnNonnullByDefault; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.NbtOps; import net.minecraft.world.item.ItemStack; import net.neoforged.neoforge.fluids.FluidStack; @@ -47,8 +43,8 @@ public class SimpleFluidFilter implements FluidFilter { @Getter protected FluidStack[] matches = new FluidStack[9]; - protected Consumer itemWriter = filter -> {}; - protected Consumer onUpdated = filter -> itemWriter.accept(filter); + protected Consumer itemWriter = filter -> {}; + protected Consumer onUpdated = filter -> itemWriter.accept(filter); @Getter protected int maxStackSize = 1; @@ -66,8 +62,9 @@ protected SimpleFluidFilter(boolean isBlackList, boolean ignoreNbt, List itemStack.set(GTDataComponents.SIMPLE_FLUID_FILTER, filter); + return handler; } @Override @@ -78,20 +75,6 @@ public void setOnUpdated(Consumer onUpdated) { }; } - public CompoundTag saveFilter() { - var tag = new CompoundTag(); - tag.putBoolean("isBlackList", isBlackList); - tag.putBoolean("matchNbt", ignoreNbt); - var list = new ListTag(); - for (var match : matches) { - list.add(FluidStack.OPTIONAL_CODEC - .encodeStart(Platform.getFrozenRegistry().createSerializationContext(NbtOps.INSTANCE), match) - .getOrThrow()); - } - tag.put("matches", list); - return tag; - } - public void setBlackList(boolean blackList) { isBlackList = blackList; onUpdated.accept(this); @@ -187,4 +170,20 @@ public void setMaxStackSize(int maxStackSize) { match.setAmount(Math.min(match.getAmount(), maxStackSize)); } } + + @Override + public final boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SimpleFluidFilter that)) return false; + + return isBlackList == that.isBlackList && ignoreNbt == that.ignoreNbt && Arrays.equals(matches, that.matches); + } + + @Override + public int hashCode() { + int result = Boolean.hashCode(isBlackList); + result = 31 * result + Boolean.hashCode(ignoreNbt); + result = 31 * result + Arrays.hashCode(matches); + return result; + } } diff --git a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/SimpleItemFilter.java b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/SimpleItemFilter.java index ed983889ac..5f9043d21e 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/SimpleItemFilter.java +++ b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/SimpleItemFilter.java @@ -5,14 +5,11 @@ import com.gregtechceu.gtceu.api.transfer.item.CustomItemStackHandler; import com.gregtechceu.gtceu.data.tag.GTDataComponents; -import com.lowdragmc.lowdraglib.Platform; import com.lowdragmc.lowdraglib.gui.widget.PhantomSlotWidget; import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup; import com.lowdragmc.lowdraglib.side.item.ItemTransferHelper; import net.minecraft.MethodsReturnNonnullByDefault; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.ListTag; import net.minecraft.world.item.ItemStack; import com.mojang.serialization.Codec; @@ -39,6 +36,7 @@ public class SimpleItemFilter implements ItemFilter { Codec.BOOL.fieldOf("ignore_components").forGetter(val -> val.ignoreNbt), ItemStack.OPTIONAL_CODEC.listOf().fieldOf("matches").forGetter(val -> Arrays.stream(val.matches).toList())) .apply(instance, SimpleItemFilter::new)); + @Getter protected boolean isBlackList; @Getter @@ -46,8 +44,8 @@ public class SimpleItemFilter implements ItemFilter { @Getter protected ItemStack[] matches = new ItemStack[9]; - protected Consumer itemWriter = filter -> {}; - protected Consumer onUpdated = filter -> itemWriter.accept(filter); + protected Consumer itemWriter = filter -> {}; + protected Consumer onUpdated = filter -> itemWriter.accept(filter); @Getter protected int maxStackSize; @@ -64,8 +62,9 @@ protected SimpleItemFilter(boolean isBlackList, boolean ignoreNbt, List itemStack.set(GTDataComponents.SIMPLE_ITEM_FILTER, filter); + return handler; } @Override @@ -76,18 +75,6 @@ public void setOnUpdated(Consumer onUpdated) { }; } - public CompoundTag saveFilter() { - var tag = new CompoundTag(); - tag.putBoolean("isBlackList", isBlackList); - tag.putBoolean("matchNbt", ignoreNbt); - var list = new ListTag(); - for (var match : matches) { - list.add(match.saveOptional(Platform.getFrozenRegistry())); - } - tag.put("matches", list); - return tag; - } - public void setBlackList(boolean blackList) { isBlackList = blackList; onUpdated.accept(this); @@ -173,4 +160,22 @@ public void setMaxStackSize(int maxStackSize) { match.setCount(Math.min(match.getCount(), maxStackSize)); } } + + @Override + public final boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SimpleItemFilter that)) return false; + + return isBlackList == that.isBlackList && ignoreNbt == that.ignoreNbt && maxStackSize == that.maxStackSize && + Arrays.equals(matches, that.matches); + } + + @Override + public int hashCode() { + int result = Boolean.hashCode(isBlackList); + result = 31 * result + Boolean.hashCode(ignoreNbt); + result = 31 * result + Arrays.hashCode(matches); + result = 31 * result + maxStackSize; + return result; + } } diff --git a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagFilter.java b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagFilter.java index 5fa5e1537f..a1921b466a 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagFilter.java +++ b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagFilter.java @@ -8,7 +8,6 @@ import com.lowdragmc.lowdraglib.gui.widget.TextFieldWidget; import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup; -import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.MutableComponent; import lombok.Getter; @@ -42,12 +41,6 @@ public abstract class TagFilter> implements Filter onUpdated) { onUpdated.accept(filter); }; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + TagFilter tagFilter = (TagFilter) o; + return oreDictFilterExpression.equals(tagFilter.oreDictFilterExpression); + } + + @Override + public int hashCode() { + return oreDictFilterExpression.hashCode(); + } } diff --git a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagFluidFilter.java b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagFluidFilter.java index caabd4e220..c075fc0dd3 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagFluidFilter.java +++ b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagFluidFilter.java @@ -7,8 +7,6 @@ import net.minecraft.world.level.material.Fluid; import net.neoforged.neoforge.fluids.FluidStack; -import com.mojang.serialization.Codec; -import com.mojang.serialization.codecs.RecordCodecBuilder; import it.unimi.dsi.fastutil.objects.Object2BooleanMap; import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap; @@ -19,24 +17,18 @@ */ public class TagFluidFilter extends TagFilter implements FluidFilter { - public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( - Codec.STRING.fieldOf("tag").forGetter(val -> val.oreDictFilterExpression)) - .apply(instance, TagFluidFilter::loadFilter)); private final Object2BooleanMap cache = new Object2BooleanOpenHashMap<>(); - protected TagFluidFilter() {} - - public static TagFluidFilter loadFilter(ItemStack itemStack) { - return itemStack.get(GTDataComponents.TAG_FLUID_FILTER); + protected TagFluidFilter(String oreDict) { + this.oreDictFilterExpression = oreDict; + OreDictExprFilter.parseExpression(this.matchRules, this.oreDictFilterExpression); } - private static TagFluidFilter loadFilter(String oreDict) { - var handler = new TagFluidFilter(); - // handler.itemWriter = itemWriter; // TODO fix - handler.oreDictFilterExpression = oreDict; - handler.matchRules.clear(); - handler.cache.clear(); - OreDictExprFilter.parseExpression(handler.matchRules, handler.oreDictFilterExpression); + public static TagFluidFilter loadFilter(ItemStack itemStack) { + var expr = itemStack.getOrDefault(GTDataComponents.TAG_FILTER_EXPRESSION, ""); + var handler = new TagFluidFilter(expr); + handler.itemWriter = filter -> itemStack.set(GTDataComponents.TAG_FILTER_EXPRESSION, + ((TagFluidFilter) filter).oreDictFilterExpression); return handler; } diff --git a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagItemFilter.java b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagItemFilter.java index c2d3d4e2b8..dd4b4e6195 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagItemFilter.java +++ b/src/main/java/com/gregtechceu/gtceu/api/cover/filter/TagItemFilter.java @@ -6,8 +6,6 @@ import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; -import com.mojang.serialization.Codec; -import com.mojang.serialization.codecs.RecordCodecBuilder; import it.unimi.dsi.fastutil.objects.Object2BooleanMap; import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap; @@ -18,25 +16,18 @@ */ public class TagItemFilter extends TagFilter implements ItemFilter { - public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( - Codec.STRING.fieldOf("tag").forGetter(val -> val.oreDictFilterExpression)) - .apply(instance, TagItemFilter::loadFilter)); - private final Object2BooleanMap cache = new Object2BooleanOpenHashMap<>(); - protected TagItemFilter() {} - - public static TagItemFilter loadFilter(ItemStack itemStack) { - return itemStack.get(GTDataComponents.TAG_ITEM_FILTER); + protected TagItemFilter(String tag) { + oreDictFilterExpression = tag; + OreDictExprFilter.parseExpression(matchRules, oreDictFilterExpression); } - private static TagItemFilter loadFilter(String oreDict) { - var handler = new TagItemFilter(); - // handler.itemWriter = itemWriter; // TODO fix - handler.oreDictFilterExpression = oreDict; - handler.matchRules.clear(); - handler.cache.clear(); - OreDictExprFilter.parseExpression(handler.matchRules, handler.oreDictFilterExpression); + public static TagItemFilter loadFilter(ItemStack itemStack) { + var expr = itemStack.getOrDefault(GTDataComponents.TAG_FILTER_EXPRESSION, ""); + var handler = new TagItemFilter(expr); + handler.itemWriter = filter -> itemStack.set(GTDataComponents.TAG_FILTER_EXPRESSION, + ((TagItemFilter) filter).oreDictFilterExpression); return handler; } diff --git a/src/main/java/com/gregtechceu/gtceu/data/tag/GTDataComponents.java b/src/main/java/com/gregtechceu/gtceu/data/tag/GTDataComponents.java index 8f0d9cf1e1..7a268c7248 100644 --- a/src/main/java/com/gregtechceu/gtceu/data/tag/GTDataComponents.java +++ b/src/main/java/com/gregtechceu/gtceu/data/tag/GTDataComponents.java @@ -3,8 +3,6 @@ import com.gregtechceu.gtceu.GTCEu; import com.gregtechceu.gtceu.api.cover.filter.SimpleFluidFilter; import com.gregtechceu.gtceu.api.cover.filter.SimpleItemFilter; -import com.gregtechceu.gtceu.api.cover.filter.TagFluidFilter; -import com.gregtechceu.gtceu.api.cover.filter.TagItemFilter; import com.gregtechceu.gtceu.api.item.LampBlockItem; import com.gregtechceu.gtceu.api.item.component.IMaterialPartItem; import com.gregtechceu.gtceu.api.item.datacomponents.*; @@ -75,12 +73,11 @@ public class GTDataComponents { builder -> builder.persistent(Codec.BYTE).networkSynchronized(ByteBufCodecs.BYTE)); public static final DeferredHolder, DataComponentType> SIMPLE_FLUID_FILTER = DATA_COMPONENTS .registerComponentType("simple_fluid_filter", builder -> builder.persistent(SimpleFluidFilter.CODEC)); - public static final DeferredHolder, DataComponentType> TAG_FLUID_FILTER = DATA_COMPONENTS - .registerComponentType("tag_fluid_filter", builder -> builder.persistent(TagFluidFilter.CODEC)); public static final DeferredHolder, DataComponentType> SIMPLE_ITEM_FILTER = DATA_COMPONENTS .registerComponentType("simple_item_filter", builder -> builder.persistent(SimpleItemFilter.CODEC)); - public static final DeferredHolder, DataComponentType> TAG_ITEM_FILTER = DATA_COMPONENTS - .registerComponentType("tag_item_filter", builder -> builder.persistent(TagItemFilter.CODEC)); + public static final DeferredHolder, DataComponentType> TAG_FILTER_EXPRESSION = DATA_COMPONENTS + .registerComponentType("tag_filter_expression", + builder -> builder.persistent(Codec.STRING).networkSynchronized(ByteBufCodecs.STRING_UTF8)); public static final DeferredHolder, DataComponentType> CIRCUIT_CONFIG = DATA_COMPONENTS .registerComponentType("circuit_config", builder -> builder.persistent(Codec.INT)); public static final DeferredHolder, DataComponentType> FLUID_CONTENT = DATA_COMPONENTS