1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-11-15 14:24:55 +00:00

Make advanced modems non-ticking

- Move getPeripheralType and getLabel from IPeripheralTile to
   TilePeripheralBase. These were mostly constant on all other tiles, so
   were rather redundant.
 - Make TileAdvancedModem extend TileGeneric, and be non-ticking (using
   similar logic to all other blocks).
This commit is contained in:
SquidDev 2019-01-20 09:34:15 +00:00
parent e8a4fbb4e3
commit 80a5759bae
8 changed files with 102 additions and 116 deletions

View File

@ -74,6 +74,8 @@ public abstract class BlockPeripheralBase extends BlockDirectional
public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player ) public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player )
{ {
TileEntity tile = world.getTileEntity( pos ); TileEntity tile = world.getTileEntity( pos );
return tile instanceof IPeripheralTile ? PeripheralItemFactory.create( (IPeripheralTile) tile ) : super.getPickBlock( state, target, world, pos, player ); return tile instanceof TilePeripheralBase
? PeripheralItemFactory.create( (TilePeripheralBase) tile )
: super.getPickBlock( state, target, world, pos, player );
} }
} }

View File

@ -7,17 +7,9 @@
package dan200.computercraft.shared.peripheral.common; package dan200.computercraft.shared.peripheral.common;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.peripheral.PeripheralType;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
public interface IPeripheralTile public interface IPeripheralTile
{ {
PeripheralType getPeripheralType();
IPeripheral getPeripheral( EnumFacing side ); IPeripheral getPeripheral( EnumFacing side );
default String getLabel()
{
return null;
}
} }

View File

