From aecc8b0889271321d9693c35c9e7bd2799a1a9b3 Mon Sep 17 00:00:00 2001 From: Electroblob77 <35599699+Electroblob77@users.noreply.github.com> Date: Mon, 13 Apr 2020 17:20:43 +0100 Subject: [PATCH] Okay it really is fixed now I promise --- .../inventory/ContainerArcaneWorkbench.java | 40 +++++++++++-------- .../inventory/ContainerBookshelf.java | 2 +- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main/java/electroblob/wizardry/inventory/ContainerArcaneWorkbench.java b/src/main/java/electroblob/wizardry/inventory/ContainerArcaneWorkbench.java index 926ffe14..3ef290d7 100644 --- a/src/main/java/electroblob/wizardry/inventory/ContainerArcaneWorkbench.java +++ b/src/main/java/electroblob/wizardry/inventory/ContainerArcaneWorkbench.java @@ -386,7 +386,7 @@ public class ContainerArcaneWorkbench extends Container implements ISpellSortabl // Not sure why mergeItemStack differentiates between full/partial merging, as far as I can tell the // following line will work for both cases slot.putStack(stack.splitStack(contents.getMaxStackSize())); - slot.onSlotChanged(); + //slot.onSlotChanged(); if(stack.isEmpty()) return true; // The whole stack has been merged, so we're done! @@ -520,24 +520,16 @@ public class ContainerArcaneWorkbench extends Container implements ISpellSortabl /** Called on initialisation and whenever a bookshelf is added or removed, to update the virtual slot list. */ public void refreshBookshelfSlots(){ - this.inventorySlots.removeAll(bookshelfSlots); - bookshelfSlots.clear(); + // We can't simply re-initialise everything because this method gets called whenever a bookshelf is updated, + // and that also happens whenever a stack is added or removed to update the book state on the client - and + // unfortunately there is no way for a world event listener to detect whether the tile entity changed or not + + // Remove slots that are no longer valid + this.inventorySlots.removeIf(s -> s instanceof VirtualSlot && !((VirtualSlot)s).isValid()); + bookshelfSlots.removeIf(s -> !s.isValid()); List bookshelves = BlockBookshelf.findNearbyBookshelves(tileentity.getWorld(), tileentity.getPos(), tileentity); - if(!bookshelves.isEmpty()){ - - for(IInventory bookshelf : bookshelves){ - for(int i = 0; i < bookshelf.getSizeInventory(); i++){ - VirtualSlot slot = new VirtualSlot(bookshelf, i); // This sets the slot INDEX (for the INVENTORY) - bookshelfSlots.add(slot); - this.addSlotToContainer(slot); // This sets the slot NUMBER (for the CONTAINER) - } - } - - if(tileentity.getWorld().isRemote) updateActiveBookshelfSlots(); - } - if(bookshelves.isEmpty() == hasBookshelves){ // If the bookshelf status changed // Move all the slots appropriately @@ -550,6 +542,22 @@ public class ContainerArcaneWorkbench extends Container implements ISpellSortabl hasBookshelves = !bookshelves.isEmpty(); } + // Ignore bookshelves we already have slots for + bookshelves.removeIf(b -> bookshelfSlots.stream().anyMatch(s -> s.inventory == b)); + + if(!bookshelves.isEmpty()){ + + for(IInventory bookshelf : bookshelves){ + for(int i = 0; i < bookshelf.getSizeInventory(); i++){ + VirtualSlot slot = new VirtualSlot(bookshelf, i); // This sets the slot INDEX (for the INVENTORY) + bookshelfSlots.add(slot); + this.addSlotToContainer(slot); // This sets the slot NUMBER (for the CONTAINER) + } + } + } + + if(tileentity.getWorld().isRemote) updateActiveBookshelfSlots(); + } } diff --git a/src/main/java/electroblob/wizardry/inventory/ContainerBookshelf.java b/src/main/java/electroblob/wizardry/inventory/ContainerBookshelf.java index 92520dea..5ece23a0 100644 --- a/src/main/java/electroblob/wizardry/inventory/ContainerBookshelf.java +++ b/src/main/java/electroblob/wizardry/inventory/ContainerBookshelf.java @@ -39,7 +39,7 @@ public class ContainerBookshelf extends Container { /** Called from individual slots when their item is changed or removed. */ public void onSlotChanged(){ - //this.tileentity.sync(); + this.tileentity.sync(); } @Override