Make spell books return to the slots they came from (provided the container isn't closed)

This commit is contained in:
Electroblob77
2020-04-09 20:08:28 +01:00
parent c5b5172481
commit d56cf37f2b
3 changed files with 111 additions and 7 deletions
@@ -20,11 +20,14 @@ import net.minecraft.tileentity.TileEntity;
public class VirtualSlot extends Slot {
private final TileEntity tileEntity;
/** Allows the virtual slot to remember what was last stored in it, so items can be put back in the same place. */
private ItemStack prevStack; // For now this doesn't persist over GUI close
public VirtualSlot(IInventory inventory, int index){
super(inventory, index, -999, -999);
if(!(inventory instanceof TileEntity)) throw new IllegalArgumentException("Inventory must be a tile entity!");
this.tileEntity = (TileEntity)inventory;
this.prevStack = getStack().copy(); // We MUST copy the stack or it will get changed from elsewhere later!
}
@Override
@@ -59,15 +62,28 @@ public class VirtualSlot extends Slot {
return isValid() ? super.onTake(player, stack) : ItemStack.EMPTY;
}
@Override
public void onSlotChanged(){
super.onSlotChanged();
if(this.getHasStack()) this.prevStack = this.getStack().copy(); // Ignore stack removal (insertion of empty stacks)
}
@Override
public ItemStack getStack(){
return isValid() ? super.getStack() : ItemStack.EMPTY;
}
/** Returns the stack that was last in this slot. */
public ItemStack getPrevStack(){
return prevStack;
}
@Override
public void putStack(ItemStack stack){
if(isValid() && inventory instanceof TileEntityBookshelf) ((TileEntityBookshelf)inventory).sync();
if(isValid()) super.putStack(stack);
if(isValid()){
if(inventory instanceof TileEntityBookshelf) ((TileEntityBookshelf)inventory).sync();
super.putStack(stack);
}
}
@Override