From be52a8f9c260f7953bc8b6b991c476120d8704ad Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Mon, 11 Nov 2024 13:45:45 +0100 Subject: Fix comma globs --- src/main/java/com/notnite/gloppers/GlobUtil.java | 10 ++++++++++ .../com/notnite/gloppers/mixin/HopperBlockEntityMixin.java | 2 +- src/test/java/com/notnite/gloppers/GlobUtilTest.java | 13 ++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/notnite/gloppers/GlobUtil.java b/src/main/java/com/notnite/gloppers/GlobUtil.java index 38d0e44..2ae051c 100644 --- a/src/main/java/com/notnite/gloppers/GlobUtil.java +++ b/src/main/java/com/notnite/gloppers/GlobUtil.java @@ -4,6 +4,16 @@ import java.util.BitSet; public class GlobUtil { + public static boolean matchGlobSequence(String name, String globs) { + int nextSplit, lastSplit = 0; + while ((nextSplit = globs.indexOf(',', lastSplit)) > 0) { + if (matchGlob(name, globs.substring(lastSplit, nextSplit))) + return true; + lastSplit = nextSplit + 1; + } + return matchGlob(name, globs.substring(lastSplit)); + } + /** * Match a string against a glob. */ diff --git a/src/main/java/com/notnite/gloppers/mixin/HopperBlockEntityMixin.java b/src/main/java/com/notnite/gloppers/mixin/HopperBlockEntityMixin.java index 06eb93e..52cbdcc 100644 --- a/src/main/java/com/notnite/gloppers/mixin/HopperBlockEntityMixin.java +++ b/src/main/java/com/notnite/gloppers/mixin/HopperBlockEntityMixin.java @@ -44,7 +44,7 @@ public abstract class HopperBlockEntityMixin { var itemName = itemRegistryEntry.get().getValue().getPath(); // Check if itemstack matches glob - if (!GlobUtil.matchGlob(itemName, glob)) return false; + if (!GlobUtil.matchGlobSequence(itemName, glob)) return false; return true; } diff --git a/src/test/java/com/notnite/gloppers/GlobUtilTest.java b/src/test/java/com/notnite/gloppers/GlobUtilTest.java index 064ced1..1ceed6a 100644 --- a/src/test/java/com/notnite/gloppers/GlobUtilTest.java +++ b/src/test/java/com/notnite/gloppers/GlobUtilTest.java @@ -5,6 +5,17 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class GlobUtilTest { + @Test + public void multiGlobPattern() { + assertTrue(GlobUtil.matchGlobSequence("test", "test,foo")); + assertTrue(GlobUtil.matchGlobSequence("foo", "test,foo")); + assertTrue(GlobUtil.matchGlobSequence("foo", "foo")); + assertTrue(GlobUtil.matchGlobSequence("foo", "test,f*")); + assertTrue(GlobUtil.matchGlobSequence("foo", "*o,f*")); + assertFalse(GlobUtil.matchGlobSequence("baa", "*o,f*")); + } + + @Test public void testGlobBeginning() { assertTrue(GlobUtil.matchGlob("test_id", "*_id")); @@ -28,7 +39,7 @@ class GlobUtilTest { } @Test - public void testSinglePlaceholder() { + public void testSingleGlobPlaceholder() { assertTrue(GlobUtil.matchGlob("test_id", "tes?_i?")); } } -- cgit