aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron <51387595+AzureAaron@users.noreply.github.com>2024-02-25 02:16:27 -0500
committerAaron <51387595+AzureAaron@users.noreply.github.com>2024-02-25 02:16:27 -0500
commit59dbed3dedf846bc92a8ae05a6472a862da8760a (patch)
tree3cc9f4083a9229e6fc27c1f26d03f18c1e28ab88
parentae28951650980bb563542ad62c13b71ce991d7e3 (diff)
downloadSkyblocker-59dbed3dedf846bc92a8ae05a6472a862da8760a.tar.gz
Skyblocker-59dbed3dedf846bc92a8ae05a6472a862da8760a.tar.bz2
Skyblocker-59dbed3dedf846bc92a8ae05a6472a862da8760a.zip
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.
-rw-r--r--src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java32
-rw-r--r--src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRulesHandler.java17
2 files changed, 42 insertions, 7 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java b/src/main/java/de/hysky/skyblocker/skyblock/chat/ChatRule.java
index 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<ChatRule> 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<ChatRule>> 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<String> replaceMessage, Optional<SoundEvent> 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<String> 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<SoundEvent> 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<String, List<ChatRule>>> 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<Map<String, List<ChatRule>>>() {}.getType();
- Map<String, List<ChatRule>> chatRules = SkyblockerMod.GSON.fromJson(reader,chatRulesType);
+ Map<String, List<ChatRule>> 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);
}
}