diff options
author | Rime <81419447+Emirlol@users.noreply.github.com> | 2024-06-19 18:57:43 +0300 |
---|---|---|
committer | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-07-22 14:20:35 +0800 |
commit | 4c398333b9802a9000cc65d50ae4a2d13f300ca8 (patch) | |
tree | 2b2e52431ddfc7610cc0070f5e5abe3c7356a02d /src/main/java/de/hysky/skyblocker/utils/container/RegexContainerMatcher.java | |
parent | 6541bb6aa43a7141ac259ee34a597891d6a45689 (diff) | |
download | Skyblocker-4c398333b9802a9000cc65d50ae4a2d13f300ca8.tar.gz Skyblocker-4c398333b9802a9000cc65d50ae4a2d13f300ca8.tar.bz2 Skyblocker-4c398333b9802a9000cc65d50ae4a2d13f300ca8.zip |
Refactor container matcher implementations into interfaces
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; + } +} |