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

@ -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));
}
}