55import net .megavex .scoreboardlibrary .api .team .enums .CollisionRule ;
66import net .megavex .scoreboardlibrary .api .team .enums .NameTagVisibility ;
77import net .megavex .scoreboardlibrary .implementation .commons .LegacyFormatUtil ;
8+ import net .megavex .scoreboardlibrary .implementation .packetAdapter .ImmutableTeamProperties ;
89import net .megavex .scoreboardlibrary .implementation .packetAdapter .util .reflect .*;
910import org .bukkit .Bukkit ;
1011import org .jetbrains .annotations .NotNull ;
@@ -17,7 +18,7 @@ public final class PacketAccessors {
1718 private PacketAccessors () {
1819 }
1920
20- public static final boolean IS_1_17_OR_ABOVE , IS_1_20_2_OR_ABOVE , IS_1_20_3_OR_ABOVE , IS_1_20_5_OR_ABOVE , IS_1_21_5_OR_ABOVE , IS_1_21_6_OR_ABOVE ;
21+ public static final boolean IS_1_17_OR_ABOVE , IS_1_20_2_OR_ABOVE , IS_1_20_3_OR_ABOVE , IS_1_20_5_OR_ABOVE , IS_1_21_5_OR_ABOVE , IS_1_21_6_OR_ABOVE , IS_26_2_OR_ABOVE ;
2122 private static final String OLD_NMS_VERSION_STRING ;
2223
2324 static {
@@ -74,6 +75,14 @@ private PacketAccessors() {
7475 } catch (ClassNotFoundException ignored ) {
7576 }
7677 IS_1_21_6_OR_ABOVE = is1_21_6OrAbove ;
78+
79+ boolean is26_2OrAbove = false ;
80+ try {
81+ Class .forName ("net.minecraft.world.scores.TeamColor" );
82+ is26_2OrAbove = true ;
83+ } catch (ClassNotFoundException ignored ) {
84+ }
85+ IS_26_2_OR_ABOVE = is26_2OrAbove ;
7786 }
7887
7988 public static final Class <?> PKT_CLASS ,
@@ -93,7 +102,7 @@ private PacketAccessors() {
93102 OBJECTIVE_CLASS ,
94103 TEAM_VISIBILITY_CLASS ,
95104 TEAM_COLLISION_RULE_CLASS ,
96- CHAT_FORMATTING_CLASS ,
105+ TEAM_COLOR_OR_CHAT_FORMATTING_CLASS ,
97106 OBJECTIVE_CRITERIA_RENDER_TYPE_CLASS ,
98107 DATA_RESULT_CLASS ,
99108 DYNAMIC_OPS_CLASS ,
@@ -121,7 +130,7 @@ private PacketAccessors() {
121130 OBJECTIVE_CLASS = ReflectUtil .getClassOrThrow ("net.minecraft.world.scores.Objective" , "net.minecraft.world.scores.ScoreboardObjective" , oldSpigotClassName ("ScoreboardObjective" ));
122131 TEAM_VISIBILITY_CLASS = ReflectUtil .getClassOrThrow ("net.minecraft.world.scores.Team$Visibility" , "net.minecraft.world.scores.ScoreboardTeamBase$EnumNameTagVisibility" , oldSpigotClassName ("ScoreboardTeamBase$EnumNameTagVisibility" ));
123132 TEAM_COLLISION_RULE_CLASS = ReflectUtil .getClassOrThrow ("net.minecraft.world.scores.Team$CollisionRule" , "net.minecraft.world.scores.ScoreboardTeamBase$EnumTeamPush" , oldSpigotClassName ("ScoreboardTeamBase$EnumTeamPush" ));
124- CHAT_FORMATTING_CLASS = ReflectUtil .getClassOrThrow ("net.minecraft.ChatFormatting" , "net.minecraft.EnumChatFormat" , oldSpigotClassName ("EnumChatFormat" ));
133+ TEAM_COLOR_OR_CHAT_FORMATTING_CLASS = ReflectUtil .getClassOrThrow ("net.minecraft.world.scores.TeamColor" , "net.minecraft.ChatFormatting" , "net.minecraft.EnumChatFormat" , oldSpigotClassName ("EnumChatFormat" ));
125134 OBJECTIVE_CRITERIA_RENDER_TYPE_CLASS = ReflectUtil .getClassOrThrow ("net.minecraft.world.scores.criteria.ObjectiveCriteria$RenderType" , "net.minecraft.world.scores.criteria.IScoreboardCriteria$EnumScoreboardHealthDisplay" , oldSpigotClassName ("IScoreboardCriteria$EnumScoreboardHealthDisplay" ));
126135 DATA_RESULT_CLASS = ReflectUtil .getOptionalClass ("com.mojang.serialization.DataResult" );
127136 DYNAMIC_OPS_CLASS = ReflectUtil .getOptionalClass ("com.mojang.serialization.DynamicOps" );
@@ -181,14 +190,14 @@ private PacketAccessors() {
181190 public static final Map <NamedTextColor , Object > NMS_CHAT_FORMATTING_MAP = new HashMap <>();
182191
183192 static {
184- Object [] chatFormattings = CHAT_FORMATTING_CLASS .getEnumConstants ();
185- FieldAccessor <Object , Object > charField = ReflectUtil .findFieldUnchecked (CHAT_FORMATTING_CLASS , 0 , char .class );
193+ Object [] chatFormattings = TEAM_COLOR_OR_CHAT_FORMATTING_CLASS .getEnumConstants ();
194+ FieldAccessor <Object , Object > nameField = ReflectUtil .findFieldUnchecked (TEAM_COLOR_OR_CHAT_FORMATTING_CLASS , 0 , String .class );
186195
187196 outer :
188197 for (NamedTextColor color : NamedTextColor .NAMES .values ()) {
189198 char c = LegacyFormatUtil .getChar (color );
190199 for (Object chatFormatting : chatFormattings ) {
191- if (c == ( char ) charField .get (chatFormatting )) {
200+ if (color . toString (). equalsIgnoreCase (( String ) nameField .get (chatFormatting ) )) {
192201 NMS_CHAT_FORMATTING_MAP .put (color , chatFormatting );
193202 continue outer ;
194203 }
@@ -217,7 +226,7 @@ private PacketAccessors() {
217226
218227 // --- OBJECTIVES ---
219228
220- public static final PacketConstructor <?> OBJECTIVE_PACKET_CONSTRUCTOR =
229+ public static final ConstructorAccessor <?> OBJECTIVE_PACKET_CONSTRUCTOR =
221230 ReflectUtil .getEmptyConstructor (SET_OBJECTIVE_PKT_CLASS );
222231 public static final FieldAccessor <Object , String > OBJECTIVE_NAME_FIELD =
223232 ReflectUtil .findFieldUnchecked (SET_OBJECTIVE_PKT_CLASS , 0 , String .class );
@@ -257,7 +266,7 @@ private PacketAccessors() {
257266
258267 // --- TEAMS ---
259268
260- public static final PacketConstructor <?> PARAMETERS_CONSTRUCTOR ;
269+ public static final ConstructorAccessor <?> PARAMETERS_CONSTRUCTOR ; // full record constructor on 26.2+, empty constructor for below
261270 public static final ConstructorAccessor <?> TEAM_PACKET_CONSTRUCTOR ;
262271
263272 public static final FieldAccessor <Object , String > TEAM_NAME_FIELD ;
@@ -275,7 +284,21 @@ private PacketAccessors() {
275284 public static final FieldAccessor <Object , Integer > OPTIONS_FIELD ;
276285
277286 static {
278- if (IS_1_17_OR_ABOVE ) {
287+ if (IS_26_2_OR_ABOVE ) {
288+ PARAMETERS_CONSTRUCTOR = ReflectUtil .findConstructor (TEAM_PARAMETERS_PKT_CLASS , COMPONENT_CLASS , COMPONENT_CLASS , COMPONENT_CLASS , TEAM_VISIBILITY_CLASS , TEAM_COLLISION_RULE_CLASS , Optional .class , byte .class );
289+ TEAM_PACKET_CONSTRUCTOR = ReflectUtil .findConstructor (SET_PLAYER_TEAM_PKT_CLASS , String .class , int .class , Optional .class , Collection .class ); // same as below
290+
291+ TEAM_NAME_FIELD = null ;
292+ TEAM_MODE_FIELD = null ;
293+ TEAM_ENTRIES_FIELD = null ;
294+ DISPLAY_NAME_FIELD = null ;
295+ PREFIX_FIELD = null ;
296+ SUFFIX_FIELD = null ;
297+ NAME_TAG_VISIBILITY_FIELD = null ;
298+ COLLISION_RULE_FIELD = null ;
299+ COLOR_FIELD = null ;
300+ OPTIONS_FIELD = null ;
301+ } else if (IS_1_17_OR_ABOVE ) {
279302 assert TEAM_PARAMETERS_PKT_CLASS != null ;
280303 PARAMETERS_CONSTRUCTOR = ReflectUtil .getEmptyConstructor (TEAM_PARAMETERS_PKT_CLASS );
281304 TEAM_PACKET_CONSTRUCTOR = ReflectUtil .findConstructor (SET_PLAYER_TEAM_PKT_CLASS , String .class , int .class , Optional .class , Collection .class );
@@ -296,7 +319,7 @@ private PacketAccessors() {
296319 COLLISION_RULE_FIELD = ReflectUtil .findFieldUnchecked (TEAM_PARAMETERS_PKT_CLASS , 1 , String .class );
297320 }
298321
299- COLOR_FIELD = ReflectUtil .findFieldUnchecked (TEAM_PARAMETERS_PKT_CLASS , 0 , CHAT_FORMATTING_CLASS );
322+ COLOR_FIELD = ReflectUtil .findFieldUnchecked (TEAM_PARAMETERS_PKT_CLASS , 0 , TEAM_COLOR_OR_CHAT_FORMATTING_CLASS );
300323 OPTIONS_FIELD = ReflectUtil .findFieldUnchecked (TEAM_PARAMETERS_PKT_CLASS , 0 , int .class );
301324 } else {
302325 PARAMETERS_CONSTRUCTOR = null ;
@@ -313,7 +336,7 @@ private PacketAccessors() {
313336 NAME_TAG_VISIBILITY_FIELD = ReflectUtil .findFieldUnchecked (SET_PLAYER_TEAM_PKT_CLASS , 1 , String .class );
314337 COLLISION_RULE_FIELD = ReflectUtil .findFieldUnchecked (SET_PLAYER_TEAM_PKT_CLASS , 2 , String .class );
315338
316- COLOR_FIELD = ReflectUtil .findFieldUnchecked (SET_PLAYER_TEAM_PKT_CLASS , 0 , CHAT_FORMATTING_CLASS );
339+ COLOR_FIELD = ReflectUtil .findFieldUnchecked (SET_PLAYER_TEAM_PKT_CLASS , 0 , TEAM_COLOR_OR_CHAT_FORMATTING_CLASS );
317340 OPTIONS_FIELD = ReflectUtil .findFieldUnchecked (SET_PLAYER_TEAM_PKT_CLASS , 1 , int .class );
318341 }
319342 }
@@ -348,6 +371,44 @@ public static Object collisionRule(CollisionRule value) {
348371 }
349372 }
350373
374+ public static Object createTeamParameters (Object displayName , Object playerPrefix , Object playerSuffix , ImmutableTeamProperties <?> otherProps ) {
375+ if (IS_26_2_OR_ABOVE ){
376+ Object chatFormattingColor = null ;
377+ if (otherProps .playerColor () != null ) {
378+ chatFormattingColor = PacketAccessors .NMS_CHAT_FORMATTING_MAP .get (otherProps .playerColor ());
379+ }
380+ Object nmsNameTagVisibility = PacketAccessors .nameTagVisibility (otherProps .nameTagVisibility ());
381+ Object nmsCollisionRule = PacketAccessors .collisionRule (otherProps .collisionRule ());
382+ return PARAMETERS_CONSTRUCTOR .invoke (displayName , playerPrefix , playerSuffix , nmsNameTagVisibility , nmsCollisionRule , Optional .ofNullable (chatFormattingColor ), (byte ) otherProps .packOptions ());
383+ }
384+
385+ Object parameters = PARAMETERS_CONSTRUCTOR .invoke ();
386+ setupOldTeamPropertiesFields (parameters , displayName , playerPrefix , playerSuffix , otherProps );
387+ return parameters ;
388+ }
389+
390+ public static void setupOldTeamPropertiesFields (Object teamPacket , Object displayName , Object playerPrefix , Object playerSuffix , ImmutableTeamProperties <?> otherProps ) {
391+ Object nmsNameTagVisibility ;
392+ Object nmsCollisionRule ;
393+ if (IS_1_21_5_OR_ABOVE ) {
394+ nmsNameTagVisibility = PacketAccessors .nameTagVisibility (otherProps .nameTagVisibility ());
395+ nmsCollisionRule = PacketAccessors .collisionRule (otherProps .collisionRule ());
396+ } else {
397+ nmsNameTagVisibility = otherProps .nameTagVisibility ().key ();
398+ nmsCollisionRule = otherProps .collisionRule ().key ();
399+ }
400+
401+ Object chatFormattingColor = PacketAccessors .NMS_CHAT_FORMATTING_MAP .get (otherProps .playerColor () != null ? otherProps .playerColor () : NamedTextColor .WHITE );
402+
403+ DISPLAY_NAME_FIELD .set (teamPacket , displayName );
404+ PREFIX_FIELD .set (teamPacket , playerPrefix );
405+ SUFFIX_FIELD .set (teamPacket , playerSuffix );
406+ NAME_TAG_VISIBILITY_FIELD .set (teamPacket , nmsNameTagVisibility );
407+ COLLISION_RULE_FIELD .set (teamPacket , nmsCollisionRule );
408+ COLOR_FIELD .set (teamPacket , chatFormattingColor );
409+ OPTIONS_FIELD .set (teamPacket , otherProps .packOptions ());
410+ }
411+
351412 public static @ NotNull Object fromAdventureComponent (@ NotNull Component component ) {
352413 return Objects .requireNonNull (PacketAccessors .ADVENTURE_COMPONENT_CONSTRUCTOR ).invoke (component );
353414 }
0 commit comments