Add support for World Workers skipping ticks without being removed from the worker list.

This commit is contained in:
LexManos committed 2018-04-03 15:23:35 -07:00
1 parent 272b337aa3
commit 501e030f3e
1 file changed
+23 -5
@@ -25,6 +25,7 @@ public class WorldWorkerManager
{
private static List<IWorker> workers = new ArrayList<IWorker>();
private static long startTime = -1;
private static int index = 0;
public static void tick(boolean start)
{
@@ -34,6 +35,7 @@ public class WorldWorkerManager
return;
}
index = 0;
IWorker task = getNext();
if (task == null)
return;
@@ -43,14 +45,18 @@ public class WorldWorkerManager
time = 10; //If ticks are lagging, give us at least 10ms to do something.
time += System.currentTimeMillis();
while (System.currentTimeMillis() < time)
while (System.currentTimeMillis() < time && task != null)
{
task.work();
boolean again = task.doWork();
if (!task.hasWork())
{
time = 0; //Break loop
remove(task);
task = getNext();
}
else if (!again)
{
task = getNext();
}
}
}
@@ -62,12 +68,13 @@ public class WorldWorkerManager
private static synchronized IWorker getNext()
{
return workers.size() > 0 ? workers.get(0) : null;
return workers.size() > index ? workers.get(index++) : null;
}
private static synchronized void remove(IWorker worker)
{
workers.remove(worker);
index--;
}
//Internal only, used to clear everything when the server shuts down.
@@ -79,6 +86,17 @@ public class WorldWorkerManager
public static interface IWorker
{
boolean hasWork();
void work();
default void work() {}; //TODO: Remove in 1.13.
/**
* Perform a task, returning true from this will have the manager call this function again this tick if there is time left.
* Returning false will skip calling this worker until next tick.
*/
default boolean doWork()
{
work();
return true;
}
}
}