From fdcc70bcc3f07fa2c51e2d1ec714294a36495fcd Mon Sep 17 00:00:00 2001 From: olim Date: Tue, 13 Feb 2024 14:30:22 +0000 Subject: start of implimentation has config ui that dose not save the rules and the basic building blocks for the rest of the feature --- src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java new file mode 100644 index 00000000..4ea8c24d --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -0,0 +1,4 @@ +package de.hysky.skyblocker.skyblock.chat; + +public class ChatRulesHandler { +} -- cgit From 8132b63b64e67eab04a232d71af6e196abb5229e Mon Sep 17 00:00:00 2001 From: olim Date: Thu, 15 Feb 2024 11:01:38 +0000 Subject: makes the config ui work the ui works and lets the user configure the rules but has a few quirks to fix in a later commit as it is usable for now --- .../java/de/hysky/skyblocker/SkyblockerMod.java | 2 + .../hysky/skyblocker/skyblock/chat/ChatRule.java | 42 +++++--------- .../skyblock/chat/ChatRuleConfigScreen.java | 20 ++++--- .../skyblock/chat/ChatRulesConfigListWidget.java | 57 +++++++++++++------ .../skyblock/chat/ChatRulesConfigScreen.java | 6 +- .../skyblocker/skyblock/chat/ChatRulesHandler.java | 65 ++++++++++++++++++++++ 6 files changed, 137 insertions(+), 55 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') diff --git a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java index 1aa97526..59383ff9 100644 --- a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java +++ b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java @@ -6,6 +6,7 @@ import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.debug.Debug; import de.hysky.skyblocker.skyblock.*; import de.hysky.skyblocker.skyblock.crimson.kuudra.Kuudra; +import de.hysky.skyblocker.skyblock.chat.ChatRulesHandler; import de.hysky.skyblocker.skyblock.dungeon.*; import de.hysky.skyblocker.skyblock.dungeon.partyfinder.PartyFinderScreen; import de.hysky.skyblocker.skyblock.dungeon.puzzle.boulder.Boulder; @@ -109,6 +110,7 @@ public class SkyblockerMod implements ClientModInitializer { CrystalsLocationsManager.init(); ChatMessageListener.init(); Shortcuts.init(); + ChatRulesHandler.init(); DiscordRPCManager.init(); LividColor.init(); FishingHelper.init(); 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 ebf0a310..e212b2a0 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java @@ -1,6 +1,7 @@ package de.hysky.skyblocker.skyblock.chat; import de.hysky.skyblocker.utils.Utils; +import dev.isxander.yacl3.config.v2.api.SerialEntry; import net.minecraft.client.MinecraftClient; import net.minecraft.client.sound.Sound; import net.minecraft.item.ItemStack; @@ -13,22 +14,21 @@ import java.util.regex.Pattern; */ public class ChatRule { - private String name; + public String name; //inputs - private Boolean enabled; - private Boolean isPartialMatch; - private Boolean isRegex; - private String filter; - private LocationOption validLocation; - private List validItems; - //output - private Boolean hideMessage; - private Boolean showActionBar; - private Boolean showAnnouncement; - private String replaceMessage; //todo extract parts of original message - private Sound customSound; + public Boolean enabled; + public Boolean isPartialMatch; + public Boolean isRegex; + public String filter; + public LocationOption validLocation; + //output + public Boolean hideMessage; + public Boolean showActionBar; + public Boolean showAnnouncement; + public String replaceMessage; //todo extract parts of original message + public Sound customSound; /** * Creates a chat rule with default options. */ @@ -40,7 +40,6 @@ public class ChatRule { this.isRegex = false; this.filter = ""; this.validLocation = LocationOption.None; - this.validItems = List.of(); this.hideMessage = true; this.showActionBar = false; @@ -57,7 +56,6 @@ public class ChatRule { this.isRegex = isRegex; this.filter = filter; this.validLocation = validLocation; - this.validItems = validItems; this.hideMessage = hideMessage; this.showActionBar = showActionBar; this.showAnnouncement = showAnnouncement; @@ -105,13 +103,7 @@ public class ChatRule { this.validLocation = validLocation; } - public List getValidItems() { - return validItems; - } - public void setValidItems(List validItems) { - this.validItems = validItems; - } public Boolean getHideMessage() { return hideMessage; @@ -146,7 +138,7 @@ public class ChatRule { } public Sound getCustomSound() { - return customSound; + return customSound; } public void setCustomSound(Sound customSound) { @@ -193,12 +185,6 @@ public class ChatRule { default -> {} } - //held items - if (!validItems.isEmpty()){ - if (client.player == null) return false; - ItemStack heldItem = client.player.getMainHandStack(); - if (!validItems.contains(heldItem)) return false; - } return true; } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java index ef0f6d92..0dc912d2 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java @@ -16,7 +16,8 @@ public class ChatRuleConfigScreen extends Screen { private static final int SPACER_X = 5; private static final int SPACER_Y = 25; - private ChatRule chatRule; + private final int chatRuleIndex; + private final ChatRule chatRule; //widgets private ButtonWidget finishButton; @@ -61,9 +62,10 @@ public class ChatRuleConfigScreen extends Screen { - public ChatRuleConfigScreen(Screen parent, ChatRule chatRule) { + public ChatRuleConfigScreen(Screen parent, int chatRuleIndex) { super(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen")); - this.chatRule = chatRule; + this.chatRuleIndex = chatRuleIndex; + this.chatRule = ChatRulesHandler.chatRuleList.get(chatRuleIndex); this.parent = parent; } @@ -160,8 +162,6 @@ public class ChatRuleConfigScreen extends Screen { .size(75,20) .build(); - - addDrawableChild(nameInput); addDrawableChild(filterInput); addDrawableChild(partialMatchToggle); @@ -171,8 +171,6 @@ public class ChatRuleConfigScreen extends Screen { addDrawableChild(announcementToggle); addDrawableChild(replaceMessageInput); addDrawableChild(finishButton); - - } private Text enabledButtonText(boolean enabled) { @@ -208,7 +206,15 @@ public class ChatRuleConfigScreen extends Screen { //todo add checks to see if valid rule e.g. has name //and if valid save a if (client != null ) { + save(); client.setScreen(parent); } } + private void save(){ + chatRule.setName(nameInput.getText()); + chatRule.setFilter(filterInput.getText()); + chatRule.setReplaceMessage(replaceMessageInput.getText()); + + ChatRulesHandler.chatRuleList.set(chatRuleIndex,chatRule); + } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java index e5f7ddef..07397f98 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java @@ -4,7 +4,6 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.Element; import net.minecraft.client.gui.Selectable; -import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.gui.screen.narration.NarrationPart; import net.minecraft.client.gui.widget.ButtonWidget; @@ -17,20 +16,31 @@ import java.util.List; public class ChatRulesConfigListWidget extends ElementListWidget { - private List chatRules; private final ChatRulesConfigScreen screen; + private Boolean hasChanged; + public ChatRulesConfigListWidget(MinecraftClient minecraftClient, ChatRulesConfigScreen screen, int width, int height, int y, int itemHeight) { super(minecraftClient, width, height, y, itemHeight); this.screen = screen; - chatRules = List.of(); //todo load existing + this.hasChanged = false; //add entry fall all existing rules - for (ChatRule rule : chatRules){ - addEntry(new chatRuleConfigEntry(rule)); + for (int i = 0; i < (long) ChatRulesHandler.chatRuleList.size(); i++){ + addEntry(new chatRuleConfigEntry(i)); } } + @Override + public int getRowWidth() { + return super.getRowWidth() + 100; + } + + @Override + protected int getScrollbarPositionX() { + return super.getScrollbarPositionX() + 50; + } + @Override public void setSelected(@Nullable ChatRulesConfigListWidget.chatRuleConfigEntry entry) { super.setSelected(entry); @@ -38,31 +48,38 @@ public class ChatRulesConfigListWidget extends ElementListWidget { + + protected boolean hasChanges(){ + return (hasChanged || children().stream().anyMatch(chatRuleConfigEntry::hasChange)); + } + + public class chatRuleConfigEntry extends Entry { private static final int SPACING = 20; //data + private int chatRuleIndex; private ChatRule chatRule; + private final List children; //widgets @@ -74,8 +91,9 @@ public class ChatRulesConfigListWidget extends ElementListWidget { - client.setScreen(new ChatRuleConfigScreen(screen, chatRule)); + client.setScreen(new ChatRuleConfigScreen(screen, chatRuleIndex)); }) .size(100,20) .position(currentX,5) @@ -114,10 +132,13 @@ public class ChatRulesConfigListWidget extends ElementListWidget selectableChildren() { return List.of(new Selectable() { @@ -149,5 +170,9 @@ public class ChatRulesConfigListWidget extends ElementListWidget { if (confirmedAction) { this.client.setScreen(parent); @@ -103,8 +103,6 @@ public class ChatRulesConfigScreen extends Screen { } protected void updateButtons() { - buttonDelete.active = Shortcuts.isShortcutsLoaded() && chatRulesConfigListWidget.getSelectedOrNull() != null; - buttonNew.active = Shortcuts.isShortcutsLoaded() ; //todo ? && chatRuleConfigListWidget.getCategory().isPresent() - buttonDone.active = Shortcuts.isShortcutsLoaded(); + buttonDelete.active = chatRulesConfigListWidget.getSelectedOrNull() != null; } } 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 4ea8c24d..dc2ab0f1 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -1,4 +1,69 @@ package de.hysky.skyblocker.skyblock.chat; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.reflect.TypeToken; +import de.hysky.skyblocker.SkyblockerMod; +import de.hysky.skyblocker.skyblock.shortcut.Shortcuts; +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.text.Text; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.lang.reflect.Type; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + public class ChatRulesHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(ChatRule.class); + private static final Path CHAT_RULE_FILE = SkyblockerMod.CONFIG_DIR.resolve("chatRules.json"); + + protected static final List chatRuleList = new ArrayList<>(); + + public static void init() { + loadChatRules(); + ClientReceiveMessageEvents.GAME.register(ChatRulesHandler::checkMessage); + } + + private static void loadChatRules() { + try (BufferedReader reader = Files.newBufferedReader(CHAT_RULE_FILE)) { + Type chatRulesType = new TypeToken>>() { + }.getType(); + Map> chatRules = SkyblockerMod.GSON.fromJson(reader,chatRulesType); + chatRuleList.addAll(chatRules.get("rules")); + + LOGGER.info("[Skyblocker] Loaded chat rules"); + } catch (NoSuchFileException e) { + //todo create default chat rules + LOGGER.warn("[Skyblocker] chat rule file not found, using default rules. This is normal when using for the first time."); + } catch (IOException e) { + LOGGER.error("[Skyblocker] Failed to load shortcuts file", e); + } + } + + protected static void saveChatRules() { + JsonObject chatRuleJson = new JsonObject(); + chatRuleJson.add("rules", SkyblockerMod.GSON.toJsonTree(chatRuleList)); + try (BufferedWriter writer = Files.newBufferedWriter(CHAT_RULE_FILE)) { + SkyblockerMod.GSON.toJson(chatRuleJson, writer); + LOGGER.info("[Skyblocker] Saved chat rules file"); + } catch (IOException e) { + LOGGER.error("[Skyblocker] Failed to save chat rules file", e); + } + } + + private static void checkMessage(Text message, Boolean overlay) { + + } + } -- cgit From 74a9ebefcf4e77ed17e8799175433bcf238a605b Mon Sep 17 00:00:00 2001 From: olim Date: Thu, 15 Feb 2024 15:23:50 +0000 Subject: make the rules acutaly work now scans each chat message with the rules to see if they match --- .../config/categories/MessageFilterCategory.java | 1 + .../hysky/skyblocker/skyblock/chat/ChatRule.java | 30 +++++++++++---- .../skyblocker/skyblock/chat/ChatRulesHandler.java | 44 +++++++++++++++++++++- 3 files changed, 65 insertions(+), 10 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') diff --git a/src/main/java/de/hysky/skyblocker/config/categories/MessageFilterCategory.java b/src/main/java/de/hysky/skyblocker/config/categories/MessageFilterCategory.java index 18c3bc99..ddad018a 100644 --- a/src/main/java/de/hysky/skyblocker/config/categories/MessageFilterCategory.java +++ b/src/main/java/de/hysky/skyblocker/config/categories/MessageFilterCategory.java @@ -126,6 +126,7 @@ public class MessageFilterCategory { () -> config.messages.hideDicer, newValue -> config.messages.hideDicer = newValue) .controller(ConfigUtils::createEnumCyclingListController) + .build()) //chat rules options .group(OptionGroup.createBuilder() .name(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules")) 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 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 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]", ""); } } -- cgit From d27d35e4caa652bd84a2421e2d544920ce9aa5fd Mon Sep 17 00:00:00 2001 From: olim Date: Thu, 15 Feb 2024 22:06:08 +0000 Subject: added annoucments added the announcement output option for the chat rules --- .../java/de/hysky/skyblocker/SkyblockerMod.java | 2 + .../hysky/skyblocker/config/SkyblockerConfig.java | 9 ++++ .../config/categories/MessageFilterCategory.java | 18 +++++++- .../hysky/skyblocker/skyblock/chat/ChatRule.java | 4 +- .../skyblock/chat/ChatRuleAnnouncementScreen.java | 53 ++++++++++++++++++++++ .../skyblocker/skyblock/chat/ChatRulesHandler.java | 13 ++---- .../resources/assets/skyblocker/lang/en_us.json | 4 ++ 7 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') diff --git a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java index 59383ff9..fc72ea3f 100644 --- a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java +++ b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java @@ -5,6 +5,7 @@ import com.google.gson.GsonBuilder; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.debug.Debug; import de.hysky.skyblocker.skyblock.*; +import de.hysky.skyblocker.skyblock.chat.ChatRuleAnnouncementScreen; import de.hysky.skyblocker.skyblock.crimson.kuudra.Kuudra; import de.hysky.skyblocker.skyblock.chat.ChatRulesHandler; import de.hysky.skyblocker.skyblock.dungeon.*; @@ -111,6 +112,7 @@ public class SkyblockerMod implements ClientModInitializer { ChatMessageListener.init(); Shortcuts.init(); ChatRulesHandler.init(); + ChatRuleAnnouncementScreen.init(); DiscordRPCManager.init(); LividColor.init(); FishingHelper.init(); diff --git a/src/main/java/de/hysky/skyblocker/config/SkyblockerConfig.java b/src/main/java/de/hysky/skyblocker/config/SkyblockerConfig.java index b5ddcf5d..ea1f7d43 100644 --- a/src/main/java/de/hysky/skyblocker/config/SkyblockerConfig.java +++ b/src/main/java/de/hysky/skyblocker/config/SkyblockerConfig.java @@ -1186,6 +1186,15 @@ public class SkyblockerConfig { @SerialEntry public ChatFilterResult hideDicer = ChatFilterResult.PASS; + + @SerialEntry + public ChatRuleConfig chatRuleConfig = new ChatRuleConfig(); + } + public static class ChatRuleConfig { + @SerialEntry + public int announcementLength = 60; + @SerialEntry + public int announcementScale = 3; } public enum Info { diff --git a/src/main/java/de/hysky/skyblocker/config/categories/MessageFilterCategory.java b/src/main/java/de/hysky/skyblocker/config/categories/MessageFilterCategory.java index ddad018a..0f95bcaa 100644 --- a/src/main/java/de/hysky/skyblocker/config/categories/MessageFilterCategory.java +++ b/src/main/java/de/hysky/skyblocker/config/categories/MessageFilterCategory.java @@ -6,6 +6,7 @@ import de.hysky.skyblocker.skyblock.chat.ChatRulesConfigScreen; import de.hysky.skyblocker.skyblock.dwarven.CrystalsHudConfigScreen; import de.hysky.skyblocker.utils.chat.ChatFilterResult; import dev.isxander.yacl3.api.*; +import dev.isxander.yacl3.api.controller.IntegerSliderControllerBuilder; import net.minecraft.client.MinecraftClient; import net.minecraft.text.Text; @@ -136,7 +137,22 @@ public class MessageFilterCategory { .text(Text.translatable("text.skyblocker.open")) .action((screen, opt) -> MinecraftClient.getInstance().setScreen(new ChatRulesConfigScreen(screen))) .build()) - + .option(Option.createBuilder() + .name(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.announcementLength")) + .description(OptionDescription.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.announcementLength.@Tooltip"))) + .binding(defaults.messages.chatRuleConfig.announcementLength, + () -> config.messages.chatRuleConfig.announcementLength, + newValue -> config.messages.chatRuleConfig.announcementLength = newValue) + .controller(opt -> IntegerSliderControllerBuilder.create(opt).range(5, 200).step(1)) + .build()) + .option(Option.createBuilder() + .name(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.announcementScale")) + .description(OptionDescription.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.announcementScale.@Tooltip"))) + .binding(defaults.messages.chatRuleConfig.announcementScale, + () -> config.messages.chatRuleConfig.announcementScale, + newValue -> config.messages.chatRuleConfig.announcementScale = newValue) + .controller(opt -> IntegerSliderControllerBuilder.create(opt).range(1, 8).step(1)) + .build()) .build()) .build(); } 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 9643b413..af35647f 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java @@ -174,7 +174,7 @@ public class ChatRule { } //filter - if (testFilter.isEmpty()) return false; + if (testFilter.isBlank()) return false; if(isRegex) { if (isPartialMatch) { if (! Pattern.compile(testFilter).matcher(testString).find()) return false; @@ -190,7 +190,7 @@ public class ChatRule { } //location - if (validLocations.isEmpty()){ //if no locations do not check + if (validLocations.isBlank()){ //if no locations do not check return true; } String rawLocation = Utils.getLocationRaw(); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java new file mode 100644 index 00000000..e6300808 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java @@ -0,0 +1,53 @@ +package de.hysky.skyblocker.skyblock.chat; + +import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.skyblock.dwarven.CrystalsHudConfigScreen; +import de.hysky.skyblocker.utils.scheduler.Scheduler; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; + +public class ChatRuleAnnouncementScreen { + + private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); + private static float timer; + + private static Text text = null; + + public static void init() { + HudRenderCallback.EVENT.register((context, tickDelta) -> { + if (timer <= 0 || text == null) { + return; + } + render(context, tickDelta); + }); + } + + /** + * renders {@link ChatRuleAnnouncementScreen#text} to the middle of the top of the screen. + * @param context render context + * @param tickDelta difference from last render to remove from timer + */ + private static void render(DrawContext context, float tickDelta) { + int scale = SkyblockerConfigManager.get().messages.chatRuleConfig.announcementScale; + //decrement timer + timer -= tickDelta; + //scale text up and center + MatrixStack matrices = context.getMatrices(); + matrices.push(); + matrices.translate(context.getScaledWindowWidth() / 2f, context.getScaledWindowHeight() * 0.3, 0f); + matrices.scale(scale, scale, 0f); + //render text + context.drawCenteredTextWithShadow(CLIENT.textRenderer,text,0, 0, 0xFFFFFF); + + matrices.pop(); + } + protected static void setText(Text newText) { + text = newText; + timer = SkyblockerConfigManager.get().messages.chatRuleConfig.announcementLength; + } +} 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 f8823589..3e933f1a 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -1,15 +1,10 @@ package de.hysky.skyblocker.skyblock.chat; -import com.google.gson.Gson; 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; @@ -78,14 +73,16 @@ public class ChatRulesHandler { if (rule.isMatch(plain)) { //get a replacement message Text newMessage; - if (!rule.getReplaceMessage().isEmpty()){ + if (!rule.getReplaceMessage().isBlank()){ newMessage = Text.of(rule.getReplaceMessage()); } else { newMessage =message; } - //todo show announcement + if (rule.getShowAnnouncement()){ + ChatRuleAnnouncementScreen.setText(newMessage); + } //show in action bar if (rule.getShowActionBar() && CLIENT.player != null) { @@ -102,7 +99,7 @@ public class ChatRulesHandler { return true; } private static String trimItemColor(String str) { - if (str.isEmpty()) return str; + if (str.isBlank()) return str; return str.replaceAll("§[0-9a-g]", ""); } diff --git a/src/main/resources/assets/skyblocker/lang/en_us.json b/src/main/resources/assets/skyblocker/lang/en_us.json index 390e0c7f..bdb963c2 100644 --- a/src/main/resources/assets/skyblocker/lang/en_us.json +++ b/src/main/resources/assets/skyblocker/lang/en_us.json @@ -367,6 +367,10 @@ "text.autoconfig.skyblocker.option.messages.hideDicer": "Hide Dicer Messages", "text.autoconfig.skyblocker.option.messages.hideDicer.@Tooltip": "Filters the Dicer messages from chat.", "text.autoconfig.skyblocker.option.messages.chatRules": "Custom Chat Rules", + "text.autoconfig.skyblocker.option.messages.chatRules.announcementLength": "Announcement Display Time", + "text.autoconfig.skyblocker.option.messages.chatRules.announcementLength.@Tooltip": "The amount of time the announcements will show for in ticks.", + "text.autoconfig.skyblocker.option.messages.chatRules.announcementScale": "Announcement Size", + "text.autoconfig.skyblocker.option.messages.chatRules.announcementScale.@Tooltip": "The amount to scale the size of the announcement.", "text.autoconfig.skyblocker.option.messages.chatRules.screen": "Chat Rules Config...", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen": "Chat Rules Config...", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.true": "True", -- cgit From 2787e958d990972f62834c7083978ff7012e7095 Mon Sep 17 00:00:00 2001 From: olim Date: Fri, 16 Feb 2024 21:18:16 +0000 Subject: greatly improve the config ui improve the layout and use of the config ui to be close to its final state hopefully --- .../skyblock/chat/ChatRuleConfigScreen.java | 8 +- .../skyblock/chat/ChatRulesConfigListWidget.java | 120 +++++++++++++-------- .../skyblock/chat/ChatRulesConfigScreen.java | 36 +------ .../skyblocker/skyblock/chat/ChatRulesHandler.java | 4 +- .../resources/assets/skyblocker/lang/en_us.json | 5 + 5 files changed, 93 insertions(+), 80 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java index b3cf1200..c38271f5 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java @@ -88,8 +88,8 @@ public class ChatRuleConfigScreen extends Screen { filterLabelTextPos = currentPos; lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.filter")) + SPACER_X; filterInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); - filterInput.setText(chatRule.getFilter()); filterInput.setMaxLength(96); + filterInput.setText(chatRule.getFilter()); currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); lineXOffset = 0; @@ -168,13 +168,13 @@ public class ChatRuleConfigScreen extends Screen { replaceMessageLabelTextPos = currentPos; lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace")) + SPACER_X; replaceMessageInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); - replaceMessageInput.setText(chatRule.getReplaceMessage()); replaceMessageInput.setMaxLength(96); + replaceMessageInput.setText(chatRule.getReplaceMessage()); finishButton = ButtonWidget.builder(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.finish"), a -> { close(); }) - .position((int) (this.width * 0.66), this.height - SPACER_Y) + .position(this.width - 75 - SPACER_Y, this.height - SPACER_Y) .size(75,20) .build(); @@ -193,7 +193,7 @@ public class ChatRuleConfigScreen extends Screen { /** * works out the width of the maximum line - * @return + * @return the max used width */ private int getMaxUsedWidth() { if (client == null) return 0; diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java index 07397f98..347e40a0 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java @@ -4,17 +4,19 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.Element; import net.minecraft.client.gui.Selectable; +import net.minecraft.client.gui.screen.ConfirmScreen; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.gui.screen.narration.NarrationPart; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.ElementListWidget; +import net.minecraft.screen.ScreenTexts; import net.minecraft.text.Text; import org.jetbrains.annotations.Nullable; import java.awt.*; import java.util.List; -public class ChatRulesConfigListWidget extends ElementListWidget { +public class ChatRulesConfigListWidget extends ElementListWidget { private final ChatRulesConfigScreen screen; @@ -25,6 +27,8 @@ public class ChatRulesConfigListWidget extends ElementListWidget { } - public class chatRuleConfigEntry extends Entry { + private class chatRuleLabelsEntry extends AbstractChatRuleEntry { - private static final int SPACING = 20; + @Override + public List selectableChildren() { + return List.of(); + } + + @Override + public List children() { + return List.of(); + } + + @Override + public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + context.drawCenteredTextWithShadow(client.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleName"), width / 2 - 125, y + 5, 0xFFFFFF); + context.drawCenteredTextWithShadow(client.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleEnabled"), width / 2, y + 5, 0xFFFFFF); + context.drawCenteredTextWithShadow(client.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.modify"), width / 2 + 100, y + 5, 0xFFFFFF); + } + } + private class chatRuleConfigEntry extends AbstractChatRuleEntry { //data private int chatRuleIndex; private ChatRule chatRule; + private final List children; + - private final List children; + //widgets + private final ButtonWidget enabledButton; + private final ButtonWidget openConfigButton; + private final ButtonWidget deleteButton; - //widgets - private final ButtonWidget enabledWidget; - private final ButtonWidget openConfigWidget; //text locations - private final int labelX; - private final int enabledX; + private final int nameX = width / 2 - 125; + + //saved data + private double oldScrollAmount = 0; public chatRuleConfigEntry(int chatRuleIndex) { @@ -96,48 +120,57 @@ public class ChatRulesConfigListWidget extends ElementListWidget { + + enabledButton = ButtonWidget.builder(enabledButtonText() , a -> { toggleEnabled(); }) .size(50,20) - .position(currentX,5) + .position(width / 2 - 25,5) .build() ; - currentX += 50; - currentX += SPACING; //spacer - openConfigWidget = ButtonWidget.builder(Text.of("Edit Rule"), a -> { + openConfigButton = ButtonWidget.builder(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.editRule"), a -> { client.setScreen(new ChatRuleConfigScreen(screen, chatRuleIndex)); }) - .size(100,20) - .position(currentX,5) + .size(50,20) + .position(width / 2 + 45,5) .build() ; + deleteButton = ButtonWidget.builder(Text.translatable("selectServer.delete"), a -> { + oldScrollAmount = getScrollAmount(); + client.setScreen(new ConfirmScreen(this::deleteEntry, Text.translatable("skyblocker.shortcuts.deleteQuestion"), Text.translatable("skyblocker.shortcuts.deleteWarning", chatRule.getName()), Text.translatable("selectServer.deleteButton"), ScreenTexts.CANCEL)); + }) + .size(50,20) + .position(width / 2 + 105,5) + .build() + ; + - children = List.of(enabledWidget, openConfigWidget); + children = List.of(enabledButton, openConfigButton, deleteButton); } private Text enabledButtonText() { if (chatRule.getEnabled()){ - return Text.literal("TRUE").withColor(Color.green.getRGB()); + return Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.true").withColor(Color.green.getRGB()); }else { - return Text.literal("FALSE").withColor(Color.red.getRGB()); + return Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.false").withColor(Color.red.getRGB()); } } private void toggleEnabled() { hasChanged = true; chatRule.setEnabled(!chatRule.getEnabled()); - enabledWidget.setMessage(enabledButtonText()); + enabledButton.setMessage(enabledButtonText()); } - + private void deleteEntry(boolean confirmedAction) { + if (confirmedAction){ + //delete this + ChatRulesHandler.chatRuleList.remove(chatRuleIndex); + removeEntry(this); + } + client.setScreen(screen); + setScrollAmount(oldScrollAmount); + } @Override public List selectableChildren() { @@ -162,16 +195,17 @@ public class ChatRulesConfigListWidget extends ElementListWidget { - ChatRulesConfigListWidget.chatRuleConfigEntry currentChatRuleConfigEntry = chatRulesConfigListWidget.getSelectedOrNull(); - if (client != null && currentChatRuleConfigEntry != null ) { - scrollAmount = chatRulesConfigListWidget.getScrollAmount(); - client.setScreen(new ConfirmScreen(this::deleteEntry, Text.translatable("skyblocker.shortcuts.deleteQuestion"), Text.translatable("skyblocker.shortcuts.deleteWarning", currentChatRuleConfigEntry), Text.translatable("selectServer.deleteButton"), ScreenTexts.CANCEL)); //todo load text for this config - } - }).build(); - adder.add(buttonDelete); - buttonNew = ButtonWidget.builder(Text.translatable("skyblocker.shortcuts.new"), buttonNew -> chatRulesConfigListWidget.addRuleAfterSelected()).build(); - adder.add(buttonNew); + GridWidget.Adder adder = gridWidget.createAdder(3); adder.add(ButtonWidget.builder(ScreenTexts.CANCEL, button -> { if (client != null) { close(); } }).build()); + buttonNew = ButtonWidget.builder(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.new"), buttonNew -> chatRulesConfigListWidget.addRuleAfterSelected()).build(); + adder.add(buttonNew); buttonDone = ButtonWidget.builder(ScreenTexts.DONE, button -> { chatRulesConfigListWidget.saveRules(); if (client != null) { close(); } - }).tooltip(Tooltip.of(Text.translatable("skyblocker.shortcuts.commandSuggestionTooltip"))).build(); + }).build(); adder.add(buttonDone); gridWidget.refreshPositions(); SimplePositioningWidget.setPos(gridWidget, 0, this.height - 64, this.width, 64); gridWidget.forEachChild(this::addDrawableChild); - updateButtons(); - } - - private void deleteEntry(boolean confirmedAction) { - if (client != null) { - ChatRulesConfigListWidget.chatRuleConfigEntry currentChatRuleConfigEntry = chatRulesConfigListWidget.getSelectedOrNull(); - if (confirmedAction && currentChatRuleConfigEntry != null) { - chatRulesConfigListWidget.removeEntry(currentChatRuleConfigEntry); - } - client.setScreen(this); // Re-inits the screen and keeps the old instance of ShortcutsConfigListWidget - chatRulesConfigListWidget.setScrollAmount(scrollAmount); - } } @Override @@ -101,8 +79,4 @@ public class ChatRulesConfigScreen extends Screen { this.client.setScreen(parent); } } - - protected void updateButtons() { - buttonDelete.active = chatRulesConfigListWidget.getSelectedOrNull() != null; - } } 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 3e933f1a..035e7e8f 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -69,7 +69,7 @@ public class ChatRulesHandler { 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) { + for (ChatRule rule : chatRuleList) { if (rule.isMatch(plain)) { //get a replacement message Text newMessage; @@ -77,7 +77,7 @@ public class ChatRulesHandler { newMessage = Text.of(rule.getReplaceMessage()); } else { - newMessage =message; + newMessage = message; } if (rule.getShowAnnouncement()){ diff --git a/src/main/resources/assets/skyblocker/lang/en_us.json b/src/main/resources/assets/skyblocker/lang/en_us.json index bdb963c2..bd8b9bb8 100644 --- a/src/main/resources/assets/skyblocker/lang/en_us.json +++ b/src/main/resources/assets/skyblocker/lang/en_us.json @@ -372,6 +372,11 @@ "text.autoconfig.skyblocker.option.messages.chatRules.announcementScale": "Announcement Size", "text.autoconfig.skyblocker.option.messages.chatRules.announcementScale.@Tooltip": "The amount to scale the size of the announcement.", "text.autoconfig.skyblocker.option.messages.chatRules.screen": "Chat Rules Config...", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.new": "New Chat Rule", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleName": "Rule Name", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleEnabled": "Rule Enabled", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.modify": "Modify", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.editRule": "Edit", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen": "Chat Rules Config...", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.true": "True", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.false": "False", -- cgit From 4a15a2c47d4e6e8c514a3bf0f62f1706d08e4b0e Mon Sep 17 00:00:00 2001 From: olim Date: Sat, 17 Feb 2024 19:54:48 +0000 Subject: add sound effect added the ability to choose between a few sound effects to play when a message is received --- .../hysky/skyblocker/skyblock/chat/ChatRule.java | 24 +++------- .../skyblock/chat/ChatRuleConfigScreen.java | 51 ++++++++++++++++++++-- .../skyblocker/skyblock/chat/ChatRulesHandler.java | 14 ++++-- .../resources/assets/skyblocker/lang/en_us.json | 9 ++++ 4 files changed, 72 insertions(+), 26 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') 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 0fe1c3f1..97431305 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java @@ -3,6 +3,8 @@ package de.hysky.skyblocker.skyblock.chat; import de.hysky.skyblocker.utils.Utils; import net.minecraft.client.sound.Sound; import net.minecraft.item.ItemStack; +import net.minecraft.sound.SoundEvent; +import net.minecraft.sound.SoundEvents; import java.util.List; import java.util.Objects; @@ -28,7 +30,7 @@ public class ChatRule { private Boolean showActionBar; private Boolean showAnnouncement; private String replaceMessage; //todo extract parts of original message - private Sound customSound; + private SoundEvent customSound; /** * Creates a chat rule with default options. */ @@ -49,22 +51,6 @@ public class ChatRule { this.customSound = null; } - - public ChatRule(String name, Boolean enabled, Boolean isPartialMatch, Boolean isRegex, Boolean isIgnoreCase, String filter, String validLocation, List 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.validLocations = validLocation; - this.hideMessage = hideMessage; - this.showActionBar = showActionBar; - this.showAnnouncement = showAnnouncement; - this.replaceMessage = replaceMessage; - this.customSound = customSound; - } - public Boolean getEnabled() { //todo remove unused getters and set return enabled; } @@ -137,11 +123,11 @@ public class ChatRule { this.replaceMessage = replaceMessage; } - public Sound getCustomSound() { + public SoundEvent getCustomSound() { return customSound; } - public void setCustomSound(Sound customSound) { + public void setCustomSound(SoundEvent customSound) { this.customSound = customSound; } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java index c38271f5..f46af79b 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java @@ -6,16 +6,32 @@ import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.TextFieldWidget; +import net.minecraft.sound.SoundEvent; +import net.minecraft.sound.SoundEvents; +import net.minecraft.text.MutableText; import net.minecraft.text.Text; import java.awt.*; +import java.util.Map; + +import static java.util.Map.entry; public class ChatRuleConfigScreen extends Screen { private static final int SPACER_X = 5; private static final int SPACER_Y = 25; + private final Map soundsLookup = Map.ofEntries( + entry(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.pling"), SoundEvents.BLOCK_NOTE_BLOCK_PLING.value()), + entry(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.cave"), SoundEvents.AMBIENT_CAVE.value()), + entry(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.zombie"), SoundEvents.ENTITY_ZOMBIE_AMBIENT), + entry(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.crit"), SoundEvents.ENTITY_PLAYER_ATTACK_CRIT), + entry(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.arrowHit"), SoundEvents.ENTITY_ARROW_HIT_PLAYER), + entry(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.amethyst"), SoundEvents.BLOCK_AMETHYST_BLOCK_HIT), + entry(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.anvil"), SoundEvents.BLOCK_ANVIL_LAND) + );//todo amathis / more sounds + private final int chatRuleIndex; private final ChatRule chatRule; @@ -32,8 +48,8 @@ public class ChatRuleConfigScreen extends Screen { private ButtonWidget hideMessageToggle; private ButtonWidget actionBarToggle; private ButtonWidget announcementToggle; + private ButtonWidget soundsToggle; private TextFieldWidget replaceMessageInput; - //todo custom sound thing //textLocations private IntIntPair nameLabelTextPos; @@ -51,11 +67,10 @@ public class ChatRuleConfigScreen extends Screen { private IntIntPair hideMessageTextPos; private IntIntPair actionBarTextPos; private IntIntPair announcementTextPos; - - private IntIntPair replaceMessageLabelTextPos; - private IntIntPair customSoundLabelTextPos; + private IntIntPair replaceMessageLabelTextPos; + private int currentSoundIndex; private final Screen parent; @@ -66,6 +81,7 @@ public class ChatRuleConfigScreen extends Screen { this.chatRuleIndex = chatRuleIndex; this.chatRule = ChatRulesHandler.chatRuleList.get(chatRuleIndex); this.parent = parent; + this.currentSoundIndex = soundsLookup.values().stream().toList().indexOf(chatRule.getCustomSound()); } @Override @@ -164,7 +180,23 @@ public class ChatRuleConfigScreen extends Screen { .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) .size(75,20) .build(); + lineXOffset += 75 + SPACER_X; + customSoundLabelTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset,currentPos.rightInt()); + lineXOffset += client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds")) + SPACER_X; + soundsToggle = ButtonWidget.builder(getSoundName(), a -> { + currentSoundIndex += 1; + if (currentSoundIndex == soundsLookup.size()) { + currentSoundIndex = -1; + } + MutableText newText = getSoundName(); + soundsToggle.setMessage(newText); + chatRule.setCustomSound(soundsLookup.get(newText)); + }) + .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) + .size(100,20) + .build(); currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); + replaceMessageLabelTextPos = currentPos; lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace")) + SPACER_X; replaceMessageInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); @@ -187,6 +219,7 @@ public class ChatRuleConfigScreen extends Screen { addDrawableChild(hideMessageToggle); addDrawableChild(actionBarToggle); addDrawableChild(announcementToggle); + addDrawableChild(soundsToggle); addDrawableChild(replaceMessageInput); addDrawableChild(finishButton); } @@ -239,6 +272,7 @@ public class ChatRuleConfigScreen extends Screen { context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.hideMessage"), hideMessageTextPos.leftInt(), hideMessageTextPos.rightInt() + yOffset, 0xFFFFFF); context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.actionBar"), actionBarTextPos.leftInt(), actionBarTextPos.rightInt() + yOffset, 0xFFFFFF); context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.announcement"), announcementTextPos.leftInt(), announcementTextPos.rightInt() + yOffset, 0xFFFFFF); + context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds"), customSoundLabelTextPos.leftInt(), customSoundLabelTextPos.rightInt() + yOffset, 0xFFFFFF); context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace"), replaceMessageLabelTextPos.leftInt(), replaceMessageLabelTextPos.rightInt() + yOffset, 0xFFFFFF); } @@ -259,4 +293,13 @@ public class ChatRuleConfigScreen extends Screen { ChatRulesHandler.chatRuleList.set(chatRuleIndex,chatRule); } + + private MutableText getSoundName() { + if (currentSoundIndex == -1){ + return Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.none"); + } + return soundsLookup.keySet().stream().toList().get(currentSoundIndex); + } + + } 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 035e7e8f..3f61d217 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -6,6 +6,7 @@ import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.utils.Utils; import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents; import net.minecraft.client.MinecraftClient; +import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -73,14 +74,14 @@ public class ChatRulesHandler { if (rule.isMatch(plain)) { //get a replacement message Text newMessage; - if (!rule.getReplaceMessage().isBlank()){ + if (!rule.getReplaceMessage().isBlank()) { newMessage = Text.of(rule.getReplaceMessage()); } else { newMessage = message; } - if (rule.getShowAnnouncement()){ + if (rule.getShowAnnouncement()) { ChatRuleAnnouncementScreen.setText(newMessage); } @@ -88,10 +89,17 @@ public class ChatRulesHandler { if (rule.getShowActionBar() && CLIENT.player != null) { CLIENT.player.sendMessage(newMessage, true); } + //hide message - if (!rule.getHideMessage() && CLIENT.player != null){ + if (!rule.getHideMessage() && CLIENT.player != null) { CLIENT.player.sendMessage(newMessage, false); } + + //play sound + if (rule.getCustomSound() != null && CLIENT.player != null) { + CLIENT.player.playSound(rule.getCustomSound(), 100f, 0.1f); + } + //do not send original message return false; } diff --git a/src/main/resources/assets/skyblocker/lang/en_us.json b/src/main/resources/assets/skyblocker/lang/en_us.json index bd8b9bb8..079d060e 100644 --- a/src/main/resources/assets/skyblocker/lang/en_us.json +++ b/src/main/resources/assets/skyblocker/lang/en_us.json @@ -393,6 +393,15 @@ "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.announcement": "Show Announcement:", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace": "Replace Message With:", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.finish": "finish", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds": "Play Sound:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.none": "None", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.pling": "Pling", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.cave": "Cave", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.zombie": "Zombie", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.crit": "Critical", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.arrowHit": "Dink", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.amethyst": "Amethyst", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.anvil": "Break", "text.autoconfig.skyblocker.category.slayer": "Slayers", "text.autoconfig.skyblocker.option.slayer.endermanSlayer": "[Beta] Enderman Slayer", "text.autoconfig.skyblocker.option.slayer.endermanSlayer.highlightNukekubiHeads": "Nukekubi Head Highlighting", -- cgit From 9dbe1d0311ad6ac00e55cfbc37f72336a64ac77b Mon Sep 17 00:00:00 2001 From: olim Date: Sat, 17 Feb 2024 20:06:27 +0000 Subject: fix file name --- src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') 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 3f61d217..29ee1386 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -25,7 +25,7 @@ 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"); + private static final Path CHAT_RULE_FILE = SkyblockerMod.CONFIG_DIR.resolve("chat_rules.json"); protected static final List chatRuleList = new ArrayList<>(); -- cgit From 1aa1a1328f91f6cab39a958396a7eb854ad2ca13 Mon Sep 17 00:00:00 2001 From: olim Date: Sun, 18 Feb 2024 11:48:58 +0000 Subject: add text style to the replacement text make it so styles can be added to the replacement text --- .../skyblock/chat/ChatRuleAnnouncementScreen.java | 8 ++--- .../skyblock/chat/ChatRuleConfigScreen.java | 6 +++- .../skyblock/chat/ChatRulesConfigListWidget.java | 1 - .../skyblock/chat/ChatRulesConfigScreen.java | 6 ++-- .../skyblocker/skyblock/chat/ChatRulesHandler.java | 36 ++++++++++++++++++---- .../skyblock/chat/ChatRulesHandlerTest.java | 35 +++++++++++++++++++++ 6 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandlerTest.java (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java index e6300808..0ee3c2c5 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java @@ -1,16 +1,13 @@ package de.hysky.skyblocker.skyblock.chat; import de.hysky.skyblocker.config.SkyblockerConfigManager; -import de.hysky.skyblocker.skyblock.dwarven.CrystalsHudConfigScreen; -import de.hysky.skyblocker.utils.scheduler.Scheduler; -import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; -import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; + public class ChatRuleAnnouncementScreen { private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); @@ -25,8 +22,11 @@ public class ChatRuleAnnouncementScreen { } render(context, tickDelta); }); + } + + /** * renders {@link ChatRuleAnnouncementScreen#text} to the middle of the top of the screen. * @param context render context diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java index f46af79b..7d2280f0 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java @@ -190,7 +190,11 @@ public class ChatRuleConfigScreen extends Screen { } MutableText newText = getSoundName(); soundsToggle.setMessage(newText); - chatRule.setCustomSound(soundsLookup.get(newText)); + SoundEvent sound = soundsLookup.get(newText); + chatRule.setCustomSound(sound); + if (client.player != null && sound != null) { + client.player.playSound(sound, 100f, 0.1f); + } }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) .size(100,20) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java index 4e8038d6..84e9082d 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java @@ -11,7 +11,6 @@ import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.ElementListWidget; import net.minecraft.screen.ScreenTexts; import net.minecraft.text.Text; -import org.jetbrains.annotations.Nullable; import java.awt.*; import java.util.List; diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigScreen.java index 2cbb735b..11ea5a7a 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigScreen.java @@ -1,10 +1,8 @@ package de.hysky.skyblocker.skyblock.chat; -import de.hysky.skyblocker.skyblock.shortcut.Shortcuts; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.ConfirmScreen; import net.minecraft.client.gui.screen.Screen; -import net.minecraft.client.gui.tooltip.Tooltip; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.GridWidget; import net.minecraft.client.gui.widget.SimplePositioningWidget; @@ -53,7 +51,11 @@ public class ChatRulesConfigScreen extends Screen { close(); } }).build(); + adder.add(buttonDone); + gridWidget.refreshPositions(); + SimplePositioningWidget.setPos(gridWidget, 0, this.height - 64, this.width, 64); gridWidget.forEachChild(this::addDrawableChild); + } @Override 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 29ee1386..4afedc52 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -6,8 +6,10 @@ import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.utils.Utils; import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents; import net.minecraft.client.MinecraftClient; -import net.minecraft.sound.SoundEvents; +import net.minecraft.text.MutableText; +import net.minecraft.text.Style; import net.minecraft.text.Text; +import net.minecraft.util.Formatting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -69,13 +71,13 @@ public class ChatRulesHandler { 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()); + String plain = Formatting.strip(message.getString()); for (ChatRule rule : chatRuleList) { if (rule.isMatch(plain)) { //get a replacement message Text newMessage; if (!rule.getReplaceMessage().isBlank()) { - newMessage = Text.of(rule.getReplaceMessage()); + newMessage = formatText(rule.getReplaceMessage()); } else { newMessage = message; @@ -106,9 +108,31 @@ public class ChatRulesHandler { } return true; } - private static String trimItemColor(String str) { - if (str.isBlank()) return str; - return str.replaceAll("§[0-9a-g]", ""); + + /** + * Converts a string with color codes into a formatted Text object + * @param codedString the string with color codes in + * @return formatted text + */ + + protected static MutableText formatText(String codedString) { + if (codedString.contains(String.valueOf(Formatting.FORMATTING_CODE_PREFIX)) || codedString.contains("&")){ + MutableText newText = Text.literal(""); + String[] parts = codedString.split("[" + Formatting.FORMATTING_CODE_PREFIX +"&]"); + Style style = Style.EMPTY; + for (String part : parts) { + if (part.isEmpty()) continue; + Formatting formatting = Formatting.byCode(part.charAt(0)); + if (formatting != null){ + style = style.withFormatting(formatting); + Text.literal(part.substring(1)).getWithStyle(style).forEach(newText::append); + } else { + newText.append(Text.of(part)); + } + } + return newText; + } + return Text.literal(codedString); } } diff --git a/src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandlerTest.java b/src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandlerTest.java new file mode 100644 index 00000000..2c1b7956 --- /dev/null +++ b/src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandlerTest.java @@ -0,0 +1,35 @@ +package de.hysky.skyblocker.skyblock.chat; + +import net.minecraft.text.MutableText; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.awt.*; + +import static org.junit.jupiter.api.Assertions.*; + +class ChatRulesHandlerTest { + + @Test + void formatText() { + //generate test text + MutableText testText = Text.empty(); + Style style = Style.EMPTY.withFormatting(Formatting.DARK_BLUE); + Text.of("test").getWithStyle(style).forEach(testText::append); + style = style.withFormatting(Formatting.UNDERLINE); + Text.of("line").getWithStyle(style).forEach(testText::append); + style = style.withFormatting(Formatting.DARK_GREEN); + Text.of("dark green").getWithStyle(style).forEach(testText::append); + style = style.withFormatting(Formatting.ITALIC); + Text.of("italic").getWithStyle(style).forEach(testText::append); + + //generated text + MutableText text = ChatRulesHandler.formatText("&1test&nline&2dark green&oitalic"); + + Assertions.assertEquals(text,testText); + + } +} \ No newline at end of file -- cgit From 076522fa33965fce1345bbea8cb80f82645fc201 Mon Sep 17 00:00:00 2001 From: olim Date: Sun, 18 Feb 2024 16:52:38 +0000 Subject: improve locations input use actual locations names from the api for the input --- .../hysky/skyblocker/skyblock/chat/ChatRule.java | 28 ++++++++-------- .../skyblocker/skyblock/chat/ChatRulesHandler.java | 39 +++++++++++++++++++--- 2 files changed, 50 insertions(+), 17 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') 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 97431305..60a64fad 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java @@ -181,24 +181,26 @@ public class ChatRule { } String rawLocation = Utils.getLocationRaw(); Boolean isLocationValid = null; - for (String validLocation : validLocations.replace(" ", "").split(",")) {//the locations are raw locations split by "," and start with ! if not locations - if (validLocation.startsWith("!")) {//not location ( - if (Objects.equals(validLocation.substring(1), rawLocation)) { - isLocationValid = false; - break; - } - }else { - if (Objects.equals(validLocation, rawLocation)) { //normal location - isLocationValid = true; - break; - } + for (String validLocation : validLocations.replace(" ", "").toLowerCase().split(",")) {//the locations are raw locations split by "," and start with ! if not locations + String rawValidLocation = ChatRulesHandler.locations.get(validLocation.replace("!","")); + if (rawValidLocation == null) continue; + if (validLocation.startsWith("!")) {//not location + if (Objects.equals(rawValidLocation, rawLocation.toLowerCase())) { + isLocationValid = false; + break; } + } + else { + if (Objects.equals(rawValidLocation, rawLocation.toLowerCase())) { //normal location + isLocationValid = true; + break; + } + } } - if (isLocationValid == null || isLocationValid){//if location is not in the list at all and is a not a "!" location or and is a normal location + if (isLocationValid != null && isLocationValid){//if location is not in the list at all and is a not a "!" location or and is a normal location return true; } - 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 4afedc52..29faa856 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -1,8 +1,11 @@ package de.hysky.skyblocker.skyblock.chat; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.google.gson.reflect.TypeToken; import de.hysky.skyblocker.SkyblockerMod; +import de.hysky.skyblocker.utils.Http; import de.hysky.skyblocker.utils.Utils; import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents; import net.minecraft.client.MinecraftClient; @@ -20,19 +23,25 @@ import java.lang.reflect.Type; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; 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("chat_rules.json"); - + /** + * look up table for the locations input by the users to raw locations + */ + protected static final HashMap locations = new HashMap<>(); + /** + * list of possible locations still formatted for the tool tip + */ + protected static final List locationsList = new ArrayList<>(); protected static final List chatRuleList = new ArrayList<>(); public static void init() { loadChatRules(); + loadLocations(); ClientReceiveMessageEvents.ALLOW_GAME.register(ChatRulesHandler::checkMessage); } @@ -52,6 +61,28 @@ public class ChatRulesHandler { } } + private static void loadLocations() { + try { + String response = Http.sendGetRequest("https://api.hypixel.net/v2/resources/games"); + JsonObject locationsJson = JsonParser.parseString(response).getAsJsonObject().get("games").getAsJsonObject().get("SKYBLOCK").getAsJsonObject().get("modeNames").getAsJsonObject(); + for (Map.Entry entry : locationsJson.entrySet()) { + //fix old naming todo remove when hypixel fix + if (Objects.equals(entry.getKey(), "instanced")) { + locationsList.add(entry.getValue().getAsString()); + locations.put(entry.getValue().getAsString().replace(" ", "").toLowerCase(), "kuudra"); + continue; + } + locationsList.add(entry.getValue().getAsString()); + //add to list in a simplified for so more lenient for user input + locations.put(entry.getValue().getAsString().replace(" ", "").toLowerCase(),entry.getKey()); + } + } + catch (Exception e) { + LOGGER.error("[Skyblocker] Failed to load locations!", e); + } + System.out.println(locations); + } + protected static void saveChatRules() { JsonObject chatRuleJson = new JsonObject(); chatRuleJson.add("rules", SkyblockerMod.GSON.toJsonTree(chatRuleList)); -- cgit From a48b32d58d1a425d3dd120de12351de89f7659a1 Mon Sep 17 00:00:00 2001 From: olim Date: Sun, 18 Feb 2024 18:40:50 +0000 Subject: add tooltips add tool-tips for the config ui --- .../skyblocker/skyblock/chat/ChatRuleConfigScreen.java | 16 +++++++++++++++- .../skyblock/chat/ChatRulesConfigListWidget.java | 2 ++ .../hysky/skyblocker/skyblock/chat/ChatRulesHandler.java | 1 + src/main/resources/assets/skyblocker/lang/en_us.json | 14 +++++++++++++- 4 files changed, 31 insertions(+), 2 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java index 7d2280f0..cb9a2da6 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java @@ -4,6 +4,7 @@ import it.unimi.dsi.fastutil.ints.IntIntPair; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.tooltip.Tooltip; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.sound.SoundEvent; @@ -30,7 +31,7 @@ public class ChatRuleConfigScreen extends Screen { entry(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.arrowHit"), SoundEvents.ENTITY_ARROW_HIT_PLAYER), entry(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.amethyst"), SoundEvents.BLOCK_AMETHYST_BLOCK_HIT), entry(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.anvil"), SoundEvents.BLOCK_ANVIL_LAND) - );//todo amathis / more sounds + ); private final int chatRuleIndex; private final ChatRule chatRule; @@ -96,6 +97,7 @@ public class ChatRuleConfigScreen extends Screen { lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.name")) + SPACER_X; nameInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 100, 20, Text.of("")); nameInput.setText(chatRule.getName()); + nameInput.setTooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.name.@Tooltip"))); currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); inputsLabelTextPos = currentPos; @@ -106,6 +108,7 @@ public class ChatRuleConfigScreen extends Screen { filterInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); filterInput.setMaxLength(96); filterInput.setText(chatRule.getFilter()); + filterInput.setTooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.filter.@Tooltip"))); currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); lineXOffset = 0; @@ -117,6 +120,7 @@ public class ChatRuleConfigScreen extends Screen { }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) .size(75,20) + .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.partialMatch.@Tooltip"))) .build(); lineXOffset += 75 + SPACER_X; regexTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset,currentPos.rightInt()); @@ -127,6 +131,7 @@ public class ChatRuleConfigScreen extends Screen { }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) .size(75,20) + .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.regex.@Tooltip"))) .build(); lineXOffset += 75 + SPACER_X; ignoreCaseTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset,currentPos.rightInt()); @@ -137,6 +142,7 @@ public class ChatRuleConfigScreen extends Screen { }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) .size(75,20) + .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.ignoreCase.@Tooltip"))) .build(); currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); @@ -144,6 +150,9 @@ public class ChatRuleConfigScreen extends Screen { lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.locations")) + SPACER_X; locationsInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); locationsInput.setText(chatRule.getValidLocations()); + MutableText locationToolTip = Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.locations.@Tooltip"); + ChatRulesHandler.locationsList.forEach(location -> locationToolTip.append(" " + location + ",\n")); + locationsInput.setTooltip(Tooltip.of(locationToolTip)); currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); outputsLabelTextPos = IntIntPair.of(currentPos.leftInt() - 10,currentPos.rightInt()); @@ -157,6 +166,7 @@ public class ChatRuleConfigScreen extends Screen { }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) .size(75,20) + .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.hideMessage.@Tooltip"))) .build(); lineXOffset += 75 + SPACER_X; actionBarTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset,currentPos.rightInt()); @@ -167,6 +177,7 @@ public class ChatRuleConfigScreen extends Screen { }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) .size(75,20) + .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.actionBar.@Tooltip"))) .build(); lineXOffset = 0; currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); @@ -179,6 +190,7 @@ public class ChatRuleConfigScreen extends Screen { }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) .size(75,20) + .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.announcement.@Tooltip"))) .build(); lineXOffset += 75 + SPACER_X; customSoundLabelTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset,currentPos.rightInt()); @@ -198,6 +210,7 @@ public class ChatRuleConfigScreen extends Screen { }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) .size(100,20) + .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sound.@Tooltip"))) .build(); currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); @@ -205,6 +218,7 @@ public class ChatRuleConfigScreen extends Screen { lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace")) + SPACER_X; replaceMessageInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); replaceMessageInput.setMaxLength(96); + replaceMessageInput.setTooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace.@Tooltip"))); replaceMessageInput.setText(chatRule.getReplaceMessage()); finishButton = ButtonWidget.builder(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.finish"), a -> { diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java index 84e9082d..87455a2b 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java @@ -7,6 +7,7 @@ import net.minecraft.client.gui.Selectable; import net.minecraft.client.gui.screen.ConfirmScreen; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.gui.screen.narration.NarrationPart; +import net.minecraft.client.gui.tooltip.Tooltip; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.ElementListWidget; import net.minecraft.screen.ScreenTexts; @@ -132,6 +133,7 @@ public class ChatRulesConfigListWidget extends ElementListWidget { 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 29faa856..c9efe749 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -37,6 +37,7 @@ public class ChatRulesHandler { * list of possible locations still formatted for the tool tip */ protected static final List locationsList = new ArrayList<>(); + protected static final List chatRuleList = new ArrayList<>(); public static void init() { diff --git a/src/main/resources/assets/skyblocker/lang/en_us.json b/src/main/resources/assets/skyblocker/lang/en_us.json index 079d060e..52cc52c4 100644 --- a/src/main/resources/assets/skyblocker/lang/en_us.json +++ b/src/main/resources/assets/skyblocker/lang/en_us.json @@ -377,23 +377,35 @@ "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleEnabled": "Rule Enabled", "text.autoconfig.skyblocker.option.messages.chatRules.screen.modify": "Modify", "text.autoconfig.skyblocker.option.messages.chatRules.screen.editRule": "Edit", - "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen": "Chat Rules Config...", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.editRule.@Tooltip": "Open config for rule.", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen": "Chat Rule Config...", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.true": "True", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.false": "False", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.inputs": "Inputs:", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.name": "Name:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.name.@Tooltip": "The name of the rule.", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.filter": "ChatFilter:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.filter.@Tooltip": "The string/regex to match a chat message to.", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.partialMatch": "Allow Partial Match:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.partialMatch.@Tooltip": "If the filter can match part of the chat message.", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.regex": "Is Regex:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.regex.@Tooltip": "If the filter uses regex or is just a string.", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.ignoreCase": "Ignore Case:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.ignoreCase.@Tooltip": "if the filter is case sensitive.", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.locations": "Valid Locations:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.locations.@Tooltip": "List of locations where the filter will work. Separate each location with a \",\" and use a \"!\" if you want it to work anywhere but a location. Location Names:", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.outputs": "Outputs:", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.hideMessage": "Hide Message:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.hideMessage.@Tooltip": "Remove the message from chat.", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.actionBar": "Show In Action Bar:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.actionBar.@Tooltip": "Show the message in your action bar.", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.announcement": "Show Announcement:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.announcement.@Tooltip": "Show the message in the middle of the screen.", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace": "Replace Message With:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace.@Tooltip": "Input a new message to output (can be formatted using minecraft color codes).", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.finish": "finish", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds": "Play Sound:", + "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.@Tooltip": "Play a sound when the message is sent.", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.none": "None", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.pling": "Pling", "text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.cave": "Cave", -- cgit From b3e4dabf7ffce74c8cd137d1ea7418c4ca1026dd Mon Sep 17 00:00:00 2001 From: olim Date: Sun, 18 Feb 2024 21:56:45 +0000 Subject: fix most formatting issues tried to find all of the formatting problems left in my code --- .../hysky/skyblocker/skyblock/chat/ChatRule.java | 20 ++---------- .../skyblock/chat/ChatRuleAnnouncementScreen.java | 6 +--- .../skyblock/chat/ChatRuleConfigScreen.java | 30 ++++++------------ .../skyblock/chat/ChatRulesConfigListWidget.java | 36 ++++++---------------- .../skyblock/chat/ChatRulesConfigScreen.java | 12 ++------ .../skyblocker/skyblock/chat/ChatRulesHandler.java | 3 -- 6 files changed, 26 insertions(+), 81 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') 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 60a64fad..4877fdb6 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java @@ -1,12 +1,8 @@ package de.hysky.skyblocker.skyblock.chat; import de.hysky.skyblocker.utils.Utils; -import net.minecraft.client.sound.Sound; -import net.minecraft.item.ItemStack; import net.minecraft.sound.SoundEvent; -import net.minecraft.sound.SoundEvents; -import java.util.List; import java.util.Objects; import java.util.regex.Pattern; @@ -16,8 +12,8 @@ import java.util.regex.Pattern; public class ChatRule { private String name; - //inputs + //inputs private Boolean enabled; private Boolean isPartialMatch; private Boolean isRegex; @@ -29,7 +25,7 @@ public class ChatRule { private Boolean hideMessage; private Boolean showActionBar; private Boolean showAnnouncement; - private String replaceMessage; //todo extract parts of original message + private String replaceMessage; private SoundEvent customSound; /** * Creates a chat rule with default options. @@ -51,7 +47,7 @@ public class ChatRule { this.customSound = null; } - public Boolean getEnabled() { //todo remove unused getters and set + public Boolean getEnabled() { return enabled; } @@ -211,16 +207,6 @@ public class ChatRule { public void setName(String name) { this.name = name; } - - public enum LocationOption { - None, - Island, - Hub, - Garden; //todo add more - - } - - } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java index 0ee3c2c5..2cb6ca5c 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java @@ -9,10 +9,8 @@ import net.minecraft.text.Text; public class ChatRuleAnnouncementScreen { - private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); private static float timer; - private static Text text = null; public static void init() { @@ -22,11 +20,8 @@ public class ChatRuleAnnouncementScreen { } render(context, tickDelta); }); - } - - /** * renders {@link ChatRuleAnnouncementScreen#text} to the middle of the top of the screen. * @param context render context @@ -46,6 +41,7 @@ public class ChatRuleAnnouncementScreen { matrices.pop(); } + protected static void setText(Text newText) { text = newText; timer = SkyblockerConfigManager.get().messages.chatRuleConfig.announcementLength; diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java index cb9a2da6..31611991 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java @@ -19,7 +19,6 @@ import java.util.Map; import static java.util.Map.entry; public class ChatRuleConfigScreen extends Screen { - private static final int SPACER_X = 5; private static final int SPACER_Y = 25; @@ -35,17 +34,12 @@ public class ChatRuleConfigScreen extends Screen { private final int chatRuleIndex; private final ChatRule chatRule; - - //widgets - private ButtonWidget finishButton; - private TextFieldWidget nameInput; private TextFieldWidget filterInput; private ButtonWidget partialMatchToggle; private ButtonWidget regexToggle; private ButtonWidget ignoreCaseToggle; private TextFieldWidget locationsInput; - private ButtonWidget hideMessageToggle; private ButtonWidget actionBarToggle; private ButtonWidget announcementToggle; @@ -55,16 +49,12 @@ public class ChatRuleConfigScreen extends Screen { //textLocations private IntIntPair nameLabelTextPos; private IntIntPair inputsLabelTextPos; - private IntIntPair filterLabelTextPos; private IntIntPair partialMatchTextPos; private IntIntPair regexTextPos; private IntIntPair ignoreCaseTextPos; - private IntIntPair locationLabelTextPos; - private IntIntPair outputsLabelTextPos; - private IntIntPair hideMessageTextPos; private IntIntPair actionBarTextPos; private IntIntPair announcementTextPos; @@ -75,8 +65,6 @@ public class ChatRuleConfigScreen extends Screen { private final Screen parent; - - public ChatRuleConfigScreen(Screen parent, int chatRuleIndex) { super(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen")); this.chatRuleIndex = chatRuleIndex; @@ -91,7 +79,7 @@ public class ChatRuleConfigScreen extends Screen { if (client == null) return; //start centered on the X and 1/3 down on the Y IntIntPair currentPos = IntIntPair.of((this.width - getMaxUsedWidth()) / 2,(int)((this.height -getMaxUsedHeight()) * 0.33)); - int lineXOffset = 0; + int lineXOffset; nameLabelTextPos = currentPos; lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.name")) + SPACER_X; @@ -221,11 +209,9 @@ public class ChatRuleConfigScreen extends Screen { replaceMessageInput.setTooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace.@Tooltip"))); replaceMessageInput.setText(chatRule.getReplaceMessage()); - finishButton = ButtonWidget.builder(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.finish"), a -> { - close(); - }) + ButtonWidget finishButton = ButtonWidget.builder(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.finish"), a -> close()) .position(this.width - 75 - SPACER_Y, this.height - SPACER_Y) - .size(75,20) + .size(75, 20) .build(); addDrawableChild(nameInput); @@ -243,7 +229,7 @@ public class ChatRuleConfigScreen extends Screen { } /** - * works out the width of the maximum line + * Works out the width of the maximum line * @return the max used width */ private int getMaxUsedWidth() { @@ -259,8 +245,12 @@ public class ChatRuleConfigScreen extends Screen { return total; } + /** + * Works out the height used + * @return height used by the gui + */ private int getMaxUsedHeight() { - //there are 7 rows so just times the spacer by 7 + //there are 8 rows so just times the spacer by 8 return SPACER_Y * 8; } @@ -318,6 +308,4 @@ public class ChatRuleConfigScreen extends Screen { } return soundsLookup.keySet().stream().toList().get(currentSoundIndex); } - - } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java index 87455a2b..c5d7fcc8 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java @@ -17,8 +17,6 @@ import java.awt.*; import java.util.List; public class ChatRulesConfigListWidget extends ElementListWidget { - - private final ChatRulesConfigScreen screen; private Boolean hasChanged; @@ -45,7 +43,6 @@ public class ChatRulesConfigListWidget extends ElementListWidget children; - - - //widgets + //widgets private final ButtonWidget enabledButton; private final ButtonWidget openConfigButton; private final ButtonWidget deleteButton; - - //text locations + //text location private final int nameX = width / 2 - 125; - //saved data private double oldScrollAmount = 0; @@ -118,15 +109,10 @@ public class ChatRulesConfigListWidget extends ElementListWidget { - toggleEnabled(); - }) + enabledButton = ButtonWidget.builder(enabledButtonText() , a -> toggleEnabled()) .size(50,20) .position(width / 2 - 25,5) - .build() - ; + .build(); openConfigButton = ButtonWidget.builder(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.editRule"), a -> { client.setScreen(new ChatRuleConfigScreen(screen, chatRuleIndex)); @@ -134,17 +120,15 @@ public class ChatRulesConfigListWidget extends ElementListWidget { oldScrollAmount = getScrollAmount(); client.setScreen(new ConfirmScreen(this::deleteEntry, Text.translatable("skyblocker.shortcuts.deleteQuestion"), Text.translatable("skyblocker.shortcuts.deleteWarning", chatRule.getName()), Text.translatable("selectServer.deleteButton"), ScreenTexts.CANCEL)); }) .size(50,20) .position(width / 2 + 105,5) - .build() - ; - + .build(); children = List.of(enabledButton, openConfigButton, deleteButton); } @@ -182,7 +166,7 @@ public class ChatRulesConfigListWidget extends ElementListWidget chatRulesConfigListWidget.addRuleAfterSelected()).build(); - adder.add(buttonNew); - buttonDone = ButtonWidget.builder(ScreenTexts.DONE, button -> { + ButtonWidget buttonNew1 = ButtonWidget.builder(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.new"), buttonNew -> chatRulesConfigListWidget.addRuleAfterSelected()).build(); + adder.add(buttonNew1); + ButtonWidget buttonDone = ButtonWidget.builder(ScreenTexts.DONE, button -> { chatRulesConfigListWidget.saveRules(); if (client != null) { close(); 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 c9efe749..bde55aed 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -81,7 +81,6 @@ public class ChatRulesHandler { catch (Exception e) { LOGGER.error("[Skyblocker] Failed to load locations!", e); } - System.out.println(locations); } protected static void saveChatRules() { @@ -146,7 +145,6 @@ public class ChatRulesHandler { * @param codedString the string with color codes in * @return formatted text */ - protected static MutableText formatText(String codedString) { if (codedString.contains(String.valueOf(Formatting.FORMATTING_CODE_PREFIX)) || codedString.contains("&")){ MutableText newText = Text.literal(""); @@ -166,5 +164,4 @@ public class ChatRulesHandler { } return Text.literal(codedString); } - } -- cgit From e82a97b83041cf552dae3379e6071204c8993f9e Mon Sep 17 00:00:00 2001 From: olim Date: Tue, 20 Feb 2024 12:39:15 +0000 Subject: add defalut rules on first load add rule to clean hub chat and for mining ability when first loaded as examples of what the user can do --- .../java/de/hysky/skyblocker/skyblock/chat/ChatRule.java | 15 +++++++++++++++ .../skyblocker/skyblock/chat/ChatRuleConfigScreen.java | 7 +++++-- .../hysky/skyblocker/skyblock/chat/ChatRulesHandler.java | 13 ++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') 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 f2ecefc6..93897988 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java @@ -47,6 +47,21 @@ public class ChatRule { this.customSound = null; } + public ChatRule(String name, Boolean enabled, Boolean isPartialMatch, Boolean isRegex, Boolean isIgnoreCase, String filter, String validLocations, Boolean hideMessage, Boolean showActionBar, Boolean showAnnouncement, String replaceMessage, SoundEvent customSound) { + this.name = name; + this.enabled = enabled; + this.isPartialMatch = isPartialMatch; + this.isRegex = isRegex; + this.isIgnoreCase = isIgnoreCase; + this.filter = filter; + this.validLocations = validLocations; + this.hideMessage = hideMessage; + this.showActionBar = showActionBar; + this.showAnnouncement = showAnnouncement; + this.replaceMessage = replaceMessage; + this.customSound = customSound; + } + protected String getName() { return name; } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java index 59e2efed..92cd72d7 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java @@ -155,6 +155,7 @@ public class ChatRuleConfigScreen extends Screen { locationsInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); locationsInput.setText(chatRule.getValidLocations()); MutableText locationToolTip = Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.locations.@Tooltip"); + locationToolTip.append("\n"); ChatRulesHandler.locationsList.forEach(location -> locationToolTip.append(" " + location + ",\n")); locationsInput.setTooltip(Tooltip.of(locationToolTip)); currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); @@ -300,10 +301,12 @@ public class ChatRuleConfigScreen extends Screen { context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace"), replaceMessageLabelTextPos.leftInt(), replaceMessageLabelTextPos.rightInt() + yOffset, 0xFFFFFF); } + /** + * Saves and returns to parent screen + */ @Override public void close() { - //todo add checks to see if valid rule e.g. has name - //and if valid save a + if (client != null ) { save(); client.setScreen(parent); 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 bde55aed..aa9343d8 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -9,6 +9,7 @@ import de.hysky.skyblocker.utils.Http; import de.hysky.skyblocker.utils.Utils; import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents; import net.minecraft.client.MinecraftClient; +import net.minecraft.sound.SoundEvents; import net.minecraft.text.MutableText; import net.minecraft.text.Style; import net.minecraft.text.Text; @@ -55,13 +56,23 @@ public class ChatRulesHandler { LOGGER.info("[Skyblocker] Loaded chat rules"); } catch (NoSuchFileException e) { - //todo create default chat rules + registerDefaultChatRules(); LOGGER.warn("[Skyblocker] chat rule file not found, using default rules. This is normal when using for the first time."); } catch (IOException e) { LOGGER.error("[Skyblocker] Failed to load shortcuts file", e); } } + private static void registerDefaultChatRules() { + //clean hub chat + ChatRule cleanHubRule = new ChatRule("Clean Hub Chat", false, true, true, true, "(selling)|(buying)|(lowb)|(visit)|(/p)|(/ah)|(my ah)", "hub", true, false, false, "", null); + //mining Ability + ChatRule miningAbilityRule = new ChatRule("Mining Ability Alert", false, true, false, true, "is now available!", "Crystal Hollows, Dwarven Mines", false, false, true, "&1Ability", SoundEvents.ENTITY_ARROW_HIT_PLAYER); + + chatRuleList.add(cleanHubRule); + chatRuleList.add(miningAbilityRule); + } + private static void loadLocations() { try { String response = Http.sendGetRequest("https://api.hypixel.net/v2/resources/games"); -- cgit From 980e91c202c7e015e959f30752b69d7818afd715 Mon Sep 17 00:00:00 2001 From: Aaron <51387595+AzureAaron@users.noreply.github.com> Date: Wed, 21 Feb 2024 00:00:31 -0500 Subject: Fix formatting The text rendering methods use AARRGGBB not RRGGBB so I fixed that too. --- .../hysky/skyblocker/skyblock/chat/ChatRule.java | 30 +++--- .../skyblock/chat/ChatRuleAnnouncementScreen.java | 3 +- .../skyblock/chat/ChatRuleConfigScreen.java | 109 +++++++++++---------- .../skyblock/chat/ChatRulesConfigListWidget.java | 62 ++++++------ .../skyblock/chat/ChatRulesConfigScreen.java | 2 +- .../skyblocker/skyblock/chat/ChatRulesHandler.java | 28 +++--- .../skyblocker/skyblock/chat/ChatRuleTest.java | 18 ++-- .../skyblock/chat/ChatRulesHandlerTest.java | 6 +- 8 files changed, 129 insertions(+), 129 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') 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 93897988..7a8214cb 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java @@ -30,7 +30,7 @@ public class ChatRule { /** * Creates a chat rule with default options. */ - protected ChatRule(){ + protected ChatRule() { this.name = "New Rule"; this.enabled = true; @@ -163,43 +163,46 @@ public class ChatRule { * @param inputString the chat message to check if fits * @return if the inputs are all true and the outputs should be performed */ - protected Boolean isMatch(String inputString){ + protected Boolean isMatch(String inputString) { //enabled if (!enabled) return false; //ignore case String testString; String testFilter; - if (isIgnoreCase){ + + if (isIgnoreCase) { testString = inputString.toLowerCase(); testFilter = filter.toLowerCase(); - }else { + } else { testString = inputString; testFilter = filter; } //filter if (testFilter.isBlank()) return false; - if(isRegex) { + if (isRegex) { if (isPartialMatch) { - if (! Pattern.compile(testFilter).matcher(testString).find()) return false; - }else { + if (!Pattern.compile(testFilter).matcher(testString).find()) return false; + } else { if (!testString.matches(testFilter)) return false; } - } else{ + } else { if (isPartialMatch) { if (!testString.contains(testFilter)) return false; - }else { + } else { if (!testFilter.equals(testString)) return false; } } //location - if (validLocations.isBlank()){ //if no locations do not check + if (validLocations.isBlank()) { //if no locations do not check return true; } + String rawLocation = Utils.getLocationRaw(); Boolean isLocationValid = null; + for (String validLocation : validLocations.replace(" ", "").toLowerCase().split(",")) {//the locations are raw locations split by "," and start with ! if not locations String rawValidLocation = ChatRulesHandler.locations.get(validLocation.replace("!","")); if (rawValidLocation == null) continue; @@ -208,15 +211,16 @@ public class ChatRule { isLocationValid = false; break; } - } - else { + } else { if (Objects.equals(rawValidLocation, rawLocation.toLowerCase())) { //normal location isLocationValid = true; break; } } } - if (isLocationValid != null && isLocationValid){//if location is not in the list at all and is a not a "!" location or and is a normal location + + //if location is not in the list at all and is a not a "!" location or and is a normal location + if (isLocationValid != null && isLocationValid) { return true; } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java index 2cb6ca5c..bafada27 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleAnnouncementScreen.java @@ -7,7 +7,6 @@ import net.minecraft.client.gui.DrawContext; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; - public class ChatRuleAnnouncementScreen { private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); private static float timer; @@ -37,7 +36,7 @@ public class ChatRuleAnnouncementScreen { matrices.translate(context.getScaledWindowWidth() / 2f, context.getScaledWindowHeight() * 0.3, 0f); matrices.scale(scale, scale, 0f); //render text - context.drawCenteredTextWithShadow(CLIENT.textRenderer,text,0, 0, 0xFFFFFF); + context.drawCenteredTextWithShadow(CLIENT.textRenderer, text, 0, 0, 0xFFFFFFFF); matrices.pop(); } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java index 92cd72d7..9b91d4a8 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRuleConfigScreen.java @@ -1,7 +1,6 @@ package de.hysky.skyblocker.skyblock.chat; import it.unimi.dsi.fastutil.ints.IntIntPair; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.tooltip.Tooltip; @@ -17,7 +16,6 @@ import net.minecraft.util.Identifier; import java.awt.*; import java.util.List; import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; import static java.util.Map.entry; @@ -55,7 +53,7 @@ public class ChatRuleConfigScreen extends Screen { private IntIntPair filterLabelTextPos; private IntIntPair partialMatchTextPos; private IntIntPair regexTextPos; - private IntIntPair ignoreCaseTextPos; + private IntIntPair ignoreCaseTextPos; private IntIntPair locationLabelTextPos; private IntIntPair outputsLabelTextPos; private IntIntPair hideMessageTextPos; @@ -78,8 +76,10 @@ public class ChatRuleConfigScreen extends Screen { private int getCurrentSoundIndex() { if (chatRule.getCustomSound() == null) return -1; //if no sound just return -1 + List soundOptions = soundsLookup.values().stream().toList(); Identifier ruleSoundId = chatRule.getCustomSound().getId(); + for (int i = 0; i < soundOptions.size(); i++) { if (soundOptions.get(i).getId().compareTo(ruleSoundId) == 0) { return i; @@ -94,74 +94,74 @@ public class ChatRuleConfigScreen extends Screen { super.init(); if (client == null) return; //start centered on the X and 1/3 down on the Y - IntIntPair currentPos = IntIntPair.of((this.width - getMaxUsedWidth()) / 2,(int)((this.height -getMaxUsedHeight()) * 0.33)); + IntIntPair currentPos = IntIntPair.of((this.width - getMaxUsedWidth()) / 2, (int)((this.height -getMaxUsedHeight()) * 0.33)); int lineXOffset; nameLabelTextPos = currentPos; lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.name")) + SPACER_X; - nameInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 100, 20, Text.of("")); + nameInput = new TextFieldWidget(client.textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 100, 20, Text.of("")); nameInput.setText(chatRule.getName()); nameInput.setTooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.name.@Tooltip"))); - currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); + currentPos = IntIntPair.of(currentPos.leftInt(), currentPos.rightInt() + SPACER_Y); inputsLabelTextPos = currentPos; - currentPos = IntIntPair.of(currentPos.leftInt() + 10 ,currentPos.rightInt() + SPACER_Y); + currentPos = IntIntPair.of(currentPos.leftInt() + 10, currentPos.rightInt() + SPACER_Y); filterLabelTextPos = currentPos; lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.filter")) + SPACER_X; - filterInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); + filterInput = new TextFieldWidget(client.textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); filterInput.setMaxLength(96); filterInput.setText(chatRule.getFilter()); filterInput.setTooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.filter.@Tooltip"))); currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); lineXOffset = 0; - partialMatchTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset,currentPos.rightInt()); + partialMatchTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset, currentPos.rightInt()); lineXOffset += client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.partialMatch")) + SPACER_X; partialMatchToggle = ButtonWidget.builder(enabledButtonText(chatRule.getPartialMatch()), a -> { chatRule.setPartialMatch(!chatRule.getPartialMatch()); partialMatchToggle.setMessage(enabledButtonText(chatRule.getPartialMatch())); }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) - .size(75,20) + .size(75, 20) .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.partialMatch.@Tooltip"))) .build(); lineXOffset += 75 + SPACER_X; - regexTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset,currentPos.rightInt()); + regexTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset, currentPos.rightInt()); lineXOffset += client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.regex")) + SPACER_X; regexToggle = ButtonWidget.builder(enabledButtonText(chatRule.getRegex()), a -> { chatRule.setRegex(!chatRule.getRegex()); regexToggle.setMessage(enabledButtonText(chatRule.getRegex())); }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) - .size(75,20) + .size(75, 20) .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.regex.@Tooltip"))) .build(); lineXOffset += 75 + SPACER_X; - ignoreCaseTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset,currentPos.rightInt()); + ignoreCaseTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset, currentPos.rightInt()); lineXOffset += client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.ignoreCase")) + SPACER_X; ignoreCaseToggle = ButtonWidget.builder(enabledButtonText(chatRule.getIgnoreCase()), a -> { chatRule.setIgnoreCase(!chatRule.getIgnoreCase()); ignoreCaseToggle.setMessage(enabledButtonText(chatRule.getIgnoreCase())); }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) - .size(75,20) + .size(75, 20) .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.ignoreCase.@Tooltip"))) .build(); - currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); + currentPos = IntIntPair.of(currentPos.leftInt(), currentPos.rightInt() + SPACER_Y); locationLabelTextPos = currentPos; lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.locations")) + SPACER_X; - locationsInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); + locationsInput = new TextFieldWidget(client.textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); locationsInput.setText(chatRule.getValidLocations()); MutableText locationToolTip = Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.locations.@Tooltip"); locationToolTip.append("\n"); ChatRulesHandler.locationsList.forEach(location -> locationToolTip.append(" " + location + ",\n")); locationsInput.setTooltip(Tooltip.of(locationToolTip)); - currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); + currentPos = IntIntPair.of(currentPos.leftInt(), currentPos.rightInt() + SPACER_Y); outputsLabelTextPos = IntIntPair.of(currentPos.leftInt() - 10,currentPos.rightInt()); - currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); + currentPos = IntIntPair.of(currentPos.leftInt(), currentPos.rightInt() + SPACER_Y); hideMessageTextPos = currentPos; lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.hideMessage")) + SPACER_X; @@ -170,35 +170,35 @@ public class ChatRuleConfigScreen extends Screen { hideMessageToggle.setMessage(enabledButtonText(chatRule.getHideMessage())); }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) - .size(75,20) + .size(75, 20) .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.hideMessage.@Tooltip"))) .build(); lineXOffset += 75 + SPACER_X; - actionBarTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset,currentPos.rightInt()); + actionBarTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset, currentPos.rightInt()); lineXOffset += client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.actionBar")) + SPACER_X; actionBarToggle = ButtonWidget.builder(enabledButtonText(chatRule.getShowActionBar()), a -> { chatRule.setShowActionBar(!chatRule.getShowActionBar()); actionBarToggle.setMessage(enabledButtonText(chatRule.getShowActionBar())); }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) - .size(75,20) + .size(75, 20) .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.actionBar.@Tooltip"))) .build(); lineXOffset = 0; - currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); + currentPos = IntIntPair.of(currentPos.leftInt(), currentPos.rightInt() + SPACER_Y); - announcementTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset,currentPos.rightInt()); + announcementTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset, currentPos.rightInt()); lineXOffset += client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.announcement")) + SPACER_X; announcementToggle = ButtonWidget.builder(enabledButtonText(chatRule.getShowAnnouncement()), a -> { chatRule.setShowAnnouncement(!chatRule.getShowAnnouncement()); announcementToggle.setMessage(enabledButtonText(chatRule.getShowAnnouncement())); }) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) - .size(75,20) + .size(75, 20) .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.announcement.@Tooltip"))) .build(); lineXOffset += 75 + SPACER_X; - customSoundLabelTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset,currentPos.rightInt()); + customSoundLabelTextPos = IntIntPair.of(currentPos.leftInt() + lineXOffset, currentPos.rightInt()); lineXOffset += client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds")) + SPACER_X; soundsToggle = ButtonWidget.builder(getSoundName(), a -> { currentSoundIndex += 1; @@ -211,17 +211,16 @@ public class ChatRuleConfigScreen extends Screen { chatRule.setCustomSound(sound); if (client.player != null && sound != null) { client.player.playSound(sound, 100f, 0.1f); - } - }) + }}) .position(currentPos.leftInt() + lineXOffset, currentPos.rightInt()) - .size(100,20) + .size(100, 20) .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.@Tooltip"))) .build(); - currentPos = IntIntPair.of(currentPos.leftInt(),currentPos.rightInt() + SPACER_Y); + currentPos = IntIntPair.of(currentPos.leftInt(), currentPos.rightInt() + SPACER_Y); replaceMessageLabelTextPos = currentPos; lineXOffset = client.textRenderer.getWidth(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace")) + SPACER_X; - replaceMessageInput = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); + replaceMessageInput = new TextFieldWidget(client.textRenderer, currentPos.leftInt() + lineXOffset, currentPos.rightInt(), 200, 20, Text.of("")); replaceMessageInput.setMaxLength(96); replaceMessageInput.setTooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace.@Tooltip"))); replaceMessageInput.setText(chatRule.getReplaceMessage()); @@ -259,6 +258,7 @@ public class ChatRuleConfigScreen extends Screen { total += SPACER_X * 6; //button width total += 75 * 3; + return total; } @@ -272,33 +272,33 @@ public class ChatRuleConfigScreen extends Screen { } private Text enabledButtonText(boolean enabled) { - if (enabled){ - return Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.true").withColor(Color.green.getRGB()); - }else { - return Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.false").withColor(Color.red.getRGB()); + if (enabled) { + return Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.true").withColor(Color.GREEN.getRGB()); + } else { + return Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.false").withColor(Color.RED.getRGB()); } } @Override public void render(DrawContext context, int mouseX, int mouseY, float delta) { super.render(context, mouseX, mouseY, delta); - context.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, 16, 0xFFFFFF); + context.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, 16, 0xFFFFFFFF); //draw labels ands text int yOffset = (SPACER_Y - this.textRenderer.fontHeight) / 2; - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.inputs"), inputsLabelTextPos.leftInt(), inputsLabelTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.name"), nameLabelTextPos.leftInt(), nameLabelTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.filter"), filterLabelTextPos.leftInt(), filterLabelTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.partialMatch"), partialMatchTextPos.leftInt(), partialMatchTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.regex"), regexTextPos.leftInt(), regexTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.ignoreCase"), ignoreCaseTextPos.leftInt(), ignoreCaseTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.locations"), locationLabelTextPos.leftInt(), locationLabelTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.outputs"), outputsLabelTextPos.leftInt(), outputsLabelTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.hideMessage"), hideMessageTextPos.leftInt(), hideMessageTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.actionBar"), actionBarTextPos.leftInt(), actionBarTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.announcement"), announcementTextPos.leftInt(), announcementTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds"), customSoundLabelTextPos.leftInt(), customSoundLabelTextPos.rightInt() + yOffset, 0xFFFFFF); - context.drawTextWithShadow(this.textRenderer,Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace"), replaceMessageLabelTextPos.leftInt(), replaceMessageLabelTextPos.rightInt() + yOffset, 0xFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.inputs"), inputsLabelTextPos.leftInt(), inputsLabelTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.name"), nameLabelTextPos.leftInt(), nameLabelTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.filter"), filterLabelTextPos.leftInt(), filterLabelTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.partialMatch"), partialMatchTextPos.leftInt(), partialMatchTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.regex"), regexTextPos.leftInt(), regexTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.ignoreCase"), ignoreCaseTextPos.leftInt(), ignoreCaseTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.locations"), locationLabelTextPos.leftInt(), locationLabelTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.outputs"), outputsLabelTextPos.leftInt(), outputsLabelTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.hideMessage"), hideMessageTextPos.leftInt(), hideMessageTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.actionBar"), actionBarTextPos.leftInt(), actionBarTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.announcement"), announcementTextPos.leftInt(), announcementTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds"), customSoundLabelTextPos.leftInt(), customSoundLabelTextPos.rightInt() + yOffset, 0xFFFFFFFF); + context.drawTextWithShadow(this.textRenderer, Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.replace"), replaceMessageLabelTextPos.leftInt(), replaceMessageLabelTextPos.rightInt() + yOffset, 0xFFFFFFFF); } /** @@ -306,25 +306,26 @@ public class ChatRuleConfigScreen extends Screen { */ @Override public void close() { - - if (client != null ) { + if (client != null) { save(); client.setScreen(parent); } } - private void save(){ + + private void save() { chatRule.setName(nameInput.getText()); chatRule.setFilter(filterInput.getText()); chatRule.setReplaceMessage(replaceMessageInput.getText()); chatRule.setValidLocations(locationsInput.getText()); - ChatRulesHandler.chatRuleList.set(chatRuleIndex,chatRule); + ChatRulesHandler.chatRuleList.set(chatRuleIndex, chatRule); } private MutableText getSoundName() { - if (currentSoundIndex == -1){ - return Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.none"); + if (currentSoundIndex == -1) { + return Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.ruleScreen.sounds.none"); } + return soundsLookup.keySet().stream().toList().get(currentSoundIndex); } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java index c5d7fcc8..aecfa88d 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesConfigListWidget.java @@ -18,18 +18,18 @@ import java.util.List; public class ChatRulesConfigListWidget extends ElementListWidget { private final ChatRulesConfigScreen screen; - private Boolean hasChanged; - public ChatRulesConfigListWidget(MinecraftClient minecraftClient, ChatRulesConfigScreen screen, int width, int height, int y, int itemHeight) { - super(minecraftClient, width, height, y, itemHeight); + public ChatRulesConfigListWidget(MinecraftClient client, ChatRulesConfigScreen screen, int width, int height, int y, int itemHeight) { + super(client, width, height, y, itemHeight); this.screen = screen; this.hasChanged = false; + //add labels - addEntry(new chatRuleLabelsEntry()); + addEntry(new ChatRuleLabelsEntry()); //add entry fall all existing rules - for (int i = 0; i < (long) ChatRulesHandler.chatRuleList.size(); i++){ - addEntry(new chatRuleConfigEntry(i)); + for (int i = 0; i < ChatRulesHandler.chatRuleList.size(); i++){ + addEntry(new ChatRuleConfigEntry(i)); } } @@ -46,8 +46,9 @@ public class ChatRulesConfigListWidget extends ElementListWidget { + protected static abstract class AbstractChatRuleEntry extends ElementListWidget.Entry { } - private class chatRuleLabelsEntry extends AbstractChatRuleEntry { + private class ChatRuleLabelsEntry extends AbstractChatRuleEntry { @Override public List selectableChildren() { @@ -81,13 +82,13 @@ public class ChatRulesConfigListWidget extends ElementListWidget toggleEnabled()) - .size(50,20) - .position(width / 2 - 25,5) + enabledButton = ButtonWidget.builder(enabledButtonText(), a -> toggleEnabled()) + .size(50, 20) + .position(width / 2 - 25, 5) .build(); openConfigButton = ButtonWidget.builder(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.editRule"), a -> { client.setScreen(new ChatRuleConfigScreen(screen, chatRuleIndex)); - }) - .size(50,20) - .position(width / 2 + 45,5) + }) + .size(50, 20) + .position(width / 2 + 45, 5) .tooltip(Tooltip.of(Text.translatable("text.autoconfig.skyblocker.option.messages.chatRules.screen.editRule.@Tooltip"))) .build(); @@ -126,18 +127,18 @@ public class ChatRulesConfigListWidget extends ElementListWidget>>() { - }.getType(); + Type chatRulesType = new TypeToken>>() {}.getType(); Map> chatRules = SkyblockerMod.GSON.fromJson(reader,chatRulesType); chatRuleList.addAll(chatRules.get("rules")); @@ -74,7 +73,7 @@ public class ChatRulesHandler { } private static void loadLocations() { - try { + try { String response = Http.sendGetRequest("https://api.hypixel.net/v2/resources/games"); JsonObject locationsJson = JsonParser.parseString(response).getAsJsonObject().get("games").getAsJsonObject().get("SKYBLOCK").getAsJsonObject().get("modeNames").getAsJsonObject(); for (Map.Entry entry : locationsJson.entrySet()) { @@ -86,10 +85,9 @@ public class ChatRulesHandler { } locationsList.add(entry.getValue().getAsString()); //add to list in a simplified for so more lenient for user input - locations.put(entry.getValue().getAsString().replace(" ", "").toLowerCase(),entry.getKey()); + locations.put(entry.getValue().getAsString().replace(" ", "").toLowerCase(), entry.getKey()); } - } - catch (Exception e) { + } catch (Exception e) { LOGGER.error("[Skyblocker] Failed to load locations!", e); } } @@ -110,18 +108,18 @@ public class ChatRulesHandler { * @param message the chat message * @param overlay if its overlay */ - private static boolean checkMessage(Text message, Boolean 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 = Formatting.strip(message.getString()); + for (ChatRule rule : chatRuleList) { if (rule.isMatch(plain)) { //get a replacement message Text newMessage; if (!rule.getReplaceMessage().isBlank()) { newMessage = formatText(rule.getReplaceMessage()); - } - else { + } else { newMessage = message; } @@ -135,7 +133,7 @@ public class ChatRulesHandler { } //hide message - if (!rule.getHideMessage() && CLIENT.player != null) { + if (!rule.getHideMessage() && CLIENT.player != null) { CLIENT.player.sendMessage(newMessage, false); } @@ -157,22 +155,24 @@ public class ChatRulesHandler { * @return formatted text */ protected static MutableText formatText(String codedString) { - if (codedString.contains(String.valueOf(Formatting.FORMATTING_CODE_PREFIX)) || codedString.contains("&")){ + if (codedString.contains(String.valueOf(Formatting.FORMATTING_CODE_PREFIX)) || codedString.contains("&")) { MutableText newText = Text.literal(""); String[] parts = codedString.split("[" + Formatting.FORMATTING_CODE_PREFIX +"&]"); Style style = Style.EMPTY; + for (String part : parts) { if (part.isEmpty()) continue; Formatting formatting = Formatting.byCode(part.charAt(0)); - if (formatting != null){ + + if (formatting != null) { style = style.withFormatting(formatting); Text.literal(part.substring(1)).getWithStyle(style).forEach(newText::append); } else { newText.append(Text.of(part)); } } - return newText; + return newText; } - return Text.literal(codedString); + return Text.literal(codedString); } } diff --git a/src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRuleTest.java b/src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRuleTest.java index 0c98debe..81a44ff0 100644 --- a/src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRuleTest.java +++ b/src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRuleTest.java @@ -3,8 +3,6 @@ package de.hysky.skyblocker.skyblock.chat; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - class ChatRuleTest { @Test @@ -13,25 +11,25 @@ class ChatRuleTest { //test enabled check testRule.setFilter("test"); testRule.setEnabled(false); - Assertions.assertEquals(testRule.isMatch("test"),false); + Assertions.assertEquals(testRule.isMatch("test"), false); //test simple filter works testRule.setEnabled(true); - Assertions.assertEquals(testRule.isMatch("test"),true); + Assertions.assertEquals(testRule.isMatch("test"), true); //test partial match works - Assertions.assertEquals(testRule.isMatch("test extra"),false); + Assertions.assertEquals(testRule.isMatch("test extra"), false); testRule.setPartialMatch(true); - Assertions.assertEquals(testRule.isMatch("test extra"),true); + Assertions.assertEquals(testRule.isMatch("test extra"), true); //test ignore case works - Assertions.assertEquals(testRule.isMatch("TEST"),true); + Assertions.assertEquals(testRule.isMatch("TEST"), true); testRule.setIgnoreCase(false); - Assertions.assertEquals(testRule.isMatch("TEST"),false); + Assertions.assertEquals(testRule.isMatch("TEST"), false); //test regex testRule = new ChatRule(); testRule.setRegex(true); testRule.setFilter("[0-9]+"); - Assertions.assertEquals(testRule.isMatch("1234567"),true); - Assertions.assertEquals(testRule.isMatch("1234567 test"),false); + Assertions.assertEquals(testRule.isMatch("1234567"), true); + Assertions.assertEquals(testRule.isMatch("1234567 test"), false); } } \ No newline at end of file diff --git a/src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandlerTest.java b/src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandlerTest.java index 2c1b7956..5f1bf9de 100644 --- a/src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandlerTest.java +++ b/src/test/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandlerTest.java @@ -7,10 +7,6 @@ import net.minecraft.util.Formatting; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.awt.*; - -import static org.junit.jupiter.api.Assertions.*; - class ChatRulesHandlerTest { @Test @@ -29,7 +25,7 @@ class ChatRulesHandlerTest { //generated text MutableText text = ChatRulesHandler.formatText("&1test&nline&2dark green&oitalic"); - Assertions.assertEquals(text,testText); + Assertions.assertEquals(text, testText); } } \ No newline at end of file -- cgit From 4a1026b088dbd0f6c4621999ac94a55de97d16ac Mon Sep 17 00:00:00 2001 From: Aaron <51387595+AzureAaron@users.noreply.github.com> Date: Wed, 21 Feb 2024 00:07:32 -0500 Subject: Multithread the rules and locations loading Avoids blocking the main thread --- .../java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') 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 ef44e670..791f3a3a 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -25,6 +25,7 @@ import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.util.*; +import java.util.concurrent.CompletableFuture; public class ChatRulesHandler { private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); @@ -42,8 +43,8 @@ public class ChatRulesHandler { protected static final List chatRuleList = new ArrayList<>(); public static void init() { - loadChatRules(); - loadLocations(); + CompletableFuture.runAsync(ChatRulesHandler::loadChatRules); + CompletableFuture.runAsync(ChatRulesHandler::loadLocations); ClientReceiveMessageEvents.ALLOW_GAME.register(ChatRulesHandler::checkMessage); } -- cgit From 59dbed3dedf846bc92a8ae05a6472a862da8760a Mon Sep 17 00:00:00 2001 From: Aaron <51387595+AzureAaron@users.noreply.github.com> Date: Sun, 25 Feb 2024 02:16:27 -0500 Subject: Refactor saving/loading to use Codecs The previous solution ended up inadvertently serializing the sound event class fields with their intermediary names which isn't a good idea. --- .../hysky/skyblocker/skyblock/chat/ChatRule.java | 32 ++++++++++++++++++++++ .../skyblocker/skyblock/chat/ChatRulesHandler.java | 17 +++++++----- 2 files changed, 42 insertions(+), 7 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java') 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 7a8214cb..34cc6352 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java @@ -3,13 +3,33 @@ package de.hysky.skyblocker.skyblock.chat; import de.hysky.skyblocker.utils.Utils; import net.minecraft.sound.SoundEvent; +import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.regex.Pattern; +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; + /** * Data class to contain all the settings for a chat rule */ public class ChatRule { + private static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( + Codec.STRING.fieldOf("name").forGetter(ChatRule::getName), + Codec.BOOL.fieldOf("enabled").forGetter(ChatRule::getEnabled), + Codec.BOOL.fieldOf("isPartialMatch").forGetter(ChatRule::getPartialMatch), + Codec.BOOL.fieldOf("isRegex").forGetter(ChatRule::getRegex), + Codec.BOOL.fieldOf("isIgnoreCase").forGetter(ChatRule::getIgnoreCase), + Codec.STRING.fieldOf("filter").forGetter(ChatRule::getFilter), + Codec.STRING.fieldOf("validLocations").forGetter(ChatRule::getValidLocations), + Codec.BOOL.fieldOf("hideMessage").forGetter(ChatRule::getHideMessage), + Codec.BOOL.fieldOf("showActionBar").forGetter(ChatRule::getShowActionBar), + Codec.BOOL.fieldOf("showAnnouncement").forGetter(ChatRule::getShowAnnouncement), + Codec.STRING.optionalFieldOf("replaceMessage").forGetter(ChatRule::getReplaceMessageOpt), + SoundEvent.CODEC.optionalFieldOf("customSound").forGetter(ChatRule::getCustomSoundOpt)) + .apply(instance, ChatRule::new)); + public static final Codec> LIST_CODEC = CODEC.listOf(); private String name; @@ -62,6 +82,10 @@ public class ChatRule { this.customSound = customSound; } + private ChatRule(String name, Boolean enabled, Boolean isPartialMatch, Boolean isRegex, Boolean isIgnoreCase, String filter, String validLocations, Boolean hideMessage, Boolean showActionBar, Boolean showAnnouncement, Optional replaceMessage, Optional customSound) { + this(name, enabled, isPartialMatch, isRegex, isIgnoreCase, filter, validLocations, hideMessage, showActionBar, showAnnouncement, replaceMessage.orElse(null), customSound.orElse(null)); + } + protected String getName() { return name; } @@ -138,6 +162,10 @@ public class ChatRule { return replaceMessage; } + private Optional getReplaceMessageOpt() { + return replaceMessage == null ? Optional.empty() : Optional.of(replaceMessage); + } + protected void setReplaceMessage(String replaceMessage) { this.replaceMessage = replaceMessage; } @@ -146,6 +174,10 @@ public class ChatRule { return customSound; } + private Optional getCustomSoundOpt() { + return customSound == null ? Optional.empty() : Optional.of(customSound); + } + protected void setCustomSound(SoundEvent customSound) { this.customSound = customSound; } 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 791f3a3a..13115bee 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java @@ -3,7 +3,9 @@ package de.hysky.skyblocker.skyblock.chat; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import com.google.gson.reflect.TypeToken; +import com.mojang.serialization.Codec; +import com.mojang.serialization.JsonOps; + import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.utils.Http; import de.hysky.skyblocker.utils.Utils; @@ -20,7 +22,6 @@ import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; -import java.lang.reflect.Type; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; @@ -31,6 +32,7 @@ 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("chat_rules.json"); + private static final Codec>> MAP_CODEC = Codec.unboundedMap(Codec.STRING, ChatRule.LIST_CODEC); /** * look up table for the locations input by the users to raw locations */ @@ -50,15 +52,16 @@ public class ChatRulesHandler { private static void loadChatRules() { try (BufferedReader reader = Files.newBufferedReader(CHAT_RULE_FILE)) { - Type chatRulesType = new TypeToken>>() {}.getType(); - Map> chatRules = SkyblockerMod.GSON.fromJson(reader,chatRulesType); + Map> chatRules = MAP_CODEC.parse(JsonOps.INSTANCE, JsonParser.parseReader(reader)).result().orElseThrow(); + LOGGER.info("[Sky: " + chatRules.toString()); + chatRuleList.addAll(chatRules.get("rules")); LOGGER.info("[Skyblocker] Loaded chat rules"); } catch (NoSuchFileException e) { registerDefaultChatRules(); LOGGER.warn("[Skyblocker] chat rule file not found, using default rules. This is normal when using for the first time."); - } catch (IOException e) { + } catch (Exception e) { LOGGER.error("[Skyblocker] Failed to load shortcuts file", e); } } @@ -95,11 +98,11 @@ public class ChatRulesHandler { protected static void saveChatRules() { JsonObject chatRuleJson = new JsonObject(); - chatRuleJson.add("rules", SkyblockerMod.GSON.toJsonTree(chatRuleList)); + chatRuleJson.add("rules", ChatRule.LIST_CODEC.encodeStart(JsonOps.INSTANCE, chatRuleList).result().orElseThrow()); try (BufferedWriter writer = Files.newBufferedWriter(CHAT_RULE_FILE)) { SkyblockerMod.GSON.toJson(chatRuleJson, writer); LOGGER.info("[Skyblocker] Saved chat rules file"); - } catch (IOException e) { + } catch (Exception e) { LOGGER.error("[Skyblocker] Failed to save chat rules file", e); } } -- cgit