mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-02-10 16:10:05 +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:
parent
443e0f8f76
commit
a8dad23fa3
@ -8,19 +8,15 @@ package dan200.computercraft.shared.computer.blocks;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.util.DirectionUtil;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.properties.PropertyDirection;
|
||||
import net.minecraft.block.properties.PropertyEnum;
|
||||
import net.minecraft.block.state.BlockStateContainer;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
|
@ -7,15 +7,17 @@
|
||||
package dan200.computercraft.shared.peripheral.common;
|
||||
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.shared.common.IDirectionalTile;
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
|
||||
public interface IPeripheralTile extends IDirectionalTile
|
||||
public interface IPeripheralTile
|
||||
{
|
||||
PeripheralType getPeripheralType();
|
||||
|
||||
IPeripheral getPeripheral( EnumFacing side );
|
||||
|
||||
String getLabel();
|
||||
default String getLabel()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
package dan200.computercraft.shared.peripheral.common;
|
||||
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.shared.common.IDirectionalTile;
|
||||
import dan200.computercraft.shared.common.TileGeneric;
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@ -17,8 +18,7 @@ import net.minecraft.util.NonNullList;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public abstract class TilePeripheralBase extends TileGeneric
|
||||
implements IPeripheralTile, ITickable
|
||||
public abstract class TilePeripheralBase extends TileGeneric implements IPeripheralTile, ITickable, IDirectionalTile
|
||||
{
|
||||
// Statics
|
||||
|
||||
|
@ -14,16 +14,27 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class ModemState
|
||||
{
|
||||
private boolean open = false;
|
||||
private AtomicBoolean changed = new AtomicBoolean( true );
|
||||
private final Runnable onChanged;
|
||||
private final AtomicBoolean changed = new AtomicBoolean( true );
|
||||
|
||||
private boolean open = false;
|
||||
private final IntSet channels = new IntOpenHashSet();
|
||||
|
||||
public ModemState()
|
||||
{
|
||||
this.onChanged = null;
|
||||
}
|
||||
|
||||
public ModemState( Runnable onChanged )
|
||||
{
|
||||
this.onChanged = onChanged;
|
||||
}
|
||||
|
||||
private void setOpen( boolean open )
|
||||
{
|
||||
if( this.open == open ) return;
|
||||
this.open = open;
|
||||
this.changed.set( true );
|
||||
if( !changed.getAndSet( true ) && onChanged != null ) onChanged.run();
|
||||
}
|
||||
|
||||
public boolean pollChanged()
|
||||
|
@ -7,20 +7,21 @@
|
||||
package dan200.computercraft.shared.peripheral.modem.wired;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import dan200.computercraft.shared.peripheral.common.BlockPeripheralBase;
|
||||
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
|
||||
import dan200.computercraft.shared.common.BlockGeneric;
|
||||
import dan200.computercraft.shared.common.TileGeneric;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.properties.PropertyBool;
|
||||
import net.minecraft.block.state.BlockStateContainer;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Random;
|
||||
|
||||
public class BlockWiredModemFull extends BlockPeripheralBase
|
||||
public class BlockWiredModemFull extends BlockGeneric
|
||||
{
|
||||
// Statics
|
||||
|
||||
@ -34,6 +35,7 @@ public class BlockWiredModemFull extends BlockPeripheralBase
|
||||
|
||||
public BlockWiredModemFull()
|
||||
{
|
||||
super( Material.ROCK );
|
||||
setHardness( 1.5f );
|
||||
setTranslationKey( "computercraft:wired_modem_full" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
@ -43,12 +45,6 @@ public class BlockWiredModemFull extends BlockPeripheralBase
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IBlockState getDefaultBlockState( PeripheralType type, EnumFacing placedSide )
|
||||
{
|
||||
return getDefaultState();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
protected BlockStateContainer createBlockState()
|
||||
@ -74,7 +70,7 @@ public class BlockWiredModemFull extends BlockPeripheralBase
|
||||
if( te instanceof TileWiredModemFull )
|
||||
{
|
||||
TileWiredModemFull modem = (TileWiredModemFull) te;
|
||||
int anim = modem.getAnim();
|
||||
int anim = modem.getState();
|
||||
state = state
|
||||
.withProperty( Properties.MODEM_ON, (anim & 1) != 0 )
|
||||
.withProperty( Properties.PERIPHERAL_ON, (anim & 2) != 0 );
|
||||
@ -84,19 +80,20 @@ public class BlockWiredModemFull extends BlockPeripheralBase
|
||||
}
|
||||
|
||||
@Override
|
||||
public PeripheralType getPeripheralType( int damage )
|
||||
public void updateTick( World world, BlockPos pos, IBlockState state, Random rand )
|
||||
{
|
||||
return PeripheralType.WiredModemFull;
|
||||
TileEntity te = world.getTileEntity( pos );
|
||||
if( te instanceof TileWiredModemFull ) ((TileWiredModemFull) te).updateTick();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PeripheralType getPeripheralType( IBlockState state )
|
||||
protected TileGeneric createTile( IBlockState state )
|
||||
{
|
||||
return PeripheralType.WiredModemFull;
|
||||
return new TileWiredModemFull();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TilePeripheralBase createTile( PeripheralType type )
|
||||
protected TileGeneric createTile( int damage )
|
||||
{
|
||||
return new TileWiredModemFull();
|
||||
}
|
||||
|
@ -12,8 +12,11 @@ import dan200.computercraft.api.network.wired.IWiredElement;
|
||||
import dan200.computercraft.api.network.wired.IWiredNode;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.shared.command.CommandCopy;
|
||||
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
|
||||
import dan200.computercraft.shared.common.TileGeneric;
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import dan200.computercraft.shared.peripheral.common.IPeripheralTile;
|
||||
import dan200.computercraft.shared.peripheral.modem.ModemState;
|
||||
import dan200.computercraft.shared.util.TickScheduler;
|
||||
import dan200.computercraft.shared.wired.CapabilityWiredElement;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
@ -30,7 +33,7 @@ import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
|
||||
public class TileWiredModemFull extends TilePeripheralBase
|
||||
public class TileWiredModemFull extends TileGeneric implements IPeripheralTile
|
||||
{
|
||||
private static class FullElement extends WiredModemElement
|
||||
{
|
||||
@ -85,10 +88,12 @@ public class TileWiredModemFull extends TilePeripheralBase
|
||||
private boolean m_destroyed = false;
|
||||
private boolean m_connectionsFormed = false;
|
||||
|
||||
private final ModemState m_modemState = new ModemState();
|
||||
private final ModemState m_modemState = new ModemState( () -> TickScheduler.schedule( this ) );
|
||||
private final WiredModemElement m_element = new FullElement( this );
|
||||
private final IWiredNode m_node = m_element.getNode();
|
||||
|
||||
private int m_state = 0;
|
||||
|
||||
public TileWiredModemFull()
|
||||
{
|
||||
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i] = new WiredModemLocalPeripheral();
|
||||
@ -128,17 +133,6 @@ public class TileWiredModemFull extends TilePeripheralBase
|
||||
remove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumFacing getDirection()
|
||||
{
|
||||
return EnumFacing.NORTH;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDirection( EnumFacing dir )
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNeighbourChange()
|
||||
{
|
||||
@ -231,27 +225,50 @@ public class TileWiredModemFull extends TilePeripheralBase
|
||||
return nbt;
|
||||
}
|
||||
|
||||
protected void updateAnim()
|
||||
public int getState()
|
||||
{
|
||||
int anim = 0;
|
||||
if( m_modemState.isOpen() ) anim |= 1;
|
||||
if( m_peripheralAccessAllowed ) anim |= 2;
|
||||
setAnim( anim );
|
||||
return m_state;
|
||||
}
|
||||
|
||||
private void updateState()
|
||||
{
|
||||
int state = 0;
|
||||
if( m_modemState.isOpen() ) state |= 1;
|
||||
if( m_peripheralAccessAllowed ) state |= 2;
|
||||
if( state != m_state )
|
||||
{
|
||||
m_state = state;
|
||||
updateBlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeDescription( @Nonnull NBTTagCompound nbt )
|
||||
{
|
||||
super.writeDescription( nbt );
|
||||
nbt.setInteger( "state", m_state );
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void readDescription( @Nonnull NBTTagCompound nbt )
|
||||
{
|
||||
super.readDescription( nbt );
|
||||
m_state = nbt.getInteger( "state" );
|
||||
updateBlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update()
|
||||
public void onLoad()
|
||||
{
|
||||
super.onLoad();
|
||||
if( !world.isRemote ) world.scheduleUpdate( pos, getBlockType(), 0 );
|
||||
}
|
||||
|
||||
protected void updateTick()
|
||||
{
|
||||
if( !getWorld().isRemote )
|
||||
{
|
||||
if( m_modemState.pollChanged() ) updateAnim();
|
||||
if( m_modemState.pollChanged() ) updateState();
|
||||
|
||||
if( !m_connectionsFormed )
|
||||
{
|
||||
@ -268,8 +285,6 @@ public class TileWiredModemFull extends TilePeripheralBase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super.update();
|
||||
}
|
||||
|
||||
private void connectionsChanged()
|
||||
@ -291,7 +306,6 @@ public class TileWiredModemFull extends TilePeripheralBase
|
||||
}
|
||||
}
|
||||
|
||||
// private stuff
|
||||
private void togglePeripheralAccess()
|
||||
{
|
||||
if( !m_peripheralAccessAllowed )
|
||||
@ -317,7 +331,7 @@ public class TileWiredModemFull extends TilePeripheralBase
|
||||
m_node.updatePeripherals( Collections.emptyMap() );
|
||||
}
|
||||
|
||||
updateAnim();
|
||||
updateState();
|
||||
}
|
||||
|
||||
private Set<String> getConnectedPeripheralNames()
|
||||
@ -349,7 +363,7 @@ public class TileWiredModemFull extends TilePeripheralBase
|
||||
{
|
||||
// If there are no peripherals then disable access and update the display state.
|
||||
m_peripheralAccessAllowed = false;
|
||||
updateAnim();
|
||||
updateState();
|
||||
}
|
||||
|
||||
m_node.updatePeripherals( peripherals );
|
||||
@ -360,7 +374,8 @@ public class TileWiredModemFull extends TilePeripheralBase
|
||||
@Override
|
||||
public boolean hasCapability( @Nonnull Capability<?> capability, @Nullable EnumFacing facing )
|
||||
{
|
||||
return capability == CapabilityWiredElement.CAPABILITY || super.hasCapability( capability, facing );
|
||||
if( capability == CapabilityWiredElement.CAPABILITY ) return !m_destroyed;
|
||||
return super.hasCapability( capability, facing );
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@ -369,6 +384,7 @@ public class TileWiredModemFull extends TilePeripheralBase
|
||||
{
|
||||
if( capability == CapabilityWiredElement.CAPABILITY )
|
||||
{
|
||||
if( m_destroyed ) return null;
|
||||
return CapabilityWiredElement.CAPABILITY.cast( m_element );
|
||||
}
|
||||
|
||||
@ -377,9 +393,17 @@ public class TileWiredModemFull extends TilePeripheralBase
|
||||
|
||||
// IPeripheralTile
|
||||
|
||||
@Override
|
||||
public PeripheralType getPeripheralType()
|
||||
{
|
||||
return PeripheralType.WiredModemFull;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPeripheral getPeripheral( EnumFacing side )
|
||||
{
|
||||
if( m_destroyed ) return null;
|
||||
|
||||
WiredModemPeripheral peripheral = m_modems[side.ordinal()];
|
||||
if( peripheral == null )
|
||||
{
|
||||
|
@ -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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user