1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-29 00:23:23 +00:00

Merge pull request #180 from boq/master

Handle tile entity changes
This commit is contained in:
Daniel Ratcliffe 2017-05-04 21:42:34 +01:00 committed by GitHub
commit 17a3e120e8
5 changed files with 152 additions and 46 deletions

View File

@ -46,7 +46,7 @@ protected BlockGeneric( Material material )
public final void dropBlockAsItemWithChance( World world, BlockPos pos, IBlockState state, float chance, int fortune ) public final void dropBlockAsItemWithChance( World world, BlockPos pos, IBlockState state, float chance, int fortune )
{ {
} }
@Override @Override
public final List<ItemStack> getDrops( IBlockAccess world, BlockPos pos, IBlockState state, int fortune ) public final List<ItemStack> getDrops( IBlockAccess world, BlockPos pos, IBlockState state, int fortune )
{ {
@ -107,7 +107,7 @@ public final void dropItem( World world, BlockPos pos, ItemStack stack )
{ {
Block.spawnAsEntity( world, pos, stack ); Block.spawnAsEntity( world, pos, stack );
} }
@Override @Override
public final void breakBlock( World world, BlockPos pos, IBlockState newState ) public final void breakBlock( World world, BlockPos pos, IBlockState newState )
{ {
@ -162,6 +162,17 @@ public final void neighborChanged( IBlockState state, World world, BlockPos pos,
} }
} }
@Override
public final void onNeighborChange( IBlockAccess world, BlockPos pos, BlockPos neighbour )
{
TileEntity tile = world.getTileEntity( pos );
if( tile instanceof TileGeneric )
{
TileGeneric generic = (TileGeneric)tile;
generic.onNeighbourTileEntityChange( neighbour );
}
}
@Override @Override
public final boolean isSideSolid( IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing side ) public final boolean isSideSolid( IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing side )
{ {

View File

@ -95,6 +95,10 @@ public void onNeighbourChange()
{ {
} }
public void onNeighbourTileEntityChange( BlockPos neighbour )
{
}
public boolean isSolidOnSide( int side ) public boolean isSolidOnSide( int side )
{ {
return true; return true;

View File

@ -197,6 +197,12 @@ public void onNeighbourChange()
updateInput(); updateInput();
} }
@Override
public void onNeighbourTileEntityChange( BlockPos neighbour )
{
updateInput( neighbour );
}
@Override @Override
public void update() public void update()
{ {
@ -308,6 +314,21 @@ protected int remapLocalSide( int localSide )
return localSide; return localSide;
} }
private void updateSideInput( ServerComputer computer, EnumFacing dir, BlockPos offset )
{
EnumFacing offsetSide = dir.getOpposite();
int localDir = remapLocalSide( DirectionUtil.toLocal( this, dir ) );
if( !isRedstoneBlockedOnSide( localDir ) )
{
computer.setRedstoneInput( localDir, RedstoneUtil.getRedstoneOutput( worldObj, offset, offsetSide ) );
computer.setBundledRedstoneInput( localDir, RedstoneUtil.getBundledRedstoneOutput( worldObj, offset, offsetSide ) );
}
if( !isPeripheralBlockedOnSide( localDir ) )
{
computer.setPeripheral( localDir, PeripheralUtil.getPeripheral( worldObj, offset, offsetSide ) );
}
}
public void updateInput() public void updateInput()
{ {
if( worldObj == null || worldObj.isRemote ) if( worldObj == null || worldObj.isRemote )
@ -316,6 +337,24 @@ public void updateInput()
} }
// Update redstone and peripherals // Update redstone and peripherals
ServerComputer computer = getServerComputer();
if( computer != null )
{
BlockPos pos = computer.getPosition();
for( EnumFacing dir : EnumFacing.VALUES )
{
updateSideInput( computer, dir, pos.offset( dir ) );
}
}
}
public void updateInput( BlockPos neighbour )
{
if( worldObj == null || worldObj.isRemote )
{
return;
}
ServerComputer computer = getServerComputer(); ServerComputer computer = getServerComputer();
if( computer != null ) if( computer != null )
{ {
@ -323,16 +362,10 @@ public void updateInput()
for( EnumFacing dir : EnumFacing.VALUES ) for( EnumFacing dir : EnumFacing.VALUES )
{ {
BlockPos offset = pos.offset( dir ); BlockPos offset = pos.offset( dir );
EnumFacing offsetSide = dir.getOpposite(); if ( offset.equals( neighbour ) )
int localDir = remapLocalSide( DirectionUtil.toLocal( this, dir ) );
if( !isRedstoneBlockedOnSide( localDir ) )
{ {
computer.setRedstoneInput( localDir, RedstoneUtil.getRedstoneOutput( worldObj, offset, offsetSide ) ); updateSideInput( computer, dir, offset );
computer.setBundledRedstoneInput( localDir, RedstoneUtil.getBundledRedstoneOutput( worldObj, offset, offsetSide ) ); break;
}
if( !isPeripheralBlockedOnSide( localDir ) )
{
computer.setPeripheral( localDir, PeripheralUtil.getPeripheral( worldObj, offset, offsetSide ) );
} }
} }
} }

View File

@ -46,12 +46,19 @@ public class TileTurtle extends TileComputerBase
public static final int INVENTORY_HEIGHT = 4; public static final int INVENTORY_HEIGHT = 4;
// Members // Members
enum MoveState
{
NOT_MOVED,
IN_PROGRESS,
MOVED
}
private ItemStack[] m_inventory; private ItemStack[] m_inventory;
private ItemStack[] m_previousInventory; private ItemStack[] m_previousInventory;
private boolean m_inventoryChanged; private boolean m_inventoryChanged;
private TurtleBrain m_brain; private TurtleBrain m_brain;
private boolean m_moved; private MoveState m_moveState;
public TileTurtle() public TileTurtle()
{ {
@ -59,12 +66,12 @@ public TileTurtle()
m_previousInventory = new ItemStack[ getSizeInventory() ]; m_previousInventory = new ItemStack[ getSizeInventory() ];
m_inventoryChanged = false; m_inventoryChanged = false;
m_brain = createBrain(); m_brain = createBrain();
m_moved = false; m_moveState = MoveState.NOT_MOVED;
} }
public boolean hasMoved() public boolean hasMoved()
{ {
return m_moved; return m_moveState == MoveState.MOVED;
} }
protected TurtleBrain createBrain() protected TurtleBrain createBrain()
@ -276,6 +283,41 @@ public void update()
} }
} }
@Override
public void onNeighbourChange()
{
if ( m_moveState == MoveState.NOT_MOVED )
{
super.onNeighbourChange();
}
}
@Override
public void onNeighbourTileEntityChange(BlockPos neighbour)
{
if ( m_moveState == MoveState.NOT_MOVED )
{
super.onNeighbourTileEntityChange( neighbour );
}
}
public void notifyMoveStart()
{
if (m_moveState == MoveState.NOT_MOVED)
{
m_moveState = MoveState.IN_PROGRESS;
}
}
public void notifyMoveEnd()
{
// MoveState.MOVED is final
if (m_moveState == MoveState.IN_PROGRESS)
{
m_moveState = MoveState.NOT_MOVED;
}
}
@Override @Override
public void readFromNBT( NBTTagCompound nbttagcompound ) public void readFromNBT( NBTTagCompound nbttagcompound )
{ {
@ -401,7 +443,7 @@ public float getToolRenderAngle( TurtleSide side, float f )
} }
// IInventory // IInventory
@Override @Override
public int getSizeInventory() public int getSizeInventory()
{ {
@ -431,7 +473,7 @@ public ItemStack removeStackFromSlot( int slot )
return result; return result;
} }
} }
@Override @Override
public ItemStack decrStackSize( int slot, int count ) public ItemStack decrStackSize( int slot, int count )
{ {
@ -447,13 +489,13 @@ public ItemStack decrStackSize( int slot, int count )
{ {
return null; return null;
} }
if( stack.stackSize <= count ) if( stack.stackSize <= count )
{ {
setInventorySlotContents( slot, null ); setInventorySlotContents( slot, null );
return stack; return stack;
} }
ItemStack part = stack.splitStack( count ); ItemStack part = stack.splitStack( count );
onInventoryDefinitelyChanged(); onInventoryDefinitelyChanged();
return part; return part;
@ -496,7 +538,7 @@ public void clear()
} }
} }
} }
@Override @Override
public String getName() public String getName()
{ {
@ -550,7 +592,7 @@ public int getInventoryStackLimit()
public void openInventory( EntityPlayer player ) public void openInventory( EntityPlayer player )
{ {
} }
@Override @Override
public void closeInventory( EntityPlayer player ) public void closeInventory( EntityPlayer player )
{ {
@ -561,7 +603,7 @@ public boolean isItemValidForSlot( int slot, ItemStack stack )
{ {
return true; return true;
} }
@Override @Override
public void markDirty() public void markDirty()
{ {
@ -664,6 +706,6 @@ public void transferStateFrom( TileTurtle copy )
m_inventoryChanged = copy.m_inventoryChanged; m_inventoryChanged = copy.m_inventoryChanged;
m_brain = copy.m_brain; m_brain = copy.m_brain;
m_brain.setOwner( this ); m_brain.setOwner( this );
copy.m_moved = true; copy.m_moveState = MoveState.MOVED;
} }
} }

View File

@ -495,6 +495,7 @@ public boolean teleportTo( World world, BlockPos pos )
// Cache info about the old turtle (so we don't access this after we delete ourselves) // Cache info about the old turtle (so we don't access this after we delete ourselves)
World oldWorld = getWorld(); World oldWorld = getWorld();
TileTurtle oldOwner = m_owner;
BlockPos oldPos = m_owner.getPos(); BlockPos oldPos = m_owner.getPos();
Block oldBlock = m_owner.getBlock(); Block oldBlock = m_owner.getBlock();
@ -504,36 +505,51 @@ public boolean teleportTo( World world, BlockPos pos )
return true; return true;
} }
// Create a new turtle if ( !world.isBlockLoaded( pos ) )
if( world.isBlockLoaded( pos ) && world.setBlockState( pos, oldBlock.getDefaultState(), 0 ) )
{ {
Block block = world.getBlockState( pos ).getBlock(); return false;
if( block == oldBlock ) }
oldOwner.notifyMoveStart();
try
{
// Create a new turtle
if( world.setBlockState( pos, oldBlock.getDefaultState(), 0 ) )
{ {
TileEntity newTile = world.getTileEntity( pos ); Block block = world.getBlockState( pos ).getBlock();
if( newTile != null && newTile instanceof TileTurtle ) if( block == oldBlock )
{ {
// Copy the old turtle state into the new turtle TileEntity newTile = world.getTileEntity( pos );
TileTurtle newTurtle = (TileTurtle)newTile; if( newTile != null && newTile instanceof TileTurtle )
newTurtle.setWorldObj( world ); {
newTurtle.setPos( pos ); // Copy the old turtle state into the new turtle
newTurtle.transferStateFrom( m_owner ); TileTurtle newTurtle = (TileTurtle)newTile;
newTurtle.createServerComputer().setWorld( world ); newTurtle.setWorldObj( world );
newTurtle.createServerComputer().setPosition( pos ); newTurtle.setPos( pos );
newTurtle.transferStateFrom( oldOwner );
newTurtle.createServerComputer().setWorld( world );
newTurtle.createServerComputer().setPosition( pos );
// Remove the old turtle // Remove the old turtle
oldWorld.setBlockToAir( oldPos ); oldWorld.setBlockToAir( oldPos );
// Make sure everybody knows about it // Make sure everybody knows about it
newTurtle.updateBlock(); newTurtle.updateBlock();
newTurtle.updateInput(); newTurtle.updateInput();
newTurtle.updateOutput(); newTurtle.updateOutput();
return true; return true;
}
} }
}
// Something went wrong, remove the newly created turtle // Something went wrong, remove the newly created turtle
world.setBlockToAir( pos ); world.setBlockToAir( pos );
}
}
finally
{
// whatever happens, unblock old turtle in case it's still in world
oldOwner.notifyMoveEnd();
} }
return false; return false;