From 2e626f785e2b3c1a367d4cb55e7d8085256e11da Mon Sep 17 00:00:00 2001 From: TheMode Date: Mon, 12 Aug 2019 08:59:15 +0200 Subject: [PATCH] Improved thread safety over entities update --- .../fr/themode/minestom/instance/Instance.java | 14 ++++++-------- .../themode/minestom/utils/GroupedCollections.java | 7 +++---- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/main/java/fr/themode/minestom/instance/Instance.java b/src/main/java/fr/themode/minestom/instance/Instance.java index f6b36476e..13d9ea251 100644 --- a/src/main/java/fr/themode/minestom/instance/Instance.java +++ b/src/main/java/fr/themode/minestom/instance/Instance.java @@ -9,12 +9,16 @@ import fr.themode.minestom.utils.GroupedCollections; import java.util.Collections; import java.util.Set; import java.util.UUID; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArraySet; public class Instance { private UUID uniqueId; + private GroupedCollections creatures = new GroupedCollections<>(new CopyOnWriteArrayList()); + private GroupedCollections players = new GroupedCollections<>(new CopyOnWriteArrayList()); + private Set chunksSet = new CopyOnWriteArraySet<>(); public Instance(UUID uniqueId) { @@ -87,18 +91,10 @@ public class Instance { } public GroupedCollections getCreatures() { - GroupedCollections creatures = new GroupedCollections(); - for (Chunk chunk : getChunks()) { - creatures.addCollection(chunk.creatures); - } return creatures; } public GroupedCollections getPlayers() { - GroupedCollections players = new GroupedCollections(); - for (Chunk chunk : getChunks()) { - players.addCollection(chunk.players); - } return players; } @@ -108,6 +104,8 @@ public class Instance { private Chunk createChunk(Biome biome, int chunkX, int chunkZ) { Chunk chunk = new Chunk(biome, chunkX, chunkZ); + this.creatures.addCollection(chunk.creatures); + this.players.addCollection(chunk.players); this.chunksSet.add(chunk); return chunk; } diff --git a/src/main/java/fr/themode/minestom/utils/GroupedCollections.java b/src/main/java/fr/themode/minestom/utils/GroupedCollections.java index bcccb94b1..3ea56db83 100644 --- a/src/main/java/fr/themode/minestom/utils/GroupedCollections.java +++ b/src/main/java/fr/themode/minestom/utils/GroupedCollections.java @@ -1,15 +1,14 @@ package fr.themode.minestom.utils; -import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class GroupedCollections implements Iterable { - private ArrayList> collections; + private Collection> collections; - public GroupedCollections() { - this.collections = new ArrayList<>(); + public GroupedCollections(Collection collection) { + this.collections = collection; } public int size() {