aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuuxel <6596629+Juuxel@users.noreply.github.com>2020-04-09 18:20:31 +0300
committerJuuxel <6596629+Juuxel@users.noreply.github.com>2020-04-09 18:35:38 +0300
commitd7adec7814d89824351a7045db132449d4e30e4f (patch)
treeb028e8cf99d1f6fa27938bb75a09dbbcb6798aa2 /src
parent4d24ab1bb8f54b471cf543d72af3cc9e95ee10d0 (diff)
downloadLibGui-d7adec7814d89824351a7045db132449d4e30e4f.tar.gz
LibGui-d7adec7814d89824351a7045db132449d4e30e4f.tar.bz2
LibGui-d7adec7814d89824351a7045db132449d4e30e4f.zip
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)
Diffstat (limited to 'src')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java32
1 files 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<Inventory> 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<Inventory> 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<Inventory> 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<Slot> inventorySlots = new ArrayList<>();
for(Slot slot : slots) {
@@ -189,13 +189,13 @@ public class CottonCraftingController extends CraftingContainer<Inventory> 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<inventorySlots.size(); i++) {
Slot curSlot = inventorySlots.get(i);
- if (insertIntoExisting(toInsert, curSlot)) inserted = true;
+ if (insertIntoExisting(toInsert, curSlot, player)) inserted = true;
if (toInsert.isEmpty()) break;
}
@@ -222,7 +222,7 @@ public class CottonCraftingController extends CraftingContainer<Inventory> 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<Slot> storageSlots = new ArrayList<>();
ArrayList<Slot> hotbarSlots = new ArrayList<>();
@@ -246,7 +246,7 @@ public class CottonCraftingController extends CraftingContainer<Inventory> imple
//swap from hotbar to storage
for(int i=0; i<storageSlots.size(); i++) {
Slot curSlot = storageSlots.get(i);
- if (insertIntoExisting(toInsert, curSlot)) inserted = true;
+ if (insertIntoExisting(toInsert, curSlot, player)) inserted = true;
if (toInsert.isEmpty()) break;
}
if (!toInsert.isEmpty()) {
@@ -260,7 +260,7 @@ public class CottonCraftingController extends CraftingContainer<Inventory> imple
//swap from storage to hotbar
for(int i=0; i<hotbarSlots.size(); i++) {
Slot curSlot = hotbarSlots.get(i);
- if (insertIntoExisting(toInsert, curSlot)) inserted = true;
+ if (insertIntoExisting(toInsert, curSlot, player)) inserted = true;
if (toInsert.isEmpty()) break;
}
if (!toInsert.isEmpty()) {
@@ -343,13 +343,19 @@ public class CottonCraftingController extends CraftingContainer<Inventory> 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;
}