cleanup inventory change handling

This commit is contained in:
Florian Scandella
2017-08-31 13:37:26 +02:00
parent 1a918be658
commit a3269826f9
2 changed files with 37 additions and 31 deletions
@@ -219,34 +219,30 @@ public class AppEngInternalAEInventory implements IInternalItemHandler, Iterable
@Override
public void setStackInSlot( final int slot, final ItemStack newItemStack )
{
final ItemStack oldStack = this.getStackInSlot( slot );
ItemStack oldStack = this.getStackInSlot( slot ).copy();
this.inv[slot] = AEApi.instance().storage().createItemStack( newItemStack );
if( this.te != null && Platform.isServer() )
{
ItemStack removed = oldStack;
ItemStack added = newItemStack;
ItemStack newStack = newItemStack.copy();
InvOperation op = InvOperation.SET;
if( !oldStack.isEmpty() && !newItemStack.isEmpty() && Platform.itemComparisons().isEqualItem( oldStack, newItemStack ) )
if( Platform.itemComparisons().isEqualItem( oldStack, newStack ) )
{
if( oldStack.getCount() > newItemStack.getCount() )
if( newStack.getCount() > oldStack.getCount() )
{
removed = removed.copy();
removed.grow( -newItemStack.getCount() );
added = ItemStack.EMPTY;
}
else if( oldStack.getCount() < newItemStack.getCount() )
{
added = added.copy();
added.grow( -oldStack.getCount() );
removed = ItemStack.EMPTY;
newStack.shrink( oldStack.getCount() );
oldStack = ItemStack.EMPTY;
op = InvOperation.INSERT;
}
else
{
removed = added = ItemStack.EMPTY;
oldStack.shrink( newStack.getCount() );
newStack = ItemStack.EMPTY;
op = InvOperation.EXTRACT;
}
}
this.fireOnChangeInventory( slot, InvOperation.SET, removed, added );
this.fireOnChangeInventory( slot, op, oldStack, newStack );
}
}
@@ -255,6 +251,7 @@ public class AppEngInternalAEInventory implements IInternalItemHandler, Iterable
if( this.te != null && Platform.isServer() )
{
this.te.onChangeInventory( this, slot, op, removed, inserted );
this.te.saveChanges();
}
}
@@ -41,7 +41,6 @@ public class AppEngInternalInventory extends ItemStackHandler implements IIntern
private boolean enableClientEvents = false;
private IAEAppEngInventory te;
private final int[] maxStack;
private InvOperation currentOp;
private ItemStack previousStack = ItemStack.EMPTY;
private IAEItemFilter filter;
@@ -78,8 +77,7 @@ public class AppEngInternalInventory extends ItemStackHandler implements IIntern
@Override
public void setStackInSlot( int slot, @Nonnull ItemStack stack )
{
this.currentOp = InvOperation.SET;
this.previousStack = this.getStackInSlot( slot );
this.previousStack = this.getStackInSlot( slot ).copy();
super.setStackInSlot( slot, stack );
}
@@ -92,8 +90,10 @@ public class AppEngInternalInventory extends ItemStackHandler implements IIntern
return stack;
}
this.currentOp = InvOperation.INSERT;
this.previousStack = this.getStackInSlot( slot ).copy();
if( !simulate )
{
this.previousStack = this.getStackInSlot( slot ).copy();
}
return super.insertItem( slot, stack, simulate );
}
@@ -106,8 +106,10 @@ public class AppEngInternalInventory extends ItemStackHandler implements IIntern
return ItemStack.EMPTY;
}
this.currentOp = InvOperation.EXTRACT;
this.previousStack = this.getStackInSlot( slot );
if( !simulate )
{
this.previousStack = this.getStackInSlot( slot ).copy();
}
return super.extractItem( slot, amount, simulate );
}
@@ -116,22 +118,29 @@ public class AppEngInternalInventory extends ItemStackHandler implements IIntern
{
if( this.getTileEntity() != null && this.eventsEnabled() )
{
ItemStack added = this.getStackInSlot( slot ).copy();
ItemStack removed = this.previousStack.copy();
ItemStack newStack = this.getStackInSlot( slot ).copy();
ItemStack oldStack = this.previousStack;
InvOperation op = InvOperation.SET;
if( !added.isEmpty() && !removed.isEmpty() && Platform.itemComparisons().isEqualItem( added, removed ) )
if( newStack.isEmpty() || oldStack.isEmpty() || Platform.itemComparisons().isEqualItem( newStack, oldStack ) )
{
if( this.currentOp == InvOperation.INSERT )
if( newStack.getCount() > oldStack.getCount() )
{
added.grow( -removed.getCount() );
newStack.shrink( oldStack.getCount() );
oldStack = ItemStack.EMPTY;
op = InvOperation.INSERT;
}
else if( this.currentOp == InvOperation.EXTRACT )
else
{
removed.grow( -added.getCount() );
oldStack.shrink( newStack.getCount() );
newStack = ItemStack.EMPTY;
op = InvOperation.EXTRACT;
}
}
this.getTileEntity().onChangeInventory( this, slot, this.currentOp, removed, added );
this.getTileEntity().onChangeInventory( this, slot, op, oldStack, newStack );
this.getTileEntity().saveChanges();
this.previousStack = ItemStack.EMPTY;
}
super.onContentsChanged( slot );
}