1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00

Make TileCable non-ticking

- Move updateTick onto BlockGeneric/TileGeneric instead of the full
   wired modem, as it is used by several tiles now.
 - Make *Cable extend from *Generic, and schedule ticks instead of
   running every tick.
This commit is contained in:
SquidDev 2019-01-19 22:12:47 +00:00
parent a8dad23fa3
commit e8a4fbb4e3
8 changed files with 137 additions and 116 deletions

View File

@ -21,6 +21,7 @@
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import java.util.Random;
public abstract class BlockGeneric extends Block implements ITileEntityProvider
{
@ -138,6 +139,13 @@ public final void onNeighborChange( IBlockAccess world, BlockPos pos, BlockPos n
}
}
@Override
public void updateTick( World world, BlockPos pos, IBlockState state, Random rand )
{
TileEntity te = world.getTileEntity( pos );
if( te instanceof TileGeneric ) ((TileGeneric) te).updateTick();
}
@Override
@Deprecated
public final boolean canProvidePower( IBlockState state )

View File

@ -72,6 +72,10 @@ public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour )
{
}
protected void updateTick()
{
}
public boolean getRedstoneConnectivity( EnumFacing side )
{
return false;

View File

@ -34,7 +34,7 @@ public static ItemStack create( PeripheralType type, String label, int quantity
return ComputerCraft.Items.peripheral.create( type, label, quantity );
case WiredModem:
case Cable:
return ComputerCraft.Items.cable.create( type, label, quantity );
return ComputerCraft.Items.cable.create( type, quantity );
case AdvancedModem:
return new ItemStack( ComputerCraft.Blocks.advancedModem, quantity );
case WiredModemFull:

View File

@ -9,11 +9,12 @@
import com.google.common.collect.ImmutableMap;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.shared.common.BlockGeneric;
import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.common.BlockPeripheralBase;
import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockFaceShape;
@ -38,7 +39,7 @@
import java.util.EnumMap;
import java.util.List;
public class BlockCable extends BlockPeripheralBase
public class BlockCable extends BlockGeneric
{
// Statics
@ -65,6 +66,7 @@ public static class Properties
public BlockCable()
{
super( Material.ROCK );
setHardness( 1.5f );
setTranslationKey( "computercraft:cable" );
setCreativeTab( ComputerCraft.mainCreativeTab );
@ -141,10 +143,12 @@ else if( cable )
return meta;
}
@Override
public IBlockState getDefaultBlockState( PeripheralType type, EnumFacing placedSide )
@Deprecated
public final IBlockState getStateForPlacement( World world, BlockPos pos, EnumFacing placedSide, float hitX, float hitY, float hitZ, int damage, EntityLivingBase placer )
{
switch( type )
switch( ComputerCraft.Items.cable.getPeripheralType( damage ) )
{
case Cable:
return getDefaultState()
@ -189,7 +193,7 @@ public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess worl
.withProperty( Properties.DOWN, doesConnectVisually( state, world, pos, EnumFacing.DOWN ) );
TileEntity tile = world.getTileEntity( pos );
int anim = tile instanceof TilePeripheralBase ? ((TilePeripheralBase) tile).getAnim() : 0;
int anim = tile instanceof TileCable ? ((TileCable) tile).getState() : 0;
BlockCableModemVariant modem = state.getValue( Properties.MODEM );
if( modem != BlockCableModemVariant.None )
@ -208,13 +212,6 @@ public boolean shouldSideBeRendered( IBlockState state, @Nonnull IBlockAccess wo
return true;
}
@Override
public PeripheralType getPeripheralType( int damage )
{
return ComputerCraft.Items.cable.getPeripheralType( damage );
}
@Override
public PeripheralType getPeripheralType( IBlockState state )
{
boolean cable = state.getValue( Properties.CABLE );
@ -234,7 +231,13 @@ else if( modem != BlockCableModemVariant.None )
}
@Override
public TilePeripheralBase createTile( PeripheralType type )
protected TileGeneric createTile( IBlockState state )
{
return new TileCable();
}
@Override
protected TileGeneric createTile( int damage )
{
return new TileCable();
}

View File

@ -16,10 +16,8 @@
import net.minecraft.tileentity.TileEntity;
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 BlockGeneric
{
@ -79,13 +77,6 @@ public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess worl
return state;
}
@Override
public void updateTick( World world, BlockPos pos, IBlockState state, Random rand )
{
TileEntity te = world.getTileEntity( pos );
if( te instanceof TileWiredModemFull ) ((TileWiredModemFull) te).updateTick();
}
@Override
protected TileGeneric createTile( IBlockState state )
{

View File

@ -34,7 +34,7 @@ public ItemCable( Block block )
}
@Nonnull
public ItemStack create( PeripheralType type, String label, int quantity )
public ItemStack create( PeripheralType type, int quantity )
{
ItemStack stack;
switch( type )
@ -54,10 +54,7 @@ public ItemStack create( PeripheralType type, String label, int quantity )
return ItemStack.EMPTY;
}
}
if( label != null )
{
stack.setStackDisplayName( label );
}
return stack;
}
@ -81,11 +78,11 @@ public EnumActionResult onItemUse( @Nonnull EntityPlayer player, World world, @N
// Try to add a cable to a modem
PeripheralType type = getPeripheralType( stack );
Block existing = world.getBlockState( pos ).getBlock();
IBlockState existingState = world.getBlockState( pos );
Block existing = existingState.getBlock();
if( existing == ComputerCraft.Blocks.cable )
{
PeripheralType existingType = ComputerCraft.Blocks.cable.getPeripheralType( world, pos );
PeripheralType existingType = ComputerCraft.Blocks.cable.getPeripheralType( existingState );
if( existingType == PeripheralType.WiredModem && type == PeripheralType.Cable )
{
if( !stack.isEmpty() )
@ -112,12 +109,12 @@ public EnumActionResult onItemUse( @Nonnull EntityPlayer player, World world, @N
if( !existing.isAir( existingState, world, pos ) && (type == PeripheralType.Cable || existingState.isSideSolid( world, pos, side )) )
{
BlockPos offset = pos.offset( side );
Block offsetExisting = world.getBlockState( offset ).getBlock();
IBlockState offsetExistingState = world.getBlockState( offset );
Block offsetExisting = offsetExistingState.getBlock();
if( offsetExisting == ComputerCraft.Blocks.cable )
{
// Try to add a modem to a cable
PeripheralType offsetExistingType = ComputerCraft.Blocks.cable.getPeripheralType( world, offset );
PeripheralType offsetExistingType = ComputerCraft.Blocks.cable.getPeripheralType( offsetExistingState );
if( offsetExistingType == PeripheralType.Cable && type == PeripheralType.WiredModem )
{
if( !stack.isEmpty() )

View File

@ -7,17 +7,19 @@
package dan200.computercraft.shared.peripheral.modem.wired;
import com.google.common.base.Objects;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.ComputerCraftAPI;
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.common.BlockGeneric;
import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.common.IPeripheralTile;
import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;
import dan200.computercraft.shared.peripheral.modem.ModemPeripheral;
import dan200.computercraft.shared.peripheral.modem.ModemState;
import dan200.computercraft.shared.peripheral.modem.TileModemBase;
import dan200.computercraft.shared.util.TickScheduler;
import dan200.computercraft.shared.wired.CapabilityWiredElement;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
@ -37,42 +39,35 @@
import java.util.Collections;
import java.util.Map;
public class TileCable extends TileModemBase
public class TileCable extends TileGeneric implements IPeripheralTile
{
private static class CableElement extends WiredModemElement
private class CableElement extends WiredModemElement
{
private final TileCable m_entity;
private CableElement( TileCable m_entity )
{
this.m_entity = m_entity;
}
@Nonnull
@Override
public World getWorld()
{
return m_entity.getWorld();
return TileCable.this.getWorld();
}
@Nonnull
@Override
public Vec3d getPosition()
{
BlockPos pos = m_entity.getPos();
BlockPos pos = TileCable.this.getPos();
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
}
@Override
protected void attachPeripheral( String name, IPeripheral peripheral )
{
((WiredModemPeripheral) m_entity.m_modem).attachPeripheral( name, peripheral );
m_modem.attachPeripheral( name, peripheral );
}
@Override
protected void detachPeripheral( String name )
{
((WiredModemPeripheral) m_entity.m_modem).detachPeripheral( name );
m_modem.detachPeripheral( name );
}
}
@ -83,35 +78,35 @@ protected void detachPeripheral( String name )
private boolean m_destroyed = false;
private boolean m_hasDirection = false;
private EnumFacing modemDirection;
private boolean hasModemDirection = false;
private boolean m_connectionsFormed = false;
private WiredModemElement m_cable;
private IWiredNode m_node;
@Override
protected ModemPeripheral createPeripheral()
private final WiredModemElement m_cable = new CableElement();
private final IWiredNode m_node = m_cable.getNode();
private final WiredModemPeripheral m_modem = new WiredModemPeripheral(
new ModemState( () -> TickScheduler.schedule( this ) ),
m_cable
)
{
m_cable = new CableElement( this );
m_node = m_cable.getNode();
return new WiredModemPeripheral( new ModemState(), m_cable )
@Nonnull
@Override
protected WiredModemLocalPeripheral getLocalPeripheral()
{
@Nonnull
@Override
protected WiredModemLocalPeripheral getLocalPeripheral()
{
return m_peripheral;
}
return m_peripheral;
}
@Nonnull
@Override
public Vec3d getPosition()
{
BlockPos pos = getPos().offset( getCachedDirection() );
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
}
};
}
@Nonnull
@Override
public Vec3d getPosition()
{
BlockPos pos = getPos().offset( modemDirection );
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
}
};
private int m_state = 0;
private void remove()
{
@ -151,43 +146,36 @@ public void invalidate()
public void onLoad()
{
super.onLoad();
updateDirection();
if( !world.isRemote )
{
updateDirection();
world.scheduleUpdate( pos, getBlockType(), 0 );
}
}
@Override
public void updateContainingBlockInfo()
{
m_hasDirection = false;
hasModemDirection = false;
if( !world.isRemote ) world.scheduleUpdate( pos, getBlockType(), 0 );
}
private void updateDirection()
{
if( !m_hasDirection )
if( !hasModemDirection )
{
m_hasDirection = true;
m_dir = getDirection();
hasModemDirection = true;
modemDirection = getDirection();
}
}
@Override
public EnumFacing getDirection()
private EnumFacing getDirection()
{
IBlockState state = getBlockState();
EnumFacing facing = state.getValue( BlockCable.Properties.MODEM ).getFacing();
return facing != null ? facing : EnumFacing.NORTH;
}
@Override
public void setDirection( EnumFacing dir )
{
IBlockState state = getBlockState();
BlockCableModemVariant modem = state.getValue( BlockCable.Properties.MODEM );
if( modem != BlockCableModemVariant.None )
{
setBlockState( state.withProperty( BlockCable.Properties.MODEM, BlockCableModemVariant.fromFacing( dir ) ) );
}
}
@Override
public void getDroppedItems( @Nonnull NonNullList<ItemStack> drops, boolean creative )
{
@ -199,12 +187,12 @@ public void getDroppedItems( @Nonnull NonNullList<ItemStack> drops, boolean crea
case Cable:
case WiredModem:
{
drops.add( PeripheralItemFactory.create( type, getLabel(), 1 ) );
drops.add( PeripheralItemFactory.create( type, null, 1 ) );
break;
}
case WiredModemWithCable:
{
drops.add( PeripheralItemFactory.create( PeripheralType.WiredModem, getLabel(), 1 ) );
drops.add( PeripheralItemFactory.create( PeripheralType.WiredModem, null, 1 ) );
drops.add( PeripheralItemFactory.create( PeripheralType.Cable, null, 1 ) );
break;
}
@ -236,7 +224,6 @@ public void onNeighbourChange()
{
// Drop the modem and convert to cable
((BlockGeneric) getBlockType()).dropItem( getWorld(), getPos(), PeripheralItemFactory.create( PeripheralType.WiredModem, getLabel(), 1 ) );
setLabel( null );
setBlockState( getBlockState().withProperty( BlockCable.Properties.MODEM, BlockCableModemVariant.None ) );
modemChanged();
connectionsChanged();
@ -328,21 +315,46 @@ public NBTTagCompound writeToNBT( NBTTagCompound nbt )
}
@Override
protected void updateAnim()
protected void writeDescription( @Nonnull NBTTagCompound nbt )
{
int anim = 0;
if( m_modem.getModemState().isOpen() ) anim |= 1;
if( m_peripheralAccessAllowed ) anim |= 2;
setAnim( anim );
super.writeDescription( nbt );
nbt.setInteger( "state", m_state );
}
@Override
public void update()
public final void readDescription( @Nonnull NBTTagCompound nbt )
{
super.readDescription( nbt );
m_state = nbt.getInteger( "state" );
updateBlock();
}
public int getState()
{
return m_state;
}
private void updateState()
{
int state = 0;
if( m_modem.getModemState().isOpen() ) state |= 1;
if( m_peripheralAccessAllowed ) state |= 2;
if( state != m_state )
{
m_state = state;
updateBlock();
}
}
@Override
protected void updateTick()
{
super.update();
updateDirection();
if( !getWorld().isRemote )
{
updateDirection();
if( m_modem.getModemState().pollChanged() ) updateState();
if( !m_connectionsFormed )
{
m_connectionsFormed = true;
@ -350,7 +362,7 @@ public void update()
connectionsChanged();
if( m_peripheralAccessAllowed )
{
m_peripheral.attach( world, pos, m_dir );
m_peripheral.attach( world, pos, modemDirection );
updateConnectedPeripherals();
}
}
@ -396,7 +408,7 @@ public void modemChanged()
m_peripheral.detach();
m_node.updatePeripherals( Collections.emptyMap() );
markDirty();
updateAnim();
updateState();
}
}
@ -419,7 +431,7 @@ private void togglePeripheralAccess()
m_node.updatePeripherals( Collections.emptyMap() );
}
updateAnim();
updateState();
}
private void updateConnectedPeripherals()
@ -429,7 +441,7 @@ private void updateConnectedPeripherals()
{
// If there are no peripherals then disable access and update the display state.
m_peripheralAccessAllowed = false;
updateAnim();
updateState();
}
m_node.updatePeripherals( peripherals );
@ -441,12 +453,14 @@ public boolean canRenderBreaking()
return true;
}
// IWiredElement capability
@Override
public boolean hasCapability( @Nonnull Capability<?> capability, @Nullable EnumFacing facing )
{
if( capability == CapabilityWiredElement.CAPABILITY ) return BlockCable.canConnectIn( getBlockState(), facing );
if( capability == CapabilityWiredElement.CAPABILITY )
{
return !m_destroyed && BlockCable.canConnectIn( getBlockState(), facing );
}
return super.hasCapability( capability, facing );
}
@ -456,21 +470,24 @@ public <T> T getCapability( @Nonnull Capability<T> capability, @Nullable EnumFac
{
if( capability == CapabilityWiredElement.CAPABILITY )
{
return BlockCable.canConnectIn( getBlockState(), facing ) ? CapabilityWiredElement.CAPABILITY.cast( m_cable ) : null;
return !m_destroyed && BlockCable.canConnectIn( getBlockState(), facing )
? CapabilityWiredElement.CAPABILITY.cast( m_cable )
: null;
}
return super.getCapability( capability, facing );
}
// IPeripheralTile
@Override
public IPeripheral getPeripheral( EnumFacing side )
{
if( getPeripheralType() != PeripheralType.Cable )
{
return super.getPeripheral( side );
}
return null;
return !m_destroyed && getPeripheralType() != PeripheralType.Cable && side == getDirection() ? m_modem : null;
}
@Override
public PeripheralType getPeripheralType()
{
IBlockState state = getBlockState();
return ComputerCraft.Blocks.cable.getPeripheralType( state );
}
}

View File

@ -264,6 +264,7 @@ public void onLoad()
if( !world.isRemote ) world.scheduleUpdate( pos, getBlockType(), 0 );
}
@Override
protected void updateTick()
{
if( !getWorld().isRemote )