1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-21 07:44:49 +00:00

Begin investigations into reducing ticking of TEs

- Move IDirectionalTile constraint from IPeripheralTile to
   TilePeripheralBase.
 - Make *WiredModemFull no longer inherit from *PeripheralBase. While
   there is still some shared logic (namely in the syncing of "anim"),
   it's largely fine as we don't store label or direction in NBT.
 - Add a TickScheduler. This is a thread-safe version of
   World.scheduleUpdate. We simply build a set of all TEs, and schedule
   them to be updated the next tick.
 - Make ModemState receive an "onChanged" listener, which is fired
   whenever the modem changes.
 - Make WiredModemFull no longer tick, instead scheduling updates when
   it is first loaded and whenever the modem changes.
This commit is contained in:
SquidDev
2019-01-19 10:16:41 +00:00
parent 443e0f8f76
commit a8dad23fa3
7 changed files with 154 additions and 56 deletions

View File

@@ -0,0 +1,68 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.util;
import com.google.common.collect.MapMaker;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.common.TileGeneric;
import net.minecraft.block.Block;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
/**
* A thread-safe version of {@link World#scheduleUpdate(BlockPos, Block, int)}.
*
* We use this when modems and other peripherals change a block in a different thread.
*/
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID )
public final class TickScheduler
{
private TickScheduler()
{
}
private static final Set<TileEntity> toTick = Collections.newSetFromMap(
new MapMaker()
.weakKeys()
.makeMap()
);
public static void schedule( TileGeneric tile )
{
World world = tile.getWorld();
if( world != null && !world.isRemote ) toTick.add( tile );
}
@SubscribeEvent
public static void tick( TickEvent.ServerTickEvent event )
{
if( event.phase != TickEvent.Phase.START ) return;
Iterator<TileEntity> iterator = toTick.iterator();
while( iterator.hasNext() )
{
TileEntity tile = iterator.next();
iterator.remove();
World world = tile.getWorld();
BlockPos pos = tile.getPos();
if( world != null && pos != null && world.isBlockLoaded( pos ) && world.getTileEntity( pos ) == tile )
{
world.scheduleUpdate( pos, tile.getBlockType(), 0 );
}
}
}
}