Skip to content

Commit 87855c8

Browse files
committed
initial work on API
these are most of the elements modders will interact with - still missing is - json serdes - interoperability with vanilla (conversion between vanilla Text, and methods for e.g. rendering OSL components)
1 parent cfc9ffc commit 87855c8

16 files changed

Lines changed: 983 additions & 0 deletions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
setUpLibrary(project)
2+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library_id = text-components
2+
library_name = Text Components
3+
library_description = Versatile text components.
4+
library_version = 0.1.0-alpha.1
5+
6+
osl_dependencies = core:>=0.7.0
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package net.ornithemc.osl.text.api;
2+
3+
import java.util.Objects;
4+
5+
public class ClickEvent {
6+
7+
private final Action action;
8+
private final String value;
9+
10+
private ClickEvent(Action action, String value) {
11+
this.action = action;
12+
this.value = value;
13+
}
14+
15+
@Override
16+
public boolean equals(Object o) {
17+
if (this == o) {
18+
return true;
19+
}
20+
if (!(o instanceof ClickEvent)) {
21+
return false;
22+
}
23+
ClickEvent event = (ClickEvent) o;
24+
return this.action == event.action && Objects.equals(this.value, event.value);
25+
}
26+
27+
public Action getAction() {
28+
return this.action;
29+
}
30+
31+
public String getValue() {
32+
return this.value;
33+
}
34+
35+
public static ClickEvent openUrl(String url) {
36+
return new ClickEvent(Action.OPEN_URL, url);
37+
}
38+
39+
public static ClickEvent runCommand(String command) {
40+
return new ClickEvent(Action.RUN_COMMAND, command);
41+
}
42+
43+
public static ClickEvent suggestCommand(String command) {
44+
return new ClickEvent(Action.SUGGEST_COMMAND, command);
45+
}
46+
47+
public static ClickEvent copyToClipboard(String text) {
48+
return new ClickEvent(Action.COPY_TO_CLIPBOARD, text);
49+
}
50+
51+
public enum Action {
52+
53+
OPEN_URL, RUN_COMMAND, SUGGEST_COMMAND, COPY_TO_CLIPBOARD
54+
55+
}
56+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package net.ornithemc.osl.text.api;
2+
3+
public enum Formatting {
4+
5+
BLACK ('0', 0x000000),
6+
DARK_BLUE ('1', 0x0000AA),
7+
DARK_GREEN ('2', 0x00AA00),
8+
DARK_AQUA ('3', 0x00AAAA),
9+
DARK_RED ('4', 0xAA0000),
10+
DARK_PURPLE ('5', 0xAA00AA),
11+
GOLD ('6', 0xFFAA00),
12+
GRAY ('7', 0xAAAAAA),
13+
DARK_GRAY ('8', 0x555555),
14+
BLUE ('9', 0x5555FF),
15+
GREEN ('a', 0x55FF55),
16+
AQUA ('b', 0x55FFFF),
17+
RED ('c', 0xFF5555),
18+
LIGHT_PURPLE ('d', 0xFF55FF),
19+
YELLOW ('e', 0xFFFF55),
20+
WHITE ('f', 0xFFFFFF),
21+
OBFUSCATED ('k'),
22+
BOLD ('l'),
23+
STRIKETHROUGH('m'),
24+
UNDERLINED ('n'),
25+
ITALIC ('o'),
26+
RESET ('r');
27+
28+
public static final char PREFIX = '§';
29+
30+
final char code;
31+
final Integer color;
32+
33+
private Formatting(char code) {
34+
this(code, null);
35+
}
36+
37+
private Formatting(char code, Integer color) {
38+
this.code = code;
39+
this.color = color;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "" + PREFIX + this.code;
45+
}
46+
47+
public char getCode() {
48+
return this.code;
49+
}
50+
51+
public boolean isColor() {
52+
return this.color != null;
53+
}
54+
55+
public Integer getColor() {
56+
return this.color;
57+
}
58+
59+
public static Formatting byCode(char code) {
60+
for (Formatting f : Formatting.values()) {
61+
if (f.code == code) {
62+
return f;
63+
}
64+
}
65+
66+
throw new IllegalStateException("unknown text formatting code " + code);
67+
}
68+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package net.ornithemc.osl.text.api;
2+
3+
import java.util.Objects;
4+
5+
public class HoverEvent {
6+
7+
private final Action action;
8+
private final Object value;
9+
10+
private HoverEvent(Action action, Object value) {
11+
this.action = action;
12+
this.value = value;
13+
}
14+
15+
@Override
16+
public boolean equals(Object o) {
17+
if (this == o) {
18+
return true;
19+
}
20+
if (!(o instanceof HoverEvent)) {
21+
return false;
22+
}
23+
HoverEvent event = (HoverEvent) o;
24+
return this.action == event.action && Objects.equals(this.value, event.value);
25+
}
26+
27+
public Action getAction() {
28+
return this.action;
29+
}
30+
31+
public <T> T getValue() {
32+
return (T) this.value;
33+
}
34+
35+
public static HoverEvent showText(TextComponent text) {
36+
return new HoverEvent(Action.SHOW_TEXT, text);
37+
}
38+
39+
public enum Action {
40+
41+
SHOW_TEXT
42+
43+
}
44+
}

0 commit comments

Comments
 (0)