Repository navigation
Expand file tree
/
Copy pathCC.java
More file actions
171 lines (156 loc) · 6.48 KB
/
Copy pathCC.java
File metadata and controls
171 lines (156 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package me.nimodev.yourPlugin.utils;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.asset.util.ColorParseUtil;
import java.awt.*;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class CC {
private static final Pattern CODE_PATTERN = Pattern.compile("&(#?[0-9a-fA-F]{6}|[0-9a-flomr])");
private static final Map<String, String> COLOR_MAP = Map.ofEntries(
Map.entry("&0", "\u001B[30m"),
Map.entry("&1", "\u001B[34m"),
Map.entry("&2", "\u001B[32m"),
Map.entry("&3", "\u001B[36m"),
Map.entry("&4", "\u001B[31m"),
Map.entry("&5", "\u001B[35m"),
Map.entry("&6", "\u001B[33m"),
Map.entry("&7", "\u001B[37m"),
Map.entry("&8", "\u001B[90m"),
Map.entry("&9", "\u001B[94m"),
Map.entry("&a", "\u001B[92m"),
Map.entry("&b", "\u001B[96m"),
Map.entry("&c", "\u001B[91m"),
Map.entry("&d", "\u001B[95m"),
Map.entry("&e", "\u001B[93m"),
Map.entry("&f", "\u001B[97m"),
Map.entry("&r", "\u001B[0m")
);
private static final Map<String, Color> COLOR_CODES = Map.ofEntries((Map.Entry<? extends String, ? extends Color>[])new Map.Entry[] {
Map.entry("&0", new Color(0, 0, 0)),
Map.entry("&1", new Color(0, 0, 170)),
Map.entry("&2", new Color(0, 170, 0)),
Map.entry("&3", new Color(0, 170, 170)),
Map.entry("&4", new Color(170, 0, 0)),
Map.entry("&5", new Color(170, 0, 170)),
Map.entry("&6", new Color(255, 170, 0)),
Map.entry("&7", new Color(170, 170, 170)),
Map.entry("&8", new Color(85, 85, 85)),
Map.entry("&9", new Color(85, 85, 255)),
Map.entry("&a", new Color(85, 255, 85)),
Map.entry("&b", new Color(85, 255, 255)),
Map.entry("&c", new Color(255, 85, 85)),
Map.entry("&d", new Color(255, 85, 255)),
Map.entry("&e", new Color(255, 255, 85)),
Map.entry("&f", new Color(255, 255, 255)) });
public static String line(String color){
return translate(color+"&m------------------------");
}
public static String translate(String message) {
Matcher matcher = CODE_PATTERN.matcher(message);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(
sb,
COLOR_MAP.getOrDefault(matcher.group(), matcher.group())
);
}
matcher.appendTail(sb);
return sb.toString() + "\u001B[0m";
}
private void parseContent(Message base, String content) {
int lastIndex = 0;
Color currentColor = Color.WHITE;
boolean bold = false, italic = false, mono = false;
Matcher matcher = CODE_PATTERN.matcher(content);
while (matcher.find()) {
if (matcher.start() > lastIndex)
base.insert(
Message.raw(content.substring(lastIndex, matcher.start()))
.color(currentColor)
.bold(bold)
.italic(italic)
.monospace(mono));
String code = matcher.group().toLowerCase();
if (code.startsWith("&#")) {
try {
currentColor = Color.decode(code.substring(1));
} catch (NumberFormatException numberFormatException) {}
} else {
switch (code) {
case "&l":
bold = true;
break;
case "&o":
italic = true;
break;
case "&m":
mono = true;
break;
case "&r":
currentColor = Color.WHITE;
bold = italic = mono = false;
break;
default:
currentColor = COLOR_CODES.getOrDefault(code, currentColor);
break;
}
}
lastIndex = matcher.end();
}
if (lastIndex < content.length())
base.insert(
Message.raw(content.substring(lastIndex))
.color(currentColor)
.bold(bold)
.italic(italic)
.monospace(mono));
}
private void insertAutoGradient(Message base, String text, Color baseColor, boolean bold, boolean italic, boolean mono) {
int len = text.length();
if (len == 0)
return;
Color end = brighten(baseColor);
int r1 = baseColor.getRed();
int g1 = baseColor.getGreen();
int b1 = baseColor.getBlue();
int r2 = end.getRed();
int g2 = end.getGreen();
int b2 = end.getBlue();
for (int i = 0; i < len; i++) {
float t = (len == 1) ? 0.0F : (i / (len - 1));
t *= t;
int r = (int)(r1 + t * (r2 - r1));
int g = (int)(g1 + t * (g2 - g1));
int b = (int)(b1 + t * (b2 - b1));
base.insert(
Message.raw(String.valueOf(text.charAt(i)))
.color(new Color(r, g, b))
.bold(bold)
.italic(italic)
.monospace(mono));
}
}
private Color brighten(Color c) {
int r = Math.max(0, c.getRed() - 25);
int g = Math.max(0, c.getGreen() - 25);
int b = Math.max(0, c.getBlue() - 25);
return new Color(
Math.min(255, r + 110),
Math.min(255, g + 110),
Math.min(255, b + 110));
}
public static List<String> translate(List<String> source) {
return source.stream().map(CC::translate).collect(Collectors.toList());
}
public static void print(String msg){
YourPlugin.getInstance().getLogger().atInfo().log(translate(msg));
// Make Sure to add the getInstance method on your Plugin
}
public static void log(String message) {
YourPlugin.getInstance().getLogger().atInfo().log(translate("&b[YourPlugin] &3"+message));
// Make Sure to add the getInstance method on your Plugin
}
}