Use flare thread-safe collections

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-12-16 05:29:11 +01:00
parent d865e9f75a
commit 895642dd44
3 changed files with 16 additions and 22 deletions

View File

@ -108,6 +108,8 @@ dependencies {
// https://mvnrepository.com/artifact/it.unimi.dsi/fastutil
api 'it.unimi.dsi:fastutil:8.5.6'
implementation group: 'space.vectrix.flare', name: 'flare', version: '2.0.0'
implementation group: 'space.vectrix.flare', name: 'flare-fastutil', version: '2.0.0'
// https://mvnrepository.com/artifact/com.google.code.gson/gson
api 'com.google.code.gson:gson:2.8.9'

View File

@ -53,6 +53,7 @@ import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import space.vectrix.flare.fastutil.Int2ObjectSyncMap;
import java.time.Duration;
import java.time.temporal.TemporalUnit;
@ -73,7 +74,7 @@ import java.util.function.UnaryOperator;
*/
public class Entity implements Viewable, Tickable, Schedulable, TagHandler, PermissionHandler, HoverEventSource<ShowEntity>, Sound.Emitter {
private static final Map<Integer, Entity> ENTITY_BY_ID = new ConcurrentHashMap<>();
private static final Int2ObjectSyncMap<Entity> ENTITY_BY_ID = Int2ObjectSyncMap.hashmap();
private static final Map<UUID, Entity> ENTITY_BY_UUID = new ConcurrentHashMap<>();
private static final AtomicInteger LAST_ENTITY_ID = new AtomicInteger();
@ -217,7 +218,7 @@ public class Entity implements Viewable, Tickable, Schedulable, TagHandler, Perm
* @return the entity having the specified id, null if not found
*/
public static @Nullable Entity getEntity(int id) {
return Entity.ENTITY_BY_ID.getOrDefault(id, null);
return Entity.ENTITY_BY_ID.get(id);
}
/**

View File

@ -28,6 +28,7 @@ import net.minestom.server.world.DimensionType;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import space.vectrix.flare.fastutil.Long2ObjectSyncMap;
import java.util.*;
import java.util.concurrent.CompletableFuture;
@ -48,7 +49,7 @@ public class InstanceContainer extends Instance {
private ChunkGenerator chunkGenerator;
// (chunk index -> chunk) map, contains all the chunks in the instance
// used as a monitor when access is required
private final Long2ObjectMap<Chunk> chunks = new Long2ObjectOpenHashMap<>();
private final Long2ObjectSyncMap<Chunk> chunks = Long2ObjectSyncMap.hashmap();
private final Long2ObjectMap<CompletableFuture<Chunk>> loadingChunks = new Long2ObjectOpenHashMap<>();
private final Lock changingBlockLock = new ReentrantLock();
@ -220,9 +221,7 @@ public class InstanceContainer extends Instance {
// Remove all entities in chunk
getEntityTracker().chunkEntities(chunkX, chunkZ, EntityTracker.Target.ENTITIES, Entity::remove);
// Clear cache
synchronized (chunks) {
this.chunks.remove(index);
}
this.chunks.remove(index);
chunk.unload();
UPDATE_MANAGER.signalChunkUnload(chunk);
}
@ -230,9 +229,7 @@ public class InstanceContainer extends Instance {
@Override
public Chunk getChunk(int chunkX, int chunkZ) {
final long index = ChunkUtils.getChunkIndex(chunkX, chunkZ);
synchronized (chunks) {
return chunks.get(index);
}
return chunks.get(index);
}
@Override
@ -379,13 +376,11 @@ public class InstanceContainer extends Instance {
InstanceContainer copiedInstance = new InstanceContainer(UUID.randomUUID(), getDimensionType());
copiedInstance.srcInstance = this;
copiedInstance.lastBlockChangeTime = lastBlockChangeTime;
synchronized (chunks) {
for (Chunk chunk : chunks.values()) {
final int chunkX = chunk.getChunkX();
final int chunkZ = chunk.getChunkZ();
final Chunk copiedChunk = chunk.copy(copiedInstance, chunkX, chunkZ);
copiedInstance.cacheChunk(copiedChunk);
}
for (Chunk chunk : chunks.values()) {
final int chunkX = chunk.getChunkX();
final int chunkZ = chunk.getChunkZ();
final Chunk copiedChunk = chunk.copy(copiedInstance, chunkX, chunkZ);
copiedInstance.cacheChunk(copiedChunk);
}
return copiedInstance;
}
@ -437,9 +432,7 @@ public class InstanceContainer extends Instance {
*/
@Override
public @NotNull Collection<@NotNull Chunk> getChunks() {
synchronized (chunks) {
return List.copyOf(chunks.values());
}
return chunks.values();
}
/**
@ -529,9 +522,7 @@ public class InstanceContainer extends Instance {
private void cacheChunk(@NotNull Chunk chunk) {
final long index = ChunkUtils.getChunkIndex(chunk);
synchronized (chunks) {
this.chunks.put(index, chunk);
}
this.chunks.put(index, chunk);
UPDATE_MANAGER.signalChunkLoad(chunk);
}
}