5757import net .opmasterleo .packet .nms .packet .PacketViews ;
5858import net .opmasterleo .packet .nms .packet .PlayerActionType ;
5959
60+ import static net .opmasterleo .packet .nms .shared .Reflect .constructSetTime ;
61+ import static net .opmasterleo .packet .nms .shared .Reflect .field ;
62+ import static net .opmasterleo .packet .nms .shared .Reflect .get ;
6063import static net .opmasterleo .packet .nms .shared .Reflect .invoke ;
6164
6265public final class SharedPacketBridge implements PacketBridge {
@@ -78,23 +81,16 @@ public String classify(Object packet) {
7881
7982 @ Override
8083 public int entityId (Object packet ) {
81- if (packet instanceof ClientboundAddEntityPacket add ) {
82- return add .getId ();
83- }
84- if (packet instanceof ClientboundSetEntityDataPacket data ) {
85- return data .id ();
86- }
87- if (packet instanceof ClientboundSetEquipmentPacket equipment ) {
88- return equipment .getEntity ();
89- }
90- if (packet instanceof ClientboundSetEntityMotionPacket motion ) {
91- return motion .getId ();
84+ if (packet == null ) {
85+ return -1 ;
9286 }
93- if (packet instanceof ServerboundInteractPacket interact ) {
94- return interact .getEntityId ();
87+ // Method names drift across modern buckets (id/getId/getEntity/getEntityId).
88+ Object value = invoke (packet , "getEntityId" , "getEntity" , "getId" , "id" , "entityId" );
89+ if (value instanceof Integer i ) {
90+ return i ;
9591 }
96- Object value = invoke ( packet , "getEntityId" , " id" , "entityId" , "getId" );
97- return value instanceof Integer i ? i : -1 ;
92+ Object fieldValue = get ( field ( packet . getClass () , "id" , "entityId" , "entity" ), packet );
93+ return fieldValue instanceof Integer i ? i : -1 ;
9894 }
9995
10096 @ Override
@@ -254,7 +250,11 @@ public Object gameEvent(int eventId, float value) {
254250
255251 @ Override
256252 public Object setTime (long gameTime , long dayTime ) {
257- return new ClientboundSetTimePacket (gameTime , dayTime , true );
253+ Object packet = constructSetTime (ClientboundSetTimePacket .class , gameTime , dayTime );
254+ if (packet == null ) {
255+ throw new IllegalStateException ("Unable to construct ClientboundSetTimePacket" );
256+ }
257+ return packet ;
258258 }
259259
260260 @ Override
@@ -339,8 +339,9 @@ public PacketViews.InteractView interact(Object packet) {
339339 if (!(packet instanceof ServerboundInteractPacket interact )) {
340340 return PacketBridge .super .interact (packet );
341341 }
342- InteractAction action = interact .isAttack () ? InteractAction .ATTACK : InteractAction .INTERACT ;
343- return new PacketViews .InteractView (interact .getEntityId (), action , Hand .MAIN_HAND );
342+ boolean attack = Boolean .TRUE .equals (invoke (interact , "isAttack" ));
343+ InteractAction action = attack ? InteractAction .ATTACK : InteractAction .INTERACT ;
344+ return new PacketViews .InteractView (entityId (interact ), action , Hand .MAIN_HAND );
344345 }
345346
346347 @ Override
@@ -353,14 +354,17 @@ public PacketViews.PlayerActionView playerAction(Object packet) {
353354 }
354355
355356 private static PlayerActionType playerActionType (ServerboundPlayerActionPacket .Action action ) {
356- return switch (action ) {
357- case START_DESTROY_BLOCK -> PlayerActionType .START_DESTROY_BLOCK ;
358- case ABORT_DESTROY_BLOCK -> PlayerActionType .ABORT_DESTROY_BLOCK ;
359- case STOP_DESTROY_BLOCK -> PlayerActionType .STOP_DESTROY_BLOCK ;
360- case DROP_ALL_ITEMS -> PlayerActionType .DROP_ALL_ITEMS ;
361- case DROP_ITEM -> PlayerActionType .DROP_ITEM ;
362- case RELEASE_USE_ITEM -> PlayerActionType .RELEASE_USE_ITEM ;
363- case SWAP_ITEM_WITH_OFFHAND -> PlayerActionType .SWAP_ITEM_WITH_OFFHAND ;
357+ // String switch keeps older modern buckets compiling when newer enums add values (e.g. STAB).
358+ return switch (action .name ()) {
359+ case "START_DESTROY_BLOCK" -> PlayerActionType .START_DESTROY_BLOCK ;
360+ case "ABORT_DESTROY_BLOCK" -> PlayerActionType .ABORT_DESTROY_BLOCK ;
361+ case "STOP_DESTROY_BLOCK" -> PlayerActionType .STOP_DESTROY_BLOCK ;
362+ case "DROP_ALL_ITEMS" -> PlayerActionType .DROP_ALL_ITEMS ;
363+ case "DROP_ITEM" -> PlayerActionType .DROP_ITEM ;
364+ case "RELEASE_USE_ITEM" -> PlayerActionType .RELEASE_USE_ITEM ;
365+ case "SWAP_ITEM_WITH_OFFHAND" -> PlayerActionType .SWAP_ITEM_WITH_OFFHAND ;
366+ case "STAB" -> PlayerActionType .UNKNOWN ;
367+ default -> PlayerActionType .UNKNOWN ;
364368 };
365369 }
366370
0 commit comments