From 6011b2dd49e69c18abbb7ca535abe6d8e4942ad0 Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Sun, 7 Jun 2020 12:41:32 +0200 Subject: [PATCH] Added custom method for updating block state from TileEntity state changes. --- .../java/appeng/block/AEBaseTileBlock.java | 24 ++++++++++++++++++ .../java/appeng/block/storage/BlockChest.java | 25 +++++++------------ src/main/java/appeng/tile/AEBaseTile.java | 16 ++++++------ 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/main/java/appeng/block/AEBaseTileBlock.java b/src/main/java/appeng/block/AEBaseTileBlock.java index 6109ab2c6..cad39ba7b 100644 --- a/src/main/java/appeng/block/AEBaseTileBlock.java +++ b/src/main/java/appeng/block/AEBaseTileBlock.java @@ -327,4 +327,28 @@ public abstract class AEBaseTileBlock extends AEBaseBlock return this.getTileEntity( w, pos ); } + /** + * Returns the BlockState based on the given BlockState while considering the state of the given TileEntity. + * + * If the given TileEntity is not of the right type for this block, the state is returned unchanged, + * this is also the case if the given block state does not belong to this block. + */ + public final BlockState getTileEntityBlockState(BlockState current, TileEntity te) { + if (current.getBlock() != this || !tileEntityClass.isInstance(te)) { + return current; + } + + return updateBlockStateFromTileEntity(current, tileEntityClass.cast(te)); + } + + /** + * Reimplement this in subclasses to allow tile-entities to update the state of their block when their own + * state changes. + * + * It is guaranteed that te is not-null and the block of the given block state is this exact block instance. + */ + protected BlockState updateBlockStateFromTileEntity(BlockState currentState, T te) { + return currentState; + } + } diff --git a/src/main/java/appeng/block/storage/BlockChest.java b/src/main/java/appeng/block/storage/BlockChest.java index b817d6c34..e75c68da3 100644 --- a/src/main/java/appeng/block/storage/BlockChest.java +++ b/src/main/java/appeng/block/storage/BlockChest.java @@ -62,27 +62,20 @@ public class BlockChest extends AEBaseTileBlock } @Override - public BlockState updatePostPlacement(BlockState state, Direction facing, BlockState facingState, IWorld worldIn, BlockPos pos, BlockPos facingPos) { - // FIXME Check tile-entity updated prop - + protected BlockState updateBlockStateFromTileEntity(BlockState currentState, TileChest te) { DriveSlotState slotState = DriveSlotState.EMPTY; - TileChest te = this.getTileEntity( worldIn, pos ); - - if( te != null ) + if( te.getCellCount() >= 1 ) { - if( te.getCellCount() >= 1 ) - { - slotState = DriveSlotState.fromCellStatus( te.getCellStatus( 0 ) ); - } - // Power-state has to be checked separately - if( !te.isPowered() && slotState != DriveSlotState.EMPTY ) - { - slotState = DriveSlotState.OFFLINE; - } + slotState = DriveSlotState.fromCellStatus( te.getCellStatus( 0 ) ); + } + // Power-state has to be checked separately + if( !te.isPowered() && slotState != DriveSlotState.EMPTY ) + { + slotState = DriveSlotState.OFFLINE; } - return state.with( SLOT_STATE, slotState ); + return currentState.with( SLOT_STATE, slotState ); } @Override diff --git a/src/main/java/appeng/tile/AEBaseTile.java b/src/main/java/appeng/tile/AEBaseTile.java index 84d57cfac..5ce6d3b35 100644 --- a/src/main/java/appeng/tile/AEBaseTile.java +++ b/src/main/java/appeng/tile/AEBaseTile.java @@ -25,7 +25,6 @@ import appeng.api.util.IConfigManager; import appeng.api.util.IConfigurableObject; import appeng.api.util.IOrientable; import appeng.block.AEBaseTileBlock; -import appeng.client.render.FacingToRotation; import appeng.core.AELog; import appeng.core.features.IStackSrc; import appeng.helpers.ICustomNameObject; @@ -298,14 +297,17 @@ public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile, // TODO: Optimize Network Load if( this.world != null ) { - // Let the block update + // Let the block update it's own state with our internal state changes BlockState currentState = getBlockState(); - BlockState newState = currentState.updatePostPlacement(Direction.EAST, currentState, world, pos, pos); - - AELog.blockUpdate( this.pos, currentState, newState, this ); - if (currentState != newState) { - this.world.setBlockState(pos, newState); + if (currentState.getBlock() instanceof AEBaseTileBlock) { + AEBaseTileBlock tileBlock = (AEBaseTileBlock) currentState.getBlock(); + BlockState newState = tileBlock.getTileEntityBlockState(currentState, this); + if (currentState != newState) { + AELog.blockUpdate(this.pos, currentState, newState, this); + this.world.setBlockState(pos, newState); + } } + this.requestModelDataUpdate(); } }