Fix HEX color parsing - normalize to lowercase - Added normalizeMiniMessageHex to convert existing <#RRGGBB> tags to lowercase - convertHexColors now outputs lowercase hex codes - Added send/getComponent methods to MessageConfig
This commit is contained in:
parent
d2c45b3f0c
commit
f4752000c8
2 changed files with 51 additions and 4 deletions
|
|
@ -2,6 +2,8 @@ package com.loki.lochat.config;
|
|||
|
||||
import com.loki.lochat.LoChat;
|
||||
import com.loki.lochat.utils.ChatFormatter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
|
|
@ -43,6 +45,34 @@ public class MessageConfig {
|
|||
return ChatFormatter.convertAllColors(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает сообщение как Component
|
||||
*/
|
||||
public Component getComponent(String path) {
|
||||
return ChatFormatter.parse(get(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает сообщение как Component с заменами
|
||||
*/
|
||||
public Component getComponent(String path, String... replacements) {
|
||||
return ChatFormatter.parse(get(path, replacements));
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправляет сообщение игроку
|
||||
*/
|
||||
public void send(CommandSender sender, String path) {
|
||||
sender.sendMessage(getComponent(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправляет сообщение игроку с заменами
|
||||
*/
|
||||
public void send(CommandSender sender, String path, String... replacements) {
|
||||
sender.sendMessage(getComponent(path, replacements));
|
||||
}
|
||||
|
||||
// Shortcuts
|
||||
public String getPrefix() {
|
||||
return get("prefix");
|
||||
|
|
|
|||
|
|
@ -39,15 +39,32 @@ public class ChatFormatter {
|
|||
// 2. Конвертируем §коды -> &коды (для унификации)
|
||||
result = convertSectionToAmpersand(result);
|
||||
|
||||
// 3. Конвертируем &#RRGGBB -> <#RRGGBB> (только если еще не в MiniMessage формате)
|
||||
// 3. Нормализуем существующие MiniMessage HEX теги в lowercase
|
||||
result = normalizeMiniMessageHex(result);
|
||||
|
||||
// 4. Конвертируем &#RRGGBB -> <#RRGGBB> (только если еще не в MiniMessage формате)
|
||||
result = convertHexColors(result);
|
||||
|
||||
// 4. Конвертируем &коды -> MiniMessage теги
|
||||
// 5. Конвертируем &коды -> MiniMessage теги
|
||||
result = convertLegacyColors(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Нормализует существующие MiniMessage HEX теги в lowercase
|
||||
*/
|
||||
public static String normalizeMiniMessageHex(String message) {
|
||||
if (message == null) return "";
|
||||
Matcher matcher = MINIMESSAGE_HEX_PATTERN.matcher(message);
|
||||
StringBuilder result = new StringBuilder();
|
||||
while (matcher.find()) {
|
||||
matcher.appendReplacement(result, "<#" + matcher.group(1).toLowerCase() + ">");
|
||||
}
|
||||
matcher.appendTail(result);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Конвертирует §x§R§R§G§G§B§B в <#RRGGBB>
|
||||
*/
|
||||
|
|
@ -83,7 +100,7 @@ public class ChatFormatter {
|
|||
Matcher matcher = HEX_PATTERN.matcher(result);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (matcher.find()) {
|
||||
matcher.appendReplacement(sb, "<#" + matcher.group(1) + ">");
|
||||
matcher.appendReplacement(sb, "<#" + matcher.group(1).toLowerCase() + ">");
|
||||
}
|
||||
matcher.appendTail(sb);
|
||||
result = sb.toString();
|
||||
|
|
@ -92,7 +109,7 @@ public class ChatFormatter {
|
|||
matcher = HEX_PATTERN_NO_AMPERSAND.matcher(result);
|
||||
sb = new StringBuilder();
|
||||
while (matcher.find()) {
|
||||
matcher.appendReplacement(sb, "<#" + matcher.group(1) + ">");
|
||||
matcher.appendReplacement(sb, "<#" + matcher.group(1).toLowerCase() + ">");
|
||||
}
|
||||
matcher.appendTail(sb);
|
||||
return sb.toString();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue