Improved thread safety over entities update

This commit is contained in:
TheMode 2019-08-12 08:59:15 +02:00
parent e0172022b2
commit 2e626f785e
2 changed files with 9 additions and 12 deletions

View File

@ -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<EntityCreature> creatures = new GroupedCollections<>(new CopyOnWriteArrayList());
private GroupedCollections<Player> players = new GroupedCollections<>(new CopyOnWriteArrayList());
private Set<Chunk> chunksSet = new CopyOnWriteArraySet<>();
public Instance(UUID uniqueId) {
@ -87,18 +91,10 @@ public class Instance {
}
public GroupedCollections<EntityCreature> getCreatures() {
GroupedCollections<EntityCreature> creatures = new GroupedCollections();
for (Chunk chunk : getChunks()) {
creatures.addCollection(chunk.creatures);
}
return creatures;
}
public GroupedCollections<Player> getPlayers() {
GroupedCollections<Player> 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;
}

View File

@ -1,15 +1,14 @@
package fr.themode.minestom.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class GroupedCollections<E> implements Iterable<E> {
private ArrayList<Collection<E>> collections;
private Collection<Collection<E>> collections;
public GroupedCollections() {
this.collections = new ArrayList<>();
public GroupedCollections(Collection collection) {
this.collections = collection;
}
public int size() {