55import it .unimi .dsi .fastutil .longs .LongArraySet ;
66import it .unimi .dsi .fastutil .longs .LongOpenHashSet ;
77import it .unimi .dsi .fastutil .objects .ObjectArraySet ;
8- import net .minecraft .util .math .BlockPos ;
98
109import java .util .Collection ;
1110import java .util .Map ;
@@ -33,7 +32,7 @@ public class WorldEntityTracker {
3332 public WorldEntityTracker () {}
3433
3534 public void join (ModdedEntity entity ) {
36- long chunk = ChunkPos .asLong (entity .getPosition ());
35+ long chunk = ChunkPos .asLongExcludeY (entity .getPosition ());
3736 int x = ChunkPos .x (chunk );
3837 int y = ChunkPos .y (chunk );
3938 int z = ChunkPos .z (chunk );
@@ -47,9 +46,11 @@ public void join(ModdedEntity entity) {
4746
4847 for (int i = x - HORIZONTAL_SEARCH_RADIUS_CHUNKS ; i <= x + HORIZONTAL_SEARCH_RADIUS_CHUNKS ; i ++) {
4948 for (int j = z - HORIZONTAL_SEARCH_RADIUS_CHUNKS ; j <= z + HORIZONTAL_SEARCH_RADIUS_CHUNKS ; j ++) {
50- for (int k = y - VERTICAL_SEARCH_RADIUS_CHUNKS ; k <= y + VERTICAL_SEARCH_RADIUS_CHUNKS ; k ++) {
49+ // for (int k = y - VERTICAL_SEARCH_RADIUS_CHUNKS; k <= y + VERTICAL_SEARCH_RADIUS_CHUNKS; k++) {
50+ //Handle Y below 1.17 is likely to cause more bug
51+ int k = 0 ;
5152 scanningRange .put (chunk , ChunkPos .asLong (i , k , j ));
52- }
53+ // }
5354 }
5455 }
5556 }
@@ -60,20 +61,19 @@ public void join(ModdedEntity entity) {
6061 }
6162
6263 public void leave (ModdedEntity entity ) {
63- BlockPos pos = entity .getPosition ();
64- long sec = ChunkPos .asLong (pos );
64+ long chunk = ChunkPos .asLongExcludeY (entity .getPosition ());
6565
6666 lock .writeLock ().lock ();
6767 try {
68- if (!umcEntities .containsKey (sec )) {
68+ if (!umcEntities .containsKey (chunk )) {
6969 return ;
7070 }
71- Collection <ModdedEntity > moddedEntities = umcEntities .get (sec );
71+ Collection <ModdedEntity > moddedEntities = umcEntities .get (chunk );
7272 moddedEntities .remove (entity );
7373
7474 if (moddedEntities .isEmpty ()) {
75- umcEntities .remove (sec );
76- scanningRange .removeKey (sec );
75+ umcEntities .remove (chunk );
76+ scanningRange .removeKey (chunk );
7777 }
7878 } finally {
7979 lock .writeLock ().unlock ();
@@ -83,6 +83,12 @@ public void leave(ModdedEntity entity) {
8383 public void move (ModdedEntity entity , long oldSection , long newSection ) {
8484 lock .writeLock ().lock ();
8585 try {
86+ if (!umcEntities .containsKey (oldSection )) {
87+ //Newly added, no need to process here
88+ //Let join handle it
89+ return ;
90+ }
91+
8692 // remove from old section
8793 Set <ModdedEntity > moddedEntities = umcEntities .get (oldSection );
8894 if (moddedEntities != null ) {
@@ -93,7 +99,7 @@ public void move(ModdedEntity entity, long oldSection, long newSection) {
9399 }
94100 }
95101
96- long chunk = ChunkPos .asLong (entity .getPosition ());
102+ long chunk = ChunkPos .asLongExcludeY (entity .getPosition ());
97103 int x = ChunkPos .x (chunk );
98104 int y = ChunkPos .y (chunk );
99105 int z = ChunkPos .z (chunk );
@@ -105,9 +111,10 @@ public void move(ModdedEntity entity, long oldSection, long newSection) {
105111
106112 for (int i = x - HORIZONTAL_SEARCH_RADIUS_CHUNKS ; i <= x + HORIZONTAL_SEARCH_RADIUS_CHUNKS ; i ++) {
107113 for (int j = z - HORIZONTAL_SEARCH_RADIUS_CHUNKS ; j <= z + HORIZONTAL_SEARCH_RADIUS_CHUNKS ; j ++) {
108- for (int k = y - VERTICAL_SEARCH_RADIUS_CHUNKS ; k <= y + VERTICAL_SEARCH_RADIUS_CHUNKS ; k ++) {
114+ // for (int k = y - VERTICAL_SEARCH_RADIUS_CHUNKS; k <= y + VERTICAL_SEARCH_RADIUS_CHUNKS; k++) {
115+ int k = 0 ;
109116 scanningRange .put (newSection , ChunkPos .asLong (i , k , j ));
110- }
117+ // }
111118 }
112119 }
113120 }
0 commit comments