-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTellraw.java
More file actions
206 lines (183 loc) · 6.21 KB
/
Tellraw.java
File metadata and controls
206 lines (183 loc) · 6.21 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package org.comroid.mcsd.util;
import lombok.*;
import lombok.experimental.NonFinal;
import org.comroid.abstr.DataNode;
import org.comroid.api.Named;
import org.comroid.api.StringAttribute;
import org.comroid.api.TextDecoration;
import org.comroid.api.Vector;
import org.comroid.util.JSON;
import org.comroid.util.Markdown;
import org.comroid.util.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public interface Tellraw {
@With
@Value
@Builder
class Command implements Tellraw {
ISelector selector;
@Singular
@NonFinal
List<Component> components;
@Override
@SneakyThrows
public String toString() {
return "tellraw " + selector + " " + components.stream()
.map(Component::toString)
.collect(Collectors.joining(",","[","]"));
}
public Command appendMarkdown(String txt) {
final var map = TextDecoration.styles(Markdown.class, McFormatCode.class);
return append(TextDecoration.replacers(Markdown.class, McFormatCode.class)
.map(Pair::getFirst)
.map(".*%s.*"::formatted)
.map(Pattern::compile)
.map(pattern -> pattern.matcher(txt))
.filter(Matcher::matches)
.flatMap(matcher -> {
final var ls = new ArrayList<Component>();
var ignored = matcher.replaceAll(result -> {
var format = StringAttribute.valueOf(result.group(1), Markdown.class)
.orElse(Markdown.None);
var msg = result.group(2);
ls.add(Tellraw.Component.builder()
.format(map.get(format))
.text(msg)
.build());
return msg;
});
return ls.stream();
})
.toArray(Tellraw.Component[]::new));
}
public Command append(Component... components) {
this.components = Stream.concat(this.components.stream(), Stream.of(components)).toList();
return this;
}
}
interface ISelector extends Tellraw {}
@With
@Value
@Builder
class Selector implements ISelector {
@Getter
public enum Base implements ISelector {
NEAREST_PLAYER("@p"),
RANDOM_PLAYER("@r"),
ALL_PLAYERS("@a"),
ALL_ENTITIES("@e"),
EXECUTOR("@s");
private final String string;
Base(String string) {
this.string = string;
}
@Override
@SneakyThrows
public String toString() {
return string;
}
}
Base base;
@Nullable Vector.N3 coordinate;
@Nullable Double distance;
@Nullable Vector.N3 dimensions;
@Nullable Double Pitch;
@Nullable Double Yaw;
@Nullable String tag;
@Nullable String team;
@Nullable String name;
@Nullable String type;
@Nullable String predicate;
@Nullable String nbt;
@Nullable Double level;
@Nullable String gamemode;
@Nullable @Singular List<String> scores;
@Nullable @Singular List<String> advancements;
@Override
@SneakyThrows
public String toString() {
return base.string; // todo
}
}
@With
@Value
@Builder
class Component implements Tellraw {
@Nullable String text;
@Nullable @Singular("format")
Set<McFormatCode> format;
@Nullable Event clickEvent;
@Nullable Event hoverEvent;
public JSON.Object json() {
var json = new JSON.Object();
if (text!=null)json.set("text", text);
if (format!=null) for (var code : format) {
if (code.isFormat())
json.set(code.name().toLowerCase(), true);
else if (code.isColor())
json.set("color", code.name().toLowerCase());
else if (code.isReset()) {
for (var format : McFormatCode.FORMATS)
json.set(format.name().toLowerCase(), false);
json.set("color", McFormatCode.White.name().toLowerCase());
}
}
if (clickEvent!=null)json.put("clickEvent", clickEvent.json());
if (hoverEvent!=null)json.put("hoverEvent", hoverEvent.json());
return json;
}
@Override
@SneakyThrows
public String toString() {
return json().toString();
}
public String toFullString() {
return "[%s]".formatted(toString());
}
}
@With
@Value
@Builder
@AllArgsConstructor
class Event implements Tellraw {
@NotNull Action action;
@NotNull String value;
public JSON.Object json() {
var json = new JSON.Object();
json.set("action", action.name());
if (action != Action.show_text)
json.set("value", value);
else json.set("contents", Arrays.stream(value.split("\n"))
.map(DataNode.Value::new)
.collect(Collectors.toCollection(JSON.Array::new)));
return json;
}
@Override
@SneakyThrows
public String toString() {
return json().toString();
}
@SuppressWarnings("unused")
public enum Action implements Named {
open_url,
run_command,
suggest_command,
change_page,
show_text,
show_item,
show_entity;
public Event value(String value) {
return new Event(this, value);
}
}
}
}