package net.minestom.server; import net.minestom.server.acquirable.Acquirable; import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Instance; import net.minestom.server.instance.InstanceManager; import net.minestom.server.monitoring.TickMonitor; import net.minestom.server.network.ConnectionManager; import net.minestom.server.network.player.NettyPlayerConnection; import net.minestom.server.thread.SingleThreadProvider; import net.minestom.server.thread.ThreadProvider; import net.minestom.server.utils.async.AsyncUtils; import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.locks.LockSupport; import java.util.function.Consumer; import java.util.function.LongConsumer; /** * Manager responsible for the server ticks. *
* The {@link ThreadProvider} manages the multi-thread aspect of chunk ticks.
*/
public final class UpdateManager {
private volatile boolean stopRequested;
// TODO make configurable
private ThreadProvider threadProvider = new SingleThreadProvider();
private final Queue
* WARNING: should be automatically done by the {@link InstanceManager}.
*
* @param instance the instance
*/
public void signalInstanceCreate(Instance instance) {
this.threadProvider.onInstanceCreate(instance);
}
/**
* Signals the {@link ThreadProvider} that an instance has been deleted.
*
* WARNING: should be automatically done by the {@link InstanceManager}.
*
* @param instance the instance
*/
public void signalInstanceDelete(Instance instance) {
this.threadProvider.onInstanceDelete(instance);
}
/**
* Signals the {@link ThreadProvider} that a chunk has been loaded.
*
* WARNING: should be automatically done by the {@link Instance} implementation.
*
* @param chunk the loaded chunk
*/
public void signalChunkLoad(@NotNull Chunk chunk) {
this.threadProvider.onChunkLoad(chunk);
}
/**
* Signals the {@link ThreadProvider} that a chunk has been unloaded.
*
* WARNING: should be automatically done by the {@link Instance} implementation.
*
* @param chunk the unloaded chunk
*/
public void signalChunkUnload(@NotNull Chunk chunk) {
this.threadProvider.onChunkUnload(chunk);
}
/**
* Adds a callback executed at the start of the next server tick.
*
* The long in the consumer represents the starting time (in ms) of the tick.
*
* @param callback the tick start callback
*/
public void addTickStartCallback(@NotNull LongConsumer callback) {
this.tickStartCallbacks.add(callback);
}
/**
* Removes a tick start callback.
*
* @param callback the callback to remove
*/
public void removeTickStartCallback(@NotNull LongConsumer callback) {
this.tickStartCallbacks.remove(callback);
}
/**
* Adds a callback executed at the end of the next server tick.
*
* The long in the consumer represents the duration (in ms) of the tick.
*
* @param callback the tick end callback
*/
public void addTickEndCallback(@NotNull LongConsumer callback) {
this.tickEndCallbacks.add(callback);
}
/**
* Removes a tick end callback.
*
* @param callback the callback to remove
*/
public void removeTickEndCallback(@NotNull LongConsumer callback) {
this.tickEndCallbacks.remove(callback);
}
public void addTickMonitor(@NotNull Consumer