78 lines
3.0 KiB
Java
78 lines
3.0 KiB
Java
/*
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
|
* Copyright (C) 2016-2023 ViaVersion and contributors
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
package com.viaversion.viaversion.util;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
|
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
|
|
import java.util.regex.Pattern;
|
|
|
|
// Based on https://github.com/SpigotMC/BungeeCord/blob/master/chat/src/main/java/net/md_5/bungee/api/ChatColor.java
|
|
public class ChatColorUtil {
|
|
|
|
public static final String ALL_CODES = "0123456789AaBbCcDdEeFfKkLlMmNnOoRrXx";
|
|
public static final char COLOR_CHAR = '§';
|
|
public static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + COLOR_CHAR + "[0-9A-FK-ORX]");
|
|
private static final Int2IntMap COLOR_ORDINALS = new Int2IntOpenHashMap();
|
|
private static int ordinalCounter;
|
|
|
|
static {
|
|
addColorOrdinal('0', '9');
|
|
addColorOrdinal('a', 'f');
|
|
addColorOrdinal('k', 'o');
|
|
addColorOrdinal('r');
|
|
}
|
|
|
|
public static boolean isColorCode(char c) {
|
|
return COLOR_ORDINALS.containsKey(c);
|
|
}
|
|
|
|
public static int getColorOrdinal(char c) {
|
|
return COLOR_ORDINALS.getOrDefault(c, -1);
|
|
}
|
|
|
|
public static String translateAlternateColorCodes(String s) {
|
|
char[] chars = s.toCharArray();
|
|
for (int i = 0; i < chars.length - 1; i++) {
|
|
if (chars[i] == '&' && ALL_CODES.indexOf(chars[i + 1]) > -1) {
|
|
chars[i] = COLOR_CHAR;
|
|
chars[i + 1] = Character.toLowerCase(chars[i + 1]);
|
|
}
|
|
}
|
|
return new String(chars);
|
|
}
|
|
|
|
public static String stripColor(final String input) {
|
|
return STRIP_COLOR_PATTERN.matcher(input).replaceAll("");
|
|
}
|
|
|
|
private static void addColorOrdinal(int from, int to) {
|
|
for (int c = from; c <= to; c++) {
|
|
addColorOrdinal(c);
|
|
}
|
|
}
|
|
|
|
private static void addColorOrdinal(int colorChar) {
|
|
COLOR_ORDINALS.put(colorChar, ordinalCounter++);
|
|
}
|
|
}
|