1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-09-27 06:38:20 +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 )
{
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;
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;
}
}

View File

@ -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 );
}

View File

@ -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 )

View File

@ -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();

View File

@ -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 )
{

View File

@ -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 )

View File

@ -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;
}
}