From d7adec7814d89824351a7045db132449d4e30e4f Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Thu, 9 Apr 2020 18:20:31 +0300 Subject: Fix #43, fix null inventories not being converted to EmptyInventory There's no Nullable on InventoryProvider.getInventory, but some mods return null there. (cherry picked from commit 54cab8890fae9b3f7803b368fb97208d570447b1) --- .../cotton/gui/CottonCraftingController.java | 32 +++++++++++++--------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java b/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java index 54038ec..807cfc0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java @@ -113,15 +113,15 @@ public class CottonCraftingController extends CraftingContainer imple if (blockInventory!=null) { if (slot.inventory==blockInventory) { //Try to transfer the item from the block into the player's inventory - if (!this.insertItem(toTransfer, this.playerInventory, true)) { + if (!this.insertItem(toTransfer, this.playerInventory, true, player)) { return ItemStack.EMPTY; } - } else if (!this.insertItem(toTransfer, this.blockInventory, false)) { //Try to transfer the item from the player to the block + } else if (!this.insertItem(toTransfer, this.blockInventory, false, player)) { //Try to transfer the item from the player to the block return ItemStack.EMPTY; } } else { //There's no block, just swap between the player's storage and their hotbar - if (!swapHotbar(toTransfer, slotNumber, this.playerInventory)) { + if (!swapHotbar(toTransfer, slotNumber, this.playerInventory, player)) { return ItemStack.EMPTY; } } @@ -140,9 +140,9 @@ public class CottonCraftingController extends CraftingContainer imple } /** WILL MODIFY toInsert! Returns true if anything was inserted. */ - private boolean insertIntoExisting(ItemStack toInsert, Slot slot) { + private boolean insertIntoExisting(ItemStack toInsert, Slot slot, PlayerEntity player) { ItemStack curSlotStack = slot.getStack(); - if (!curSlotStack.isEmpty() && canStacksCombine(toInsert, curSlotStack)) { + if (!curSlotStack.isEmpty() && canStacksCombine(toInsert, curSlotStack) && slot.canTakeItems(player)) { int combinedAmount = curSlotStack.getCount() + toInsert.getCount(); if (combinedAmount <= toInsert.getMaxCount()) { toInsert.setCount(0); @@ -176,7 +176,7 @@ public class CottonCraftingController extends CraftingContainer imple return false; } - private boolean insertItem(ItemStack toInsert, Inventory inventory, boolean walkBackwards) { + private boolean insertItem(ItemStack toInsert, Inventory inventory, boolean walkBackwards, PlayerEntity player) { //Make a unified list of slots *only from this inventory* ArrayList inventorySlots = new ArrayList<>(); for(Slot slot : slots) { @@ -189,13 +189,13 @@ public class CottonCraftingController extends CraftingContainer imple if (walkBackwards) { for(int i=inventorySlots.size()-1; i>=0; i--) { Slot curSlot = inventorySlots.get(i); - if (insertIntoExisting(toInsert, curSlot)) inserted = true; + if (insertIntoExisting(toInsert, curSlot, player)) inserted = true; if (toInsert.isEmpty()) break; } } else { for(int i=0; i imple return inserted; } - private boolean swapHotbar(ItemStack toInsert, int slotNumber, Inventory inventory) { + private boolean swapHotbar(ItemStack toInsert, int slotNumber, Inventory inventory, PlayerEntity player) { //Feel out the slots to see what's storage versus hotbar ArrayList storageSlots = new ArrayList<>(); ArrayList hotbarSlots = new ArrayList<>(); @@ -246,7 +246,7 @@ public class CottonCraftingController extends CraftingContainer imple //swap from hotbar to storage for(int i=0; i imple //swap from storage to hotbar for(int i=0; i imple Block b = state.getBlock(); if (b instanceof InventoryProvider) { - return ((InventoryProvider)b).getInventory(state, world, pos); + Inventory inventory = ((InventoryProvider)b).getInventory(state, world, pos); + if (inventory != null) { + return inventory; + } } BlockEntity be = world.getBlockEntity(pos); if (be!=null) { if (be instanceof InventoryProvider) { - return ((InventoryProvider)be).getInventory(state, world, pos); + Inventory inventory = ((InventoryProvider)be).getInventory(state, world, pos); + if (inventory != null) { + return inventory; + } } else if (be instanceof Inventory) { return (Inventory)be; } -- cgit