Skip to content

Commit

Permalink
Configuration improvements
Browse files Browse the repository at this point in the history
- Added support for using legacy (&X) codes in chat (they are converted internally to tags). It's toggled off by default
- You can now change which formatting is available for players by default
  • Loading branch information
Patbox committed Jun 16, 2021
1 parent c9ffd4b commit 4678251
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 15 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
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.0
mod_version = 1.0.1
maven_group = eu.pb4
archives_base_name = styled-chat

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/eu/pb4/styledchat/config/Config.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
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;
import net.minecraft.text.Text;
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> permissionStyle;
public final Object2BooleanArrayMap<String> 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<>();
Expand All @@ -35,6 +34,7 @@ public Config(ConfigData data) {
}
}

this.defaultFormattingCodes = new Object2BooleanArrayMap<>(this.configData.defaultEnabledFormatting);
}

public Text getDisplayName(ServerPlayerEntity player, Text vanillaDisplayName) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/eu/pb4/styledchat/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/eu/pb4/styledchat/config/data/ConfigData.java
Original file line number Diff line number Diff line change
@@ -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<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 static ConfigData transform(ConfigData configData) {
for (Map.Entry<String, Boolean> entry : getDefaultFormatting().entrySet()) {
configData.defaultEnabledFormatting.putIfAbsent(entry.getKey(), entry.getValue());
}
return configData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -43,18 +44,35 @@ private Text replaceDisconnectMessage(Text text) {
private void replaceChatMessage(PlayerManager playerManager, Text serverMessage, Function<ServerPlayerEntity, Text> playerMessageFactory, MessageType playerMessageType, UUID sender, TextStream.Message message) {
HashMap<String, TextParser.TextFormatterHandler> handlers = new HashMap<>();
ServerCommandSource source = this.player.getCommandSource();
Config config = ConfigManager.getConfig();


for (Map.Entry<String, TextParser.TextFormatterHandler> 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);
}
Expand Down

0 comments on commit 4678251

Please sign in to comment.