diff options
author | olim <bobq4582@gmail.com> | 2024-02-15 15:23:50 +0000 |
---|---|---|
committer | olim <bobq4582@gmail.com> | 2024-02-15 15:23:50 +0000 |
commit | 4d0d772537648d424631a1a932780601a642b1a5 (patch) | |
tree | 9bb9cef55fd1daf3a651f5c50d6159c2fa252f53 /src/main/java/de/hysky/skyblocker/skyblock/chat | |
parent | b9aeadfc2fe33b60555f978c82bef0294286e92a (diff) | |
download | Skyblocker-4d0d772537648d424631a1a932780601a642b1a5.tar.gz Skyblocker-4d0d772537648d424631a1a932780601a642b1a5.tar.bz2 Skyblocker-4d0d772537648d424631a1a932780601a642b1a5.zip |
make the rules acutaly work
now scans each chat message with the rules to see if they match
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java | 30 | ||||
-rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java | 44 |
2 files changed, 64 insertions, 10 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java index e212b2a0..afc409ac 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java @@ -20,6 +20,7 @@ public class ChatRule { public Boolean enabled; public Boolean isPartialMatch; public Boolean isRegex; + public Boolean isIgnoreCase; public String filter; public LocationOption validLocation; @@ -38,6 +39,7 @@ public class ChatRule { this.enabled = true; this.isPartialMatch = false; this.isRegex = false; + this.isIgnoreCase = true; this.filter = ""; this.validLocation = LocationOption.None; @@ -49,11 +51,12 @@ public class ChatRule { } - public ChatRule(String name, Boolean enabled, Boolean isPartialMatch, Boolean isRegex, String filter, LocationOption validLocation, List<ItemStack> validItems, Boolean hideMessage, Boolean showActionBar, Boolean showAnnouncement, String replaceMessage, Sound customSound) { + public ChatRule(String name, Boolean enabled, Boolean isPartialMatch, Boolean isRegex, Boolean isIgnoreCase, String filter, LocationOption validLocation, List<ItemStack> validItems, Boolean hideMessage, Boolean showActionBar, Boolean showAnnouncement, String replaceMessage, Sound customSound) { this.name = name; this.enabled = enabled; this.isPartialMatch = isPartialMatch; this.isRegex = isRegex; + this.isIgnoreCase = isIgnoreCase; this.filter = filter; this.validLocation = validLocation; this.hideMessage = hideMessage; @@ -147,26 +150,37 @@ public class ChatRule { /** * checks every input option and if the games state and the inputted str matches them returns true. - * @param str the chat message to check if fits + * @param inputString the chat message to check if fits * @return if the inputs are all true and the outputs should be performed */ - public Boolean isMatch(String str, MinecraftClient client){ + public Boolean isMatch(String inputString){ //enabled if (!enabled) return false; + //ignore case + String testString; + String testFilter; + if (isIgnoreCase){ + testString = inputString.toLowerCase(); + testFilter = filter.toLowerCase(); + }else { + testString = inputString; + testFilter = filter; + } + //filter - if (filter.isEmpty()) return false; + if (testFilter.isEmpty()) return false; if(isRegex) { if (isPartialMatch) { - if (! Pattern.compile(filter).matcher(str).find()) return false; + if (! Pattern.compile(testFilter).matcher(testString).find()) return false; }else { - if (!str.matches(filter)) return false; + if (!testString.matches(testFilter)) return false; } } else{ if (isPartialMatch) { - if (!str.contains(filter)) return false; + if (!testString.contains(testFilter)) return false; }else { - if (!filter.equals(str)) return false; + if (!testFilter.equals(testString)) return false; } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java index dc2ab0f1..f8823589 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -5,10 +5,12 @@ import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.skyblock.shortcut.Shortcuts; +import de.hysky.skyblocker.utils.Utils; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents; import net.fabricmc.fabric.api.client.message.v1.ClientSendMessageEvents; +import net.minecraft.client.MinecraftClient; import net.minecraft.text.Text; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,6 +27,7 @@ import java.util.List; import java.util.Map; public class ChatRulesHandler { + private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); private static final Logger LOGGER = LoggerFactory.getLogger(ChatRule.class); private static final Path CHAT_RULE_FILE = SkyblockerMod.CONFIG_DIR.resolve("chatRules.json"); @@ -32,7 +35,7 @@ public class ChatRulesHandler { public static void init() { loadChatRules(); - ClientReceiveMessageEvents.GAME.register(ChatRulesHandler::checkMessage); + ClientReceiveMessageEvents.ALLOW_GAME.register(ChatRulesHandler::checkMessage); } private static void loadChatRules() { @@ -62,8 +65,45 @@ public class ChatRulesHandler { } } - private static void checkMessage(Text message, Boolean overlay) { + /** + * Checks each rule in {@link ChatRulesHandler#chatRuleList} to see if they are a match for the message and if so change outputs based on the options set in the {@link ChatRule}. + * @param message the chat message + * @param overlay if its overlay + */ + private static boolean checkMessage(Text message, Boolean overlay) { + if (!Utils.isOnSkyblock()) return true; //do not work not on skyblock + if (overlay) return true; //ignore messages in overlay + String plain = trimItemColor(message.getString()); + for (ChatRule rule : chatRuleList) { + if (rule.isMatch(plain)) { + //get a replacement message + Text newMessage; + if (!rule.getReplaceMessage().isEmpty()){ + newMessage = Text.of(rule.getReplaceMessage()); + } + else { + newMessage =message; + } + //todo show announcement + + //show in action bar + if (rule.getShowActionBar() && CLIENT.player != null) { + CLIENT.player.sendMessage(newMessage, true); + } + //hide message + if (!rule.getHideMessage() && CLIENT.player != null){ + CLIENT.player.sendMessage(newMessage, false); + } + //do not send original message + return false; + } + } + return true; + } + private static String trimItemColor(String str) { + if (str.isEmpty()) return str; + return str.replaceAll("ยง[0-9a-g]", ""); } } |