-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTaskScheduler.java
More file actions
37 lines (31 loc) · 1.26 KB
/
TaskScheduler.java
File metadata and controls
37 lines (31 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package gregtech.api.util;
import gregtech.api.util.function.Task;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;
import java.util.*;
public class TaskScheduler {
private static Map<World, List<Task>> tasksPerWorld = new HashMap<>();
public static void scheduleTask(World world, Task task) {
if(!world.isClient) {
throw new IllegalArgumentException("Attempt to schedule task on client world!");
}
List<Task> taskList = tasksPerWorld.computeIfAbsent(world, k -> new ArrayList<>());
taskList.add(task);
}
static {
ServerWorldEvents.UNLOAD.register((MinecraftServer server, ServerWorld world)->{
if(world.isClient()) {
tasksPerWorld.remove(world);
}
});
ServerTickEvents.START_WORLD_TICK.register((ServerWorld world) -> {
if(world.isClient()) {
List<Task> taskList = tasksPerWorld.getOrDefault(world, Collections.emptyList());
taskList.removeIf(task -> !task.run());
}
});
}
}