11package github .kasuminova .mmce .common .world ;
22
33import github .kasuminova .mmce .client .world .BlockModelHider ;
4+ import it .unimi .dsi .fastutil .longs .Long2ObjectMap ;
5+ import it .unimi .dsi .fastutil .longs .Long2ObjectOpenHashMap ;
46import net .minecraft .block .state .IBlockState ;
57import net .minecraft .entity .Entity ;
68import net .minecraft .entity .player .EntityPlayer ;
2123import javax .annotation .Nullable ;
2224import java .util .HashMap ;
2325import java .util .Map ;
24- import java .util .concurrent .ConcurrentHashMap ;
2526
2627public class MMWorldEventListener implements IWorldEventListener {
2728
2829 public static final MMWorldEventListener INSTANCE = new MMWorldEventListener ();
2930
30- private final Map <World , Map <ChunkPos , StructureBoundingBox >> worldChangedChunksLastTick = new ConcurrentHashMap <>();
31- private final Map <World , Map <ChunkPos , StructureBoundingBox >> worldChangedChunks = new ConcurrentHashMap <>();
31+ private final Map <World , Long2ObjectMap <StructureBoundingBox >> worldChangedChunks = new HashMap <>();
3232
3333 private MMWorldEventListener () {
3434 }
@@ -51,19 +51,16 @@ public void onWorldUnloaded(WorldEvent.Unload event) {
5151 return ;
5252 }
5353 worldChangedChunks .remove (world );
54- worldChangedChunksLastTick .remove (world );
5554 MachineComponentManager .INSTANCE .removeWorld (world );
5655 }
5756
5857 @ SubscribeEvent
59- public void onServerTickStart (TickEvent .ServerTickEvent event ) {
58+ public void onWorldTickStart (TickEvent .WorldTickEvent event ) {
6059 if (event .side != Side .SERVER || event .phase != TickEvent .Phase .START ) {
6160 return ;
6261 }
6362
64- worldChangedChunksLastTick .clear ();
65- worldChangedChunksLastTick .putAll (worldChangedChunks );
66- worldChangedChunks .clear ();
63+ worldChangedChunks .put (event .world , new Long2ObjectOpenHashMap <>());
6764 }
6865
6966 @ SubscribeEvent
@@ -79,7 +76,7 @@ public void onChunkUnload(final ChunkEvent.Unload event) {
7976 int zEnd = pos .getZEnd ();
8077
8178 StructureBoundingBox structureArea = new StructureBoundingBox (xStart , zStart , xEnd , zEnd );
82- worldChangedChunks .get (world ).put (pos , structureArea );
79+ worldChangedChunks .get (world ).put (ChunkPos . asLong ( pos . x , pos . z ) , structureArea );
8380 }
8481
8582 public boolean isAreaChanged (@ Nonnull final World worldIn ,
@@ -94,9 +91,9 @@ public boolean isAreaChanged(@Nonnull final World worldIn,
9491
9592 for (int chunkX = minChunkX ; chunkX <= maxChunkX ; chunkX ++) {
9693 for (int chunkZ = minChunkZ ; chunkZ <= maxChunkZ ; chunkZ ++) {
97- Map < ChunkPos , StructureBoundingBox > chunkPosBoundingBoxMap = worldChangedChunksLastTick .get (worldIn );
98- if (chunkPosBoundingBoxMap != null ) {
99- StructureBoundingBox changedArea = chunkPosBoundingBoxMap .get (new ChunkPos (chunkX , chunkZ ));
94+ Long2ObjectMap < StructureBoundingBox > changedChunks = worldChangedChunks .get (worldIn );
95+ if (changedChunks != null ) {
96+ StructureBoundingBox changedArea = changedChunks .get (ChunkPos . asLong (chunkX , chunkZ ));
10097 if (changedArea != null && changedArea .intersectsWith (structureArea )) {
10198 return true ;
10299 }
@@ -111,16 +108,17 @@ public void notifyBlockUpdate(@Nonnull final World worldIn,
111108 @ Nonnull final BlockPos pos ,
112109 @ Nonnull final IBlockState oldState ,
113110 @ Nonnull final IBlockState newState , final int flags ) {
114- if ((flags != 1 && flags != 3 ) || oldState == newState ) {
111+ if ((flags & 1 ) == 0 || oldState == newState ) {
115112 return ;
116113 }
117114
118- Map < ChunkPos , StructureBoundingBox > chunkPosHeightSetMap = worldChangedChunks .computeIfAbsent (worldIn , v -> new HashMap <>());
115+ Long2ObjectMap < StructureBoundingBox > changedChunks = worldChangedChunks .computeIfAbsent (worldIn , v -> new Long2ObjectOpenHashMap <>());
119116 ChunkPos chunkPos = new ChunkPos (pos .getX () >> 4 , pos .getZ () >> 4 );
120- StructureBoundingBox changedArea = chunkPosHeightSetMap .get (chunkPos );
117+ long longChunkPos = ChunkPos .asLong (chunkPos .x , chunkPos .z );
118+ StructureBoundingBox changedArea = changedChunks .get (longChunkPos );
121119
122120 if (changedArea == null ) {
123- chunkPosHeightSetMap .put (chunkPos , new StructureBoundingBox (pos , pos ));
121+ changedChunks .put (longChunkPos , new StructureBoundingBox (pos , pos ));
124122 } else {
125123 changedArea .expandTo (new StructureBoundingBox (pos , pos ));
126124 }
0 commit comments