Skip to content

Commit

Permalink
Filter Fix (#2265)
Browse files Browse the repository at this point in the history
  • Loading branch information
krossgg authored Nov 6, 2024
1 parent c10e783 commit da603d5
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,8 +14,6 @@ public interface Filter<T, S extends Filter<T, S>> extends Predicate<T> {

WidgetGroup openConfigurator(int x, int y);

CompoundTag saveFilter();

void setOnUpdated(Consumer<S> onUpdated);

default boolean isBlackList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<FluidFilter> onUpdated) {
throw new NotImplementedException("Not available for empty fluid filter");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<ItemFilter> onUpdated) {
throw new NotImplementedException("Not available for empty item filter");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -47,8 +43,8 @@ public class SimpleFluidFilter implements FluidFilter {
@Getter
protected FluidStack[] matches = new FluidStack[9];

protected Consumer<FluidFilter> itemWriter = filter -> {};
protected Consumer<FluidFilter> onUpdated = filter -> itemWriter.accept(filter);
protected Consumer<SimpleFluidFilter> itemWriter = filter -> {};
protected Consumer<SimpleFluidFilter> onUpdated = filter -> itemWriter.accept(filter);

@Getter
protected int maxStackSize = 1;
Expand All @@ -66,8 +62,9 @@ protected SimpleFluidFilter(boolean isBlackList, boolean ignoreNbt, List<FluidSt
}

public static SimpleFluidFilter loadFilter(ItemStack itemStack) {
// FIXME handler.itemWriter = itemWriter;
return itemStack.get(GTDataComponents.SIMPLE_FLUID_FILTER);
var handler = itemStack.getOrDefault(GTDataComponents.SIMPLE_FLUID_FILTER, new SimpleFluidFilter());
handler.itemWriter = filter -> itemStack.set(GTDataComponents.SIMPLE_FLUID_FILTER, filter);
return handler;
}

@Override
Expand All @@ -78,20 +75,6 @@ public void setOnUpdated(Consumer<FluidFilter> 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);
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,15 +36,16 @@ 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
protected boolean ignoreNbt;
@Getter
protected ItemStack[] matches = new ItemStack[9];

protected Consumer<ItemFilter> itemWriter = filter -> {};
protected Consumer<ItemFilter> onUpdated = filter -> itemWriter.accept(filter);
protected Consumer<SimpleItemFilter> itemWriter = filter -> {};
protected Consumer<SimpleItemFilter> onUpdated = filter -> itemWriter.accept(filter);

@Getter
protected int maxStackSize;
Expand All @@ -64,8 +62,9 @@ protected SimpleItemFilter(boolean isBlackList, boolean ignoreNbt, List<ItemStac
}

public static SimpleItemFilter loadFilter(ItemStack itemStack) {
// handler.itemWriter = itemWriter; TODO fix
return itemStack.get(GTDataComponents.SIMPLE_ITEM_FILTER);
SimpleItemFilter handler = itemStack.getOrDefault(GTDataComponents.SIMPLE_ITEM_FILTER, new SimpleItemFilter());
handler.itemWriter = filter -> itemStack.set(GTDataComponents.SIMPLE_ITEM_FILTER, filter);
return handler;
}

@Override
Expand All @@ -76,18 +75,6 @@ public void setOnUpdated(Consumer<ItemFilter> 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);
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,12 +41,6 @@ public abstract class TagFilter<T, S extends Filter<T, S>> implements Filter<T,

protected TagFilter() {}

public CompoundTag saveFilter() {
var tag = new CompoundTag();
tag.putString("oreDict", oreDictFilterExpression);
return tag;
}

public void setOreDict(String oreDict) {
this.oreDictFilterExpression = oreDict;
matchRules.clear();
Expand Down Expand Up @@ -126,4 +119,18 @@ public void setOnUpdated(Consumer<S> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,24 +17,18 @@
*/
public class TagFluidFilter extends TagFilter<FluidStack, FluidFilter> implements FluidFilter {

public static final Codec<TagFluidFilter> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("tag").forGetter(val -> val.oreDictFilterExpression))
.apply(instance, TagFluidFilter::loadFilter));
private final Object2BooleanMap<Fluid> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,25 +16,18 @@
*/
public class TagItemFilter extends TagFilter<ItemStack, ItemFilter> implements ItemFilter {

public static final Codec<TagItemFilter> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("tag").forGetter(val -> val.oreDictFilterExpression))
.apply(instance, TagItemFilter::loadFilter));

private final Object2BooleanMap<Item> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -75,12 +73,11 @@ public class GTDataComponents {
builder -> builder.persistent(Codec.BYTE).networkSynchronized(ByteBufCodecs.BYTE));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<SimpleFluidFilter>> SIMPLE_FLUID_FILTER = DATA_COMPONENTS
.registerComponentType("simple_fluid_filter", builder -> builder.persistent(SimpleFluidFilter.CODEC));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<TagFluidFilter>> TAG_FLUID_FILTER = DATA_COMPONENTS
.registerComponentType("tag_fluid_filter", builder -> builder.persistent(TagFluidFilter.CODEC));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<SimpleItemFilter>> SIMPLE_ITEM_FILTER = DATA_COMPONENTS
.registerComponentType("simple_item_filter", builder -> builder.persistent(SimpleItemFilter.CODEC));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<TagItemFilter>> TAG_ITEM_FILTER = DATA_COMPONENTS
.registerComponentType("tag_item_filter", builder -> builder.persistent(TagItemFilter.CODEC));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<String>> TAG_FILTER_EXPRESSION = DATA_COMPONENTS
.registerComponentType("tag_filter_expression",
builder -> builder.persistent(Codec.STRING).networkSynchronized(ByteBufCodecs.STRING_UTF8));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Integer>> CIRCUIT_CONFIG = DATA_COMPONENTS
.registerComponentType("circuit_config", builder -> builder.persistent(Codec.INT));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<SimpleFluidContent>> FLUID_CONTENT = DATA_COMPONENTS
Expand Down

0 comments on commit da603d5

Please sign in to comment.