|
3 | 3 | import org.jetbrains.annotations.NotNull; |
4 | 4 | import org.jetbrains.annotations.Nullable; |
5 | 5 |
|
| 6 | +import java.util.Optional; |
| 7 | + |
6 | 8 | import com.comphenix.protocol.injector.StructureCache; |
7 | 9 | import com.comphenix.protocol.reflect.StructureModifier; |
8 | 10 | import com.comphenix.protocol.utility.MinecraftReflection; |
@@ -90,12 +92,26 @@ public TeamCollisionRule getCollisionRule() { |
90 | 92 |
|
91 | 93 | @NotNull |
92 | 94 | public EnumWrappers.ChatFormatting getColor() { |
| 95 | + if (MinecraftVersion.v26_2.atOrAbove()) { |
| 96 | + Optional<?> optional = modifier.<Optional<?>>withType(Optional.class).read(0); |
| 97 | + Object teamColor = optional != null ? optional.orElse(null) : null; |
| 98 | + if (teamColor == null) { |
| 99 | + return EnumWrappers.ChatFormatting.RESET; |
| 100 | + } |
| 101 | + |
| 102 | + return EnumWrappers.ChatFormatting.valueOf(((Enum<?>) teamColor).name()); |
| 103 | + } |
| 104 | + |
93 | 105 | return modifier |
94 | 106 | .withType(EnumWrappers.getChatFormattingClass(), EnumWrappers.getChatFormattingConverter()) |
95 | 107 | .read(0); |
96 | 108 | } |
97 | 109 |
|
98 | 110 | public int getOptions() { |
| 111 | + if (MinecraftVersion.v26_2.atOrAbove()) { |
| 112 | + return (byte) modifier.withType(byte.class).read(0); |
| 113 | + } |
| 114 | + |
99 | 115 | return (int) modifier.withType(int.class).read(0); |
100 | 116 | } |
101 | 117 |
|
@@ -210,9 +226,27 @@ public WrappedTeamParameters build() { |
210 | 226 | wrapped.modifier.withType(String.class).writeSafely(1, collisionRule.toString()); |
211 | 227 | } |
212 | 228 |
|
213 | | - wrapped.modifier.withType(EnumWrappers.getChatFormattingClass()).write(0, EnumWrappers.getChatFormattingConverter().getGeneric(color)); |
214 | | - wrapped.modifier.withType(int.class).write(0, options); |
| 229 | + if (MinecraftVersion.v26_2.atOrAbove()) { |
| 230 | + wrapped.modifier.withType(Optional.class).write(0, Optional.ofNullable(toNmsTeamColor(color))); |
| 231 | + wrapped.modifier.withType(byte.class).write(0, (byte) options); |
| 232 | + } else { |
| 233 | + wrapped.modifier.withType(EnumWrappers.getChatFormattingClass()).write(0, EnumWrappers.getChatFormattingConverter().getGeneric(color)); |
| 234 | + wrapped.modifier.withType(int.class).write(0, options); |
| 235 | + } |
215 | 236 | return wrapped; |
216 | 237 | } |
| 238 | + |
| 239 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 240 | + private static Object toNmsTeamColor(EnumWrappers.ChatFormatting color) { |
| 241 | + Class<?> teamColorClass = MinecraftReflection.getTeamColorClass() |
| 242 | + .orElseThrow(() -> new IllegalStateException("TeamColor class doesn't exist on this server version")); |
| 243 | + |
| 244 | + try { |
| 245 | + return Enum.valueOf((Class) teamColorClass, color.name()); |
| 246 | + } catch (IllegalArgumentException ex) { |
| 247 | + // formatting-only values (e.g. RESET) don't map to a team color |
| 248 | + return null; |
| 249 | + } |
| 250 | + } |
217 | 251 | } |
218 | 252 | } |
0 commit comments