@@ -134,7 +134,7 @@ public final class SpigotReflectionUtil {
134134 NBT_ACCOUNTER_UNLIMITED_HEAP , CHUNK_CACHE_GET_IBLOCKACCESS , CHUNK_CACHE_GET_ICHUNKACCESS ,
135135 IBLOCKACCESS_GET_BLOCK_DATA , CHUNK_GET_BLOCK_DATA , PLAYER_CHUNK_MAP_GET_PLAYER_CHUNK , PLAYER_CHUNK_GET_CHUNK ,
136136 LEGACY_DATA_WATCHER_WRITE_METHOD , CLIENTBOUND_SET_ENTITY_DATA_PACKET_WRITE_DATA_WATCHER_METHOD , GET_DATA_VALUE_FROM_DATA_ITEM_METHOD ,
137- GET_TPS , REGISTRY_OPS_CREATE ;
137+ GET_TPS , REGISTRY_OPS_CREATE , GET_NEXT_ENTITY_ID ;
138138
139139 //Constructors
140140 private static Constructor <?> NMS_ITEM_STACK_CONSTRUCTOR , NMS_PACKET_DATA_SERIALIZER_CONSTRUCTOR ,
@@ -294,6 +294,7 @@ private static void initMethods() {
294294 GET_DATA_VALUE_FROM_DATA_ITEM_METHOD = Reflection .getMethod (DATA_WATCHER_ITEM_CLASS , DATA_WATCHER_VALUE_CLASS , 0 );
295295 GET_TPS = Reflection .getMethod (Server .class , "getTPS" );
296296 REGISTRY_OPS_CREATE = Reflection .getMethod (REGISTRY_OPS , REGISTRY_OPS , 0 );
297+ GET_NEXT_ENTITY_ID = Reflection .getMethod (SERVER_LEVEL_CLASS , "getNextEntityId" );
297298 }
298299
299300 private static void initFields () {
@@ -331,6 +332,9 @@ private static void initFields() {
331332 if (ENTITY_ID_COUNTER == null ) {
332333 ENTITY_ID_COUNTER = Reflection .getField (NMS_ENTITY_CLASS , AtomicInteger .class , 0 );
333334 }
335+ if (ENTITY_ID_COUNTER == null ) {
336+ ENTITY_ID_COUNTER = Reflection .getField (SERVER_LEVEL_CLASS , "ENTITY_COUNTER" );
337+ }
334338 }
335339
336340 private static void initClasses () {
@@ -806,6 +810,19 @@ public static String fromStringToJSON(String message) {
806810 return "{\" text\" : \" " + message + "\" }" ;
807811 }
808812
813+ public static int generateEntityId (World world ) {
814+ // use vanilla logic if available
815+ if (GET_NEXT_ENTITY_ID != null ) {
816+ try {
817+ Object level = GET_CRAFT_WORLD_HANDLE_METHOD .invoke (world );
818+ return (int ) GET_NEXT_ENTITY_ID .invoke (level );
819+ } catch (ReflectiveOperationException exception ) {
820+ throw new RuntimeException ("Failed to generate entity id: " , exception );
821+ }
822+ }
823+ return generateEntityId ();
824+ }
825+
809826 public static int generateEntityId () {
810827 try {
811828 if (ENTITY_ID_COUNTER .getType () == AtomicInteger .class ) {
0 commit comments