mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-02 11:21:15 +01:00
Use of a synchronized wrapper for InstanceContainer chunks map
This commit is contained in:
parent
5966cdaa47
commit
a8c1c73282
@ -1,6 +1,7 @@
|
|||||||
package net.minestom.server.instance;
|
package net.minestom.server.instance;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
||||||
|
import it.unimi.dsi.fastutil.longs.Long2ObjectMaps;
|
||||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.data.Data;
|
import net.minestom.server.data.Data;
|
||||||
@ -57,7 +58,7 @@ public class InstanceContainer extends Instance {
|
|||||||
// the chunk generator used, can be null
|
// the chunk generator used, can be null
|
||||||
private ChunkGenerator chunkGenerator;
|
private ChunkGenerator chunkGenerator;
|
||||||
// (chunk index -> chunk) map, contains all the chunks in the instance
|
// (chunk index -> chunk) map, contains all the chunks in the instance
|
||||||
private final Long2ObjectMap<Chunk> chunks = new Long2ObjectOpenHashMap<>();
|
private final Long2ObjectMap<Chunk> chunks = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>());
|
||||||
// contains all the chunks to remove during the next instance tick
|
// contains all the chunks to remove during the next instance tick
|
||||||
protected final Set<Chunk> scheduledChunksToRemove = new HashSet<>();
|
protected final Set<Chunk> scheduledChunksToRemove = new HashSet<>();
|
||||||
|
|
||||||
@ -451,11 +452,9 @@ public class InstanceContainer extends Instance {
|
|||||||
@Override
|
@Override
|
||||||
public Chunk getChunk(int chunkX, int chunkZ) {
|
public Chunk getChunk(int chunkX, int chunkZ) {
|
||||||
final long index = ChunkUtils.getChunkIndex(chunkX, chunkZ);
|
final long index = ChunkUtils.getChunkIndex(chunkX, chunkZ);
|
||||||
synchronized (chunks) {
|
|
||||||
final Chunk chunk = chunks.get(index);
|
final Chunk chunk = chunks.get(index);
|
||||||
return ChunkUtils.isLoaded(chunk) ? chunk : null;
|
return ChunkUtils.isLoaded(chunk) ? chunk : null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the instance ({@link #getUniqueId()} {@link #getData()}) and call {@link #saveChunksToStorage(Runnable)}.
|
* Saves the instance ({@link #getUniqueId()} {@link #getData()}) and call {@link #saveChunksToStorage(Runnable)}.
|
||||||
@ -495,11 +494,9 @@ public class InstanceContainer extends Instance {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveChunksToStorage(@Nullable Runnable callback) {
|
public void saveChunksToStorage(@Nullable Runnable callback) {
|
||||||
synchronized (chunks) {
|
|
||||||
Collection<Chunk> chunksCollection = chunks.values();
|
Collection<Chunk> chunksCollection = chunks.values();
|
||||||
this.chunkLoader.saveChunks(chunksCollection, callback);
|
this.chunkLoader.saveChunks(chunksCollection, callback);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BlockBatch createBlockBatch() {
|
public BlockBatch createBlockBatch() {
|
||||||
@ -645,7 +642,6 @@ public class InstanceContainer extends Instance {
|
|||||||
copiedInstance.srcInstance = this;
|
copiedInstance.srcInstance = this;
|
||||||
copiedInstance.lastBlockChangeTime = lastBlockChangeTime;
|
copiedInstance.lastBlockChangeTime = lastBlockChangeTime;
|
||||||
|
|
||||||
synchronized (chunks) {
|
|
||||||
for (Chunk chunk : chunks.values()) {
|
for (Chunk chunk : chunks.values()) {
|
||||||
final int chunkX = chunk.getChunkX();
|
final int chunkX = chunk.getChunkX();
|
||||||
final int chunkZ = chunk.getChunkZ();
|
final int chunkZ = chunk.getChunkZ();
|
||||||
@ -655,7 +651,6 @@ public class InstanceContainer extends Instance {
|
|||||||
copiedInstance.cacheChunk(copiedChunk);
|
copiedInstance.cacheChunk(copiedChunk);
|
||||||
UPDATE_MANAGER.signalChunkLoad(copiedInstance, chunkX, chunkZ);
|
UPDATE_MANAGER.signalChunkLoad(copiedInstance, chunkX, chunkZ);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return copiedInstance;
|
return copiedInstance;
|
||||||
}
|
}
|
||||||
@ -701,10 +696,8 @@ public class InstanceContainer extends Instance {
|
|||||||
*/
|
*/
|
||||||
public void cacheChunk(@NotNull Chunk chunk) {
|
public void cacheChunk(@NotNull Chunk chunk) {
|
||||||
final long index = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
|
final long index = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
|
||||||
synchronized (chunks) {
|
|
||||||
this.chunks.put(index, chunk);
|
this.chunks.put(index, chunk);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChunkGenerator getChunkGenerator() {
|
public ChunkGenerator getChunkGenerator() {
|
||||||
@ -833,9 +826,7 @@ public class InstanceContainer extends Instance {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Clear cache
|
// Clear cache
|
||||||
synchronized (chunks) {
|
|
||||||
this.chunks.remove(index);
|
this.chunks.remove(index);
|
||||||
}
|
|
||||||
synchronized (chunkEntities) {
|
synchronized (chunkEntities) {
|
||||||
this.chunkEntities.remove(index);
|
this.chunkEntities.remove(index);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user