91 lines
2.7 KiB
Java
91 lines
2.7 KiB
Java
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 org.jetbrains.annotations.ApiStatus;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* Energy Ticker
|
|
*/
|
|
@ApiStatus.Internal
|
|
public class EnergyTicker {
|
|
private static final List<EnergyTickable> scheduled = new ArrayList<>();
|
|
private static List<EnergyTickable> allLoaded = Collections.emptyList();
|
|
|
|
/**
|
|
* Get All Loaded Energy Tickers
|
|
*
|
|
* Only Usable In {@link EnergyTickable#logicTick()}
|
|
* @return List Of All Loaded Energy Tickers
|
|
*/
|
|
public static List<EnergyTickable> getAllLoaded() {
|
|
return allLoaded;
|
|
}
|
|
|
|
/**
|
|
* 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<EnergyTickable> started = new ArrayList<>();
|
|
|
|
List<EnergyTickable> temp = new ArrayList<>(scheduled);
|
|
List<EnergyTickable> 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();
|
|
}
|
|
|
|
allLoaded = Collections.unmodifiableList(started);
|
|
|
|
world.getProfiler().visit("shuffle");
|
|
Collections.shuffle(started, world.random);
|
|
world.getProfiler().pop();
|
|
|
|
for (EnergyTickable tickable : started) {
|
|
world.getProfiler().push(() -> tickable.getID() + " logicTick");
|
|
|
|
tickable.logicTick();
|
|
|
|
world.getProfiler().pop();
|
|
}
|
|
|
|
world.getProfiler().pop();
|
|
}
|
|
scheduled.clear();
|
|
|
|
allLoaded = Collections.emptyList();
|
|
}
|
|
}
|