Kovalski
Elmas Güneş Gibi Parıldıyor
- Katılım
- 17 Kasım 2015
- Mesajlar
- 579
- Elmaslar
- 224
- Puan
- 14.520
- Minecraft
- Kovalski
Merhabalar, bu konuda minestom ile sunucu yaparken en genel herkesin işine yarayacağını düşündüğüm kodlarımı paylaşacağım.
Bu util sayesinde bukkitten alışık olduğumuz &lMerhaba tarzı stil ve renk kodlarını kullanabilirsiniz, sadece &a &b gibi olanları eklemedim onun yerine 𵭶 şeklinde hex kodlarının desteğini ekledim. Bence standart renklerin kullanımından uzaklaşmalıyız isteyen desteğini ekleyebilir tabi.
Bu util sayesinde bukkitten alışık olduğumuz &lMerhaba tarzı stil ve renk kodlarını kullanabilirsiniz, sadece &a &b gibi olanları eklemedim onun yerine 𵭶 şeklinde hex kodlarının desteğini ekledim. Bence standart renklerin kullanımından uzaklaşmalıyız isteyen desteğini ekleyebilir tabi.
Kod:
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ComponentUtils {
private ComponentUtils() {
throw new UnsupportedOperationException("Utility class");
}
// Regex to match color codes (HTML color codes: &#RRGGBB)
private static final Pattern COLOR_CODE_PATTERN = Pattern.compile("&#([A-Fa-f0-9]{6})");
// Regex to match style codes (e.g., bold, italic, etc.)
private static final Pattern STYLE_CODE_PATTERN = Pattern.compile("&([a-zA-Z])");
// Converts a colored string into a list of TextComponent objects
public static Component toTextComponent(String input) {
List<Component> components = new ArrayList<>();
int lastEnd = 0;
Matcher matcher = Pattern.compile(COLOR_CODE_PATTERN.pattern() + "|" + STYLE_CODE_PATTERN.pattern()).matcher(input);
Style currentStyle = Style.empty();
TextColor currentColor = null;
while (matcher.find()) {
// Add the plain text before the code
if (lastEnd != matcher.start()) {
String plainText = input.substring(lastEnd, matcher.start());
components.add(Component.text(plainText).style(currentStyle.color(currentColor)));
}
if (matcher.group(1) != null) { // Color code
String hexColor = matcher.group(1);
int r = Integer.parseInt(hexColor.substring(0, 2), 16);
int g = Integer.parseInt(hexColor.substring(2, 4), 16);
int b = Integer.parseInt(hexColor.substring(4, 6), 16);
currentColor = TextColor.color(r, g, b);
} else if (matcher.group(2) != null) { // Style code
char styleCode = matcher.group(2).toLowerCase().charAt(0);
currentStyle = switch (styleCode) {
case 'l' -> currentStyle.decorate(TextDecoration.BOLD); // Bold
case 'o' -> currentStyle.decorate(TextDecoration.ITALIC); // Italic
case 'n' -> currentStyle.decorate(TextDecoration.UNDERLINED); // Underlined
case 'm' -> currentStyle.decorate(TextDecoration.STRIKETHROUGH); // Strikethrough
case 'k' -> currentStyle.decorate(TextDecoration.OBFUSCATED); // Obfuscated
case 'r' -> Style.empty(); // Reset
default -> currentStyle; // Ignore unknown styles
};
}
lastEnd = matcher.end();
}
// Add the remaining text
if (lastEnd < input.length()) {
components.add(Component.text(input.substring(lastEnd)).style(currentStyle.color(currentColor)));
}
// Combine the list of components into one
return joinComponents(components);
}
private static Component joinComponents(List<Component> components) {
Component result = Component.empty();
for (Component component : components) {
result = result.append(component);
}
return result;
}
// Converts a list of strings with color codes into a single colored TextComponent
public static Component toTextComponentFromList(List<String> inputs) {
List<Component> components = new ArrayList<>();
// Process each string in the list
for (String input : inputs) {
// Trim the input to remove leading/trailing spaces
String trimmedInput = input.trim();
if (trimmedInput.isEmpty()) {
// If the string is empty after trimming, add an empty line
components.add(Component.text("")); // Add an empty TextComponent for empty line
} else {
// Process valid string (with color codes)
components.add(toTextComponent(input)); // Call toTextComponent for each string
}
}
// Manually add line breaks between components
Component result = components.getFirst(); // Start with the first component
for (int i = 1; i < components.size(); i++) {
result = result.append(Component.text("\n")); // Add line break
result = result.append(components.get(i)); // Append next component
}
return result;
}
}
Son düzenleme:
