package com.thebrokenrail.energonrelics.api.energy.tick; import com.thebrokenrail.energonrelics.EnergonRelics; import com.thebrokenrail.energonrelics.component.NetworkComponent; import net.minecraft.server.world.ServerWorld; import net.minecraft.world.World; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; /** * Energy Ticker */ public class EnergyTicker { private static final List scheduled = new ArrayList<>(); /** * Schedule For Next Energy Tick * @param tickable Object To Tick */ public static void schedule(EnergyTickable tickable) { scheduled.add(tickable); } /** * Tick Energy * @param world World */ public static void tick(World world) { if (Objects.requireNonNull(world.getServer()).getThread() == Thread.currentThread()) { world.getProfiler().push(EnergonRelics.NAMESPACE); NetworkComponent.getInstance((ServerWorld) world).clearCache(); List started = new ArrayList<>(); List temp = new ArrayList<>(scheduled); List temp2 = new ArrayList<>(); while (!temp.isEmpty()) { for (EnergyTickable tickable : temp) { if (!started.contains(tickable)) { world.getProfiler().push(() -> tickable.getID() + " startTick"); temp2.addAll(tickable.startTick()); started.add(tickable); world.getProfiler().pop(); } } temp.clear(); temp.addAll(temp2); temp2.clear(); } Collections.shuffle(started, world.random); for (EnergyTickable tickable : started) { world.getProfiler().push(() -> tickable.getID() + " logicTick"); tickable.logicTick(); world.getProfiler().pop(); } world.getProfiler().pop(); } scheduled.clear(); } }