Dateien nach "src/main/java/com/mviper/zenithjoin/utils" hochladen
This commit is contained in:
95
src/main/java/com/mviper/zenithjoin/utils/ChatUtils.java
Normal file
95
src/main/java/com/mviper/zenithjoin/utils/ChatUtils.java
Normal file
@@ -0,0 +1,95 @@
|
||||
package com.mviper.zenithjoin.utils;
|
||||
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Eine Utility-Klasse für Chat-bezogene Funktionen, wie z.B. das Zentrieren von Text.
|
||||
*/
|
||||
public class ChatUtils {
|
||||
|
||||
// Die Standard-Breite des Minecraft-Chats in Pixeln.
|
||||
private static final int CHAT_WIDTH = 320;
|
||||
// Die Breite eines Standard-Leerzeichens in Pixeln.
|
||||
private static final int SPACE_WIDTH = 4;
|
||||
|
||||
// Eine Map, die die Pixelbreite für jedes Zeichen speichert.
|
||||
private static final Map<Character, Integer> CHAR_WIDTHS = new HashMap<>();
|
||||
|
||||
// Statischer Initialisierungsblock, um die Zeichenbreiten zu füllen.
|
||||
static {
|
||||
// Standardbreite für die meisten Zeichen.
|
||||
for (char c = ' '; c <= '~'; c++) {
|
||||
CHAR_WIDTHS.put(c, 6);
|
||||
}
|
||||
|
||||
// Spezielle Zeichen mit abweichender Breite.
|
||||
CHAR_WIDTHS.put('i', 2);
|
||||
CHAR_WIDTHS.put('j', 3);
|
||||
CHAR_WIDTHS.put('l', 3);
|
||||
CHAR_WIDTHS.put('t', 4);
|
||||
CHAR_WIDTHS.put('f', 4);
|
||||
CHAR_WIDTHS.put('k', 4);
|
||||
CHAR_WIDTHS.put('r', 4);
|
||||
CHAR_WIDTHS.put('!', 2);
|
||||
CHAR_WIDTHS.put('.', 2);
|
||||
CHAR_WIDTHS.put(',', 2);
|
||||
CHAR_WIDTHS.put(':', 3);
|
||||
CHAR_WIDTHS.put(';', 3);
|
||||
CHAR_WIDTHS.put('|', 2);
|
||||
CHAR_WIDTHS.put('I', 5);
|
||||
CHAR_WIDTHS.put('<', 5);
|
||||
CHAR_WIDTHS.put('>', 5);
|
||||
CHAR_WIDTHS.put('(', 5);
|
||||
CHAR_WIDTHS.put(')', 5);
|
||||
|
||||
// Deutsche Umlaute und andere Sonderzeichen
|
||||
CHAR_WIDTHS.put('ä', 6);
|
||||
CHAR_WIDTHS.put('ö', 6);
|
||||
CHAR_WIDTHS.put('ü', 6);
|
||||
CHAR_WIDTHS.put('Ä', 7);
|
||||
CHAR_WIDTHS.put('Ö', 7);
|
||||
CHAR_WIDTHS.put('Ü', 7);
|
||||
CHAR_WIDTHS.put('ß', 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Zentriert einen Text im Minecraft-Chat.
|
||||
* Farbcodes (& oder §) werden bei der Berechnung der Breite ignoriert.
|
||||
*
|
||||
* @param text Der zu zentrierende Text.
|
||||
* @return Der zentrierte Text mit führenden Leerzeichen.
|
||||
*/
|
||||
public static String centerText(String text) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return text;
|
||||
}
|
||||
|
||||
// Entfernt alle Farbcodes für die Breitenberechnung.
|
||||
String strippedText = ChatColor.stripColor(text);
|
||||
|
||||
// Berechnet die Gesamtbreite des Textes in Pixeln.
|
||||
int textWidth = 0;
|
||||
for (char c : strippedText.toCharArray()) {
|
||||
textWidth += CHAR_WIDTHS.getOrDefault(c, 6);
|
||||
}
|
||||
|
||||
// Berechnet, wie viele Leerzeichen benötigt werden, um den Text zu zentrieren.
|
||||
int totalSpaceWidth = CHAT_WIDTH - textWidth;
|
||||
if (totalSpaceWidth < 0) {
|
||||
return text; // Text ist zu breit, kann nicht zentriert werden.
|
||||
}
|
||||
int spacesNeeded = totalSpaceWidth / (2 * SPACE_WIDTH);
|
||||
|
||||
// Erstellt den zentrierten String.
|
||||
StringBuilder centeredText = new StringBuilder();
|
||||
for (int i = 0; i < spacesNeeded; i++) {
|
||||
centeredText.append(' ');
|
||||
}
|
||||
centeredText.append(text);
|
||||
|
||||
return centeredText.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user