mirror of https://github.com/Minestom/Minestom.git
Fix ticks
This commit is contained in:
parent
981a1a2fe8
commit
7aaf6a6ec8
|
@ -47,7 +47,7 @@ public class Main {
|
|||
|
||||
//MojangAuth.init();
|
||||
|
||||
minecraftServer.start("localhost", 25565, PlayerInit.getResponseDataConsumer());
|
||||
minecraftServer.start("0.0.0.0", 25565, PlayerInit.getResponseDataConsumer());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -61,8 +61,8 @@ public class PlayerInit {
|
|||
end.setChunkGenerator(noiseTestGenerator);
|
||||
|
||||
// Load some chunks beforehand
|
||||
final int loopStart = -10;
|
||||
final int loopEnd = 10;
|
||||
final int loopStart = -4;
|
||||
final int loopEnd = 4;
|
||||
for (int x = loopStart; x < loopEnd; x++)
|
||||
for (int z = loopStart; z < loopEnd; z++) {
|
||||
instanceContainer.loadChunk(x, z);
|
||||
|
|
|
@ -14,6 +14,8 @@ import net.minestom.server.utils.validate.Check;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class UpdateManager {
|
||||
|
||||
|
@ -26,7 +28,9 @@ public final class UpdateManager {
|
|||
|
||||
private ThreadProvider threadProvider;
|
||||
|
||||
private ArrayList<Runnable> tickCallbacks = new ArrayList<>();
|
||||
private ArrayList<Runnable> tickStartCallbacks = new ArrayList<>();
|
||||
|
||||
private ArrayList<Consumer<Double>> tickEndCallbacks = new ArrayList<>();
|
||||
|
||||
{
|
||||
//threadProvider = new PerInstanceThreadProvider();
|
||||
|
@ -55,10 +59,10 @@ public final class UpdateManager {
|
|||
final long time = System.currentTimeMillis();
|
||||
|
||||
//Tick Callbacks
|
||||
tickCallbacks.forEach(Runnable::run);
|
||||
tickStartCallbacks.forEach(Runnable::run);
|
||||
|
||||
// Server tick (instance/chunk/entity)
|
||||
threadProvider.update(time);
|
||||
ArrayList<Future<?>> futures = threadProvider.update(time);
|
||||
|
||||
// Waiting players update (newly connected waiting to get into the server)
|
||||
entityManager.updateWaitingPlayers();
|
||||
|
@ -75,6 +79,19 @@ public final class UpdateManager {
|
|||
}
|
||||
}
|
||||
|
||||
for (final Future<?> future : futures) {
|
||||
try {
|
||||
future.get();
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Tick Callbacks
|
||||
double tickTime = (System.nanoTime() - currentTime) / 1000000D;
|
||||
tickEndCallbacks.forEach(doubleConsumer -> doubleConsumer.accept(tickTime));
|
||||
|
||||
// Sleep until next tick
|
||||
long sleepTime = (tickDistance - (System.nanoTime() - currentTime)) / 1000000;
|
||||
sleepTime = Math.max(1, sleepTime);
|
||||
|
@ -135,12 +152,20 @@ public final class UpdateManager {
|
|||
this.threadProvider.onChunkUnload(instance, chunkX, chunkZ);
|
||||
}
|
||||
|
||||
public void addTickCallback(Runnable callback) {
|
||||
tickCallbacks.add(callback);
|
||||
public void addTickStartCallback(Runnable callback) {
|
||||
tickStartCallbacks.add(callback);
|
||||
}
|
||||
|
||||
public void removeTickCallback(Runnable callback) {
|
||||
tickCallbacks.remove(callback);
|
||||
public void removeTickStartCallback(Runnable callback) {
|
||||
tickStartCallbacks.remove(callback);
|
||||
}
|
||||
|
||||
public void addTickEndCallback(Consumer<Double> callback) {
|
||||
tickEndCallbacks.add(callback);
|
||||
}
|
||||
|
||||
public void removeTickEndCallback(Consumer<Double> callback) {
|
||||
tickEndCallbacks.remove(callback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -10,6 +10,7 @@ import net.minestom.server.utils.chunk.ChunkUtils;
|
|||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
|
@ -102,10 +103,12 @@ public class PerGroupChunkProvider extends ThreadProvider {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void update(long time) {
|
||||
public ArrayList<Future<?>> update(long time) {
|
||||
// Set of already-updated instances this tick
|
||||
final Set<Instance> updatedInstance = new HashSet<>();
|
||||
|
||||
ArrayList<Future<?>> futures = new ArrayList<>();
|
||||
|
||||
instanceInstanceMap.entrySet().forEach(entry -> {
|
||||
final Instance instance = entry.getKey();
|
||||
final Map<LongSet, Instance> instanceMap = entry.getValue();
|
||||
|
@ -118,7 +121,7 @@ public class PerGroupChunkProvider extends ThreadProvider {
|
|||
final LongSet chunksIndexes = ent.getKey();
|
||||
|
||||
final boolean shouldUpdateInstance = updatedInstance.add(instance);
|
||||
pool.execute(() -> {
|
||||
futures.add(pool.submit(() -> {
|
||||
// Used to check if the instance has already been updated this tick
|
||||
if (shouldUpdateInstance) {
|
||||
updateInstance(instance, time);
|
||||
|
@ -141,11 +144,12 @@ public class PerGroupChunkProvider extends ThreadProvider {
|
|||
|
||||
updateEntities(instance, chunk, time);
|
||||
}
|
||||
});
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
return futures;
|
||||
}
|
||||
|
||||
private Long2ObjectMap<LongSet> getChunksGroupMap(Instance instance) {
|
||||
|
|
|
@ -6,8 +6,10 @@ import net.minestom.server.instance.Chunk;
|
|||
import net.minestom.server.instance.Instance;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* Separate work between instance (1 instance = 1 thread execution)
|
||||
|
@ -34,12 +36,14 @@ public class PerInstanceThreadProvider extends ThreadProvider {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void update(long time) {
|
||||
public ArrayList<Future<?>> update(long time) {
|
||||
ArrayList<Future<?>> futures = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<Instance, LongSet> entry : instanceChunkMap.entrySet()) {
|
||||
final Instance instance = entry.getKey();
|
||||
final LongSet chunkIndexes = entry.getValue();
|
||||
|
||||
pool.execute(() -> {
|
||||
futures.add(pool.submit(() -> {
|
||||
updateInstance(instance, time);
|
||||
|
||||
for (long chunkIndex : chunkIndexes) {
|
||||
|
@ -53,8 +57,9 @@ public class PerInstanceThreadProvider extends ThreadProvider {
|
|||
updateEntities(instance, chunk, time);
|
||||
|
||||
}
|
||||
});
|
||||
}));
|
||||
}
|
||||
return futures;
|
||||
}
|
||||
|
||||
private LongSet getChunkCoordinates(Instance instance) {
|
||||
|
|
|
@ -6,8 +6,10 @@ import net.minestom.server.instance.Chunk;
|
|||
import net.minestom.server.instance.Instance;
|
||||
import net.minestom.server.utils.thread.MinestomThread;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
|
@ -52,8 +54,9 @@ public abstract class ThreadProvider {
|
|||
* Perform a server tick for all chunks based on their linked thread
|
||||
*
|
||||
* @param time the update time in milliseconds
|
||||
* @return
|
||||
*/
|
||||
public abstract void update(long time);
|
||||
public abstract ArrayList<Future<?>> update(long time);
|
||||
|
||||
/**
|
||||
* Get the current size of the thread pool
|
||||
|
|
Loading…
Reference in New Issue