Skip to content

Commit fbf36f0

Browse files
committed
refactor colorizer
1 parent 25e80f9 commit fbf36f0

13 files changed

Lines changed: 142 additions & 250 deletions

File tree

src/main/java/ru/overwrite/protect/bukkit/ServerProtectorManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.jetbrains.annotations.NotNull;
1919
import ru.overwrite.protect.bukkit.api.CaptureReason;
2020
import ru.overwrite.protect.bukkit.api.ServerProtectorAPI;
21+
import ru.overwrite.protect.bukkit.color.ColorizerProvider;
2122
import ru.overwrite.protect.bukkit.commands.PasCommand;
2223
import ru.overwrite.protect.bukkit.commands.UspCommand;
2324
import ru.overwrite.protect.bukkit.configuration.Config;
@@ -148,7 +149,7 @@ protected void setupProxy(ConfigurationSection mainSettings) {
148149

149150
protected void loadConfigs(FileConfiguration config) {
150151
this.config = config;
151-
Utils.setupColorizer(config.getConfigurationSection("main-settings"));
152+
ColorizerProvider.init(config.getConfigurationSection("main-settings"));
152153
final ConfigurationSection fileSettings = config.getConfigurationSection("file-settings");
153154
boolean fullPath = fileSettings.getBoolean("use-full-path", false);
154155
String absolutePath = getDataFolder().getAbsolutePath();
@@ -165,7 +166,7 @@ protected void loadConfigs(FileConfiguration config) {
165166
public void reloadConfigs(FileConfiguration config) {
166167
runner.runAsync(() -> {
167168
this.config = config;
168-
Utils.setupColorizer(config.getConfigurationSection("main-settings"));
169+
ColorizerProvider.init(config.getConfigurationSection("main-settings"));
169170
String absolutePath = getDataFolder().getAbsolutePath();
170171
messageFile = pluginConfig.getFile(absolutePath, "message.yml");
171172
final ConfigurationSection fileSettings = config.getConfigurationSection("file-settings");

src/main/java/ru/overwrite/protect/bukkit/utils/color/Colorizer.java renamed to src/main/java/ru/overwrite/protect/bukkit/color/Colorizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ru.overwrite.protect.bukkit.utils.color;
1+
package ru.overwrite.protect.bukkit.color;
22

33
public interface Colorizer {
44

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ru.overwrite.protect.bukkit.color;
2+
3+
import lombok.experimental.UtilityClass;
4+
import org.bukkit.configuration.ConfigurationSection;
5+
import ru.overwrite.protect.bukkit.color.impl.LegacyColorizer;
6+
import ru.overwrite.protect.bukkit.color.impl.MiniMessageColorizer;
7+
8+
import java.util.Locale;
9+
10+
@UtilityClass
11+
public class ColorizerProvider {
12+
public Colorizer COLORIZER;
13+
14+
public void init(ConfigurationSection config) {
15+
String serializerType = config.getString("serializer", "LEGACY").toUpperCase(Locale.ENGLISH);
16+
COLORIZER = "MINIMESSAGE".equals(serializerType)
17+
? new MiniMessageColorizer()
18+
: new LegacyColorizer();
19+
}
20+
}
21+
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package ru.overwrite.protect.bukkit.color.impl;
2+
3+
import ru.overwrite.protect.bukkit.color.Colorizer;
4+
5+
public class LegacyColorizer implements Colorizer {
6+
7+
private static final char COLOR_CHAR = '§';
8+
private static final char ALT_COLOR_CHAR = '&';
9+
10+
private static final boolean[] HEX = new boolean[128];
11+
private static final boolean[] COLOR = new boolean[128];
12+
13+
static {
14+
for (int c = '0'; c <= '9'; c++) {
15+
HEX[c] = true;
16+
COLOR[c] = true;
17+
}
18+
for (int c = 'a'; c <= 'f'; c++) {
19+
HEX[c] = true;
20+
COLOR[c] = true;
21+
}
22+
for (int c = 'A'; c <= 'F'; c++) {
23+
HEX[c] = true;
24+
COLOR[c] = true;
25+
}
26+
for (int c : new int[]{'r', 'R', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O'}) {
27+
COLOR[c] = true;
28+
}
29+
}
30+
31+
@Override
32+
public String colorize(String message) {
33+
if (message == null) {
34+
return null;
35+
}
36+
final char[] s = message.toCharArray();
37+
final int len = s.length;
38+
if (len == 0) {
39+
return message;
40+
}
41+
42+
final int firstAmp = message.indexOf(ALT_COLOR_CHAR);
43+
if (firstAmp < 0) {
44+
return message;
45+
}
46+
47+
final char[] d = new char[len * 3];
48+
int w = 0;
49+
int i = 0;
50+
51+
if (firstAmp > 8) {
52+
System.arraycopy(s, 0, d, 0, firstAmp);
53+
w = firstAmp;
54+
i = firstAmp;
55+
} else {
56+
while (i < firstAmp) d[w++] = s[i++];
57+
}
58+
59+
while (i < len) {
60+
final char c = s[i];
61+
62+
if (c != ALT_COLOR_CHAR) {
63+
d[w++] = c;
64+
i++;
65+
continue;
66+
}
67+
68+
final int remaining = len - i;
69+
70+
if (remaining >= 8 && s[i + 1] == '#') {
71+
final char h0 = s[i + 2], h1 = s[i + 3], h2 = s[i + 4], h3 = s[i + 5], h4 = s[i + 6], h5 = s[i + 7];
72+
if (h0 < 128 && HEX[h0] && h1 < 128 && HEX[h1] && h2 < 128 && HEX[h2] && h3 < 128 && HEX[h3] && h4 < 128 && HEX[h4] && h5 < 128 && HEX[h5]) {
73+
d[w] = COLOR_CHAR;
74+
d[w + 1] = 'x';
75+
d[w + 2] = COLOR_CHAR;
76+
d[w + 3] = h0;
77+
d[w + 4] = COLOR_CHAR;
78+
d[w + 5] = h1;
79+
d[w + 6] = COLOR_CHAR;
80+
d[w + 7] = h2;
81+
d[w + 8] = COLOR_CHAR;
82+
d[w + 9] = h3;
83+
d[w + 10] = COLOR_CHAR;
84+
d[w + 11] = h4;
85+
d[w + 12] = COLOR_CHAR;
86+
d[w + 13] = h5;
87+
w += 14;
88+
i += 8;
89+
continue;
90+
}
91+
}
92+
93+
if (remaining >= 2) {
94+
final char next = s[i + 1];
95+
if (next < 128 && COLOR[next]) {
96+
d[w] = COLOR_CHAR;
97+
d[w + 1] = next;
98+
w += 2;
99+
i += 2;
100+
continue;
101+
}
102+
}
103+
104+
d[w++] = ALT_COLOR_CHAR;
105+
i++;
106+
}
107+
108+
return new String(d, 0, w);
109+
}
110+
}

src/main/java/ru/overwrite/protect/bukkit/utils/color/impl/MiniMessageColorizer.java renamed to src/main/java/ru/overwrite/protect/bukkit/color/impl/MiniMessageColorizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package ru.overwrite.protect.bukkit.utils.color.impl;
1+
package ru.overwrite.protect.bukkit.color.impl;
22

33
import net.kyori.adventure.text.Component;
44
import net.kyori.adventure.text.minimessage.MiniMessage;
55
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
6-
import ru.overwrite.protect.bukkit.utils.color.Colorizer;
6+
import ru.overwrite.protect.bukkit.color.Colorizer;
77

88
public class MiniMessageColorizer implements Colorizer {
99

src/main/java/ru/overwrite/protect/bukkit/configuration/Config.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.bukkit.potion.PotionEffect;
1111
import org.bukkit.potion.PotionEffectType;
1212
import ru.overwrite.protect.bukkit.ServerProtectorManager;
13+
import ru.overwrite.protect.bukkit.color.ColorizerProvider;
1314
import ru.overwrite.protect.bukkit.configuration.data.*;
1415
import ru.overwrite.protect.bukkit.logging.Logger;
1516
import ru.overwrite.protect.bukkit.utils.Utils;
@@ -619,9 +620,9 @@ public void loadSystemMessages(FileConfiguration message) {
619620

620621
public String getMessage(ConfigurationSection section, String key) {
621622
if (section == null) {
622-
return Utils.COLORIZER.colorize("&4&lERROR&r: " + key + " does not exist!");
623+
return ColorizerProvider.COLORIZER.colorize("&4&lERROR&r: " + key + " does not exist!");
623624
}
624-
return Utils.COLORIZER.colorize(section.getString(key, "&4&lERROR&r: " + key + " does not exist!").replace("%prefix%", mainSettings.prefix()));
625+
return ColorizerProvider.COLORIZER.colorize(section.getString(key, "&4&lERROR&r: " + key + " does not exist!").replace("%prefix%", mainSettings.prefix()));
625626
}
626627

627628
public FileConfiguration getFile(String path, String fileName) {

src/main/java/ru/overwrite/protect/bukkit/utils/PAPIUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import lombok.experimental.UtilityClass;
44
import me.clip.placeholderapi.PlaceholderAPI;
55
import org.bukkit.entity.Player;
6+
import ru.overwrite.protect.bukkit.color.ColorizerProvider;
67

78
@UtilityClass
89
public class PAPIUtils {
910

1011
public String parsePlaceholders(Player player, String message) {
11-
return Utils.COLORIZER.colorize(PlaceholderAPI.setPlaceholders(player, message));
12+
return ColorizerProvider.COLORIZER.colorize(PlaceholderAPI.setPlaceholders(player, message));
1213
}
1314
}

src/main/java/ru/overwrite/protect/bukkit/utils/Utils.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
import lombok.experimental.UtilityClass;
44
import org.bukkit.Bukkit;
55
import org.bukkit.Sound;
6-
import org.bukkit.configuration.ConfigurationSection;
76
import org.bukkit.entity.Player;
87
import ru.overwrite.protect.bukkit.ServerProtectorManager;
9-
import ru.overwrite.protect.bukkit.utils.color.Colorizer;
10-
import ru.overwrite.protect.bukkit.utils.color.impl.LegacyAdvancedColorizer;
11-
import ru.overwrite.protect.bukkit.utils.color.impl.LegacyColorizer;
12-
import ru.overwrite.protect.bukkit.utils.color.impl.MiniMessageColorizer;
13-
import ru.overwrite.protect.bukkit.utils.color.impl.VanillaColorizer;
148

159
import java.io.BufferedReader;
1610
import java.io.IOException;
@@ -44,17 +38,6 @@ public class Utils {
4438
FOLIA = folia;
4539
}
4640

47-
public Colorizer COLORIZER;
48-
49-
public void setupColorizer(ConfigurationSection mainSettings) {
50-
COLORIZER = switch (mainSettings.getString("serializer", "LEGACY").toUpperCase()) {
51-
case "MINIMESSAGE" -> new MiniMessageColorizer();
52-
case "LEGACY" -> SUB_VERSION >= 16 ? new LegacyColorizer() : new VanillaColorizer();
53-
case "LEGACY_ADVANCED" -> new LegacyAdvancedColorizer();
54-
default -> new VanillaColorizer();
55-
};
56-
}
57-
5841
public String getIp(Player player) {
5942
return player.getAddress().getAddress().getHostAddress();
6043
}
@@ -89,29 +72,6 @@ public void sendSound(String[] soundArgs, Player player) {
8972
player.playSound(player.getLocation(), sound, volume, pitch);
9073
}
9174

92-
public final char COLOR_CHAR = '§';
93-
94-
public String translateAlternateColorCodes(char altColorChar, String textToTranslate) {
95-
final char[] b = textToTranslate.toCharArray();
96-
97-
for (int i = 0, length = b.length - 1; i < length; i++) {
98-
if (b[i] == altColorChar && isValidColorCharacter(b[i + 1])) {
99-
b[i++] = COLOR_CHAR;
100-
b[i] |= 0x20;
101-
}
102-
}
103-
104-
return new String(b);
105-
}
106-
107-
private boolean isValidColorCharacter(char c) {
108-
return switch (c) {
109-
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D',
110-
'E', 'F', 'r', 'R', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'x', 'X' -> true;
111-
default -> false;
112-
};
113-
}
114-
11575
public void checkUpdates(ServerProtectorManager plugin, Consumer<String> consumer) {
11676
plugin.getRunner().runDelayedAsync(() -> {
11777
try (BufferedReader reader = new BufferedReader(new InputStreamReader(

0 commit comments

Comments
 (0)