Skip to content

Commit

Permalink
Add proposed changes
Browse files Browse the repository at this point in the history
- Removed click tag from parsing on chat
- Added <pos> tag to chat parsing. It displays players current position
- Added support for clickable links
- You can now change first time join message
  • Loading branch information
Patbox committed Jun 29, 2021
1 parent cb24bf5 commit c130fd2
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 35 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.17
yarn_mappings=1.17+build.1
yarn_mappings=1.17+build.13
loader_version=0.11.3

# Mod Properties
mod_version = 1.0.2
mod_version = 1.1.0
maven_group = eu.pb4
archives_base_name = styled-chat

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/eu/pb4/styledchat/StyledChatMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class StyledChatMod implements ModInitializer {
public static final Logger LOGGER = LogManager.getLogger("Styled Chat");
public static String VERSION = FabricLoader.getInstance().getModContainer("styledchat").get().getMetadata().getVersion().getFriendlyString();

public static final String URL_REGEX = "(https?:\\/\\/[-a-zA-Z0-9@:%._\\+~#=]+\\.[^ ]+)";

@Override
public void onInitialize() {
Commands.register();
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/eu/pb4/styledchat/command/Commands.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package eu.pb4.styledchat.command;


import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import eu.pb4.styledchat.StyledChatMod;
import eu.pb4.styledchat.config.ConfigManager;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Formatting;

import java.util.Collection;
import java.util.Locale;


import static net.minecraft.server.command.CommandManager.literal;

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/eu/pb4/styledchat/config/ChatStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class ChatStyle {
public final Text displayName;
public final Text chat;
public final Text join;
public final Text joinFirstTime;
public final Text joinRenamed;
public final Text left;
public final Text death;
Expand All @@ -24,6 +25,7 @@ public ChatStyle(ChatStyleData data, ChatStyle defaultStyle) {
this.displayName = data.displayName != null ? TextParser.parse(data.displayName.replace("%player:displayname%", "")) : defaultStyle.displayName;
this.chat = data.chat != null ? TextParser.parse(data.chat) : defaultStyle.chat;
this.join = data.join != null ? TextParser.parse(data.join) : defaultStyle.join;
this.joinFirstTime = data.joinFirstTime != null ? TextParser.parse(data.joinFirstTime) : this.join;
this.joinRenamed = data.joinRenamed != null ? TextParser.parse(data.joinRenamed) : defaultStyle.joinRenamed;
this.left = data.left != null ? TextParser.parse(data.left) : defaultStyle.left;
this.death = data.death != null ? TextParser.parse(data.death) : defaultStyle.death;
Expand All @@ -38,6 +40,7 @@ public ChatStyle(ChatStyleData data) {
this.chat = data.chat != null ? TextParser.parse(data.chat) : null;
this.join = data.join != null ? TextParser.parse(data.join) : null;
this.joinRenamed = data.joinRenamed != null ? TextParser.parse(data.joinRenamed) : null;
this.joinFirstTime = data.joinFirstTime != null ? TextParser.parse(data.joinFirstTime) : null;
this.left = data.left != null ? TextParser.parse(data.left) : null;
this.death = data.death != null ? TextParser.parse(data.death) : null;
this.advancementTask = data.advancementTask != null ? TextParser.parse(data.advancementTask) : null;
Expand Down Expand Up @@ -84,6 +87,18 @@ public Text getJoin(ServerPlayerEntity player) {
);
}

public Text getJoinFirstTime(ServerPlayerEntity player) {
if (this.joinFirstTime == null) {
return null;
}

return PlaceholderAPI.parsePredefinedText(
PlaceholderAPI.parseText(this.joinFirstTime, player),
PlaceholderAPI.PREDEFINED_PLACEHOLDER_PATTERN,
Map.of("player", player.getDisplayName())
);
}

public Text getJoinRenamed(ServerPlayerEntity player, String oldName) {
if (this.joinRenamed == null) {
return null;
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/eu/pb4/styledchat/config/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.pb4.styledchat.config;


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;
Expand All @@ -12,19 +13,20 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public final class Config {
public final ConfigData configData;
private final ChatStyle defaultStyle;
private final List<PermissionStyle> permissionStyle;
public final Object2BooleanArrayMap<String> defaultFormattingCodes;
public final Text linkStyle;

public Config(ConfigData data) {
this.configData = data;
this.defaultStyle = new ChatStyle(data.defaultStyle, new ChatStyle(ChatStyleData.DEFAULT));

this.permissionStyle = new ArrayList<>();
this.linkStyle = TextParser.parse(data.linkStyle);

for (ConfigData.PermissionPriorityStyle entry : data.permissionStyles) {
try {
Expand Down Expand Up @@ -76,6 +78,23 @@ public Text getJoin(ServerPlayerEntity player) {
return this.defaultStyle.getJoin(player);
}

public Text getJoinFirstTime(ServerPlayerEntity player) {
ServerCommandSource source = player.getCommandSource();
for (PermissionStyle entry : this.permissionStyle) {
if (Permissions.check(source, entry.permission, entry.opLevel)) {
Text text = entry.style.getJoinFirstTime(player);
if (text != null) {
return text;
}
}
}
Text text = this.defaultStyle.getJoinFirstTime(player);
if (text != null) {
return text;
}
return this.getJoin(player);
}

public Text getJoinRenamed(ServerPlayerEntity player, String oldName) {
ServerCommandSource source = player.getCommandSource();
for (PermissionStyle entry : this.permissionStyle) {
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/eu/pb4/styledchat/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@ public class ConfigManager {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();

private static Config CONFIG;
private static boolean ENABLED = false;

public static Config getConfig() {
return CONFIG;
}

public static boolean isEnabled() {
return ENABLED;
}

public static boolean loadConfig() {
ENABLED = false;

CONFIG = null;
try {
ConfigData config;
Expand All @@ -53,14 +46,13 @@ public static boolean loadConfig() {


CONFIG = new Config(config);
ENABLED = true;
return true;
}
catch(IOException exception) {
ENABLED = false;
StyledChatMod.LOGGER.error("Something went wrong while reading config!");
exception.printStackTrace();
CONFIG = new Config(new ConfigData());
return false;
}

return ENABLED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class ChatStyleData {
public String chat;
public String join;
public String joinRenamed;
public String joinFirstTime;
public String left;
public String death;
public String advancementTask;
Expand All @@ -21,6 +22,7 @@ private static ChatStyleData getDefault() {
data.chat = "<${player}> ${message}";
data.join = "<yellow><lang:multiplayer.player.joined:'${player}'></yellow>";
data.joinRenamed = "<yellow><lang:multiplayer.player.joined.renamed:'${player}':'${old_name}'></yellow>";
data.joinFirstTime = "<yellow><lang:multiplayer.player.joined:'${player}'></yellow>";
data.left = "<yellow><lang:multiplayer.player.left:'${player}'></yellow>";
data.death = "${default_message}";
data.advancementTask = "<lang:chat.type.advancement.task:'${player}':'${advancement}'>";
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/eu/pb4/styledchat/config/data/ConfigData.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ public class ConfigData {
public ChatStyleData defaultStyle = ChatStyleData.DEFAULT;
public List<PermissionPriorityStyle> permissionStyles = new ArrayList<>();
public boolean legacyChatFormatting = false;
public boolean parseLinksInChat = true;
public String linkStyle = "<underline><blue>${link}";
public HashMap<String, Boolean> defaultEnabledFormatting = getDefaultFormatting();


private static HashMap<String, Boolean> getDefaultFormatting() {
HashMap<String, Boolean> map = new HashMap<>();
for (String string : TextParser.getRegisteredTags().keySet()) {
if (string.equals("click")) {
continue;
}
map.put(string, false);
}
map.put("item", true);
map.put("pos", true);
return map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.network.ClientConnection;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import org.spongepowered.asm.mixin.Mixin;
Expand Down Expand Up @@ -31,8 +32,11 @@ private void removeStoredPlayer(ClientConnection connection, ServerPlayerEntity

@ModifyArg(method = "onPlayerConnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;broadcastChatMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/MessageType;Ljava/util/UUID;)V"))
private Text updatePlayerNameAfterMessage(Text text) {
Object[] args = ((TranslatableText) text).getArgs();
if (this.temporaryPlayer.getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(Stats.LEAVE_GAME)) == 0) {
return ConfigManager.getConfig().getJoinFirstTime(this.temporaryPlayer);
}

Object[] args = ((TranslatableText) text).getArgs();
if (args.length == 1) {
return ConfigManager.getConfig().getJoin(this.temporaryPlayer);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package eu.pb4.styledchat.mixin;


import eu.pb4.placeholders.PlaceholderAPI;
import eu.pb4.placeholders.TextParser;
import eu.pb4.placeholders.util.GeneralUtils;
import eu.pb4.placeholders.util.TextParserUtils;
import eu.pb4.styledchat.StyledChatMod;
import eu.pb4.styledchat.config.Config;
import eu.pb4.styledchat.config.ConfigManager;
import me.lucko.fabric.api.permissions.v0.Permissions;
Expand All @@ -12,9 +15,7 @@
import net.minecraft.server.filter.TextStream;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.*;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -27,6 +28,8 @@
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Mixin(ServerPlayNetworkHandler.class)
Expand All @@ -46,34 +49,58 @@ private void replaceChatMessage(PlayerManager playerManager, Text serverMessage,
ServerCommandSource source = this.player.getCommandSource();
Config config = ConfigManager.getConfig();

String rawMessage = message.getRaw();
String filteredMessage = message.getRaw();

for (Map.Entry<String, TextParser.TextFormatterHandler> entry : TextParser.getRegisteredTags().entrySet()) {
if (Permissions.check(source, "styledchat.format." + entry.getKey(), 2)
|| Permissions.check(source, "styledchat.format." + entry.getKey(), config.defaultFormattingCodes.getBoolean(entry.getKey()))) {
if (!entry.getKey().equals("click")
&& config.defaultFormattingCodes.getBoolean(entry.getKey())
|| Permissions.check(source, "styledchat.format." + entry.getKey(), 2)) {
handlers.put(entry.getKey(), entry.getValue());
}
}

if (Permissions.check(source, "styledchat.format.item", 2)
|| Permissions.check(source, "styledchat.format.item", config.defaultFormattingCodes.getBoolean("item"))) {
if (config.defaultFormattingCodes.getBoolean("item") ||
Permissions.check(source, "styledchat.format.item", 2)) {
handlers.put("item", (tag, data, input, buildInHandlers, endAt) -> new GeneralUtils.TextLengthPair((MutableText) player.getStackInHand(Hand.MAIN_HAND).toHoverableText(), 0));
}

String rawMessage = message.getRaw();
String filteredMessage = message.getRaw();
if (config.defaultFormattingCodes.getBoolean("pos") ||
Permissions.check(source, "styledchat.format.pos", 2)) {
handlers.put("pos", (tag, data, input, buildInHandlers, endAt) -> new GeneralUtils.TextLengthPair(new LiteralText(player.getBlockPos().toShortString()), 0));
}

if (config.configData.parseLinksInChat
|| Permissions.check(source, "styledchat.links", 2)) {
handlers.put("sc-link", (tag, data, input, buildInHandlers, endAt) -> {
String url = TextParserUtils.cleanArgument(data);
return new GeneralUtils.TextLengthPair(
(MutableText) PlaceholderAPI.parsePredefinedText(
config.linkStyle,
PlaceholderAPI.PREDEFINED_PLACEHOLDER_PATTERN,
Map.of("link", new LiteralText(url).setStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url))))
), 0);
});


rawMessage = rawMessage.replaceAll(StyledChatMod.URL_REGEX, "<sc-link:'$1'>");
filteredMessage = filteredMessage.replaceAll(StyledChatMod.URL_REGEX, "<sc-link:'$1'>");
}

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() + ">");
rawMessage = rawMessage.replace(String.copyValueOf(new char[] {'&', formatting.getCode()}), "<" + formatting.getName() + ">");
filteredMessage = filteredMessage.replace(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);
if (rawText != null && filteredText != null) {
playerManager.broadcast(rawText, (player) -> this.player.shouldFilterMessagesSentTo(player) ? filteredText : rawText, playerMessageType, sender);
}
}
}

0 comments on commit c130fd2

Please sign in to comment.