-
-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathWrappedTeamParameters.java
More file actions
252 lines (210 loc) · 9.53 KB
/
Copy pathWrappedTeamParameters.java
File metadata and controls
252 lines (210 loc) · 9.53 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package com.comphenix.protocol.wrappers;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import com.comphenix.protocol.injector.StructureCache;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.EnumWrappers.TeamCollisionRule;
import com.comphenix.protocol.wrappers.EnumWrappers.TeamVisibility;
import com.google.common.base.Preconditions;
/**
* A wrapper around the team parameters NMS class.
*
* @author vytskalt
* @since 1.17
*/
public class WrappedTeamParameters extends AbstractWrapper {
public static Class<?> getNmsClassOrThrow() {
return MinecraftReflection.getTeamParametersClass()
.orElseThrow(() -> new IllegalStateException("Team parameters class doesn't exist on this server version"));
}
/**
* @return Whether the team parameters class exists on the current server version
*/
public static boolean isSupported() {
return MinecraftReflection.getTeamParametersClass().isPresent();
}
public static Builder newBuilder() {
return newBuilder(null);
}
public static Builder newBuilder(@Nullable WrappedTeamParameters template) {
return new Builder(template);
}
private final StructureModifier<Object> modifier;
private final StructureModifier<TeamVisibility> visiblityModifier;
private final StructureModifier<TeamCollisionRule> collisionRuleModifier;
public WrappedTeamParameters(Object handle) {
super(getNmsClassOrThrow());
setHandle(handle);
this.modifier = new StructureModifier<>(getNmsClassOrThrow()).withTarget(handle);
this.visiblityModifier = this.modifier.withType(EnumWrappers.getTeamVisibilityClass(), EnumWrappers.getTeamVisibilityConverter());
this.collisionRuleModifier = this.modifier.withType(EnumWrappers.getTeamCollisionRuleClass(), EnumWrappers.getTeamCollisionRuleConverter());
}
@NotNull
public WrappedChatComponent getDisplayName() {
return readComponent(0);
}
@NotNull
public WrappedChatComponent getPrefix() {
return readComponent(1);
}
@NotNull
public WrappedChatComponent getSuffix() {
return readComponent(2);
}
@NotNull
public TeamVisibility getNametagVisibility() {
TeamVisibility visibility = this.visiblityModifier.readSafely(0);
if (visibility == null) {
return TeamVisibility.fromName(modifier.<String>withType(String.class).read(0));
} else {
return visibility;
}
}
@NotNull
public TeamCollisionRule getCollisionRule() {
TeamCollisionRule collisionRule = this.collisionRuleModifier.readSafely(0);
if (collisionRule == null) {
return TeamCollisionRule.fromName(modifier.<String>withType(String.class).read(1));
} else {
return collisionRule;
}
}
@NotNull
public EnumWrappers.ChatFormatting getColor() {
if (MinecraftVersion.v26_2.atOrAbove()) {
Optional<?> optional = modifier.<Optional<?>>withType(Optional.class).read(0);
Object teamColor = optional != null ? optional.orElse(null) : null;
if (teamColor == null) {
return EnumWrappers.ChatFormatting.RESET;
}
return EnumWrappers.ChatFormatting.valueOf(((Enum<?>) teamColor).name());
}
return modifier
.withType(EnumWrappers.getChatFormattingClass(), EnumWrappers.getChatFormattingConverter())
.read(0);
}
public int getOptions() {
if (MinecraftVersion.v26_2.atOrAbove()) {
return (byte) modifier.withType(byte.class).read(0);
}
return (int) modifier.withType(int.class).read(0);
}
private WrappedChatComponent readComponent(int index) {
Object handle = modifier.withType(MinecraftReflection.getIChatBaseComponentClass()).read(index);
return WrappedChatComponent.fromHandle(handle);
}
private void writeComponent(int index, WrappedChatComponent component) {
modifier.withType(MinecraftReflection.getIChatBaseComponentClass()).write(index, component.getHandle());
}
public static class Builder {
private WrappedChatComponent displayName, prefix, suffix;
private TeamVisibility nametagVisibility;
private TeamCollisionRule collisionRule;
private EnumWrappers.ChatFormatting color;
private int options;
private Builder(@Nullable WrappedTeamParameters template) {
if (template != null) {
this.displayName = template.getDisplayName();
this.prefix = template.getPrefix();
this.suffix = template.getSuffix();
this.nametagVisibility = template.getNametagVisibility();
this.collisionRule = template.getCollisionRule();
this.color = template.getColor();
this.options = template.getOptions();
}
}
public Builder displayName(@NotNull WrappedChatComponent displayName) {
Preconditions.checkNotNull(displayName);
this.displayName = displayName;
return this;
}
public Builder prefix(@NotNull WrappedChatComponent prefix) {
Preconditions.checkNotNull(prefix);
this.prefix = prefix;
return this;
}
public Builder suffix(@NotNull WrappedChatComponent suffix) {
Preconditions.checkNotNull(suffix);
this.suffix = suffix;
return this;
}
public Builder nametagVisibility(@NotNull TeamVisibility nametagVisibility) {
Preconditions.checkNotNull(nametagVisibility);
this.nametagVisibility = nametagVisibility;
return this;
}
/**
* Compatibility stub for FeatherBoard – accepts raw String visibility.
*/
public Builder nametagVisibility(@NotNull String visibility) {
Preconditions.checkNotNull(visibility, "visibility string cannot be null");
this.nametagVisibility = TeamVisibility.fromName(visibility);
return this;
}
public Builder collisionRule(@NotNull TeamCollisionRule collisionRule) {
Preconditions.checkNotNull(collisionRule);
this.collisionRule = collisionRule;
return this;
}
/**
* Compatibility stub for FeatherBoard – accepts raw String collision rules.
*/
public Builder collisionRule(@NotNull String rule) {
Preconditions.checkNotNull(rule, "collision rule cannot be null");
this.collisionRule = TeamCollisionRule.fromName(rule);
return this;
}
public Builder color(@NotNull EnumWrappers.ChatFormatting color) {
Preconditions.checkNotNull(color);
this.color = color;
return this;
}
public Builder options(int options) {
Preconditions.checkNotNull(collisionRule);
this.options = options;
return this;
}
public WrappedTeamParameters build() {
Preconditions.checkNotNull(displayName, "Display name not set");
Preconditions.checkNotNull(prefix, "Prefix not set");
Preconditions.checkNotNull(suffix, "Suffix not set");
Preconditions.checkNotNull(nametagVisibility, "Nametag visibility not set");
Preconditions.checkNotNull(collisionRule, "Collision rule not set");
Preconditions.checkNotNull(color, "Color not set");
Object handle = StructureCache.newInstance(getNmsClassOrThrow());
WrappedTeamParameters wrapped = new WrappedTeamParameters(handle);
wrapped.writeComponent(0, displayName);
wrapped.writeComponent(1, prefix);
wrapped.writeComponent(2, suffix);
if (MinecraftVersion.v1_21_5.atOrAbove()) {
wrapped.visiblityModifier.writeSafely(0, nametagVisibility);
wrapped.collisionRuleModifier.writeSafely(0, collisionRule);
} else {
wrapped.modifier.withType(String.class).writeSafely(0, nametagVisibility.toString());
wrapped.modifier.withType(String.class).writeSafely(1, collisionRule.toString());
}
if (MinecraftVersion.v26_2.atOrAbove()) {
wrapped.modifier.withType(Optional.class).write(0, Optional.ofNullable(toNmsTeamColor(color)));
wrapped.modifier.withType(byte.class).write(0, (byte) options);
} else {
wrapped.modifier.withType(EnumWrappers.getChatFormattingClass()).write(0, EnumWrappers.getChatFormattingConverter().getGeneric(color));
wrapped.modifier.withType(int.class).write(0, options);
}
return wrapped;
}
@SuppressWarnings({"unchecked", "rawtypes"})
private static Object toNmsTeamColor(EnumWrappers.ChatFormatting color) {
Class<?> teamColorClass = MinecraftReflection.getTeamColorClass()
.orElseThrow(() -> new IllegalStateException("TeamColor class doesn't exist on this server version"));
try {
return Enum.valueOf((Class) teamColorClass, color.name());
} catch (IllegalArgumentException ex) {
// formatting-only values (e.g. RESET) don't map to a team color
return null;
}
}
}
}