11package com .github .elic0de .thejpspit .nms ;
22
3+ import java .lang .reflect .Constructor ;
4+ import java .lang .reflect .Field ;
5+ import java .lang .reflect .Method ;
6+ import java .util .Arrays ;
7+ import java .util .Optional ;
8+ import java .util .UUID ;
39import net .minecraft .network .protocol .game .PacketPlayOutEntityDestroy ;
410import net .minecraft .network .protocol .game .PacketPlayOutEntityMetadata ;
11+ import net .minecraft .network .protocol .game .PacketPlayOutScoreboardTeam ;
512import net .minecraft .network .protocol .game .PacketPlayOutSpawnEntity ;
613import net .minecraft .network .syncher .DataWatcher ;
714import net .minecraft .server .level .WorldServer ;
815import net .minecraft .world .entity .EntityLiving ;
916import net .minecraft .world .entity .decoration .EntityArmorStand ;
17+ import net .minecraft .world .scores .Scoreboard ;
18+ import net .minecraft .world .scores .ScoreboardTeam ;
1019import org .bukkit .Bukkit ;
1120import org .bukkit .Location ;
1221import org .bukkit .World ;
1322import org .bukkit .entity .ArmorStand ;
1423import org .bukkit .entity .Player ;
1524import org .jetbrains .annotations .NotNull ;
1625
17- import java .lang .reflect .Field ;
18- import java .lang .reflect .Method ;
19-
2026/**
2127 * Implementation of the packet manager for the 1.19 minecraft java version.
2228 * The implementation uses a mixture of direct calls against the re-obfuscated server internals and reflection.
2329 */
2430public final class PacketManager1_19_R1 implements PacketManager {
2531
32+ private final Class <?> scoreboardClass ;
2633 private final Method entityGetIdMethod ;
2734 private final Method entityGetDataWatcherMethod ;
2835 private final Method entityGetHandleMethod ;
@@ -32,13 +39,15 @@ public final class PacketManager1_19_R1 implements PacketManager {
3239
3340 private final Field entityPlayerPlayerConnectionField ;
3441
35- public PacketManager1_19_R1 (final @ NotNull Method entityGetIdMethod ,
42+ public PacketManager1_19_R1 (final @ NotNull Class scoreboardClass ,
43+ final @ NotNull Method entityGetIdMethod ,
3644 final @ NotNull Method entityGetDataWatcherMethod ,
3745 final @ NotNull Method entityGetHandleMethod ,
3846 final @ NotNull Method entityGetBukkitEntityMethod ,
3947 final @ NotNull Method worldGetHandleMethod ,
4048 final @ NotNull Method playerConnectionSendPacketMethod ,
4149 final @ NotNull Field entityPlayerPlayerConnectionField ) {
50+ this .scoreboardClass = scoreboardClass ;
4251 this .entityGetIdMethod = entityGetIdMethod ;
4352 this .entityGetDataWatcherMethod = entityGetDataWatcherMethod ;
4453 this .entityGetHandleMethod = entityGetHandleMethod ;
@@ -52,6 +61,7 @@ public PacketManager1_19_R1(final @NotNull Method entityGetIdMethod,
5261 public static PacketManager1_19_R1 make () {
5362 try {
5463 return new PacketManager1_19_R1 (
64+ getMojangClass ("network.protocol.game.PacketPlayOutScoreboardTeam" ),
5565 getMojangClass ("world.entity.Entity" ).getMethod ("ae" ),
5666 getMojangClass ("world.entity.Entity" ).getMethod ("ai" ),
5767 getCBClass ("entity.CraftEntity" ).getMethod ("getHandle" ),
@@ -83,6 +93,29 @@ public Object buildEntitySpawnPacket(@NotNull Object entity) {
8393 return new PacketPlayOutSpawnEntity ((EntityLiving ) entity );
8494 }
8595
96+ @ NotNull
97+ @ Override
98+ public Object buildScoreboardTeam (Player player ) {
99+ try {
100+ final Scoreboard scoreboard = new Scoreboard ();
101+ final ScoreboardTeam scoreboardTeam = new ScoreboardTeam (scoreboard , UUID .randomUUID ().toString ().substring (0 , 15 ));
102+ final PacketPlayOutScoreboardTeam .b packetPlayOutScoreTeamB = new PacketPlayOutScoreboardTeam .b (scoreboardTeam );
103+ final Constructor <?> sc = getConstructor (scoreboardClass , 4 );
104+ final Field collisionRule = packetPlayOutScoreTeamB .getClass ().getDeclaredField ("e" );
105+
106+ sc .setAccessible (true );
107+ collisionRule .setAccessible (true );
108+ collisionRule .set (packetPlayOutScoreTeamB , "never" );
109+ return sc .newInstance ("" , 0 , Optional .of (packetPlayOutScoreTeamB ), Arrays .asList (player .getName ()));
110+ } catch (final ReflectiveOperationException e ) {
111+ throw new NMSAccessException ("Failed to create entity metadata packet" , e );
112+ }
113+ }
114+
115+ private Constructor <?> getConstructor (Class <?> clazz , int numParams ) {
116+ return Arrays .stream ((Constructor [])clazz .getDeclaredConstructors ()).filter (constructor -> (constructor .getParameterCount () == numParams )).findFirst ().orElse (null );
117+ }
118+
86119 @ NotNull
87120 @ Override
88121 public Object buildEntityMetadataPacket (@ NotNull Object entity , boolean forceUpdateAll ) {
0 commit comments