package com.minelittlepony.unicopia; import java.util.List; import java.util.Queue; import java.util.function.Consumer; import java.util.stream.Collectors; import com.google.common.collect.Lists; import com.google.common.collect.Queues; import net.minecraft.world.World; public class AwaitTickQueue { private static final Queue> tickTasks = Queues.newArrayDeque(); private static List delayedTasks = Lists.newArrayList(); private static final Object locker = new Object(); public static void enqueueTask(Consumer task) { synchronized (locker) { tickTasks.add(task); } } public static void scheduleTask(Consumer task, int ticksLater) { synchronized (locker) { delayedTasks.add(new DelayedTask(task, ticksLater)); } } public void tick(World world) { synchronized (locker) { delayedTasks = delayedTasks.stream().filter(DelayedTask::tick).collect(Collectors.toList()); Consumer task; while ((task = tickTasks.poll()) != null) { try { task.accept(world); } catch (Exception e) { UnicopiaCore.LOGGER.error(e); } } } } private static class DelayedTask { final Consumer task; int ticksDelay; DelayedTask(Consumer task, int ticks) { this.task = task; this.ticksDelay = ticks; } boolean tick() { if (ticksDelay-- <= 0) { tickTasks.add(task); return false; } return true; } } }