@ -15,7 +15,7 @@ import javax.annotation.Nonnull;
public class PeripheralItemFactory public class PeripheralItemFactory
{ {
@Nonnull @Nonnull
public static ItemStack create( IPeripheralTile tile ) public static ItemStack create( TilePeripheralBase tile )
{ {
return create( tile.getPeripheralType(), tile.getLabel(), 1 ); return create( tile.getPeripheralType(), tile.getLabel(), 1 );
} }

View File

@ -53,9 +53,6 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe
} }
} }
// IPeripheralTile implementation
@Override
public final PeripheralType getPeripheralType() public final PeripheralType getPeripheralType()
{ {
return getBlock().getPeripheralType( getBlockState() ); return getBlock().getPeripheralType( getBlockState() );
@ -67,7 +64,6 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe
return null; return null;
} }
@Override
public String getLabel() public String getLabel()
{ {
if( m_label != null && m_label.length() > 0 ) if( m_label != null && m_label.length() > 0 )

View File

@ -123,9 +123,9 @@ public class TileCable extends TileGeneric implements IPeripheralTile
if( !m_destroyed ) if( !m_destroyed )
{ {
m_destroyed = true; m_destroyed = true;
m_modem.destroy();
remove(); remove();
} }
super.destroy();
} }
@Override @Override
@ -156,6 +156,7 @@ public class TileCable extends TileGeneric implements IPeripheralTile
@Override @Override
public void updateContainingBlockInfo() public void updateContainingBlockInfo()
{ {
super.updateContainingBlockInfo();
hasModemDirection = false; hasModemDirection = false;
if( !world.isRemote ) world.scheduleUpdate( pos, getBlockType(), 0 ); if( !world.isRemote ) world.scheduleUpdate( pos, getBlockType(), 0 );
} }
@ -223,7 +224,7 @@ public class TileCable extends TileGeneric implements IPeripheralTile
case WiredModemWithCable: case WiredModemWithCable:
{ {
// 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, null, 1 ) );
setBlockState( getBlockState().withProperty( BlockCable.Properties.MODEM, BlockCableModemVariant.None ) ); setBlockState( getBlockState().withProperty( BlockCable.Properties.MODEM, BlockCableModemVariant.None ) );
modemChanged(); modemChanged();
connectionsChanged(); connectionsChanged();
@ -484,7 +485,6 @@ public class TileCable extends TileGeneric implements IPeripheralTile
return !m_destroyed && getPeripheralType() != PeripheralType.Cable && side == getDirection() ? m_modem : null; return !m_destroyed && getPeripheralType() != PeripheralType.Cable && side == getDirection() ? m_modem : null;
} }
@Override
public PeripheralType getPeripheralType() public PeripheralType getPeripheralType()
{ {
IBlockState state = getBlockState(); IBlockState state = getBlockState();

View File

@ -13,7 +13,6 @@ 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.TileGeneric; 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.IPeripheralTile;
import dan200.computercraft.shared.peripheral.modem.ModemState; import dan200.computercraft.shared.peripheral.modem.ModemState;
import dan200.computercraft.shared.util.TickScheduler; import dan200.computercraft.shared.util.TickScheduler;
@ -392,14 +391,6 @@ public class TileWiredModemFull extends TileGeneric implements IPeripheralTile
return super.getCapability( capability, facing ); return super.getCapability( capability, facing );
} }
// IPeripheralTile
@Override
public PeripheralType getPeripheralType()
{
return PeripheralType.WiredModemFull;
}
@Override @Override
public IPeripheral getPeripheral( EnumFacing side ) public IPeripheral getPeripheral( EnumFacing side )
{ {

View File

@ -7,33 +7,37 @@
package dan200.computercraft.shared.peripheral.modem.wireless; package dan200.computercraft.shared.peripheral.modem.wireless;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.common.BlockGeneric;
import dan200.computercraft.shared.peripheral.common.BlockPeripheralBase; import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
import dan200.computercraft.shared.peripheral.modem.ModemBounds; import dan200.computercraft.shared.peripheral.modem.ModemBounds;
import net.minecraft.block.BlockDirectional;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyBool; import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection; import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockFaceShape; import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.AxisAlignedBB;
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;
public class BlockAdvancedModem extends BlockPeripheralBase public class BlockAdvancedModem extends BlockGeneric
{ {
public static class Properties public static class Properties
{ {
public static final PropertyDirection FACING = PropertyDirection.create( "facing" ); public static final PropertyDirection FACING = BlockDirectional.FACING;
public static final PropertyBool ON = PropertyBool.create( "on" ); public static final PropertyBool ON = PropertyBool.create( "on" );
} }
public BlockAdvancedModem() public BlockAdvancedModem()
{ {
super( Material.ROCK );
setHardness( 2.0f ); setHardness( 2.0f );
setTranslationKey( "computercraft:advanced_modem" ); setTranslationKey( "computercraft:advanced_modem" );
setCreativeTab( ComputerCraft.mainCreativeTab ); setCreativeTab( ComputerCraft.mainCreativeTab );
@ -55,17 +59,13 @@ public class BlockAdvancedModem extends BlockPeripheralBase
@Deprecated @Deprecated
public IBlockState getStateFromMeta( int meta ) public IBlockState getStateFromMeta( int meta )
{ {
IBlockState state = getDefaultState(); return getDefaultState().withProperty( Properties.FACING, EnumFacing.byIndex( meta ) );
state = state.withProperty( Properties.FACING, EnumFacing.byIndex( meta ) );
state = state.withProperty( Properties.ON, false );
return state;
} }
@Override @Override
public int getMetaFromState( IBlockState state ) public int getMetaFromState( IBlockState state )
{ {
EnumFacing dir = state.getValue( Properties.FACING ); return state.getValue( Properties.FACING ).getIndex();
return dir.getIndex();
} }
@Nonnull @Nonnull
@ -73,47 +73,26 @@ public class BlockAdvancedModem extends BlockPeripheralBase
@Deprecated @Deprecated
public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess world, BlockPos pos ) public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess world, BlockPos pos )
{ {
int anim;
EnumFacing dir;
TileEntity tile = world.getTileEntity( pos ); TileEntity tile = world.getTileEntity( pos );
if( tile instanceof TilePeripheralBase ) return state.withProperty( Properties.ON, tile instanceof TileAdvancedModem && ((TileAdvancedModem) tile).isOn() );
{
TilePeripheralBase peripheral = (TilePeripheralBase) tile;
anim = peripheral.getAnim();
dir = peripheral.getDirection();
}
else
{
anim = 0;
dir = state.getValue( Properties.FACING );
}
state = state.withProperty( Properties.FACING, dir );
state = state.withProperty( Properties.ON, anim > 0 );
return state;
} }
@Nonnull
@Override @Override
public IBlockState getDefaultBlockState( PeripheralType type, EnumFacing placedSide ) @Deprecated
public IBlockState getStateForPlacement( World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer )
{ {
EnumFacing dir = placedSide.getOpposite(); return getDefaultState().withProperty( Properties.FACING, facing.getOpposite() );
return getDefaultState().withProperty( Properties.FACING, dir );
} }
@Override @Override
public PeripheralType getPeripheralType( int damage ) protected TileGeneric createTile( IBlockState state )
{ {
return PeripheralType.AdvancedModem; return new TileAdvancedModem();
} }
@Override @Override
public PeripheralType getPeripheralType( IBlockState state ) protected TileGeneric createTile( int damage )
{
return PeripheralType.AdvancedModem;
}
@Override
public TilePeripheralBase createTile( PeripheralType type )
{ {
return new TileAdvancedModem(); return new TileAdvancedModem();
} }
@ -140,6 +119,7 @@ public class BlockAdvancedModem extends BlockPeripheralBase
return BlockFaceShape.UNDEFINED; return BlockFaceShape.UNDEFINED;
} }
@Nonnull
@Override @Override
@Deprecated @Deprecated
public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos ) public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos )

