1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-05-11 11:54:11 +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.IBlockAccess;
import net.minecraft.world.World; import net.minecraft.world.World;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Random;
public abstract class BlockGeneric extends Block implements ITileEntityProvider public abstract class BlockGeneric extends Block implements ITileEntityProvider
{ {
@ -138,6 +139,13 @@ public abstract class BlockGeneric extends Block implements ITileEntityProvider
} }
} }
@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 @Override
@Deprecated @Deprecated
public final boolean canProvidePower( IBlockState state ) public final boolean canProvidePower( IBlockState state )

View File

@ -72,6 +72,10 @@ public abstract class TileGeneric extends TileEntity
{ {
} }
protected void updateTick()
{
}
public boolean getRedstoneConnectivity( EnumFacing side ) public boolean getRedstoneConnectivity( EnumFacing side )
{ {
return false; return false;

View File

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

View File

@ -9,11 +9,12 @@ package dan200.computercraft.shared.peripheral.modem.wired;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.ComputerCraftAPI; 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.PeripheralType;
import dan200.computercraft.shared.peripheral.common.BlockPeripheralBase;
import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory; import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
import dan200.computercraft.shared.util.WorldUtil; import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyBool; import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyEnum; import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockFaceShape; import net.minecraft.block.state.BlockFaceShape;
@ -38,7 +39,7 @@ import java.util.ArrayList;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.List; import java.util.List;
public class BlockCable extends BlockPeripheralBase public class BlockCable extends BlockGeneric
{ {
// Statics // Statics
@ -65,6 +66,7 @@ public class BlockCable extends BlockPeripheralBase
public BlockCable() public BlockCable()
{ {
super( Material.ROCK );
setHardness( 1.5f ); setHardness( 1.5f );
setTranslationKey( "computercraft:cable" ); setTranslationKey( "computercraft:cable" );
setCreativeTab( ComputerCraft.mainCreativeTab ); setCreativeTab( ComputerCraft.mainCreativeTab );
@ -141,10 +143,12 @@ public class BlockCable extends BlockPeripheralBase
return meta; return meta;
} }
@Override @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: case Cable:
return getDefaultState() return getDefaultState()
@ -189,7 +193,7 @@ public class BlockCable extends BlockPeripheralBase
.withProperty( Properties.DOWN, doesConnectVisually( state, world, pos, EnumFacing.DOWN ) ); .withProperty( Properties.DOWN, doesConnectVisually( state, world, pos, EnumFacing.DOWN ) );
TileEntity tile = world.getTileEntity( pos ); 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 ); BlockCableModemVariant modem = state.getValue( Properties.MODEM );
if( modem != BlockCableModemVariant.None ) if( modem != BlockCableModemVariant.None )
@ -208,13 +212,6 @@ public class BlockCable extends BlockPeripheralBase
return true; return true;
} }
@Override
public PeripheralType getPeripheralType( int damage )
{
return ComputerCraft.Items.cable.getPeripheralType( damage );
}
@Override
public PeripheralType getPeripheralType( IBlockState state ) public PeripheralType getPeripheralType( IBlockState state )
{ {
boolean cable = state.getValue( Properties.CABLE ); boolean cable = state.getValue( Properties.CABLE );
@ -234,7 +231,13 @@ public class BlockCable extends BlockPeripheralBase
} }
@Override @Override
public TilePeripheralBase createTile( PeripheralType type ) protected TileGeneric createTile( IBlockState state )
{
return new TileCable();
}
@Override
protected TileGeneric createTile( int damage )
{ {
return new TileCable(); return new TileCable();
} }

View File

@ -16,10 +16,8 @@ import net.minecraft.block.state.IBlockState;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess; import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Random;
public class BlockWiredModemFull extends BlockGeneric public class BlockWiredModemFull extends BlockGeneric
{ {
@ -79,13 +77,6 @@ public class BlockWiredModemFull extends BlockGeneric
return state; 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 @Override
protected TileGeneric createTile( IBlockState state ) protected TileGeneric createTile( IBlockState state )
{ {

View File

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

View File

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

View File

@ -264,6 +264,7 @@ public class TileWiredModemFull extends TileGeneric implements IPeripheralTile
if( !world.isRemote ) world.scheduleUpdate( pos, getBlockType(), 0 ); if( !world.isRemote ) world.scheduleUpdate( pos, getBlockType(), 0 );
} }
@Override
protected void updateTick() protected void updateTick()
{ {
if( !getWorld().isRemote ) if( !getWorld().isRemote )