-
Notifications
You must be signed in to change notification settings - Fork 0
/
Colors.java
105 lines (92 loc) · 3.6 KB
/
Colors.java
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
package fi.natroutter.natlibs.utilities;
import fi.natroutter.natlibs.config.IConfig;
import fi.natroutter.natlibs.handlers.CustomResolver;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class Colors {
private static MiniMessage mm = MiniMessage.builder()
.build();
private static LegacyComponentSerializer lcs = LegacyComponentSerializer
.builder()
.hexColors()
.character('&')
.build();
/**
* Translates component to legacy string without colors
*
* @param component component to translate
* @return plain string
*/
public static String plain(Component component) {
return PlainTextComponentSerializer.plainText().serialize(component);
}
/**
* Translates component to legacy string with colors
*
* @param component component to translate
* @return legacy chat mesage
*/
public static String legacy(Component component) {
return lcs.serialize(component);
}
/**
* Translates string with color codes and minimessage to chat component
*
* @param config config entry to translate
* @param placeholders list of placeholders to parse
* @return component
*/
public static Component translate(IConfig config, TagResolver... placeholders) {
return translate(config.asString(), true, placeholders);
}
/**
* Translates string with color codes and minimessage to chat component
*
* @param str string to translate
* @param placeholders list of placeholders to parse
* @return component
*/
public static Component translate(String str, TagResolver... placeholders) {
return translate(str, true, placeholders);
}
/**
* Translates string with color codes and minimessage to chat component
*
* @param str string to translate
* @param placeholders list of placeholders to parse
* @return component
*/
public static Component translate(String str, List<TagResolver> placeholders) {
return translate(str, true, placeholders.toArray(new TagResolver[0]));
}
/**
* Translates string with color codes and minimessage to chat component
*
* @param str string to translate
* @param useCustom Do you want to use custom resolvers
* @param placeholders list of placeholders to parse
* @return component
*/
public static Component translate(String str, boolean useCustom, @Nullable TagResolver... placeholders) {
List<TagResolver> list = new ArrayList<>();
if (placeholders != null) {
list.addAll(List.of(placeholders));
if (useCustom) {
list.addAll(CustomResolver.resolvers());
}
}
//TODO fix this its so fucking stupid :D
Component italicFix = Component.text("").style(style -> style.decoration(TextDecoration.ITALIC, false));
TextComponent deserialize = lcs.deserialize(str.replace("§", "&"));
String serialize = mm.serialize(deserialize).replace("\\<", "<");
return italicFix.append(mm.deserialize(serialize, list.toArray(new TagResolver[0])));
}
}