1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00

Fix turtle peripherals becoming desynced

When a turtle was unloaded but not actually disposed of, the
m_peripheral map hangs around. As a result, when creating a new
ServerComputer, the peripherals aren't considered changed and so they're
never attached.

Fixes #50.

Also fix that blumin' deprecated method which has been around for a wee
while now.
This commit is contained in:
SquidDev 2019-02-18 23:28:51 +00:00
parent e46ab1e267
commit 11e4d0de82
4 changed files with 23 additions and 51 deletions

View File

@ -55,12 +55,7 @@ public final boolean onBlockActivated( World world, BlockPos pos, IBlockState st
public final void neighborChanged( IBlockState state, World world, BlockPos pos, Block block, BlockPos neighbour )
{
TileEntity tile = world.getTileEntity( pos );
if( tile instanceof TileGeneric )
{
TileGeneric generic = (TileGeneric) tile;
generic.onNeighbourChange();
generic.onNeighbourChange( neighbour );
}
if( tile instanceof TileGeneric ) ((TileGeneric) tile).onNeighbourChange( neighbour );
}
@Override

View File

@ -63,8 +63,10 @@ public void onNeighbourChange()
{
}
@SuppressWarnings( "deprecation" )
public void onNeighbourChange( @Nonnull BlockPos neighbour )
{
onNeighbourChange();
}
public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour )

View File

@ -80,7 +80,7 @@ public TileTurtle( ComputerFamily family )
m_inventory = NonNullList.withSize( INVENTORY_SIZE, ItemStack.EMPTY );
m_previousInventory = NonNullList.withSize( INVENTORY_SIZE, ItemStack.EMPTY );
m_inventoryChanged = false;
m_brain = createBrain();
m_brain = new TurtleBrain( this );
m_moveState = MoveState.NOT_MOVED;
m_family = family;
}
@ -90,21 +90,12 @@ public boolean hasMoved()
return m_moveState == MoveState.MOVED;
}
protected TurtleBrain createBrain()
{
return new TurtleBrain( this );
}
protected final ServerComputer createComputer( int instanceID, int id, int termWidth, int termHeight )
@Override
protected ServerComputer createComputer( int instanceID, int id )
{
ServerComputer computer = new ServerComputer(
getWorld(),
id,
m_label,
instanceID,
getFamily(),
termWidth,
termHeight
getWorld(), id, m_label, instanceID, getFamily(),
ComputerCraft.terminalWidth_turtle, ComputerCraft.terminalHeight_turtle
);
computer.setPosition( getPos() );
computer.addAPI( new TurtleAPI( computer.getAPIEnvironment(), getAccess() ) );
@ -112,12 +103,6 @@ protected final ServerComputer createComputer( int instanceID, int id, int termW
return computer;
}
@Override
protected ServerComputer createComputer( int instanceID, int id )
{
return createComputer( instanceID, id, ComputerCraft.terminalWidth_turtle, ComputerCraft.terminalHeight_turtle );
}
@Override
public ComputerProxy createProxy()
{

View File

@ -54,7 +54,7 @@ public class TurtleBrain implements ITurtleAccess
private int m_commandsIssued = 0;
private Map<TurtleSide, ITurtleUpgrade> m_upgrades = new EnumMap<>( TurtleSide.class );
private Map<TurtleSide, IPeripheral> m_peripherals = new EnumMap<>( TurtleSide.class );
private Map<TurtleSide, IPeripheral> peripherals = new EnumMap<>( TurtleSide.class );
private Map<TurtleSide, NBTTagCompound> m_upgradeNBTData = new EnumMap<>( TurtleSide.class );
private int m_selectedSlot = 0;
@ -824,9 +824,9 @@ public void setUpgrade( @Nonnull TurtleSide side, ITurtleUpgrade upgrade )
@Override
public IPeripheral getPeripheral( @Nonnull TurtleSide side )
{
if( m_peripherals.containsKey( side ) )
if( peripherals.containsKey( side ) )
{
return m_peripherals.get( side );
return peripherals.get( side );
}
return null;
}
@ -924,13 +924,9 @@ private int toDirection( TurtleSide side )
}
}
public void updatePeripherals( ServerComputer serverComputer )
private void updatePeripherals( ServerComputer serverComputer )
{
if( serverComputer == null )
{
// Nothing to do
return;
}
if( serverComputer == null ) return;
// Update peripherals
for( TurtleSide side : TurtleSide.values() )
@ -942,26 +938,20 @@ public void updatePeripherals( ServerComputer serverComputer )
peripheral = upgrade.createPeripheral( this, side );
}
int dir = toDirection( side );
if( peripheral != null )
IPeripheral existing = peripherals.get( side );
if( existing == peripheral || (existing != null && peripheral != null && existing.equals( peripheral )) )
{
if( !m_peripherals.containsKey( side ) )
{
serverComputer.setPeripheral( dir, peripheral );
m_peripherals.put( side, peripheral );
}
else if( !m_peripherals.get( side ).equals( peripheral ) )
{
serverComputer.setPeripheral( dir, peripheral );
m_peripherals.remove( side );
m_peripherals.put( side, peripheral );
}
// If the peripheral is the same, just use that.
peripheral = existing;
}
else if( m_peripherals.containsKey( side ) )
else
{
serverComputer.setPeripheral( dir, null );
m_peripherals.remove( side );
// Otherwise update our map
peripherals.put( side, peripheral );
}
// Always update the computer: it may not be the same computer as before!
serverComputer.setPeripheral( toDirection( side ), peripheral );
}
}