From 80a5759bae61e6e4d9c78cfddaf2e24bce273392 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sun, 20 Jan 2019 09:34:15 +0000 Subject: [PATCH] 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). --- .../common/BlockPeripheralBase.java | 4 +- .../peripheral/common/IPeripheralTile.java | 8 -- .../common/PeripheralItemFactory.java | 2 +- .../peripheral/common/TilePeripheralBase.java | 4 - .../peripheral/modem/wired/TileCable.java | 6 +- .../modem/wired/TileWiredModemFull.java | 9 -- .../modem/wireless/BlockAdvancedModem.java | 60 +++------ .../modem/wireless/TileAdvancedModem.java | 125 +++++++++++------- 8 files changed, 102 insertions(+), 116 deletions(-) diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java index a46bbecab..bd46859e8 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java @@ -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 ) { 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 ); } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/IPeripheralTile.java b/src/main/java/dan200/computercraft/shared/peripheral/common/IPeripheralTile.java index 02af7c4b2..364f3f7c9 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/IPeripheralTile.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/IPeripheralTile.java @@ -7,17 +7,9 @@ package dan200.computercraft.shared.peripheral.common; import dan200.computercraft.api.peripheral.IPeripheral; -import dan200.computercraft.shared.peripheral.PeripheralType; import net.minecraft.util.EnumFacing; public interface IPeripheralTile { - PeripheralType getPeripheralType(); - IPeripheral getPeripheral( EnumFacing side ); - - default String getLabel() - { - return null; - } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/PeripheralItemFactory.java b/src/main/java/dan200/computercraft/shared/peripheral/common/PeripheralItemFactory.java index 2d0352d79..a333c1197 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/PeripheralItemFactory.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/PeripheralItemFactory.java @@ -15,7 +15,7 @@ import javax.annotation.Nonnull; public class PeripheralItemFactory { @Nonnull - public static ItemStack create( IPeripheralTile tile ) + public static ItemStack create( TilePeripheralBase tile ) { return create( tile.getPeripheralType(), tile.getLabel(), 1 ); } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java b/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java index 6d3ff894e..e89d78520 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java @@ -53,9 +53,6 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe } } - // IPeripheralTile implementation - - @Override public final PeripheralType getPeripheralType() { return getBlock().getPeripheralType( getBlockState() ); @@ -67,7 +64,6 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe return null; } - @Override public String getLabel() { if( m_label != null && m_label.length() > 0 ) diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java index 051bd00fc..305499335 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java @@ -123,9 +123,9 @@ public class TileCable extends TileGeneric implements IPeripheralTile if( !m_destroyed ) { m_destroyed = true; + m_modem.destroy(); remove(); } - super.destroy(); } @Override @@ -156,6 +156,7 @@ public class TileCable extends TileGeneric implements IPeripheralTile @Override public void updateContainingBlockInfo() { + super.updateContainingBlockInfo(); hasModemDirection = false; if( !world.isRemote ) world.scheduleUpdate( pos, getBlockType(), 0 ); } @@ -223,7 +224,7 @@ public class TileCable extends TileGeneric implements IPeripheralTile case WiredModemWithCable: { // 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 ) ); modemChanged(); connectionsChanged(); @@ -484,7 +485,6 @@ public class TileCable extends TileGeneric implements IPeripheralTile return !m_destroyed && getPeripheralType() != PeripheralType.Cable && side == getDirection() ? m_modem : null; } - @Override public PeripheralType getPeripheralType() { IBlockState state = getBlockState(); diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java index 9b2ee6606..cd7142ae8 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileWiredModemFull.java @@ -13,7 +13,6 @@ import dan200.computercraft.api.network.wired.IWiredNode; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.shared.command.CommandCopy; 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; @@ -392,14 +391,6 @@ public class TileWiredModemFull extends TileGeneric implements IPeripheralTile return super.getCapability( capability, facing ); } - // IPeripheralTile - - @Override - public PeripheralType getPeripheralType() - { - return PeripheralType.WiredModemFull; - } - @Override public IPeripheral getPeripheral( EnumFacing side ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/BlockAdvancedModem.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/BlockAdvancedModem.java index 31b292924..e856b3e2c 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/BlockAdvancedModem.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/BlockAdvancedModem.java @@ -7,33 +7,37 @@ package dan200.computercraft.shared.peripheral.modem.wireless; 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 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.PropertyDirection; import net.minecraft.block.state.BlockFaceShape; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.EntityLivingBase; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; import javax.annotation.Nonnull; -public class BlockAdvancedModem extends BlockPeripheralBase +public class BlockAdvancedModem extends BlockGeneric { 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 BlockAdvancedModem() { + super( Material.ROCK ); setHardness( 2.0f ); setTranslationKey( "computercraft:advanced_modem" ); setCreativeTab( ComputerCraft.mainCreativeTab ); @@ -55,17 +59,13 @@ public class BlockAdvancedModem extends BlockPeripheralBase @Deprecated public IBlockState getStateFromMeta( int meta ) { - IBlockState state = getDefaultState(); - state = state.withProperty( Properties.FACING, EnumFacing.byIndex( meta ) ); - state = state.withProperty( Properties.ON, false ); - return state; + return getDefaultState().withProperty( Properties.FACING, EnumFacing.byIndex( meta ) ); } @Override public int getMetaFromState( IBlockState state ) { - EnumFacing dir = state.getValue( Properties.FACING ); - return dir.getIndex(); + return state.getValue( Properties.FACING ).getIndex(); } @Nonnull @@ -73,47 +73,26 @@ public class BlockAdvancedModem extends BlockPeripheralBase @Deprecated public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess world, BlockPos pos ) { - int anim; - EnumFacing dir; TileEntity tile = world.getTileEntity( pos ); - if( tile instanceof TilePeripheralBase ) - { - 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; + return state.withProperty( Properties.ON, tile instanceof TileAdvancedModem && ((TileAdvancedModem) tile).isOn() ); } + @Nonnull @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, dir ); + return getDefaultState().withProperty( Properties.FACING, facing.getOpposite() ); } @Override - public PeripheralType getPeripheralType( int damage ) + protected TileGeneric createTile( IBlockState state ) { - return PeripheralType.AdvancedModem; + return new TileAdvancedModem(); } @Override - public PeripheralType getPeripheralType( IBlockState state ) - { - return PeripheralType.AdvancedModem; - } - - @Override - public TilePeripheralBase createTile( PeripheralType type ) + protected TileGeneric createTile( int damage ) { return new TileAdvancedModem(); } @@ -140,6 +119,7 @@ public class BlockAdvancedModem extends BlockPeripheralBase return BlockFaceShape.UNDEFINED; } + @Nonnull @Override @Deprecated public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos ) diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileAdvancedModem.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileAdvancedModem.java index 76ba2c17a..ec4135d6f 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileAdvancedModem.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileAdvancedModem.java @@ -6,11 +6,14 @@ package dan200.computercraft.shared.peripheral.modem.wireless; +import dan200.computercraft.ComputerCraft; 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.ModemState; -import dan200.computercraft.shared.peripheral.modem.TileModemBase; -import net.minecraft.block.state.IBlockState; +import dan200.computercraft.shared.util.TickScheduler; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; @@ -18,54 +21,46 @@ import net.minecraft.world.World; import javax.annotation.Nonnull; -public class TileAdvancedModem extends TileModemBase +public class TileAdvancedModem extends TileGeneric implements IPeripheralTile { - // Statics - private static class Peripheral extends WirelessModemPeripheral { - private TileModemBase m_entity; + private TileAdvancedModem tile; - public Peripheral( TileModemBase entity ) + Peripheral( TileAdvancedModem entity ) { - super( new ModemState(), true ); - m_entity = entity; + super( new ModemState( () -> TickScheduler.schedule( entity ) ), true ); + tile = entity; } @Nonnull @Override public World getWorld() { - return m_entity.getWorld(); + return tile.getWorld(); } @Nonnull @Override 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() ); } @Override public boolean equals( IPeripheral other ) { - if( other instanceof Peripheral ) - { - Peripheral otherModem = (Peripheral) other; - return otherModem.m_entity == m_entity; - } - return false; + return this == other; } } - // Members - private boolean m_hasDirection = false; + private boolean hasModemDirection = false; + private EnumFacing modemDirection = EnumFacing.DOWN; + private final ModemPeripheral modem = new Peripheral( this ); + private boolean destroyed = false; - public TileAdvancedModem() - { - m_dir = EnumFacing.DOWN; - } + private boolean on = false; @Override public void onLoad() @@ -75,47 +70,77 @@ public class TileAdvancedModem extends TileModemBase } @Override - public void updateContainingBlockInfo() + public void destroy() { - m_hasDirection = false; - } - - @Override - public void update() - { - super.update(); - updateDirection(); - } - - private void updateDirection() - { - if( !m_hasDirection ) + if( !destroyed ) { - m_hasDirection = true; - m_dir = getDirection(); + modem.destroy(); + destroyed = true; } } @Override - public EnumFacing getDirection() + public void updateContainingBlockInfo() { - // Wireless Modem - IBlockState state = getBlockState(); - return state.getValue( BlockAdvancedModem.Properties.FACING ); + hasModemDirection = false; + super.updateContainingBlockInfo(); + world.scheduleUpdate( getPos(), ComputerCraft.Blocks.advancedModem, 0 ); } @Override - public void setDirection( EnumFacing dir ) + public void updateTick() { - // Wireless Modem - setBlockState( getBlockState() - .withProperty( BlockAdvancedModem.Properties.FACING, dir ) - ); + updateDirection(); + + 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 - 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; } }