diff options
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils/container/RegexContainerMatcher.java')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/utils/container/RegexContainerMatcher.java | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/container/RegexContainerMatcher.java b/src/main/java/de/hysky/skyblocker/utils/container/RegexContainerMatcher.java new file mode 100644 index 00000000..a8a8e3c2 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/container/RegexContainerMatcher.java @@ -0,0 +1,68 @@ +package de.hysky.skyblocker.utils.container; + +import de.hysky.skyblocker.skyblock.ChestValue; +import net.minecraft.client.gui.screen.ingame.HandledScreen; +import org.intellij.lang.annotations.Language; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A regex implementation of {@link AbstractContainerMatcher} that matches the title of the screen. + */ +public abstract class RegexContainerMatcher implements AbstractContainerMatcher { + /** + * The title of the screen must match this pattern for this to be applied. Null means it will be applied to all screens. + * + * @implNote Don't end your regex with a {@code $} as {@link ChestValue} appends text to the end of the title, + * so the regex will stop matching if the player uses chest value. + */ + @Nullable + public final Pattern titlePattern; + + @Nullable + public String[] groups = null; + + @Override + public boolean test(@NotNull HandledScreen<?> screen) { + return test(screen.getTitle().getString()); + } + + public boolean test(@NotNull String title) { + if (titlePattern == null) return true; + Matcher matcher = titlePattern.matcher(title); + if (matcher.matches()) { + int groupCount = matcher.groupCount(); + if (groupCount >= 1) { //No need to initialize the array if there are no groups + groups = new String[groupCount]; + for (int i = 0; i < groupCount; i++) { + groups[i] = matcher.group(i + 1); // +1 because first group is the whole match, which is useless + } + } + return true; + } + return false; + } + + protected RegexContainerMatcher() { + this((Pattern) null); + } + + protected RegexContainerMatcher(@NotNull @Language("RegExp") String titlePattern) { + this(Pattern.compile(titlePattern)); + } + + protected RegexContainerMatcher(@Nullable Pattern titlePattern) { + this.titlePattern = titlePattern; + } + + public @Nullable Pattern getTitlePattern() { + return titlePattern; + } + + public final @Nullable String[] getGroups() { + return groups; + } +} |