Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer Redstone Cache Initialisation to the start of the world tick #1648

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import gregtech.api.block.machines.BlockMachine;
import gregtech.api.cover.CoverBehavior;
import gregtech.api.gui.IUIHolder;
import gregtech.api.util.FirstTickScheduler;
import gregtech.api.util.FirstTickTask;
import gregtech.api.util.GTControlledRegistry;
import gregtech.api.util.GTLog;
import net.minecraft.block.state.IBlockState;
Expand All @@ -25,7 +27,7 @@
import java.util.List;
import java.util.stream.Collectors;

public class MetaTileEntityHolder extends TickableTileEntityBase implements IUIHolder {
public class MetaTileEntityHolder extends TickableTileEntityBase implements IUIHolder, FirstTickTask {

private MetaTileEntity metaTileEntity;
private boolean needToUpdateLightning = false;
Expand Down Expand Up @@ -215,8 +217,15 @@ public void markAsDirty() {
@Override
public void onLoad() {
super.onLoad();
if (metaTileEntity != null) {
metaTileEntity.onLoad();
if (this.metaTileEntity != null) {
FirstTickScheduler.addTask(this);
}
}

@Override
public void handleFirstTick() {
if (this.metaTileEntity != null) {
this.metaTileEntity.onLoad();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import gregtech.api.pipenet.WorldPipeNet;
import gregtech.api.pipenet.block.BlockPipe;
import gregtech.api.pipenet.block.IPipeType;
import gregtech.api.util.FirstTickScheduler;
import gregtech.api.util.FirstTickTask;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
Expand All @@ -23,7 +25,7 @@
import javax.annotation.Nullable;
import java.util.function.Consumer;

public abstract class TileEntityPipeBase<PipeType extends Enum<PipeType> & IPipeType<NodeDataType>, NodeDataType> extends SyncedTileEntityBase implements IPipeTile<PipeType, NodeDataType> {
public abstract class TileEntityPipeBase<PipeType extends Enum<PipeType> & IPipeType<NodeDataType>, NodeDataType> extends SyncedTileEntityBase implements IPipeTile<PipeType, NodeDataType>, FirstTickTask {

private TIntIntMap blockedConnectionsMap = new TIntIntHashMap();
private int blockedConnections = 0;
Expand Down Expand Up @@ -283,6 +285,11 @@ public void readFromNBT(NBTTagCompound compound) {
@Override
public void onLoad() {
super.onLoad();
FirstTickScheduler.addTask(this);
}

@Override
public void handleFirstTick() {
this.coverableImplementation.onLoad();
}

Expand Down
52 changes: 52 additions & 0 deletions src/main/java/gregtech/api/util/FirstTickScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/gregtech/api/util/FirstTickTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gregtech.api.util;

import net.minecraft.world.World;

public interface FirstTickTask {

void handleFirstTick();

World getWorld();
}