Skip to content

Commit

Permalink
- Fixed permissionStyle order not being respected
Browse files Browse the repository at this point in the history
- Changed config version to 2, old configs should autoupdate
  • Loading branch information
Patbox committed Jun 18, 2021
1 parent 4678251 commit cb24bf5
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 22 deletions.
73 changes: 61 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,7 +57,7 @@ It supports all default ones with addition of `<item>` 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}",
Expand All @@ -66,13 +70,58 @@ It supports all default ones with addition of `<item>` tag.
"advancementChallenge": "<gray>✔</gray> <gold><lang:chat.type.advancement.challenge:'${player}':'${advancement}'>",
"advancementGoal": "<gray>✔</gray> <gold><lang:chat.type.advancement.goal:'${player}':'${advancement}'>"
},
"permissionStyles": {
"group.admin": {
"chat": "${player} <dark_gray>»</dark_gray> <gold>${message}"
"permissionStyles": [
{
"permission": "group.test",
"style": {
"displayName": "<dark_gray>[<dark_red>Admin</dark_red> | <yellow>%player:playtime%</yellow>]</dark_gray> <red>${vanillaDisplayName}</red>"
}
},
"group.test": {
"displayName": "<dark_gray>[<dark_red>Admin</dark_red> | <yellow>%player:playtime%</yellow>]</dark_gray> <red>${vanillaDisplayName}</red>"
{
"permission": "group.admin",
"style": {
"displayName": "<dark_gray>[<dark_red>Admin</dark_red>]</dark_gray> <red>${vanillaDisplayName}</red>",
"chat": "${player} <dark_gray>»</dark_gray> <gold>${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
}
}
```
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/eu/pb4/styledchat/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ChatStyleData> 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)));
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/eu/pb4/styledchat/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


public class ChatStyleData {
public static ChatStyleData DEFAULT = getDefault();

public String displayName;
public String chat;
public String join;
Expand All @@ -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}";
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/eu/pb4/styledchat/config/data/ConfigData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ChatStyleData> permissionStyles = new HashMap<>();
public ChatStyleData defaultStyle = ChatStyleData.DEFAULT;
public List<PermissionPriorityStyle> permissionStyles = new ArrayList<>();
public boolean legacyChatFormatting = false;
public HashMap<String, Boolean> defaultEnabledFormatting = getDefaultFormatting();

Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
38 changes: 38 additions & 0 deletions src/main/java/eu/pb4/styledchat/config/data/old/ConfigDataV1.java
Original file line number Diff line number Diff line change
@@ -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<String, ChatStyleData> permissionStyles = new HashMap<>();
public boolean legacyChatFormatting = false;
public HashMap<String, Boolean> defaultEnabledFormatting = getDefaultFormatting();


private static HashMap<String, Boolean> getDefaultFormatting() {
HashMap<String, Boolean> 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;
}
}

0 comments on commit cb24bf5

Please sign in to comment.