View File

@ -6,11 +6,14 @@
package dan200.computercraft.shared.peripheral.modem.wireless; package dan200.computercraft.shared.peripheral.modem.wireless;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.peripheral.common.IPeripheralTile;
import dan200.computercraft.shared.peripheral.modem.ModemPeripheral; 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 net.minecraft.block.state.IBlockState; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
@ -18,54 +21,46 @@ import net.minecraft.world.World;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public class TileAdvancedModem extends TileModemBase public class TileAdvancedModem extends TileGeneric implements IPeripheralTile
{ {
// Statics
private static class Peripheral extends WirelessModemPeripheral private static class Peripheral extends WirelessModemPeripheral
{ {
private TileModemBase m_entity; private TileAdvancedModem tile;
public Peripheral( TileModemBase entity ) Peripheral( TileAdvancedModem entity )
{ {
super( new ModemState(), true ); super( new ModemState( () -> TickScheduler.schedule( entity ) ), true );
m_entity = entity; tile = entity;
} }
@Nonnull @Nonnull
@Override @Override
public World getWorld() public World getWorld()
{ {
return m_entity.getWorld(); return tile.getWorld();
} }
@Nonnull @Nonnull
@Override @Override
public Vec3d getPosition() public Vec3d getPosition()
{ {
BlockPos pos = m_entity.getPos().offset( m_entity.getCachedDirection() ); BlockPos pos = tile.getPos().offset( tile.modemDirection );
return new Vec3d( pos.getX(), pos.getY(), pos.getZ() ); return new Vec3d( pos.getX(), pos.getY(), pos.getZ() );
} }
@Override @Override
public boolean equals( IPeripheral other ) public boolean equals( IPeripheral other )
{ {
if( other instanceof Peripheral ) return this == other;
{
Peripheral otherModem = (Peripheral) other;
return otherModem.m_entity == m_entity;
}
return false;
} }
} }
// Members private boolean hasModemDirection = false;
private boolean m_hasDirection = false; private EnumFacing modemDirection = EnumFacing.DOWN;
private final ModemPeripheral modem = new Peripheral( this );
private boolean destroyed = false;
public TileAdvancedModem() private boolean on = false;
{
m_dir = EnumFacing.DOWN;
}
@Override @Override
public void onLoad() public void onLoad()
@ -75,47 +70,77 @@ public class TileAdvancedModem extends TileModemBase
} }
@Override @Override
public void updateContainingBlockInfo() public void destroy()
{ {
m_hasDirection = false; if( !destroyed )
}
@Override
public void update()
{
super.update();
updateDirection();
}
private void updateDirection()
{
if( !m_hasDirection )
{ {
m_hasDirection = true; modem.destroy();
m_dir = getDirection(); destroyed = true;
} }
} }
@Override @Override
public EnumFacing getDirection() public void updateContainingBlockInfo()
{ {
// Wireless Modem hasModemDirection = false;
IBlockState state = getBlockState(); super.updateContainingBlockInfo();
return state.getValue( BlockAdvancedModem.Properties.FACING ); world.scheduleUpdate( getPos(), ComputerCraft.Blocks.advancedModem, 0 );
} }
@Override @Override
public void setDirection( EnumFacing dir ) public void updateTick()
{ {
// Wireless Modem updateDirection();
setBlockState( getBlockState()
.withProperty( BlockAdvancedModem.Properties.FACING, dir ) if( modem.getModemState().pollChanged() )
); {
boolean newOn = modem.getModemState().isOpen();
if( newOn != on )
{
on = newOn;
updateBlock();
}
}
}
private void updateDirection()
{
if( !hasModemDirection )
{
hasModemDirection = true;
modemDirection = getDirection();
}
}
private EnumFacing getDirection()
{
return getBlockState().getValue( BlockAdvancedModem.Properties.FACING );
} }
@Override @Override
protected ModemPeripheral createPeripheral() protected void writeDescription( @Nonnull NBTTagCompound nbt )
{ {
return new Peripheral( this ); super.writeDescription( nbt );
nbt.setBoolean( "on", on );
}
@Override
public final void readDescription( @Nonnull NBTTagCompound nbt )
{
super.readDescription( nbt );
on = nbt.getBoolean( "on" );
updateBlock();
}
public boolean isOn()
{
return on;
}
@Override
public IPeripheral getPeripheral( EnumFacing side )
{
return !destroyed && side == getDirection() ? modem : null;
} }
} }