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 @Override
public void setStackInSlot( final int slot, final ItemStack newItemStack ) 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 ); this.inv[slot] = AEApi.instance().storage().createItemStack( newItemStack );
if( this.te != null && Platform.isServer() ) if( this.te != null && Platform.isServer() )
{ {
ItemStack removed = oldStack; ItemStack newStack = newItemStack.copy();
ItemStack added = newItemStack; 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(); newStack.shrink( oldStack.getCount() );
removed.grow( -newItemStack.getCount() ); oldStack = ItemStack.EMPTY;
added = ItemStack.EMPTY; op = InvOperation.INSERT;
}
else if( oldStack.getCount() < newItemStack.getCount() )
{
added = added.copy();
added.grow( -oldStack.getCount() );
removed = ItemStack.EMPTY;
} }
else 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() ) if( this.te != null && Platform.isServer() )
{ {
this.te.onChangeInventory( this, slot, op, removed, inserted ); 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 boolean enableClientEvents = false;
private IAEAppEngInventory te; private IAEAppEngInventory te;
private final int[] maxStack; private final int[] maxStack;
private InvOperation currentOp;
private ItemStack previousStack = ItemStack.EMPTY; private ItemStack previousStack = ItemStack.EMPTY;
private IAEItemFilter filter; private IAEItemFilter filter;
@@ -78,8 +77,7 @@ public class AppEngInternalInventory extends ItemStackHandler implements IIntern
@Override @Override
public void setStackInSlot( int slot, @Nonnull ItemStack stack ) public void setStackInSlot( int slot, @Nonnull ItemStack stack )
{ {
this.currentOp = InvOperation.SET; this.previousStack = this.getStackInSlot( slot ).copy();
this.previousStack = this.getStackInSlot( slot );
super.setStackInSlot( slot, stack ); super.setStackInSlot( slot, stack );
} }
@@ -92,8 +90,10 @@ public class AppEngInternalInventory extends ItemStackHandler implements IIntern
return stack; return stack;
} }
this.currentOp = InvOperation.INSERT; if( !simulate )
this.previousStack = this.getStackInSlot( slot ).copy(); {
this.previousStack = this.getStackInSlot( slot ).copy();
}
return super.insertItem( slot, stack, simulate ); return super.insertItem( slot, stack, simulate );
} }
@@ -106,8 +106,10 @@ public class AppEngInternalInventory extends ItemStackHandler implements IIntern
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
this.currentOp = InvOperation.EXTRACT; if( !simulate )
this.previousStack = this.getStackInSlot( slot ); {
this.previousStack = this.getStackInSlot( slot ).copy();
}
return super.extractItem( slot, amount, simulate ); return super.extractItem( slot, amount, simulate );
} }
@@ -116,22 +118,29 @@ public class AppEngInternalInventory extends ItemStackHandler implements IIntern
{ {
if( this.getTileEntity() != null && this.eventsEnabled() ) if( this.getTileEntity() != null && this.eventsEnabled() )
{ {
ItemStack added = this.getStackInSlot( slot ).copy(); ItemStack newStack = this.getStackInSlot( slot ).copy();
ItemStack removed = this.previousStack.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.getTileEntity().saveChanges();
this.previousStack = ItemStack.EMPTY;
} }
super.onContentsChanged( slot ); super.onContentsChanged( slot );
} }