Full hex color support in admin/party chat

This commit is contained in:
nossr50
2020-11-06 15:16:26 -08:00
parent 1383457eba
commit 4abf64f625
8 changed files with 88 additions and 10 deletions

View File

@ -54,7 +54,7 @@ public class AdminChatMailer extends AbstractChatMailer {
*/
public @NotNull TextComponent addStyle(@NotNull Author author, @NotNull String message, boolean canColor) {
if(canColor) {
return TextUtils.ofLegacyTextRaw(LocaleLoader.getString("Chat.Style.Admin", author.getAuthoredName(ChatChannel.ADMIN), LocaleLoader.addColors(message)));
return LocaleLoader.getTextComponent("Chat.Style.Admin", TextUtils.sanitizeAuthorName(author, ChatChannel.ADMIN), message);
} else {
return TextUtils.ofLegacyTextRaw(LocaleLoader.getString("Chat.Style.Admin", author.getAuthoredName(ChatChannel.ADMIN), message));
}
@ -75,4 +75,6 @@ public class AdminChatMailer extends AbstractChatMailer {
sendMail(chatMessage);
}
}
}

View File

@ -46,13 +46,17 @@ public class PartyChatMailer extends AbstractChatMailer {
*/
public @NotNull TextComponent addStyle(@NotNull Author author, @NotNull String message, boolean canColor, boolean isLeader) {
if(canColor) {
message = LocaleLoader.addColors(message);
}
if(isLeader) {
return TextUtils.ofLegacyTextRaw(LocaleLoader.getString("Chat.Style.Party.Leader", author.getAuthoredName(ChatChannel.PARTY), message));
if(isLeader) {
return LocaleLoader.getTextComponent("Chat.Style.Party.Leader", TextUtils.sanitizeAuthorName(author, ChatChannel.PARTY), message);
} else {
return LocaleLoader.getTextComponent("Chat.Style.Party", author.getAuthoredName(ChatChannel.PARTY), message);
}
} else {
return TextUtils.ofLegacyTextRaw(LocaleLoader.getString("Chat.Style.Party", author.getAuthoredName(ChatChannel.PARTY), message));
if(isLeader) {
return TextUtils.ofLegacyTextRaw(LocaleLoader.getString("Chat.Style.Party.Leader", author.getAuthoredName(ChatChannel.PARTY), message));
} else {
return TextUtils.ofLegacyTextRaw(LocaleLoader.getString("Chat.Style.Party", author.getAuthoredName(ChatChannel.PARTY), message));
}
}
}

View File

@ -46,7 +46,7 @@ public class PartyChatMessage extends AbstractChatMessage {
//Sends to everyone but console
audience.sendMessage(author, componentMessage);
TextComponent spyMessage = TextUtils.ofLegacyTextRaw(LocaleLoader.getString("Chat.Spy.Party", author.getAuthoredName(ChatChannel.PARTY), rawMessage, party.getName()));
TextComponent spyMessage = LocaleLoader.getTextComponent("Chat.Spy.Party", TextUtils.sanitizeAuthorName(author, ChatChannel.PARTY), rawMessage, party.getName());
//Relay to spies
messagePartyChatSpies(spyMessage);

View File

@ -59,6 +59,7 @@ public class CommandManager {
BukkitCommandIssuer issuer = context.getIssuer();
if(issuer.getIssuer() instanceof Player) {
validateLoadedData(issuer.getPlayer());
validateAdmin(issuer.getPlayer());
}
});

View File

@ -36,6 +36,9 @@ public class AdminChatCommand extends BaseCommand {
if(bukkitCommandIssuer.isPlayer()) {
McMMOPlayer mmoPlayer = UserManager.getPlayer(bukkitCommandIssuer.getPlayer());
if(mmoPlayer == null)
return;
//Message contains the original command so it needs to be passed to this method to trim it
pluginRef.getChatManager().processPlayerMessage(mmoPlayer, args, ChatChannel.ADMIN);
} else {

View File

@ -2,6 +2,8 @@ package com.gmail.nossr50.locale;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.text.TextUtils;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.ChatColor;
import java.io.IOException;
@ -42,6 +44,23 @@ public final class LocaleLoader {
return formatString(rawMessage, messageArguments);
}
//TODO: Remove this hacky crap with something better later
/**
* Gets the appropriate TextComponent representation of a formatted string from the Locale files.
*
* @param key The key to look up the string with
* @param messageArguments Any arguments to be added to the text component
* @return The properly formatted text component
*/
public static TextComponent getTextComponent(String key, Object... messageArguments) {
if (bundle == null) {
initialize();
}
String rawMessage = bundleCache.computeIfAbsent(key, LocaleLoader::getRawString);
return formatComponent(rawMessage, messageArguments);
}
/**
* Reloads locale
*/
@ -90,6 +109,16 @@ public final class LocaleLoader {
return string;
}
public static TextComponent formatComponent(String string, Object... messageArguments) {
if (messageArguments != null) {
MessageFormat formatter = new MessageFormat("");
formatter.applyPattern(string.replace("'", "''"));
string = formatter.format(messageArguments);
}
return TextUtils.colorizeText(string);
}
public static Locale getCurrentLocale() {
if (bundle == null) {
initialize();

View File

@ -1,5 +1,7 @@
package com.gmail.nossr50.util.text;
import com.gmail.nossr50.chat.author.Author;
import com.gmail.nossr50.datatypes.chat.ChatChannel;
import com.gmail.nossr50.mcMMO;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentBuilder;
@ -14,6 +16,9 @@ import org.jetbrains.annotations.Nullable;
import java.util.List;
public class TextUtils {
private static @Nullable LegacyComponentSerializer customLegacySerializer;
/**
* Makes a single component from an array of components, can optionally add prefixes and suffixes to come before and after each component
* @param componentsArray target array
@ -103,4 +108,30 @@ public class TextUtils {
public static @NotNull TextComponent ofLegacyTextRaw(@NotNull String rawString) {
return LegacyComponentSerializer.legacySection().deserialize(rawString);
}
public static @NotNull TextComponent colorizeText(String rawtext) {
if(customLegacySerializer == null) {
customLegacySerializer = getSerializer();
}
return customLegacySerializer.deserialize(rawtext);
}
@NotNull
private static LegacyComponentSerializer getSerializer() {
return LegacyComponentSerializer.builder().hexColors().useUnusualXRepeatedCharacterHexFormat().character('&').hexCharacter('#').build();
}
public static @NotNull String sanitizeForSerializer(@NotNull String string) {
if(customLegacySerializer == null) {
customLegacySerializer = getSerializer();
}
TextComponent componentForm = ofLegacyTextRaw(string);
return customLegacySerializer.serialize(componentForm);
}
public static @NotNull String sanitizeAuthorName(@NotNull Author author, @NotNull ChatChannel chatChannel) {
return sanitizeForSerializer(author.getAuthoredName(ChatChannel.ADMIN));
}
}