diff --git a/README.md b/README.md index 53b18e8..248a81e 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,16 @@ Additionally, every message type has few own local variables. "advancementChallenge": "...", // Finishing advancement challenge (local variables: ${player}, ${advancement}) "advancementGoal": "...", // Finishing advancement goal (local variables: ${player}, ${advancement}) }, - "permissionStyles": { // Permission based overrides, applied from highest to lowest - "permission.test": { // Permission string required to use - // The same values as in "defaultStyle", however it will handle missing ones just fine - // By applying next valid - } - }, + "permissionStyles": [ // Permission based overrides, applied from highest to lowest + { + "permission": "...", // Permission string required to use + "style": { + // The same values as in "defaultStyle", however it will handle missing ones just fine + // By applying next valid + } + } // You can have as many permission overrides as possible + // Just remember to have most important ones above least (so for example Admin, Moderator, Helper) + ], "legacyChatFormatting": false, // Enables support for legacy (&x) codes in chat (only when typed by player) "defaultEnabledFormatting": { "type": false @@ -53,7 +57,7 @@ It supports all default ones with addition of `` tag. ## Example config ```json { - "CONFIG_VERSION_DONT_TOUCH_THIS": 1, + "CONFIG_VERSION_DONT_TOUCH_THIS": 2, "_comment": "Before changing anything, see https://github.com/Patbox/StyledChat#configuration", "defaultStyle": { "displayName": "${vanillaDisplayName}", @@ -66,13 +70,58 @@ It supports all default ones with addition of `` tag. "advancementChallenge": " ", "advancementGoal": " " }, - "permissionStyles": { - "group.admin": { - "chat": "${player} » ${message}" + "permissionStyles": [ + { + "permission": "group.test", + "style": { + "displayName": "[Admin | %player:playtime%] ${vanillaDisplayName}" + } }, - "group.test": { - "displayName": "[Admin | %player:playtime%] ${vanillaDisplayName}" + { + "permission": "group.admin", + "style": { + "displayName": "[Admin] ${vanillaDisplayName}", + "chat": "${player} » ${message}" + } } + ], + "legacyChatFormatting": true, + "defaultEnabledFormatting": { + "dark_red": true, + "color": false, + "underline": true, + "yellow": false, + "insert": false, + "italic": false, + "dark_blue": false, + "dark_purple": true, + "gold": true, + "red": false, + "aqua": false, + "hover": false, + "gray": false, + "light_purple": false, + "white": false, + "dark_gray": false, + "strikethrough": false, + "lang": false, + "obfuscated": false, + "key": false, + "item": true, + "green": false, + "c": true, + "dark_green": false, + "gradient": false, + "black": false, + "bold": false, + "gr": false, + "click": false, + "rb": false, + "rainbow": false, + "blue": false, + "dark_aqua": false, + "reset": true, + "font": false } } ``` \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index be26b83..1a02344 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.11.3 # Mod Properties - mod_version = 1.0.1 + mod_version = 1.0.2 maven_group = eu.pb4 archives_base_name = styled-chat diff --git a/src/main/java/eu/pb4/styledchat/config/Config.java b/src/main/java/eu/pb4/styledchat/config/Config.java index 532a673..94146a1 100644 --- a/src/main/java/eu/pb4/styledchat/config/Config.java +++ b/src/main/java/eu/pb4/styledchat/config/Config.java @@ -22,15 +22,15 @@ public final class Config { public Config(ConfigData data) { this.configData = data; - this.defaultStyle = new ChatStyle(data.defaultStyle, new ChatStyle(ChatStyleData.getDefault())); + this.defaultStyle = new ChatStyle(data.defaultStyle, new ChatStyle(ChatStyleData.DEFAULT)); this.permissionStyle = new ArrayList<>(); - for (Map.Entry entry : data.permissionStyles.entrySet()) { + for (ConfigData.PermissionPriorityStyle entry : data.permissionStyles) { try { - this.permissionStyle.add(new PermissionStyle(entry.getKey(), MathHelper.clamp(Integer.parseInt(entry.getKey()), 1, 4), new ChatStyle(entry.getValue()))); + this.permissionStyle.add(new PermissionStyle(entry.permission, MathHelper.clamp(Integer.parseInt(entry.permission), 1, 4), new ChatStyle(entry.style))); } catch (Exception e) { - this.permissionStyle.add(new PermissionStyle(entry.getKey(), 4, new ChatStyle(entry.getValue()))); + this.permissionStyle.add(new PermissionStyle(entry.permission, 4, new ChatStyle(entry.style))); } } diff --git a/src/main/java/eu/pb4/styledchat/config/ConfigManager.java b/src/main/java/eu/pb4/styledchat/config/ConfigManager.java index 4d6f7a0..9d694ae 100644 --- a/src/main/java/eu/pb4/styledchat/config/ConfigManager.java +++ b/src/main/java/eu/pb4/styledchat/config/ConfigManager.java @@ -4,12 +4,15 @@ import com.google.gson.GsonBuilder; import eu.pb4.styledchat.StyledChatMod; import eu.pb4.styledchat.config.data.ConfigData; +import eu.pb4.styledchat.config.data.VersionConfigData; +import eu.pb4.styledchat.config.data.old.ConfigDataV1; import net.fabricmc.loader.api.FabricLoader; +import org.apache.commons.io.IOUtils; import java.io.*; public class ConfigManager { - public static final int VERSION = 1; + public static final int VERSION = 2; private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); private static Config CONFIG; @@ -33,7 +36,13 @@ public static boolean loadConfig() { if (configFile.exists()) { - config = ConfigData.transform(GSON.fromJson(new InputStreamReader(new FileInputStream(configFile), "UTF-8"), ConfigData.class)); + String json = IOUtils.toString(new InputStreamReader(new FileInputStream(configFile), "UTF-8")); + VersionConfigData versionConfigData = GSON.fromJson(json, VersionConfigData.class); + + config = ConfigData.transform(switch (versionConfigData.CONFIG_VERSION_DONT_TOUCH_THIS) { + case 1 -> GSON.fromJson(json, ConfigDataV1.class).updateToV2(); + default -> GSON.fromJson(json, ConfigData.class); + }); } else { config = new ConfigData(); } diff --git a/src/main/java/eu/pb4/styledchat/config/data/ChatStyleData.java b/src/main/java/eu/pb4/styledchat/config/data/ChatStyleData.java index 4caf0da..e817a8d 100644 --- a/src/main/java/eu/pb4/styledchat/config/data/ChatStyleData.java +++ b/src/main/java/eu/pb4/styledchat/config/data/ChatStyleData.java @@ -2,6 +2,8 @@ public class ChatStyleData { + public static ChatStyleData DEFAULT = getDefault(); + public String displayName; public String chat; public String join; @@ -13,7 +15,7 @@ public class ChatStyleData { public String advancementGoal; - public static ChatStyleData getDefault() { + private static ChatStyleData getDefault() { ChatStyleData data = new ChatStyleData(); data.displayName = "${vanillaDisplayName}"; data.chat = "<${player}> ${message}"; diff --git a/src/main/java/eu/pb4/styledchat/config/data/ConfigData.java b/src/main/java/eu/pb4/styledchat/config/data/ConfigData.java index 8df6402..c2e92bd 100644 --- a/src/main/java/eu/pb4/styledchat/config/data/ConfigData.java +++ b/src/main/java/eu/pb4/styledchat/config/data/ConfigData.java @@ -3,14 +3,16 @@ import eu.pb4.placeholders.TextParser; import eu.pb4.styledchat.config.ConfigManager; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; public class ConfigData { public int CONFIG_VERSION_DONT_TOUCH_THIS = ConfigManager.VERSION; public String _comment = "Before changing anything, see https://github.com/Patbox/StyledChat#configuration"; - public ChatStyleData defaultStyle = ChatStyleData.getDefault(); - public HashMap permissionStyles = new HashMap<>(); + public ChatStyleData defaultStyle = ChatStyleData.DEFAULT; + public List permissionStyles = new ArrayList<>(); public boolean legacyChatFormatting = false; public HashMap defaultEnabledFormatting = getDefaultFormatting(); @@ -30,4 +32,16 @@ public static ConfigData transform(ConfigData configData) { } return configData; } + + public static class PermissionPriorityStyle { + public String permission = ""; + public ChatStyleData style = ChatStyleData.DEFAULT; + + public static PermissionPriorityStyle of(String permission, ChatStyleData style) { + PermissionPriorityStyle priorityStyle = new PermissionPriorityStyle(); + priorityStyle.permission = permission; + priorityStyle.style = style; + return priorityStyle; + } + } } diff --git a/src/main/java/eu/pb4/styledchat/config/data/VersionConfigData.java b/src/main/java/eu/pb4/styledchat/config/data/VersionConfigData.java new file mode 100644 index 0000000..544439f --- /dev/null +++ b/src/main/java/eu/pb4/styledchat/config/data/VersionConfigData.java @@ -0,0 +1,7 @@ +package eu.pb4.styledchat.config.data; + +import eu.pb4.styledchat.config.ConfigManager; + +public class VersionConfigData { + public int CONFIG_VERSION_DONT_TOUCH_THIS = ConfigManager.VERSION; +} diff --git a/src/main/java/eu/pb4/styledchat/config/data/old/ConfigDataV1.java b/src/main/java/eu/pb4/styledchat/config/data/old/ConfigDataV1.java new file mode 100644 index 0000000..c6aeb74 --- /dev/null +++ b/src/main/java/eu/pb4/styledchat/config/data/old/ConfigDataV1.java @@ -0,0 +1,38 @@ +package eu.pb4.styledchat.config.data.old; + +import eu.pb4.placeholders.TextParser; +import eu.pb4.styledchat.config.data.ChatStyleData; +import eu.pb4.styledchat.config.data.ConfigData; + +import java.util.HashMap; +import java.util.stream.Collectors; + +public class ConfigDataV1 { + public int CONFIG_VERSION_DONT_TOUCH_THIS = 1; + public String _comment = "Before changing anything, see https://github.com/Patbox/StyledChat#configuration"; + public ChatStyleData defaultStyle = ChatStyleData.DEFAULT; + public HashMap permissionStyles = new HashMap<>(); + public boolean legacyChatFormatting = false; + public HashMap defaultEnabledFormatting = getDefaultFormatting(); + + + private static HashMap getDefaultFormatting() { + HashMap map = new HashMap<>(); + for (String string : TextParser.getRegisteredTags().keySet()) { + map.put(string, false); + } + map.put("item", true); + return map; + } + + public ConfigData updateToV2() { + ConfigData data = new ConfigData(); + data.defaultStyle = this.defaultStyle; + data.legacyChatFormatting = this.legacyChatFormatting; + data.defaultEnabledFormatting = this.defaultEnabledFormatting; + + data.permissionStyles = this.permissionStyles.entrySet().stream().map((entry) -> ConfigData.PermissionPriorityStyle.of(entry.getKey(), entry.getValue())).collect(Collectors.toList()); + + return data; + } +}