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> SCHEDULED_TASKS = Queues.newArrayDeque(); private static List DELAYED_TASKS = Lists.newArrayList(); private static final Object LOCKER = new Object(); public static void enqueueTask(Consumer task) { synchronized (LOCKER) { SCHEDULED_TASKS.add(task); } } public static void scheduleTask(Consumer task, int ticksLater) { synchronized (LOCKER) { DELAYED_TASKS.add(new DelayedTask(task, ticksLater)); } } static void tick(World world) { if (world.isClient) { return; } synchronized (LOCKER) { DELAYED_TASKS = DELAYED_TASKS.stream().filter(DelayedTask::tick).collect(Collectors.toList()); Consumer task; while ((task = SCHEDULED_TASKS.poll()) != null) { try { task.accept(world); } catch (Exception e) { Unicopia.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) { SCHEDULED_TASKS.add(task); return false; } return true; } } }