-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathFirstTickScheduler.java
More file actions
executable file
·52 lines (43 loc) · 1.67 KB
/
FirstTickScheduler.java
File metadata and controls
executable file
·52 lines (43 loc) · 1.67 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gregtech.api.util;
import java.util.Map;
import java.util.Queue;
import com.google.common.collect.Maps;
import com.google.common.collect.Queues;
import gregtech.api.GTValues;
import net.minecraft.world.World;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
@EventBusSubscriber(modid = GTValues.MODID)
public class FirstTickScheduler {
private static Map<World, Queue<FirstTickTask>> tasksByWorld = Maps.newConcurrentMap();
public static void addTask(final FirstTickTask task) {
final World world = task.getWorld();
if (!world.isRemote) {
final Queue<FirstTickTask> tasks = tasksByWorld.computeIfAbsent(world, k -> Queues.newConcurrentLinkedQueue());
tasks.add(task);
} else {
task.handleFirstTick();
}
}
@SubscribeEvent
public static void onWorldUnload(final WorldEvent.Unload event) {
tasksByWorld.remove(event.getWorld());
}
@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void onWorldTick(TickEvent.WorldTickEvent event) {
if (event.phase == TickEvent.Phase.START) {
final Queue<FirstTickTask> tasks = tasksByWorld.get(event.world);
if (tasks == null) {
return;
}
FirstTickTask task = tasks.poll();
while (task != null) {
task.handleFirstTick();
task = tasks.poll();
}
}
}
}