Skip to content

Commit 4e82561

Browse files
committed
add alt names for colors/styles
1 parent 59d02bd commit 4e82561

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group "me.dags"
10-
version "0.3.5"
10+
version "0.3.6"
1111
def spongeAPI = "7.1.0"
1212
description = "A markup syntax for Sponge"
1313

src/main/java/me/dags/text/Property.java

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.IOException;
3535
import java.net.URL;
3636
import java.util.Collections;
37+
import java.util.HashMap;
3738
import java.util.Map;
3839
import java.util.Optional;
3940
import java.util.stream.Collectors;
@@ -124,27 +125,54 @@ static Property parseURL(String in, Predicate predicate) throws IOException {
124125
}
125126

126127
static <T> Optional<T> match(String in, Map<String, T> map) {
127-
T t = map.get(in);
128-
if (t != null) {
129-
return Optional.of(t);
130-
}
131-
String match = "";
132-
for (String key : map.keySet()) {
133-
if (key.startsWith(in)) {
134-
if (match.isEmpty()) {
135-
match = key;
136-
} else {
137-
return Optional.empty();
138-
}
139-
}
140-
}
141-
return Optional.ofNullable(map.get(match));
128+
return Optional.ofNullable(map.get(in));
129+
}
130+
131+
static Map<String, String[]> altNames() {
132+
Map<String, String[]> altNames = new HashMap<>();
133+
// colors
134+
altNames.put("aqua", new String[]{"aqua", "aq", "&b", "b"});
135+
altNames.put("black", new String[]{"black", "&0", "0"});
136+
altNames.put("blue", new String[]{"blue", "blu", "&9", "9"});
137+
altNames.put("dark_aqua", new String[]{"dark_aqua", "*aqua", "aqua*", "*aq", "aq*", "&3", "3"});
138+
altNames.put("dark_blue", new String[]{"dark_blue", "*blue", "blue*", "*blu", "blu*", "&1", "1"});
139+
altNames.put("dark_gray", new String[]{"dark_gray", "*gray", "gray*", "*grey", "grey*", "*gry", "gry*", "&8", "8"});
140+
altNames.put("dark_green", new String[]{"dark_green", "*green", "green*", "*grn", "grn*", "&2", "2"});
141+
altNames.put("dark_purple", new String[]{"dark_purple", "*purple", "purple*", "*pur", "pur*", "&5", "5"});
142+
altNames.put("dark_red", new String[]{"dark_red", "*red", "red*", "&4", "4"});
143+
altNames.put("gray", new String[]{"gray", "grey", "gry", "&7", "7"});
144+
altNames.put("green", new String[]{"green", "grn", "&a", "d"});
145+
altNames.put("gold", new String[]{"gold", "dark_yellow", "*yellow", "yellow*", "*yel", "yel*", "&e", "e"});
146+
altNames.put("light_purple", new String[]{"light_purple", "purple", "pur", "&d", "d"});
147+
altNames.put("red", new String[]{"red", "&c", "c"});
148+
altNames.put("white", new String[]{"white", "whi", "&f", "f"});
149+
altNames.put("yellow", new String[]{"yellow", "yel", "&e", "e"});
150+
// styles
151+
altNames.put("bold", new String[]{"bold", "bld", "&l", "l", "*"});
152+
altNames.put("italic", new String[]{"italic", "ita", "&o", "o", "_"});
153+
altNames.put("obfuscated", new String[]{"obfuscated", "obfuscate", "obf", "&k", "k", "?"});
154+
altNames.put("strikethrough", new String[]{"strikethrough", "strike", "&m", "m", "~"});
155+
altNames.put("underline", new String[]{"underlined", "underline", "und", "&n", "n", "#"});
156+
// reset
157+
altNames.put("reset", new String[]{"reset", "rst", "&r", "r"});
158+
return altNames;
142159
}
143160

144161
static Map<String, TextColor> textColors() {
145162
try {
146-
return Sponge.getRegistry().getAllOf(TextColor.class).stream()
147-
.collect(Collectors.toMap(c -> c.getName().toLowerCase(), c -> c));
163+
Map<String, String[]> altNames = altNames();
164+
Map<String, TextColor> map = new HashMap<>();
165+
Sponge.getRegistry().getAllOf(TextColor.class).forEach(color -> {
166+
String[] names = altNames.get(color.getName().toLowerCase());
167+
if (names != null) {
168+
for (String name : names) {
169+
map.put(name, color);
170+
}
171+
} else {
172+
map.put(color.getName().toLowerCase(), color);
173+
}
174+
});
175+
return map;
148176
} catch (Throwable t) {
149177
t.printStackTrace();
150178
return Collections.emptyMap();
@@ -153,8 +181,19 @@ static Map<String, TextColor> textColors() {
153181

154182
static Map<String, TextStyle> textStyles() {
155183
try {
156-
return Sponge.getRegistry().getAllOf(TextStyle.Base.class).stream()
157-
.collect(Collectors.toMap(c -> c.getName().toLowerCase(), s -> s));
184+
Map<String, String[]> altNames = altNames();
185+
Map<String, TextStyle> map = new HashMap<>();
186+
Sponge.getRegistry().getAllOf(TextStyle.Base.class).forEach(style -> {
187+
String[] names = altNames.get(style.getName().toLowerCase());
188+
if (names != null) {
189+
for (String name : names) {
190+
map.put(name, style);
191+
}
192+
} else {
193+
map.put(style.getName().toLowerCase(), style);
194+
}
195+
});
196+
return map;
158197
} catch (Throwable t) {
159198
t.printStackTrace();
160199
return Collections.emptyMap();

src/test/java/ParseTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void test4() {
6565

6666
@Test
6767
public void test5() {
68-
test("hello [world](red)", Text.builder("hello ")
68+
test("hello [world](&c)", Text.builder("hello ")
6969
.append(Text.builder("world").color(TestColor.RED).build())
7070
.build());
7171
}
@@ -103,8 +103,9 @@ public void test8() {
103103

104104
@Test
105105
public void test9() {
106+
// * = bold, blu = blue, yel* = gold, # = underline
106107
test(
107-
"Outer1 [inner1 [inner2 [inner3](red,bold)](blue,italic)](green,[hover text](gold,underline)) outer2",
108+
"Outer1 [inner1 [inner2 [inner3](red,*)](blu,italic)](green,[hover text](yel*,#)) outer2",
108109
Text.builder("Outer1 ")
109110
.append(Text.builder("inner1 ")
110111
.color(TestColor.GREEN)

0 commit comments

Comments
 (0)