From 822cba54e6dfac156adf7559b1c92e83a1be405d Mon Sep 17 00:00:00 2001 From: P3pp3rF1y Date: Mon, 23 Oct 2023 20:17:24 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Fix=20refill=20upgrade=20?= =?UTF-8?q?to=20not=20delete=20item=20in=20hand=20when=20block=20is=20midd?= =?UTF-8?q?le=20click=20picked=20from=20backpack=20but=20there's=20no=20pl?= =?UTF-8?q?ace=20for=20the=20item=20in=20player's=20hand=20in=20backpack?= =?UTF-8?q?=20or=20player's=20inventory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../upgrades/refill/RefillUpgradeWrapper.java | 47 +++++++++++++++---- .../sophisticatedbackpacks/lang/en_us.json | 1 + 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/gradle.properties b/gradle.properties index 2a514d29..758e5ed8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ org.gradle.daemon=false minecraft_version=1.19.2 forge_version=43.2.13 -mod_version=3.18.63 +mod_version=3.18.64 jei_mc_version=1.19.2-forge jei_version=11.6.0.+ curios_version=1.19.2-5.1.1.+ diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/refill/RefillUpgradeWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/refill/RefillUpgradeWrapper.java index a55e870f..54aa58d4 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/refill/RefillUpgradeWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/refill/RefillUpgradeWrapper.java @@ -15,7 +15,6 @@ import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemHandlerHelper; import net.p3pp3rf1y.sophisticatedbackpacks.api.IBlockPickResponseUpgrade; -import net.p3pp3rf1y.sophisticatedbackpacks.backpack.BackpackItem; import net.p3pp3rf1y.sophisticatedbackpacks.client.gui.SBPTranslationHelper; import net.p3pp3rf1y.sophisticatedcore.api.IStorageWrapper; import net.p3pp3rf1y.sophisticatedcore.inventory.ITrackedContentsItemHandler; @@ -143,19 +142,27 @@ public boolean pickBlock(Player player, ItemStack filter) { InventoryHelper.iterate(inventoryHandler, (slot, stack) -> { if (ItemHandlerHelper.canItemStacksStack(stack, filter)) { hasItemInBackpack.set(true); - if (stack.getCount() <= stack.getMaxStackSize()) { - stashSlot.set(slot); - } + stashSlot.set(slot); } }, () -> stashSlot.get() > -1); ItemStack mainHandItem = player.getMainHandItem(); - if (hasItemInBackpack.get() && !(mainHandItem.getItem() instanceof BackpackItem) && - (mainHandItem.isEmpty() || (stashSlot.get() > -1 && inventoryHandler.isItemValid(stashSlot.get(), mainHandItem)) || inventoryHandler.insertItem(mainHandItem, true).isEmpty())) { - ItemStack toExtract = filter.copy(); - toExtract.setCount(filter.getMaxStackSize()); - ItemStack extracted = InventoryHelper.extractFromInventory(toExtract, inventoryHandler, false); - if (!extracted.isEmpty()) { + ItemStack toExtract = filter.copy(); + toExtract.setCount(filter.getMaxStackSize()); + if (hasItemInBackpack.get() && !InventoryHelper.extractFromInventory(toExtract, inventoryHandler, true).isEmpty()) { + if ((inventoryHandler.getStackInSlot(stashSlot.get()).getCount() > filter.getMaxStackSize() || !inventoryHandler.isItemValid(stashSlot.get(), mainHandItem)) + && !inventoryHandler.insertItem(mainHandItem, true).isEmpty()) { + if (canMoveMainHandToInventory(player)) { + ItemStack extracted = InventoryHelper.extractFromInventory(toExtract, inventoryHandler, false); + player.setItemInHand(InteractionHand.MAIN_HAND, extracted); + player.getInventory().add(mainHandItem); + return true; + } else { + player.displayClientMessage(Component.translatable("gui.sophisticatedbackpacks.status.no_space_for_mainhand_item"), true); + return false; + } + } else { + ItemStack extracted = InventoryHelper.extractFromInventory(toExtract, inventoryHandler, false); inventoryHandler.insertItem(mainHandItem, false); player.setItemInHand(InteractionHand.MAIN_HAND, extracted); return true; @@ -164,6 +171,25 @@ public boolean pickBlock(Player player, ItemStack filter) { return false; } + private boolean canMoveMainHandToInventory(Player player) { + int countToAdd = player.getMainHandItem().getCount(); + for (int slot = 0; slot < player.getInventory().getContainerSize() - 5; slot++) { + if (slot == player.getInventory().selected) { + continue; + } + ItemStack slotStack = player.getInventory().getItem(slot); + if (slotStack.isEmpty()) { + return true; + } else if (ItemHandlerHelper.canItemStacksStack(slotStack, player.getMainHandItem())) { + countToAdd -= (slotStack.getMaxStackSize() - slotStack.getCount()); + if (countToAdd <= 0) { + return true; + } + } + } + return false; + } + public enum TargetSlot implements StringRepresentable { ANY("any", SBPTranslationHelper.INSTANCE.translUpgrade("refill.target_slot.any"), SBPTranslationHelper.INSTANCE.translUpgrade("refill.target_slot.any.tooltip").withStyle(ChatFormatting.DARK_GREEN), (player, playerInvHandler, filter) -> InventoryHelper.getCountMissingInHandler(playerInvHandler, filter, filter.getMaxStackSize()), @@ -309,3 +335,4 @@ private static ItemStack refillSlot(Supplier getSlotContents, ItemSta } } } + diff --git a/src/main/resources/assets/sophisticatedbackpacks/lang/en_us.json b/src/main/resources/assets/sophisticatedbackpacks/lang/en_us.json index bce2c5dc..4823829c 100644 --- a/src/main/resources/assets/sophisticatedbackpacks/lang/en_us.json +++ b/src/main/resources/assets/sophisticatedbackpacks/lang/en_us.json @@ -112,6 +112,7 @@ "gui.sophisticatedbackpacks.status.no_tool_found_for_entity": "No tool was found that works on entity", "gui.sophisticatedbackpacks.status.no_tool_swap_upgrade_present": "No upgrade present that can do tool swap on key press", "gui.sophisticatedbackpacks.status.backpack_cannot_be_open_by_another_player": "This backpack cannot be open by another player", + "gui.sophisticatedbackpacks.status.no_space_for_mainhand_item": "There's no place for item in hand in either backpack or player's inventory", "gui.sophisticatedbackpacks.settings.tooltip": "Backpack Settings", "gui.sophisticatedbackpacks.settings.title": "Backpack Settings", "gui.sophisticatedbackpacks.back_to_backpack.tooltip": "Back To Backpack",