From a42a9a8bc20678e33d702716bba5baa103e0fe73 Mon Sep 17 00:00:00 2001 From: Zorbatron <46525467+Zorbatron@users.noreply.github.com> Date: Sun, 16 Feb 2025 01:57:16 -0500 Subject: [PATCH 1/2] item filter --- .../java/com/zorbatron/zbgt/api/ZBGTAPI.java | 4 +++ .../covers/RegistryNameItemFilterCover.java | 26 +++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/zorbatron/zbgt/api/ZBGTAPI.java b/src/main/java/com/zorbatron/zbgt/api/ZBGTAPI.java index f0b1496c..41329a28 100644 --- a/src/main/java/com/zorbatron/zbgt/api/ZBGTAPI.java +++ b/src/main/java/com/zorbatron/zbgt/api/ZBGTAPI.java @@ -1,5 +1,7 @@ package com.zorbatron.zbgt.api; +import java.util.regex.Pattern; + import net.minecraft.block.state.IBlockState; import net.minecraftforge.fluids.FluidStack; @@ -33,4 +35,6 @@ public class ZBGTAPI { public static FluidStack pyrotheum; public static FluidStack cryotheum; + + public static final Pattern EMPTY_PATTERN = Pattern.compile(""); } diff --git a/src/main/java/com/zorbatron/zbgt/common/covers/RegistryNameItemFilterCover.java b/src/main/java/com/zorbatron/zbgt/common/covers/RegistryNameItemFilterCover.java index 3f72ada2..95153b72 100644 --- a/src/main/java/com/zorbatron/zbgt/common/covers/RegistryNameItemFilterCover.java +++ b/src/main/java/com/zorbatron/zbgt/common/covers/RegistryNameItemFilterCover.java @@ -2,6 +2,7 @@ import java.util.function.Consumer; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -10,6 +11,7 @@ import org.jetbrains.annotations.NotNull; +import com.zorbatron.zbgt.api.ZBGTAPI; import com.zorbatron.zbgt.api.util.ZBGTUtility; import com.zorbatron.zbgt.client.widgets.FilterTestSlot; @@ -24,7 +26,7 @@ public class RegistryNameItemFilterCover extends ItemFilter { @NotNull - private String regex = ""; + private Pattern regex = ZBGTAPI.EMPTY_PATTERN; private final Object2ObjectOpenCustomHashMap matchCache = new Object2ObjectOpenCustomHashMap<>( ItemStackHashStrategy.builder().compareItem(true).build()); @@ -46,11 +48,13 @@ public Object matchItemStack(ItemStack itemStack) { } public boolean matchesItemStack(@NotNull ItemStack itemStack) { + if (regex == ZBGTAPI.EMPTY_PATTERN) return false; + return ZBGTUtility.computeIfAbsentDiffKey(matchCache, itemStack, itemStack::copy, stack -> { ResourceLocation resloc = Item.REGISTRY.getNameForObject(itemStack.getItem()); if (resloc == null) return false; String name = resloc.toString(); - return Pattern.matches(regex, name); + return regex.matcher(name).matches(); }); } @@ -68,13 +72,19 @@ public void initUI(Consumer widgetGroup) { } widgetGroup.accept(new ImageWidget(10, 22, 154, 18, GuiTextures.DISPLAY)); - widgetGroup.accept(new TextFieldWidget2(14, 26, 150, 14, this::getRegex, this::setRegex)); + widgetGroup.accept(new TextFieldWidget2(14, 26, 150, 14, this::getPattern, this::setRegex)); } private void setRegex(String newRegex) { - if (!regex.equals(newRegex)) { - regex = newRegex; + if (!newRegex.equals(getPattern())) { matchCache.clear(); + + try { + regex = Pattern.compile(newRegex); + } catch (PatternSyntaxException e) { + regex = ZBGTAPI.EMPTY_PATTERN; + } + for (FilterTestSlot testSlot : testSlots) { if (testSlot == null) continue; testSlot.update(); @@ -83,13 +93,13 @@ private void setRegex(String newRegex) { } @NotNull - private String getRegex() { - return regex; + private String getPattern() { + return regex.pattern(); } @Override public void writeToNBT(NBTTagCompound tagCompound) { - tagCompound.setString("Filter", getRegex()); + tagCompound.setString("Filter", getPattern()); } @Override From 689edf8243db9dbec5e870e0dfd4612bd7ccc353 Mon Sep 17 00:00:00 2001 From: Zorbatron <46525467+Zorbatron@users.noreply.github.com> Date: Sun, 16 Feb 2025 02:02:05 -0500 Subject: [PATCH 2/2] fluid filter --- .../covers/LocalNameFluidFilterCover.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/zorbatron/zbgt/common/covers/LocalNameFluidFilterCover.java b/src/main/java/com/zorbatron/zbgt/common/covers/LocalNameFluidFilterCover.java index 652968b6..3bcbf8f1 100644 --- a/src/main/java/com/zorbatron/zbgt/common/covers/LocalNameFluidFilterCover.java +++ b/src/main/java/com/zorbatron/zbgt/common/covers/LocalNameFluidFilterCover.java @@ -2,12 +2,14 @@ import java.util.function.Consumer; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.fluids.FluidStack; import org.jetbrains.annotations.NotNull; +import com.zorbatron.zbgt.api.ZBGTAPI; import com.zorbatron.zbgt.api.util.FluidStackHashStrategy; import com.zorbatron.zbgt.api.util.ZBGTUtility; import com.zorbatron.zbgt.client.widgets.FilterTestFluidSlot; @@ -22,7 +24,7 @@ public class LocalNameFluidFilterCover extends FluidFilter { @NotNull - private String regex = ""; + private Pattern regex = ZBGTAPI.EMPTY_PATTERN; private final Object2ObjectOpenCustomHashMap matchCache = new Object2ObjectOpenCustomHashMap<>( FluidStackHashStrategy.builder().compareFluid().build()); @@ -31,7 +33,7 @@ public class LocalNameFluidFilterCover extends FluidFilter { @Override public boolean testFluid(@NotNull FluidStack fluidStack) { return ZBGTUtility.computeIfAbsentDiffKey(matchCache, fluidStack, fluidStack::copy, - stack -> Pattern.matches(regex, stack.getUnlocalizedName())); + stack -> regex.matcher(stack.getUnlocalizedName()).matches()); } @Override @@ -53,13 +55,19 @@ public void initUI(Consumer widgetGroup) { } widgetGroup.accept(new ImageWidget(10 - 22, 22 + 5, 154, 18, GuiTextures.DISPLAY)); - widgetGroup.accept(new TextFieldWidget2(14 - 22, 26 + 5, 150, 14, this::getRegex, this::setRegex)); + widgetGroup.accept(new TextFieldWidget2(14 - 22, 26 + 5, 150, 14, this::getPattern, this::setRegex)); } private void setRegex(String newRegex) { - if (!regex.equals(newRegex)) { - regex = newRegex; + if (!newRegex.equals(getPattern())) { matchCache.clear(); + + try { + regex = Pattern.compile(newRegex); + } catch (PatternSyntaxException e) { + regex = ZBGTAPI.EMPTY_PATTERN; + } + for (FilterTestFluidSlot testSlot : testSlots) { if (testSlot == null) continue; testSlot.update(); @@ -68,13 +76,13 @@ private void setRegex(String newRegex) { } @NotNull - private String getRegex() { - return regex; + private String getPattern() { + return regex.pattern(); } @Override public void writeToNBT(NBTTagCompound tagCompound) { - tagCompound.setString("Filter", getRegex()); + tagCompound.setString("Filter", getPattern()); } @Override