From b9aeadfc2fe33b60555f978c82bef0294286e92a 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 --- .../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 ++++++++++++++++++++++ 5 files changed, 135 insertions(+), 55 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock/chat') 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