Added multiple kinds of instance update (per instance, per chunk, single-threaded)

This commit is contained in:
Felix Cravic 2020-05-05 23:27:24 +02:00
parent ca7e220543
commit 305bc78123
1 changed files with 75 additions and 2 deletions

View File

@ -16,6 +16,7 @@ public class InstanceManager {
private ExecutorService blocksPool = new MinestomThread(MinecraftServer.THREAD_COUNT_BLOCK_UPDATE, MinecraftServer.THREAD_NAME_BLOCK_UPDATE);
private Set<Instance> instances = Collections.synchronizedSet(new HashSet<>());
private UpdateType updateType = UpdateType.PER_INSTANCE;
public InstanceContainer createInstanceContainer(Dimension dimension, StorageFolder storageFolder) {
InstanceContainer instance = new InstanceContainer(UUID.randomUUID(), dimension, storageFolder);
@ -50,6 +51,38 @@ public class InstanceManager {
return;
long time = System.currentTimeMillis();
switch (updateType) {
case PER_INSTANCE:
perInstanceUpdate(time);
break;
case PER_CHUNK:
perChunkUpdate(time);
break;
case SINGLE_THREADED:
singleThreadedUpdate(time);
break;
}
}
public Set<Instance> getInstances() {
return Collections.unmodifiableSet(instances);
}
public UpdateType getUpdateType() {
return updateType;
}
public void setUpdateType(UpdateType updateType) {
this.updateType = updateType;
}
/**
* A thread per instance
*
* @param time the update time
*/
private void perInstanceUpdate(long time) {
for (Instance instance : instances) {
if (instance instanceof InstanceContainer) { // SharedInstance should be updated at the same time (verify?)
@ -64,8 +97,48 @@ public class InstanceManager {
}
}
public Set<Instance> getInstances() {
return Collections.unmodifiableSet(instances);
/**
* A thread per chunk + a different one for the instance tick
*
* @param time the update time
*/
private void perChunkUpdate(long time) {
for (Instance instance : instances) {
if (instance instanceof InstanceContainer) { // SharedInstance should be updated at the same time (verify?)
blocksPool.execute(() -> instance.tick(time));
for (Chunk chunk : instance.getChunks()) {
blocksPool.execute(() -> chunk.updateBlocks(time, instance));
}
}
}
}
/**
* Update everything on the current thread
*
* @param time the update time
*/
private void singleThreadedUpdate(long time) {
for (Instance instance : instances) {
if (instance instanceof InstanceContainer) { // SharedInstance should be updated at the same time (verify?)
instance.tick(time);
for (Chunk chunk : instance.getChunks()) {
chunk.updateBlocks(time, instance);
}
}
}
}
public enum UpdateType {
PER_INSTANCE,
PER_CHUNK,
SINGLE_THREADED
}
}