diff --git a/README.md b/README.md index f477dac..53b18e8 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ If you have any questions, you can ask them on my [Discord](https://discord.com/ ## Commands (and permissions): - `/styledchat` - Main command (`styledchat.main`, available by default) -- `/styledchatreload` - Reloads configuration and styles (requires `styledchat.reload`) +- `/styledchat reload` - Reloads configuration and styles (requires `styledchat.reload`) ## Configuration: You can find config file in `./config/styled-chat.json`. @@ -36,6 +36,11 @@ Additionally, every message type has few own local variables. // The same values as in "defaultStyle", however it will handle missing ones just fine // By applying next valid } + }, + "legacyChatFormatting": false, // Enables support for legacy (&x) codes in chat (only when typed by player) + "defaultEnabledFormatting": { + "type": false + // Here you can change which formatting is available by default for player } } ``` diff --git a/build.gradle b/build.gradle index 86d9463..43105e5 100644 --- a/build.gradle +++ b/build.gradle @@ -33,7 +33,7 @@ dependencies { modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - modImplementation include("eu.pb4:placeholder-api:1.0.0-rc1-1.17") + modImplementation include("eu.pb4:placeholder-api:1.0.0-rc2-1.17") modImplementation include("me.lucko:fabric-permissions-api:0.1-SNAPSHOT") modRuntime "supercoder79:databreaker:0.2.7" diff --git a/gradle.properties b/gradle.properties index a8acb15..be26b83 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.0 + mod_version = 1.0.1 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 35f2577..532a673 100644 --- a/src/main/java/eu/pb4/styledchat/config/Config.java +++ b/src/main/java/eu/pb4/styledchat/config/Config.java @@ -1,10 +1,9 @@ package eu.pb4.styledchat.config; -import com.google.common.collect.ImmutableMap; -import eu.pb4.placeholders.TextParser; import eu.pb4.styledchat.config.data.ChatStyleData; import eu.pb4.styledchat.config.data.ConfigData; +import it.unimi.dsi.fastutil.objects.Object2BooleanArrayMap; import me.lucko.fabric.api.permissions.v0.Permissions; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; @@ -12,17 +11,17 @@ import net.minecraft.util.math.MathHelper; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class Config { - //private final ConfigData configData; + public final ConfigData configData; private final ChatStyle defaultStyle; private final List permissionStyle; + public final Object2BooleanArrayMap defaultFormattingCodes; public Config(ConfigData data) { - //this.configData = data; + this.configData = data; this.defaultStyle = new ChatStyle(data.defaultStyle, new ChatStyle(ChatStyleData.getDefault())); this.permissionStyle = new ArrayList<>(); @@ -35,6 +34,7 @@ public Config(ConfigData data) { } } + this.defaultFormattingCodes = new Object2BooleanArrayMap<>(this.configData.defaultEnabledFormatting); } public Text getDisplayName(ServerPlayerEntity player, Text vanillaDisplayName) { diff --git a/src/main/java/eu/pb4/styledchat/config/ConfigManager.java b/src/main/java/eu/pb4/styledchat/config/ConfigManager.java index 2137336..4d6f7a0 100644 --- a/src/main/java/eu/pb4/styledchat/config/ConfigManager.java +++ b/src/main/java/eu/pb4/styledchat/config/ConfigManager.java @@ -9,6 +9,7 @@ import java.io.*; public class ConfigManager { + public static final int VERSION = 1; private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); private static Config CONFIG; @@ -32,7 +33,7 @@ public static boolean loadConfig() { if (configFile.exists()) { - config = GSON.fromJson(new InputStreamReader(new FileInputStream(configFile), "UTF-8"), ConfigData.class); + config = ConfigData.transform(GSON.fromJson(new InputStreamReader(new FileInputStream(configFile), "UTF-8"), ConfigData.class)); } else { config = new ConfigData(); } 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 1630787..8df6402 100644 --- a/src/main/java/eu/pb4/styledchat/config/data/ConfigData.java +++ b/src/main/java/eu/pb4/styledchat/config/data/ConfigData.java @@ -1,9 +1,33 @@ package eu.pb4.styledchat.config.data; +import eu.pb4.placeholders.TextParser; +import eu.pb4.styledchat.config.ConfigManager; + import java.util.HashMap; +import java.util.Map; + public class ConfigData { - public int CONFIG_VERSION_DONT_TOUCH_THIS = 1; + 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 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 static ConfigData transform(ConfigData configData) { + for (Map.Entry entry : getDefaultFormatting().entrySet()) { + configData.defaultEnabledFormatting.putIfAbsent(entry.getKey(), entry.getValue()); + } + return configData; + } } diff --git a/src/main/java/eu/pb4/styledchat/mixin/ServerPlayNetworkManagerMixin.java b/src/main/java/eu/pb4/styledchat/mixin/ServerPlayNetworkManagerMixin.java index c32be97..2c8b846 100644 --- a/src/main/java/eu/pb4/styledchat/mixin/ServerPlayNetworkManagerMixin.java +++ b/src/main/java/eu/pb4/styledchat/mixin/ServerPlayNetworkManagerMixin.java @@ -3,6 +3,7 @@ import eu.pb4.placeholders.TextParser; import eu.pb4.placeholders.util.GeneralUtils; +import eu.pb4.styledchat.config.Config; import eu.pb4.styledchat.config.ConfigManager; import me.lucko.fabric.api.permissions.v0.Permissions; import net.minecraft.network.MessageType; @@ -14,7 +15,7 @@ import net.minecraft.text.LiteralText; import net.minecraft.text.MutableText; import net.minecraft.text.Text; -import net.minecraft.text.TranslatableText; +import net.minecraft.util.Formatting; import net.minecraft.util.Hand; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -43,18 +44,35 @@ private Text replaceDisconnectMessage(Text text) { private void replaceChatMessage(PlayerManager playerManager, Text serverMessage, Function playerMessageFactory, MessageType playerMessageType, UUID sender, TextStream.Message message) { HashMap handlers = new HashMap<>(); ServerCommandSource source = this.player.getCommandSource(); + Config config = ConfigManager.getConfig(); + + for (Map.Entry entry : TextParser.getRegisteredTags().entrySet()) { - if (Permissions.check(source, "styledchat.format." + entry.getKey(), 2)) { + if (Permissions.check(source, "styledchat.format." + entry.getKey(), 2) + || Permissions.check(source, "styledchat.format." + entry.getKey(), config.defaultFormattingCodes.getBoolean(entry.getKey()))) { handlers.put(entry.getKey(), entry.getValue()); } } - if (Permissions.check(source, "styledchat.format.item", 1)) { + if (Permissions.check(source, "styledchat.format.item", 2) + || Permissions.check(source, "styledchat.format.item", config.defaultFormattingCodes.getBoolean("item"))) { handlers.put("item", (tag, data, input, buildInHandlers, endAt) -> new GeneralUtils.TextLengthPair((MutableText) player.getStackInHand(Hand.MAIN_HAND).toHoverableText(), 0)); } - Text rawText = ConfigManager.getConfig().getChat(this.player, handlers.size() > 0 ? TextParser.parse(message.getRaw(), handlers) : new LiteralText(message.getRaw())); - Text filteredText = ConfigManager.getConfig().getChat(this.player, handlers.size() > 0 ? TextParser.parse(message.getFiltered(), handlers) : new LiteralText(message.getFiltered())); + String rawMessage = message.getRaw(); + String filteredMessage = message.getRaw(); + + if (config.configData.legacyChatFormatting) { + for (Formatting formatting : Formatting.values()) { + if (handlers.get(formatting.getName()) != null) { + rawMessage = rawMessage.replaceAll(String.copyValueOf(new char[] {'&', formatting.getCode()}), "<" + formatting.getName() + ">"); + filteredMessage = filteredMessage.replaceAll(String.copyValueOf(new char[] {'&', formatting.getCode()}), "<" + formatting.getName() + ">"); + } + } + } + + Text rawText = config.getChat(this.player, handlers.size() > 0 ? TextParser.parse(rawMessage, handlers) : new LiteralText(message.getRaw())); + Text filteredText = config.getChat(this.player, handlers.size() > 0 ? TextParser.parse(filteredMessage, handlers) : new LiteralText(message.getFiltered())); playerManager.broadcast(rawText, (player) -> this.player.shouldFilterMessagesSentTo(player) ? filteredText : rawText, playerMessageType, sender); }