From a8dad23fa3b1e2aef903fd9ebfa02e6758457d9a Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sat, 19 Jan 2019 10:16:41 +0000 Subject: [PATCH 1/6] Begin investigations into reducing ticking of TEs - Move IDirectionalTile constraint from IPeripheralTile to TilePeripheralBase. - Make *WiredModemFull no longer inherit from *PeripheralBase. While there is still some shared logic (namely in the syncing of "anim"), it's largely fine as we don't store label or direction in NBT. - Add a TickScheduler. This is a thread-safe version of World.scheduleUpdate. We simply build a set of all TEs, and schedule them to be updated the next tick. - Make ModemState receive an "onChanged" listener, which is fired whenever the modem changes. - Make WiredModemFull no longer tick, instead scheduling updates when it is first loaded and whenever the modem changes. --- .../computer/blocks/BlockCommandComputer.java | 4 - .../peripheral/common/IPeripheralTile.java | 8 +- .../peripheral/common/TilePeripheralBase.java | 4 +- .../shared/peripheral/modem/ModemState.java | 17 +++- .../modem/wired/BlockWiredModemFull.java | 31 ++++---- .../modem/wired/TileWiredModemFull.java | 78 ++++++++++++------- .../shared/util/TickScheduler.java | 68 ++++++++++++++++ 7 files changed, 154 insertions(+), 56 deletions(-) create mode 100644 src/main/java/dan200/computercraft/shared/util/TickScheduler.java diff --git a/src/main/java/dan200/computercraft/shared/computer/blocks/BlockCommandComputer.java b/src/main/java/dan200/computercraft/shared/computer/blocks/BlockCommandComputer.java index 164647e55..32dd22189 100644 --- a/src/main/java/dan200/computercraft/shared/computer/blocks/BlockCommandComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/blocks/BlockCommandComputer.java @@ -8,19 +8,15 @@ package dan200.computercraft.shared.computer.blocks; import dan200.computercraft.ComputerCraft; import dan200.computercraft.shared.computer.core.ComputerFamily; -import dan200.computercraft.shared.util.DirectionUtil; import net.minecraft.block.material.Material; import net.minecraft.block.properties.PropertyDirection; import net.minecraft.block.properties.PropertyEnum; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; -import net.minecraft.world.World; import javax.annotation.Nonnull; 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 0f56adf68..02af7c4b2 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/IPeripheralTile.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/IPeripheralTile.java @@ -7,15 +7,17 @@ package dan200.computercraft.shared.peripheral.common; import dan200.computercraft.api.peripheral.IPeripheral; -import dan200.computercraft.shared.common.IDirectionalTile; import dan200.computercraft.shared.peripheral.PeripheralType; import net.minecraft.util.EnumFacing; -public interface IPeripheralTile extends IDirectionalTile +public interface IPeripheralTile { PeripheralType getPeripheralType(); IPeripheral getPeripheral( EnumFacing side ); - String getLabel(); + default String getLabel() + { + return null; + } } 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 cf35e7184..6d3ff894e 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java @@ -7,6 +7,7 @@ package dan200.computercraft.shared.peripheral.common; import dan200.computercraft.api.peripheral.IPeripheral; +import dan200.computercraft.shared.common.IDirectionalTile; import dan200.computercraft.shared.common.TileGeneric; import dan200.computercraft.shared.peripheral.PeripheralType; import net.minecraft.item.ItemStack; @@ -17,8 +18,7 @@ import net.minecraft.util.NonNullList; import javax.annotation.Nonnull; -public abstract class TilePeripheralBase extends TileGeneric - implements IPeripheralTile, ITickable +public abstract class TilePeripheralBase extends TileGeneric implements IPeripheralTile, ITickable, IDirectionalTile { // Statics diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemState.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemState.java index c5596f01e..5ef0e0f1b 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemState.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemState.java @@ -14,16 +14,27 @@ import java.util.concurrent.atomic.AtomicBoolean; public class ModemState { - private boolean open = false; - private AtomicBoolean changed = new AtomicBoolean( true ); + private final Runnable onChanged; + private final AtomicBoolean changed = new AtomicBoolean( true ); + private boolean open = false; private final IntSet channels = new IntOpenHashSet(); + public ModemState() + { + this.onChanged = null; + } + + public ModemState( Runnable onChanged ) + { + this.onChanged = onChanged; + } + private void setOpen( boolean open ) { if( this.open == open ) return; this.open = open; - this.changed.set( true ); + if( !changed.getAndSet( true ) && onChanged != null ) onChanged.run(); } public boolean pollChanged() diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockWiredModemFull.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockWiredModemFull.java index 41b447870..588aca810 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockWiredModemFull.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockWiredModemFull.java @@ -7,20 +7,21 @@ package dan200.computercraft.shared.peripheral.modem.wired; 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 net.minecraft.block.material.Material; import net.minecraft.block.properties.PropertyBool; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.EnumFacing; 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 BlockPeripheralBase +public class BlockWiredModemFull extends BlockGeneric { // Statics @@ -34,6 +35,7 @@ public class BlockWiredModemFull extends BlockPeripheralBase public BlockWiredModemFull() { + super( Material.ROCK ); setHardness( 1.5f ); setTranslationKey( "computercraft:wired_modem_full" ); setCreativeTab( ComputerCraft.mainCreativeTab ); @@ -43,12 +45,6 @@ public class BlockWiredModemFull extends BlockPeripheralBase ); } - @Override - protected IBlockState getDefaultBlockState( PeripheralType type, EnumFacing placedSide ) - { - return getDefaultState(); - } - @Nonnull @Override protected BlockStateContainer createBlockState() @@ -74,7 +70,7 @@ public class BlockWiredModemFull extends BlockPeripheralBase if( te instanceof TileWiredModemFull ) { TileWiredModemFull modem = (TileWiredModemFull) te; - int anim = modem.getAnim(); + int anim = modem.getState(); state = state .withProperty( Properties.MODEM_ON, (anim & 1) != 0 ) .withProperty( Properties.PERIPHERAL_ON, (anim & 2) != 0 ); @@ -84,19 +80,20 @@ public class BlockWiredModemFull extends BlockPeripheralBase } @Override - public PeripheralType getPeripheralType( int damage ) + public void updateTick( World world, BlockPos pos, IBlockState state, Random rand ) { - return PeripheralType.WiredModemFull; + TileEntity te = world.getTileEntity( pos ); + if( te instanceof TileWiredModemFull ) ((TileWiredModemFull) te).updateTick(); } @Override - public PeripheralType getPeripheralType( IBlockState state ) + protected TileGeneric createTile( IBlockState state ) { - return PeripheralType.WiredModemFull; + return new TileWiredModemFull(); } @Override - public TilePeripheralBase createTile( PeripheralType type ) + protected TileGeneric createTile( int damage ) { return new TileWiredModemFull(); } 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 977df5db9..4c11073bb 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 @@ -12,8 +12,11 @@ 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.peripheral.common.TilePeripheralBase; +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; import dan200.computercraft.shared.wired.CapabilityWiredElement; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; @@ -30,7 +33,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.*; -public class TileWiredModemFull extends TilePeripheralBase +public class TileWiredModemFull extends TileGeneric implements IPeripheralTile { private static class FullElement extends WiredModemElement { @@ -85,10 +88,12 @@ public class TileWiredModemFull extends TilePeripheralBase private boolean m_destroyed = false; private boolean m_connectionsFormed = false; - private final ModemState m_modemState = new ModemState(); + private final ModemState m_modemState = new ModemState( () -> TickScheduler.schedule( this ) ); private final WiredModemElement m_element = new FullElement( this ); private final IWiredNode m_node = m_element.getNode(); + private int m_state = 0; + public TileWiredModemFull() { for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i] = new WiredModemLocalPeripheral(); @@ -128,17 +133,6 @@ public class TileWiredModemFull extends TilePeripheralBase remove(); } - @Override - public EnumFacing getDirection() - { - return EnumFacing.NORTH; - } - - @Override - public void setDirection( EnumFacing dir ) - { - } - @Override public void onNeighbourChange() { @@ -231,27 +225,50 @@ public class TileWiredModemFull extends TilePeripheralBase return nbt; } - protected void updateAnim() + public int getState() { - int anim = 0; - if( m_modemState.isOpen() ) anim |= 1; - if( m_peripheralAccessAllowed ) anim |= 2; - setAnim( anim ); + return m_state; + } + + private void updateState() + { + int state = 0; + if( m_modemState.isOpen() ) state |= 1; + if( m_peripheralAccessAllowed ) state |= 2; + if( state != m_state ) + { + m_state = state; + updateBlock(); + } + } + + @Override + protected void writeDescription( @Nonnull NBTTagCompound nbt ) + { + super.writeDescription( nbt ); + nbt.setInteger( "state", m_state ); } @Override public final void readDescription( @Nonnull NBTTagCompound nbt ) { super.readDescription( nbt ); + m_state = nbt.getInteger( "state" ); updateBlock(); } @Override - public void update() + public void onLoad() + { + super.onLoad(); + if( !world.isRemote ) world.scheduleUpdate( pos, getBlockType(), 0 ); + } + + protected void updateTick() { if( !getWorld().isRemote ) { - if( m_modemState.pollChanged() ) updateAnim(); + if( m_modemState.pollChanged() ) updateState(); if( !m_connectionsFormed ) { @@ -268,8 +285,6 @@ public class TileWiredModemFull extends TilePeripheralBase } } } - - super.update(); } private void connectionsChanged() @@ -291,7 +306,6 @@ public class TileWiredModemFull extends TilePeripheralBase } } - // private stuff private void togglePeripheralAccess() { if( !m_peripheralAccessAllowed ) @@ -317,7 +331,7 @@ public class TileWiredModemFull extends TilePeripheralBase m_node.updatePeripherals( Collections.emptyMap() ); } - updateAnim(); + updateState(); } private Set getConnectedPeripheralNames() @@ -349,7 +363,7 @@ public class TileWiredModemFull extends TilePeripheralBase { // If there are no peripherals then disable access and update the display state. m_peripheralAccessAllowed = false; - updateAnim(); + updateState(); } m_node.updatePeripherals( peripherals ); @@ -360,7 +374,8 @@ public class TileWiredModemFull extends TilePeripheralBase @Override public boolean hasCapability( @Nonnull Capability capability, @Nullable EnumFacing facing ) { - return capability == CapabilityWiredElement.CAPABILITY || super.hasCapability( capability, facing ); + if( capability == CapabilityWiredElement.CAPABILITY ) return !m_destroyed; + return super.hasCapability( capability, facing ); } @Nullable @@ -369,6 +384,7 @@ public class TileWiredModemFull extends TilePeripheralBase { if( capability == CapabilityWiredElement.CAPABILITY ) { + if( m_destroyed ) return null; return CapabilityWiredElement.CAPABILITY.cast( m_element ); } @@ -377,9 +393,17 @@ public class TileWiredModemFull extends TilePeripheralBase // IPeripheralTile + @Override + public PeripheralType getPeripheralType() + { + return PeripheralType.WiredModemFull; + } + @Override public IPeripheral getPeripheral( EnumFacing side ) { + if( m_destroyed ) return null; + WiredModemPeripheral peripheral = m_modems[side.ordinal()]; if( peripheral == null ) { diff --git a/src/main/java/dan200/computercraft/shared/util/TickScheduler.java b/src/main/java/dan200/computercraft/shared/util/TickScheduler.java new file mode 100644 index 000000000..62ac366cc --- /dev/null +++ b/src/main/java/dan200/computercraft/shared/util/TickScheduler.java @@ -0,0 +1,68 @@ +/* + * This file is part of ComputerCraft - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission. + * Send enquiries to dratcliffe@gmail.com + */ + +package dan200.computercraft.shared.util; + +import com.google.common.collect.MapMaker; +import dan200.computercraft.ComputerCraft; +import dan200.computercraft.shared.common.TileGeneric; +import net.minecraft.block.Block; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; + +import java.util.Collections; +import java.util.Iterator; +import java.util.Set; + +/** + * A thread-safe version of {@link World#scheduleUpdate(BlockPos, Block, int)}. + * + * We use this when modems and other peripherals change a block in a different thread. + */ +@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID ) +public final class TickScheduler +{ + private TickScheduler() + { + } + + private static final Set toTick = Collections.newSetFromMap( + new MapMaker() + .weakKeys() + .makeMap() + ); + + public static void schedule( TileGeneric tile ) + { + World world = tile.getWorld(); + if( world != null && !world.isRemote ) toTick.add( tile ); + } + + @SubscribeEvent + public static void tick( TickEvent.ServerTickEvent event ) + { + if( event.phase != TickEvent.Phase.START ) return; + + Iterator iterator = toTick.iterator(); + while( iterator.hasNext() ) + { + TileEntity tile = iterator.next(); + iterator.remove(); + + World world = tile.getWorld(); + BlockPos pos = tile.getPos(); + + if( world != null && pos != null && world.isBlockLoaded( pos ) && world.getTileEntity( pos ) == tile ) + { + world.scheduleUpdate( pos, tile.getBlockType(), 0 ); + } + } + } +} From e8a4fbb4e3f0c978e091d691f6aa20bb296095b0 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sat, 19 Jan 2019 22:12:47 +0000 Subject: [PATCH 2/6] 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. --- .../shared/common/BlockGeneric.java | 8 + .../shared/common/TileGeneric.java | 4 + .../common/PeripheralItemFactory.java | 2 +- .../peripheral/modem/wired/BlockCable.java | 31 +-- .../modem/wired/BlockWiredModemFull.java | 9 - .../peripheral/modem/wired/ItemCable.java | 15 +- .../peripheral/modem/wired/TileCable.java | 183 ++++++++++-------- .../modem/wired/TileWiredModemFull.java | 1 + 8 files changed, 137 insertions(+), 116 deletions(-) diff --git a/src/main/java/dan200/computercraft/shared/common/BlockGeneric.java b/src/main/java/dan200/computercraft/shared/common/BlockGeneric.java index ed92c8074..68f76329a 100644 --- a/src/main/java/dan200/computercraft/shared/common/BlockGeneric.java +++ b/src/main/java/dan200/computercraft/shared/common/BlockGeneric.java @@ -21,6 +21,7 @@ import net.minecraft.world.IBlockAccess; 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 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 @Deprecated public final boolean canProvidePower( IBlockState state ) diff --git a/src/main/java/dan200/computercraft/shared/common/TileGeneric.java b/src/main/java/dan200/computercraft/shared/common/TileGeneric.java index d31b8f6dd..4932423f9 100644 --- a/src/main/java/dan200/computercraft/shared/common/TileGeneric.java +++ b/src/main/java/dan200/computercraft/shared/common/TileGeneric.java @@ -72,6 +72,10 @@ public abstract class TileGeneric extends TileEntity { } + protected void updateTick() + { + } + public boolean getRedstoneConnectivity( EnumFacing side ) { return false; 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 92dd93495..2d0352d79 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/PeripheralItemFactory.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/PeripheralItemFactory.java @@ -34,7 +34,7 @@ public class PeripheralItemFactory 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: diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java index 9c03c2f69..44114db16 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java @@ -9,11 +9,12 @@ package dan200.computercraft.shared.peripheral.modem.wired; 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.ArrayList; import java.util.EnumMap; import java.util.List; -public class BlockCable extends BlockPeripheralBase +public class BlockCable extends BlockGeneric { // Statics @@ -65,6 +66,7 @@ public class BlockCable extends BlockPeripheralBase public BlockCable() { + super( Material.ROCK ); setHardness( 1.5f ); setTranslationKey( "computercraft:cable" ); setCreativeTab( ComputerCraft.mainCreativeTab ); @@ -141,10 +143,12 @@ public class BlockCable extends BlockPeripheralBase 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 class BlockCable extends BlockPeripheralBase .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 class BlockCable extends BlockPeripheralBase 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 @@ public class BlockCable extends BlockPeripheralBase } @Override - public TilePeripheralBase createTile( PeripheralType type ) + protected TileGeneric createTile( IBlockState state ) + { + return new TileCable(); + } + + @Override + protected TileGeneric createTile( int damage ) { return new TileCable(); } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockWiredModemFull.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockWiredModemFull.java index 588aca810..f20657b25 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockWiredModemFull.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockWiredModemFull.java @@ -16,10 +16,8 @@ import net.minecraft.block.state.IBlockState; 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 class BlockWiredModemFull extends BlockGeneric 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 ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemCable.java index d46a400e1..e930343ac 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemCable.java @@ -34,7 +34,7 @@ public class ItemCable extends ItemPeripheralBase } @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 class ItemCable extends ItemPeripheralBase return ItemStack.EMPTY; } } - if( label != null ) - { - stack.setStackDisplayName( label ); - } + return stack; } @@ -81,11 +78,11 @@ public class ItemCable extends ItemPeripheralBase // 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 class ItemCable extends ItemPeripheralBase 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() ) 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 e9eb75785..051bd00fc 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 @@ -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 javax.annotation.Nullable; 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 @@ public class TileCable extends TileModemBase 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 class TileCable extends TileModemBase 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 drops, boolean creative ) { @@ -199,12 +187,12 @@ public class TileCable extends TileModemBase 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 class TileCable extends TileModemBase { // 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 class TileCable extends TileModemBase } @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 class TileCable extends TileModemBase connectionsChanged(); if( m_peripheralAccessAllowed ) { - m_peripheral.attach( world, pos, m_dir ); + m_peripheral.attach( world, pos, modemDirection ); updateConnectedPeripherals(); } } @@ -396,7 +408,7 @@ public class TileCable extends TileModemBase m_peripheral.detach(); m_node.updatePeripherals( Collections.emptyMap() ); markDirty(); - updateAnim(); + updateState(); } } @@ -419,7 +431,7 @@ public class TileCable extends TileModemBase m_node.updatePeripherals( Collections.emptyMap() ); } - updateAnim(); + updateState(); } 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. m_peripheralAccessAllowed = false; - updateAnim(); + updateState(); } m_node.updatePeripherals( peripherals ); @@ -441,12 +453,14 @@ public class TileCable extends TileModemBase 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 class TileCable extends TileModemBase { 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 ); } } 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 4c11073bb..9b2ee6606 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 @@ -264,6 +264,7 @@ public class TileWiredModemFull extends TileGeneric implements IPeripheralTile if( !world.isRemote ) world.scheduleUpdate( pos, getBlockType(), 0 ); } + @Override protected void updateTick() { if( !getWorld().isRemote ) From 80a5759bae61e6e4d9c78cfddaf2e24bce273392 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sun, 20 Jan 2019 09:34:15 +0000 Subject: [PATCH 3/6] 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; } } From 8a7e651c991d4d5789934b1158b7d91ba02fc482 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sun, 20 Jan 2019 14:06:41 +0000 Subject: [PATCH 4/6] Several miscellaneous changes - Merge BlockPeripheralBase and BlockPeripheral, as no other classes extended the former. - Make BlockPeripheral use ITilePeripheral instead of TilePeripheralBase. This allows us to use other, non-ticking tiles instead. - Convert advanced and normal modems to extend from a generic TileWirelessModemBase class, and thus neither now tick. --- .../peripheral/common/BlockPeripheral.java | 271 +++++++----------- .../common/BlockPeripheralBase.java | 81 ------ .../peripheral/common/ITilePeripheral.java | 27 ++ .../common/PeripheralItemFactory.java | 2 +- .../peripheral/common/TilePeripheralBase.java | 19 +- .../peripheral/modem/TileModemBase.java | 80 ------ .../peripheral/modem/wired/TileCable.java | 5 +- .../modem/wired/TileWiredModemFull.java | 9 + .../modem/wireless/TileAdvancedModem.java | 129 +-------- .../modem/wireless/TileWirelessModem.java | 113 ++------ .../modem/wireless/TileWirelessModemBase.java | 154 ++++++++++ 11 files changed, 323 insertions(+), 567 deletions(-) delete mode 100644 src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java create mode 100644 src/main/java/dan200/computercraft/shared/peripheral/common/ITilePeripheral.java delete mode 100644 src/main/java/dan200/computercraft/shared/peripheral/modem/TileModemBase.java create mode 100644 src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileWirelessModemBase.java diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java index 40b3b6016..032e4a1c2 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java @@ -7,6 +7,8 @@ package dan200.computercraft.shared.peripheral.common; import dan200.computercraft.ComputerCraft; +import dan200.computercraft.shared.common.BlockDirectional; +import dan200.computercraft.shared.common.TileGeneric; import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive; import dan200.computercraft.shared.peripheral.modem.ModemBounds; @@ -15,18 +17,21 @@ import dan200.computercraft.shared.peripheral.monitor.TileMonitor; import dan200.computercraft.shared.peripheral.printer.TilePrinter; import dan200.computercraft.shared.peripheral.speaker.TileSpeaker; import dan200.computercraft.shared.util.DirectionUtil; +import net.minecraft.block.material.Material; import net.minecraft.block.properties.PropertyDirection; import net.minecraft.block.properties.PropertyEnum; 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.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.BlockRenderLayer; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RayTraceResult; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; @@ -34,7 +39,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; import javax.annotation.Nonnull; -public class BlockPeripheral extends BlockPeripheralBase +public class BlockPeripheral extends BlockDirectional { public static class Properties { @@ -44,6 +49,7 @@ public class BlockPeripheral extends BlockPeripheralBase public BlockPeripheral() { + super( Material.ROCK ); setHardness( 2.0f ); setTranslationKey( "computercraft:peripheral" ); setCreativeTab( ComputerCraft.mainCreativeTab ); @@ -187,202 +193,107 @@ public class BlockPeripheral 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 ); - switch( state.getValue( Properties.VARIANT ) ) - { - case WirelessModemDownOff: - case WirelessModemDownOn: - { - dir = EnumFacing.DOWN; - break; - } - case WirelessModemUpOff: - case WirelessModemUpOn: - { - dir = EnumFacing.UP; - break; - } - } - } - PeripheralType type = getPeripheralType( state ); switch( type ) { case DiskDrive: { - state = state.withProperty( Properties.FACING, dir ); - switch( anim ) + if( !(tile instanceof TileDiskDrive) ) return state; + + TileDiskDrive drive = (TileDiskDrive) tile; + state = state.withProperty( Properties.FACING, drive.getDirection() ); + switch( drive.getAnim() ) { - case 0: default: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveEmpty ); - break; - } + case 0: + return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveEmpty ); case 1: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveInvalid ); - break; - } + return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveInvalid ); case 2: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveFull ); - break; - } + return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveFull ); } - break; } case Printer: { - state = state.withProperty( Properties.FACING, dir ); - switch( anim ) + if( !(tile instanceof TilePrinter) ) return state; + + TilePrinter printer = (TilePrinter) tile; + state = state.withProperty( Properties.FACING, printer.getDirection() ); + switch( printer.getAnim() ) { - case 0: default: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterEmpty ); - break; - } + case 0: + return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterEmpty ); case 1: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterTopFull ); - break; - } + return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterTopFull ); case 2: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBottomFull ); - break; - } + return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBottomFull ); case 3: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBothFull ); - break; - } + return state.withProperty( Properties.VARIANT, BlockPeripheralVariant.PrinterBothFull ); } - break; } case WirelessModem: { - switch( dir ) + if( !(tile instanceof TileWirelessModem) ) return state; + + TileWirelessModem modem = (TileWirelessModem) tile; + EnumFacing direction = modem.getDirection(); + switch( direction ) { case UP: - { - state = state.withProperty( Properties.FACING, EnumFacing.NORTH ); - switch( anim ) - { - case 0: - default: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemUpOff ); - break; - } - case 1: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemUpOn ); - break; - } - } - break; - } + return state + .withProperty( Properties.FACING, EnumFacing.NORTH ) + .withProperty( Properties.VARIANT, + modem.isOn() ? BlockPeripheralVariant.WirelessModemUpOn : BlockPeripheralVariant.WirelessModemUpOff ); case DOWN: - { - state = state.withProperty( Properties.FACING, EnumFacing.NORTH ); - switch( anim ) - { - case 0: - default: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemDownOff ); - break; - } - case 1: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemDownOn ); - break; - } - } - break; - } + return state + .withProperty( Properties.FACING, EnumFacing.NORTH ) + .withProperty( Properties.VARIANT, + modem.isOn() ? BlockPeripheralVariant.WirelessModemDownOn : BlockPeripheralVariant.WirelessModemDownOff ); default: { - state = state.withProperty( Properties.FACING, dir ); - switch( anim ) - { - case 0: - default: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemOff ); - break; - } - case 1: - { - state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemOn ); - break; - } - } - break; + return state + .withProperty( Properties.FACING, direction ) + .withProperty( Properties.VARIANT, + modem.isOn() ? BlockPeripheralVariant.WirelessModemOn : BlockPeripheralVariant.WirelessModemOff ); } } - break; } case Speaker: { - state = state.withProperty( Properties.FACING, dir ); - break; + if( !(tile instanceof TileSpeaker) ) return state; + return state.withProperty( Properties.FACING, ((TileSpeaker) tile).getDirection() ); } case Monitor: case AdvancedMonitor: { - EnumFacing front; - int xIndex, yIndex, width, height; - if( tile instanceof TileMonitor ) - { - TileMonitor monitor = (TileMonitor) tile; - dir = monitor.getDirection(); - front = monitor.getFront(); - xIndex = monitor.getXIndex(); - yIndex = monitor.getYIndex(); - width = monitor.getWidth(); - height = monitor.getHeight(); - } - else - { - dir = EnumFacing.NORTH; - front = EnumFacing.NORTH; - xIndex = 0; - yIndex = 0; - width = 1; - height = 1; - } + if( !(tile instanceof TileMonitor) ) return state; + + TileMonitor monitor = (TileMonitor) tile; + EnumFacing dir = monitor.getDirection(); + EnumFacing front = monitor.getFront(); + int xIndex = monitor.getXIndex(); + int yIndex = monitor.getYIndex(); + int width = monitor.getWidth(); + int height = monitor.getHeight(); BlockPeripheralVariant baseVariant; if( front == EnumFacing.UP ) { - baseVariant = (type == PeripheralType.AdvancedMonitor) ? + baseVariant = type == PeripheralType.AdvancedMonitor ? BlockPeripheralVariant.AdvancedMonitorUp : BlockPeripheralVariant.MonitorUp; } else if( front == EnumFacing.DOWN ) { - baseVariant = (type == PeripheralType.AdvancedMonitor) ? + baseVariant = type == PeripheralType.AdvancedMonitor ? BlockPeripheralVariant.AdvancedMonitorDown : BlockPeripheralVariant.MonitorDown; } else { - baseVariant = (type == PeripheralType.AdvancedMonitor) ? + baseVariant = type == PeripheralType.AdvancedMonitor ? BlockPeripheralVariant.AdvancedMonitor : BlockPeripheralVariant.Monitor; } @@ -446,20 +357,21 @@ public class BlockPeripheral extends BlockPeripheralBase } } - state = state.withProperty( Properties.FACING, dir ); - state = state.withProperty( Properties.VARIANT, - BlockPeripheralVariant.values()[baseVariant.ordinal() + subType] - ); - break; + return state + .withProperty( Properties.FACING, dir ) + .withProperty( Properties.VARIANT, BlockPeripheralVariant.values()[baseVariant.ordinal() + subType] ); } + default: + return state; } - return state; } + @Nonnull @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( getPeripheralType( damage ) ) { case DiskDrive: default: @@ -515,45 +427,32 @@ public class BlockPeripheral extends BlockPeripheralBase } } - @Override public PeripheralType getPeripheralType( int damage ) { return ComputerCraft.Items.peripheral.getPeripheralType( damage ); } - @Override public PeripheralType getPeripheralType( IBlockState state ) { return state.getValue( Properties.VARIANT ).getPeripheralType(); } - @Override - public TilePeripheralBase createTile( PeripheralType type ) + private TileGeneric createTile( PeripheralType type ) { switch( type ) { case DiskDrive: default: - { return new TileDiskDrive(); - } case WirelessModem: - { return new TileWirelessModem(); - } case Monitor: case AdvancedMonitor: - { return new TileMonitor(); - } case Printer: - { return new TilePrinter(); - } case Speaker: - { return new TileSpeaker(); - } } } @@ -567,12 +466,11 @@ public class BlockPeripheral extends BlockPeripheralBase case DiskDrive: case Printer: { - EnumFacing dir = DirectionUtil.fromEntityRot( player ); - setDirection( world, pos, dir ); - if( stack.hasDisplayName() && tile instanceof TilePeripheralBase ) + if( stack.hasDisplayName() && tile instanceof ITilePeripheral ) { - TilePeripheralBase peripheral = (TilePeripheralBase) tile; + ITilePeripheral peripheral = (ITilePeripheral) tile; peripheral.setLabel( stack.getDisplayName() ); + peripheral.setDirection( DirectionUtil.fromEntityRot( player ) ); } break; } @@ -659,6 +557,7 @@ public class BlockPeripheral extends BlockPeripheralBase @Override @Deprecated + @Nonnull public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos ) { if( getPeripheralType( state ) == PeripheralType.WirelessModem ) @@ -668,4 +567,32 @@ public class BlockPeripheral extends BlockPeripheralBase return super.getBoundingBox( state, source, pos ); } + + @Override + public final boolean canPlaceBlockOnSide( @Nonnull World world, @Nonnull BlockPos pos, EnumFacing side ) + { + return true; // ItemPeripheralBase handles this + } + + @Override + public final TileGeneric createTile( IBlockState state ) + { + return createTile( getPeripheralType( state ) ); + } + + @Override + public final TileGeneric createTile( int damage ) + { + return createTile( getPeripheralType( damage ) ); + } + + @Nonnull + @Override + public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player ) + { + TileEntity tile = world.getTileEntity( pos ); + return tile instanceof ITilePeripheral + ? PeripheralItemFactory.create( (ITilePeripheral) tile ) + : super.getPickBlock( state, target, world, pos, player ); + } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java deleted file mode 100644 index bd46859e8..000000000 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheralBase.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * This file is part of ComputerCraft - http://www.computercraft.info - * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission. - * Send enquiries to dratcliffe@gmail.com - */ - -package dan200.computercraft.shared.peripheral.common; - -import dan200.computercraft.shared.common.BlockDirectional; -import dan200.computercraft.shared.common.TileGeneric; -import dan200.computercraft.shared.peripheral.PeripheralType; -import net.minecraft.block.material.Material; -import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.RayTraceResult; -import net.minecraft.world.IBlockAccess; -import net.minecraft.world.World; - -import javax.annotation.Nonnull; - -public abstract class BlockPeripheralBase extends BlockDirectional -{ - public BlockPeripheralBase() - { - super( Material.ROCK ); - } - - protected abstract IBlockState getDefaultBlockState( PeripheralType type, EnumFacing placedSide ); - - protected abstract PeripheralType getPeripheralType( int damage ); - - protected abstract PeripheralType getPeripheralType( IBlockState state ); - - protected abstract TilePeripheralBase createTile( PeripheralType type ); - - @Override - public final boolean canPlaceBlockOnSide( @Nonnull World world, @Nonnull BlockPos pos, EnumFacing side ) - { - return true; // ItemPeripheralBase handles this - } - - @Nonnull - @Override - @Deprecated - public final IBlockState getStateForPlacement( World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, int damage, EntityLivingBase placer ) - { - return getDefaultBlockState( getPeripheralType( damage ), side ); - } - - @Override - public final TileGeneric createTile( IBlockState state ) - { - return createTile( getPeripheralType( state ) ); - } - - @Override - public final TileGeneric createTile( int damage ) - { - return createTile( getPeripheralType( damage ) ); - } - - public final PeripheralType getPeripheralType( IBlockAccess world, BlockPos pos ) - { - return getPeripheralType( world.getBlockState( pos ) ); - } - - @Nonnull - @Override - public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player ) - { - TileEntity tile = world.getTileEntity( pos ); - 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/ITilePeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/common/ITilePeripheral.java new file mode 100644 index 000000000..0d0bdb36c --- /dev/null +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/ITilePeripheral.java @@ -0,0 +1,27 @@ +/* + * This file is part of ComputerCraft - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission. + * Send enquiries to dratcliffe@gmail.com + */ + +package dan200.computercraft.shared.peripheral.common; + +import dan200.computercraft.shared.common.IDirectionalTile; +import dan200.computercraft.shared.peripheral.PeripheralType; + +/** + * The tile for {@link BlockPeripheral}. + */ +public interface ITilePeripheral extends IDirectionalTile +{ + PeripheralType getPeripheralType(); + + default String getLabel() + { + return null; + } + + default void setLabel( String label ) + { + } +} 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 a333c1197..f1c30bc9c 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( TilePeripheralBase tile ) + public static ItemStack create( ITilePeripheral 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 e89d78520..bd89df235 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java @@ -18,11 +18,9 @@ import net.minecraft.util.NonNullList; import javax.annotation.Nonnull; -public abstract class TilePeripheralBase extends TileGeneric implements IPeripheralTile, ITickable, IDirectionalTile +public abstract class TilePeripheralBase extends TileGeneric implements IPeripheralTile, ITickable, IDirectionalTile, ITilePeripheral { - // Statics - - protected EnumFacing m_dir; + private EnumFacing m_dir; private int m_anim; private boolean m_changed; @@ -39,9 +37,9 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe @Override - public BlockPeripheralBase getBlock() + public BlockPeripheral getBlock() { - return (BlockPeripheralBase) super.getBlock(); + return (BlockPeripheral) super.getBlock(); } @Override @@ -53,6 +51,7 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe } } + @Override public final PeripheralType getPeripheralType() { return getBlock().getPeripheralType( getBlockState() ); @@ -64,6 +63,7 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe return null; } + @Override public String getLabel() { if( m_label != null && m_label.length() > 0 ) @@ -86,11 +86,6 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe return m_dir; } - public EnumFacing getCachedDirection() - { - return m_dir; - } - @Override public void setDirection( EnumFacing dir ) { @@ -106,7 +101,7 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe return m_anim; } - public void setAnim( int anim ) + protected void setAnim( int anim ) { if( anim != m_anim ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileModemBase.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/TileModemBase.java deleted file mode 100644 index 7bc1f1d8d..000000000 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/TileModemBase.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * This file is part of ComputerCraft - http://www.computercraft.info - * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission. - * Send enquiries to dratcliffe@gmail.com - */ - -package dan200.computercraft.shared.peripheral.modem; - -import dan200.computercraft.api.peripheral.IPeripheral; -import dan200.computercraft.shared.common.BlockGeneric; -import dan200.computercraft.shared.peripheral.common.TilePeripheralBase; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.util.EnumFacing; - -import javax.annotation.Nonnull; - -public abstract class TileModemBase extends TilePeripheralBase -{ - - protected ModemPeripheral m_modem; - - protected TileModemBase() - { - m_modem = createPeripheral(); - } - - protected abstract ModemPeripheral createPeripheral(); - - @Override - public void destroy() - { - if( m_modem != null ) - { - m_modem.destroy(); - m_modem = null; - } - } - - @Override - public void onNeighbourChange() - { - EnumFacing dir = getDirection(); - if( !getWorld().isSideSolid( getPos().offset( dir ), dir.getOpposite() ) ) - { - // Drop everything and remove block - ((BlockGeneric) getBlockType()).dropAllItems( getWorld(), getPos(), false ); - getWorld().setBlockToAir( getPos() ); - } - } - - @Override - public void update() - { - super.update(); - if( !getWorld().isRemote && m_modem.getModemState().pollChanged() ) - { - updateAnim(); - } - } - - protected void updateAnim() - { - setAnim( m_modem.getModemState().isOpen() ? 1 : 0 ); - } - - @Override - public final void readDescription( @Nonnull NBTTagCompound nbt ) - { - super.readDescription( nbt ); - updateBlock(); - } - - // IPeripheralTile implementation - - @Override - public IPeripheral getPeripheral( EnumFacing side ) - { - return side == getDirection() ? m_modem : null; - } -} 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 305499335..de920c321 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 @@ -13,7 +13,6 @@ 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; @@ -215,7 +214,7 @@ public class TileCable extends TileGeneric implements IPeripheralTile case WiredModem: { // Drop everything and remove block - ((BlockGeneric) getBlockType()).dropAllItems( getWorld(), getPos(), false ); + getBlock().dropAllItems( getWorld(), getPos(), false ); getWorld().setBlockToAir( getPos() ); // This'll call #destroy(), so we don't need to reset the network here. @@ -224,7 +223,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, null, 1 ) ); + getBlock().dropItem( getWorld(), getPos(), PeripheralItemFactory.create( PeripheralType.WiredModem, null, 1 ) ); setBlockState( getBlockState().withProperty( BlockCable.Properties.MODEM, BlockCableModemVariant.None ) ); modemChanged(); connectionsChanged(); 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 cd7142ae8..be89a9b7b 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 @@ -7,6 +7,7 @@ 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; @@ -18,9 +19,11 @@ import dan200.computercraft.shared.peripheral.modem.ModemState; import dan200.computercraft.shared.util.TickScheduler; import dan200.computercraft.shared.wired.CapabilityWiredElement; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; +import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.text.TextComponentString; @@ -132,6 +135,12 @@ public class TileWiredModemFull extends TileGeneric implements IPeripheralTile remove(); } + @Override + public void getDroppedItems( @Nonnull NonNullList drops, boolean creative ) + { + drops.add( new ItemStack( ComputerCraft.Items.wiredModemFull ) ); + } + @Override public void onNeighbourChange() { 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 ec4135d6f..cfcd2b444 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 @@ -7,140 +7,23 @@ 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.util.TickScheduler; -import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Vec3d; -import net.minecraft.world.World; +import net.minecraft.util.NonNullList; import javax.annotation.Nonnull; -public class TileAdvancedModem extends TileGeneric implements IPeripheralTile +public class TileAdvancedModem extends TileWirelessModemBase { - private static class Peripheral extends WirelessModemPeripheral - { - private TileAdvancedModem tile; - - Peripheral( TileAdvancedModem entity ) - { - super( new ModemState( () -> TickScheduler.schedule( entity ) ), true ); - tile = entity; - } - - @Nonnull - @Override - public World getWorld() - { - return tile.getWorld(); - } - - @Nonnull - @Override - public Vec3d getPosition() - { - BlockPos pos = tile.getPos().offset( tile.modemDirection ); - return new Vec3d( pos.getX(), pos.getY(), pos.getZ() ); - } - - @Override - public boolean equals( IPeripheral other ) - { - return this == other; - } - } - - private boolean hasModemDirection = false; - private EnumFacing modemDirection = EnumFacing.DOWN; - private final ModemPeripheral modem = new Peripheral( this ); - private boolean destroyed = false; - - private boolean on = false; - @Override - public void onLoad() - { - super.onLoad(); - updateDirection(); - } - - @Override - public void destroy() - { - if( !destroyed ) - { - modem.destroy(); - destroyed = true; - } - } - - @Override - public void updateContainingBlockInfo() - { - hasModemDirection = false; - super.updateContainingBlockInfo(); - world.scheduleUpdate( getPos(), ComputerCraft.Blocks.advancedModem, 0 ); - } - - @Override - public void updateTick() - { - 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() + protected EnumFacing getDirection() { return getBlockState().getValue( BlockAdvancedModem.Properties.FACING ); } @Override - protected void writeDescription( @Nonnull NBTTagCompound nbt ) + public void getDroppedItems( @Nonnull NonNullList drops, boolean creative ) { - 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; + if( !creative ) drops.add( new ItemStack( ComputerCraft.Items.advancedModem ) ); } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileWirelessModem.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileWirelessModem.java index 809d101d6..0a40f564c 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileWirelessModem.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileWirelessModem.java @@ -7,100 +7,23 @@ package dan200.computercraft.shared.peripheral.modem.wireless; import dan200.computercraft.ComputerCraft; -import dan200.computercraft.api.peripheral.IPeripheral; +import dan200.computercraft.shared.common.IDirectionalTile; import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.common.BlockPeripheral; import dan200.computercraft.shared.peripheral.common.BlockPeripheralVariant; -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.peripheral.common.ITilePeripheral; +import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory; import net.minecraft.block.state.IBlockState; +import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; +import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; import javax.annotation.Nonnull; -public class TileWirelessModem extends TileModemBase +public class TileWirelessModem extends TileWirelessModemBase implements IDirectionalTile, ITilePeripheral { - // Statics - - private static class Peripheral extends WirelessModemPeripheral - { - private TileModemBase m_entity; - - public Peripheral( TileModemBase entity ) - { - super( new ModemState(), false ); - m_entity = entity; - } - - @Nonnull - @Override - public World getWorld() - { - return m_entity.getWorld(); - } - - @Nonnull - @Override - public Vec3d getPosition() - { - BlockPos pos = m_entity.getPos().offset( m_entity.getCachedDirection() ); - 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; - } - } - - // Members - - private boolean m_hasDirection = false; - - public TileWirelessModem() - { - m_dir = EnumFacing.DOWN; - } - - @Override - public void onLoad() - { - super.onLoad(); - updateDirection(); - } - - @Override - public void updateContainingBlockInfo() - { - m_hasDirection = false; - } - - @Override - public void update() - { - super.update(); - updateDirection(); - } - - private void updateDirection() - { - if( !m_hasDirection ) - { - m_hasDirection = true; - m_dir = getDirection(); - } - } - @Override public EnumFacing getDirection() { @@ -110,18 +33,12 @@ public class TileWirelessModem extends TileModemBase { case WirelessModemDownOff: case WirelessModemDownOn: - { return EnumFacing.DOWN; - } case WirelessModemUpOff: case WirelessModemUpOn: - { return EnumFacing.UP; - } default: - { return state.getValue( BlockPeripheral.Properties.FACING ); - } } } @@ -152,15 +69,21 @@ public class TileWirelessModem extends TileModemBase } } - @Override - protected ModemPeripheral createPeripheral() - { - return new Peripheral( this ); - } - @Override public boolean shouldRefresh( World world, BlockPos pos, @Nonnull IBlockState oldState, @Nonnull IBlockState newState ) { return super.shouldRefresh( world, pos, oldState, newState ) || ComputerCraft.Blocks.peripheral.getPeripheralType( newState ) != PeripheralType.WirelessModem; } + + @Override + public void getDroppedItems( @Nonnull NonNullList drops, boolean creative ) + { + if( !creative ) drops.add( PeripheralItemFactory.create( PeripheralType.WirelessModem, null, 1 ) ); + } + + @Override + public PeripheralType getPeripheralType() + { + return PeripheralType.WirelessModem; + } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileWirelessModemBase.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileWirelessModemBase.java new file mode 100644 index 000000000..3f970c6b6 --- /dev/null +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/TileWirelessModemBase.java @@ -0,0 +1,154 @@ +/* + * This file is part of ComputerCraft - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission. + * Send enquiries to dratcliffe@gmail.com + */ + +package dan200.computercraft.shared.peripheral.modem.wireless; + +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.util.TickScheduler; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; +import net.minecraft.world.World; + +import javax.annotation.Nonnull; + +public abstract class TileWirelessModemBase extends TileGeneric implements IPeripheralTile +{ + private static class Peripheral extends WirelessModemPeripheral + { + private final TileWirelessModemBase entity; + + Peripheral( TileWirelessModemBase entity ) + { + super( new ModemState( () -> TickScheduler.schedule( entity ) ), true ); + this.entity = entity; + } + + @Nonnull + @Override + public World getWorld() + { + return entity.getWorld(); + } + + @Nonnull + @Override + public Vec3d getPosition() + { + BlockPos pos = entity.getPos().offset( entity.modemDirection ); + return new Vec3d( pos.getX(), pos.getY(), pos.getZ() ); + } + + @Override + public boolean equals( IPeripheral other ) + { + return this == other; + } + } + + private boolean hasModemDirection = false; + private EnumFacing modemDirection = EnumFacing.DOWN; + private final ModemPeripheral modem = new Peripheral( this ); + private boolean destroyed = false; + + private boolean on = false; + + @Override + public void onLoad() + { + super.onLoad(); + updateDirection(); + world.scheduleUpdate( getPos(), getBlockType(), 0 ); + } + + @Override + public void destroy() + { + if( !destroyed ) + { + modem.destroy(); + destroyed = true; + } + } + + @Override + public void updateContainingBlockInfo() + { + hasModemDirection = false; + super.updateContainingBlockInfo(); + world.scheduleUpdate( getPos(), getBlockType(), 0 ); + } + + @Override + public void updateTick() + { + 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(); + } + } + + protected abstract EnumFacing getDirection(); + + @Override + public void onNeighbourChange() + { + EnumFacing dir = getDirection(); + if( !getWorld().isSideSolid( getPos().offset( dir ), dir.getOpposite() ) ) + { + // Drop everything and remove block + getBlock().dropAllItems( getWorld(), getPos(), false ); + getWorld().setBlockToAir( getPos() ); + } + } + + @Override + protected void writeDescription( @Nonnull NBTTagCompound nbt ) + { + 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; + } +} From 83b01d35eb4c7d8cedf37a08a543b3d07b8d2ed0 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sun, 20 Jan 2019 15:35:48 +0000 Subject: [PATCH 5/6] Make monitors non-ticking - Convert terminals from a polling-based system to a more event-driven one: they now accept an onChanged callback, which marks the parent as dirty. - Schedule ticks when monitors are marked as dirty. - Add several missing @Overrides. This has nothing to do with the rest of the changes, but I'm bad at good git practice. --- .../computercraft/core/apis/ILuaAPI.java | 1 + .../core/apis/http/ResourceQueue.java | 3 + .../core/apis/http/request/HttpRequest.java | 1 + .../computercraft/core/terminal/Terminal.java | 45 +++++++---- .../shared/common/ClientTerminal.java | 15 ++-- .../shared/common/ServerTerminal.java | 32 ++++---- .../shared/computer/core/ClientComputer.java | 2 - .../peripheral/common/BlockPeripheral.java | 21 ++++-- .../peripheral/common/ITilePeripheral.java | 3 +- .../peripheral/common/TilePeripheralBase.java | 1 + .../peripheral/monitor/ServerMonitor.java | 40 +++++++--- .../peripheral/monitor/TileMonitor.java | 74 +++++++++---------- .../pocket/core/PocketServerComputer.java | 1 + 13 files changed, 137 insertions(+), 102 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/apis/ILuaAPI.java b/src/main/java/dan200/computercraft/core/apis/ILuaAPI.java index 53b88fc67..63d49879c 100644 --- a/src/main/java/dan200/computercraft/core/apis/ILuaAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/ILuaAPI.java @@ -16,6 +16,7 @@ public interface ILuaAPI extends dan200.computercraft.api.lua.ILuaAPI { void advance( double v ); + @Override default void update() { advance( 0.05 ); diff --git a/src/main/java/dan200/computercraft/core/apis/http/ResourceQueue.java b/src/main/java/dan200/computercraft/core/apis/http/ResourceQueue.java index 021dfa7b7..9ac8eb56f 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/ResourceQueue.java +++ b/src/main/java/dan200/computercraft/core/apis/http/ResourceQueue.java @@ -26,12 +26,14 @@ public class ResourceQueue> extends ResourceGroup { } + @Override public synchronized void shutdown() { super.shutdown(); pending.clear(); } + @Override public synchronized boolean queue( Supplier resource ) { if( !active ) return false; @@ -40,6 +42,7 @@ public class ResourceQueue> extends ResourceGroup return true; } + @Override public synchronized void release( T resource ) { super.release( resource ); diff --git a/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java b/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java index 779e3f5fa..9fe2c25ed 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java +++ b/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java @@ -239,6 +239,7 @@ public class HttpRequest extends Resource if( tryClose() ) environment.queueEvent( SUCCESS_EVENT, new Object[] { address, object } ); } + @Override protected void dispose() { super.dispose(); diff --git a/src/main/java/dan200/computercraft/core/terminal/Terminal.java b/src/main/java/dan200/computercraft/core/terminal/Terminal.java index 53ae1aa92..38080b659 100644 --- a/src/main/java/dan200/computercraft/core/terminal/Terminal.java +++ b/src/main/java/dan200/computercraft/core/terminal/Terminal.java @@ -29,11 +29,18 @@ public class Terminal private final Palette m_palette; private boolean m_changed; + private final Runnable onChanged; public Terminal( int width, int height ) + { + this( width, height, null ); + } + + public Terminal( int width, int height, Runnable changedCallback ) { m_width = width; m_height = height; + this.onChanged = changedCallback; m_cursorColour = 0; m_cursorBackgroundColour = 15; @@ -65,7 +72,7 @@ public class Terminal m_cursorY = 0; m_cursorBlink = false; clear(); - m_changed = true; + setChanged(); m_palette.resetColours(); } @@ -122,7 +129,7 @@ public class Terminal m_backgroundColour[i].write( oldBackgroundColour[i] ); } } - m_changed = true; + setChanged(); } public void setCursorPos( int x, int y ) @@ -131,7 +138,7 @@ public class Terminal { m_cursorX = x; m_cursorY = y; - m_changed = true; + setChanged(); } } @@ -140,7 +147,7 @@ public class Terminal if( m_cursorBlink != blink ) { m_cursorBlink = blink; - m_changed = true; + setChanged(); } } @@ -149,7 +156,7 @@ public class Terminal if( m_cursorColour != colour ) { m_cursorColour = colour; - m_changed = true; + setChanged(); } } @@ -158,7 +165,7 @@ public class Terminal if( m_cursorBackgroundColour != colour ) { m_cursorBackgroundColour = colour; - m_changed = true; + setChanged(); } } @@ -201,7 +208,7 @@ public class Terminal m_text[y].write( text, x ); m_textColour[y].write( textColour, x ); m_backgroundColour[y].write( backgroundColour, x ); - m_changed = true; + setChanged(); } } @@ -214,7 +221,7 @@ public class Terminal m_text[y].write( text, x ); m_textColour[y].fill( base16.charAt( m_cursorColour ), x, x + text.length() ); m_backgroundColour[y].fill( base16.charAt( m_cursorBackgroundColour ), x, x + text.length() ); - m_changed = true; + setChanged(); } } @@ -244,7 +251,7 @@ public class Terminal m_text = newText; m_textColour = newTextColour; m_backgroundColour = newBackgroundColour; - m_changed = true; + setChanged(); } } @@ -256,7 +263,7 @@ public class Terminal m_textColour[y].fill( base16.charAt( m_cursorColour ) ); m_backgroundColour[y].fill( base16.charAt( m_cursorBackgroundColour ) ); } - m_changed = true; + setChanged(); } public synchronized void clearLine() @@ -267,7 +274,7 @@ public class Terminal m_text[y].fill( ' ' ); m_textColour[y].fill( base16.charAt( m_cursorColour ) ); m_backgroundColour[y].fill( base16.charAt( m_cursorBackgroundColour ) ); - m_changed = true; + setChanged(); } } @@ -285,7 +292,7 @@ public class Terminal m_text[y].write( text ); m_textColour[y].write( textColour ); m_backgroundColour[y].write( backgroundColour ); - m_changed = true; + setChanged(); } public synchronized TextBuffer getTextColourLine( int y ) @@ -306,17 +313,23 @@ public class Terminal return null; } - public boolean getChanged() + /** + * @deprecated All {@code *Changed()} methods are deprecated: one should pass in a callback + * instead. + */ + @Deprecated + public final boolean getChanged() { return m_changed; } - public void setChanged() + public final void setChanged() { m_changed = true; + if( onChanged != null ) onChanged.run(); } - public void clearChanged() + public final void clearChanged() { m_changed = false; } @@ -371,6 +384,6 @@ public class Terminal { m_palette.readFromNBT( nbt ); } - m_changed = true; + setChanged(); } } diff --git a/src/main/java/dan200/computercraft/shared/common/ClientTerminal.java b/src/main/java/dan200/computercraft/shared/common/ClientTerminal.java index 55c377033..56f86c18b 100644 --- a/src/main/java/dan200/computercraft/shared/common/ClientTerminal.java +++ b/src/main/java/dan200/computercraft/shared/common/ClientTerminal.java @@ -22,19 +22,14 @@ public class ClientTerminal implements ITerminal m_terminalChanged = false; } - public void update() - { - if( m_terminal != null ) - { - m_terminalChanged |= m_terminal.getChanged(); - m_terminal.clearChanged(); - } - } - public boolean pollTerminalChanged() { boolean changed = m_terminalChanged; m_terminalChanged = false; + + Terminal terminal = m_terminal; + if( terminal != null ) terminal.clearChanged(); + return changed; } @@ -71,7 +66,7 @@ public class ClientTerminal implements ITerminal { if( m_terminal == null ) { - m_terminal = new Terminal( width, height ); + m_terminal = new Terminal( width, height, () -> m_terminalChanged = true ); m_terminalChanged = true; } else diff --git a/src/main/java/dan200/computercraft/shared/common/ServerTerminal.java b/src/main/java/dan200/computercraft/shared/common/ServerTerminal.java index 471678370..b007ef86c 100644 --- a/src/main/java/dan200/computercraft/shared/common/ServerTerminal.java +++ b/src/main/java/dan200/computercraft/shared/common/ServerTerminal.java @@ -9,35 +9,33 @@ package dan200.computercraft.shared.common; import dan200.computercraft.core.terminal.Terminal; import net.minecraft.nbt.NBTTagCompound; +import java.util.concurrent.atomic.AtomicBoolean; + public class ServerTerminal implements ITerminal { private final boolean m_colour; private Terminal m_terminal; - private boolean m_terminalChanged; - private boolean m_terminalChangedLastFrame; + private final AtomicBoolean m_terminalChanged = new AtomicBoolean( false ); + private boolean m_terminalChangedLastFrame = false; public ServerTerminal( boolean colour ) { m_colour = colour; m_terminal = null; - m_terminalChanged = false; - m_terminalChangedLastFrame = false; } public ServerTerminal( boolean colour, int terminalWidth, int terminalHeight ) { m_colour = colour; - m_terminal = new Terminal( terminalWidth, terminalHeight ); - m_terminalChanged = false; - m_terminalChangedLastFrame = false; + m_terminal = new Terminal( terminalWidth, terminalHeight, this::markTerminalChanged ); } - public void resize( int width, int height ) + protected void resize( int width, int height ) { if( m_terminal == null ) { - m_terminal = new Terminal( width, height ); - m_terminalChanged = true; + m_terminal = new Terminal( width, height, this::markTerminalChanged ); + markTerminalChanged(); } else { @@ -50,23 +48,21 @@ public class ServerTerminal implements ITerminal if( m_terminal != null ) { m_terminal = null; - m_terminalChanged = true; + markTerminalChanged(); } } protected void markTerminalChanged() { - m_terminalChanged = true; + m_terminalChanged.set( true ); } public void update() { - m_terminalChangedLastFrame = m_terminalChanged || (m_terminal != null && m_terminal.getChanged()); - if( m_terminal != null ) - { - m_terminal.clearChanged(); - } - m_terminalChanged = false; + Terminal terminal = m_terminal; + if( terminal != null ) terminal.clearChanged(); + + m_terminalChangedLastFrame = m_terminalChanged.getAndSet( false ); } public boolean hasTerminalChanged() diff --git a/src/main/java/dan200/computercraft/shared/computer/core/ClientComputer.java b/src/main/java/dan200/computercraft/shared/computer/core/ClientComputer.java index b4991f242..c25618dd5 100644 --- a/src/main/java/dan200/computercraft/shared/computer/core/ClientComputer.java +++ b/src/main/java/dan200/computercraft/shared/computer/core/ClientComputer.java @@ -32,10 +32,8 @@ public class ClientComputer extends ClientTerminal implements IComputer m_instanceID = instanceID; } - @Override public void update() { - super.update(); m_changedLastFrame = m_changed; m_changed = false; } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java index 032e4a1c2..eb4d3c4bd 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/BlockPeripheral.java @@ -7,7 +7,7 @@ package dan200.computercraft.shared.peripheral.common; import dan200.computercraft.ComputerCraft; -import dan200.computercraft.shared.common.BlockDirectional; +import dan200.computercraft.shared.common.BlockGeneric; import dan200.computercraft.shared.common.TileGeneric; import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive; @@ -39,7 +39,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; import javax.annotation.Nonnull; -public class BlockPeripheral extends BlockDirectional +public class BlockPeripheral extends BlockGeneric { public static class Properties { @@ -466,11 +466,12 @@ public class BlockPeripheral extends BlockDirectional case DiskDrive: case Printer: { - if( stack.hasDisplayName() && tile instanceof ITilePeripheral ) + EnumFacing dir = DirectionUtil.fromEntityRot( player ); + if( stack.hasDisplayName() && tile instanceof TilePeripheralBase ) { - ITilePeripheral peripheral = (ITilePeripheral) tile; + TilePeripheralBase peripheral = (TilePeripheralBase) tile; peripheral.setLabel( stack.getDisplayName() ); - peripheral.setDirection( DirectionUtil.fromEntityRot( player ) ); + peripheral.setDirection( dir ); } break; } @@ -558,14 +559,18 @@ public class BlockPeripheral extends BlockDirectional @Override @Deprecated @Nonnull - public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos ) + public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess world, BlockPos pos ) { if( getPeripheralType( state ) == PeripheralType.WirelessModem ) { - return ModemBounds.getBounds( getDirection( source, pos ) ); + TileEntity tile = world.getTileEntity( pos ); + if( tile instanceof TileWirelessModem ) + { + return ModemBounds.getBounds( ((TileWirelessModem) tile).getDirection() ); + } } - return super.getBoundingBox( state, source, pos ); + return super.getBoundingBox( state, world, pos ); } @Override diff --git a/src/main/java/dan200/computercraft/shared/peripheral/common/ITilePeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/common/ITilePeripheral.java index 0d0bdb36c..e256eb06c 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/ITilePeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/ITilePeripheral.java @@ -6,13 +6,12 @@ package dan200.computercraft.shared.peripheral.common; -import dan200.computercraft.shared.common.IDirectionalTile; import dan200.computercraft.shared.peripheral.PeripheralType; /** * The tile for {@link BlockPeripheral}. */ -public interface ITilePeripheral extends IDirectionalTile +public interface ITilePeripheral { PeripheralType getPeripheralType(); 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 bd89df235..229dcd1bc 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/common/TilePeripheralBase.java @@ -73,6 +73,7 @@ public abstract class TilePeripheralBase extends TileGeneric implements IPeriphe return null; } + @Override public void setLabel( String label ) { m_label = label; diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/ServerMonitor.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/ServerMonitor.java index 7c0b238ea..a6ace0f71 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/ServerMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/ServerMonitor.java @@ -8,12 +8,16 @@ package dan200.computercraft.shared.peripheral.monitor; import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.shared.common.ServerTerminal; +import dan200.computercraft.shared.util.TickScheduler; + +import java.util.concurrent.atomic.AtomicBoolean; public class ServerMonitor extends ServerTerminal { private final TileMonitor origin; private int textScale = 2; - private boolean resized; + private final AtomicBoolean resized = new AtomicBoolean( false ); + private final AtomicBoolean changed = new AtomicBoolean( false ); public ServerMonitor( boolean colour, TileMonitor origin ) { @@ -41,10 +45,28 @@ public class ServerMonitor extends ServerTerminal if( oldWidth != termWidth || oldHeight != termHeight ) { getTerminal().clear(); - resized = true; + resized.set( true ); + markChanged(); } } + @Override + protected void markTerminalChanged() + { + super.markTerminalChanged(); + markChanged(); + } + + private void markChanged() + { + if( !changed.getAndSet( true ) ) TickScheduler.schedule( origin ); + } + + protected void clearChanged() + { + changed.set( false ); + } + public int getTextScale() { return textScale; @@ -57,14 +79,14 @@ public class ServerMonitor extends ServerTerminal rebuild(); } - public synchronized boolean pollResized() + public boolean pollResized() { - if( resized ) - { - resized = false; - return true; - } + return resized.getAndSet( false ); + } - return false; + public boolean pollTerminalChanged() + { + update(); + return hasTerminalChanged(); } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java index d09e5358a..253124e9f 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/monitor/TileMonitor.java @@ -11,15 +11,20 @@ import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.shared.common.ServerTerminal; +import dan200.computercraft.shared.common.TileGeneric; import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.common.BlockPeripheral; -import dan200.computercraft.shared.peripheral.common.TilePeripheralBase; +import dan200.computercraft.shared.peripheral.common.IPeripheralTile; +import dan200.computercraft.shared.peripheral.common.ITilePeripheral; +import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; +import net.minecraft.util.NonNullList; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -28,7 +33,7 @@ import javax.annotation.Nonnull; import java.util.HashSet; import java.util.Set; -public class TileMonitor extends TilePeripheralBase +public class TileMonitor extends TileGeneric implements ITilePeripheral, IPeripheralTile { // Statics @@ -78,6 +83,7 @@ public class TileMonitor extends TilePeripheralBase super.onLoad(); m_advanced = getBlockState().getValue( BlockPeripheral.Properties.VARIANT ) .getPeripheralType() == PeripheralType.AdvancedMonitor; + world.scheduleUpdate( getPos(), getBlockType(), 0 ); } @Override @@ -146,44 +152,32 @@ public class TileMonitor extends TilePeripheralBase } @Override - public void update() + public void updateTick() { - super.update(); + if( m_xIndex != 0 || m_yIndex != 0 || m_serverMonitor == null ) return; - if( !getWorld().isRemote ) + m_serverMonitor.clearChanged(); + + if( m_serverMonitor.pollResized() ) { - if( m_xIndex == 0 && m_yIndex == 0 && m_serverMonitor != null ) + for( int x = 0; x < m_width; x++ ) { - if( m_serverMonitor.pollResized() ) + for( int y = 0; y < m_height; y++ ) { - for( int x = 0; x < m_width; x++ ) - { - for( int y = 0; y < m_height; y++ ) - { - TileMonitor monitor = getNeighbour( x, y ); - if( monitor == null ) continue; + TileMonitor monitor = getNeighbour( x, y ); + if( monitor == null ) continue; - for( IComputerAccess computer : monitor.m_computers ) - { - computer.queueEvent( "monitor_resize", new Object[] { - computer.getAttachmentName() - } ); - } - } + for( IComputerAccess computer : monitor.m_computers ) + { + computer.queueEvent( "monitor_resize", new Object[] { + computer.getAttachmentName() + } ); } } + } + } - m_serverMonitor.update(); - if( m_serverMonitor.hasTerminalChanged() ) updateBlock(); - } - } - else - { - if( m_xIndex == 0 && m_yIndex == 0 && m_clientMonitor != null ) - { - m_clientMonitor.update(); - } - } + if( m_serverMonitor.pollTerminalChanged() ) updateBlock(); } // IPeripheralTile implementation @@ -197,6 +191,12 @@ public class TileMonitor extends TilePeripheralBase return m_peripheral; } + @Override + public PeripheralType getPeripheralType() + { + return m_advanced ? PeripheralType.AdvancedMonitor : PeripheralType.Monitor; + } + public ServerMonitor getCachedServerMonitor() { return m_serverMonitor; @@ -319,7 +319,6 @@ public class TileMonitor extends TilePeripheralBase } // Sizing and placement stuff - @Override public EnumFacing getDirection() { int dir = getDir() % 6; @@ -444,7 +443,7 @@ public class TileMonitor extends TilePeripheralBase return getSimilarMonitorAt( pos.offset( right, xOffset ).offset( down, yOffset ) ); } - public TileMonitor getOrigin() + private TileMonitor getOrigin() { return getNeighbour( 0, 0 ); } @@ -752,15 +751,16 @@ public class TileMonitor extends TilePeripheralBase { case Monitor: case AdvancedMonitor: - { return false; - } default: - { return true; - } } } } + @Override + public void getDroppedItems( @Nonnull NonNullList drops, boolean creative ) + { + if( !creative ) drops.add( PeripheralItemFactory.create( this ) ); + } } diff --git a/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java b/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java index 185b9c8a9..203073f44 100644 --- a/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java +++ b/src/main/java/dan200/computercraft/shared/pocket/core/PocketServerComputer.java @@ -177,6 +177,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces } } + @Override public void broadcastState( boolean force ) { super.broadcastState( force ); From 1f498dcc733df0655322fd3a4f905aff58010b7b Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sun, 20 Jan 2019 16:16:02 +0000 Subject: [PATCH 6/6] Backwards compat patch for Plethora Ughghgghghr. --- src/main/java/dan200/computercraft/ComputerCraft.java | 8 ++++++++ .../java/dan200/computercraft/shared/Registry.java | 8 ++++++++ .../shared/peripheral/speaker/SpeakerPeripheral.java | 11 +++++++++-- .../shared/peripheral/speaker/TileSpeaker.java | 2 +- .../pocket/peripherals/PocketSpeakerPeripheral.java | 2 +- .../shared/turtle/upgrades/TurtleSpeaker.java | 2 +- 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main/java/dan200/computercraft/ComputerCraft.java b/src/main/java/dan200/computercraft/ComputerCraft.java index f7162542c..e82d54a8d 100644 --- a/src/main/java/dan200/computercraft/ComputerCraft.java +++ b/src/main/java/dan200/computercraft/ComputerCraft.java @@ -229,6 +229,14 @@ public class ComputerCraft public static PocketModem wirelessModem; public static PocketModem advancedModem; public static PocketSpeaker speaker; + + @Deprecated + public static PocketSpeaker pocketSpeaker; + } + + @Deprecated + public static class Upgrades { + public static TurtleModem advancedModem; } // Registries diff --git a/src/main/java/dan200/computercraft/shared/Registry.java b/src/main/java/dan200/computercraft/shared/Registry.java index f2b323010..7ab87c33e 100644 --- a/src/main/java/dan200/computercraft/shared/Registry.java +++ b/src/main/java/dan200/computercraft/shared/Registry.java @@ -200,6 +200,7 @@ public final class Registry registerTurtleUpgrades(); registerPocketUpgrades(); + registerLegacyUpgrades(); } @SubscribeEvent @@ -336,6 +337,13 @@ public final class Registry ComputerCraftAPI.registerPocketUpgrade( ComputerCraft.PocketUpgrades.speaker ); } + @SuppressWarnings( "deprecation" ) + private static void registerLegacyUpgrades() + { + ComputerCraft.PocketUpgrades.pocketSpeaker = ComputerCraft.PocketUpgrades.speaker; + ComputerCraft.Upgrades.advancedModem = ComputerCraft.TurtleUpgrades.advancedModem; + } + @SubscribeEvent public static void remapItems( RegistryEvent.MissingMappings mappings ) { diff --git a/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java index f7dcc4455..07ffef4ac 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java @@ -16,6 +16,7 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.util.ResourceLocation; import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundEvent; +import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; @@ -39,7 +40,13 @@ public abstract class SpeakerPeripheral implements IPeripheral public abstract World getWorld(); - public abstract Vec3d getPos(); + public abstract Vec3d getPosition(); + + @Deprecated + public BlockPos getPos() + { + return new BlockPos( getPosition() ); + } public boolean madeSound( long ticks ) { @@ -125,7 +132,7 @@ public abstract class SpeakerPeripheral implements IPeripheral } World world = getWorld(); - Vec3d pos = getPos(); + Vec3d pos = getPosition(); context.issueMainThreadTask( () -> { MinecraftServer server = world.getMinecraftServer(); diff --git a/src/main/java/dan200/computercraft/shared/peripheral/speaker/TileSpeaker.java b/src/main/java/dan200/computercraft/shared/peripheral/speaker/TileSpeaker.java index f16f99d44..17dc20156 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/speaker/TileSpeaker.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/speaker/TileSpeaker.java @@ -59,7 +59,7 @@ public class TileSpeaker extends TilePeripheralBase } @Override - public Vec3d getPos() + public Vec3d getPosition() { BlockPos pos = speaker.getPos(); return new Vec3d( pos.getX(), pos.getY(), pos.getZ() ); diff --git a/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketSpeakerPeripheral.java b/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketSpeakerPeripheral.java index 624a74afc..dcdce9662 100644 --- a/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketSpeakerPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketSpeakerPeripheral.java @@ -29,7 +29,7 @@ public class PocketSpeakerPeripheral extends SpeakerPeripheral } @Override - public Vec3d getPos() + public Vec3d getPosition() { return world != null ? position : null; } diff --git a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java index bae009f61..e2f7206e7 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java +++ b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java @@ -48,7 +48,7 @@ public class TurtleSpeaker extends AbstractTurtleUpgrade } @Override - public Vec3d getPos() + public Vec3d getPosition() { BlockPos pos = turtle.getPosition(); return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );