diff --git a/gradle.properties b/gradle.properties index bbef78ba..0304ebb0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ org.gradle.daemon=false mod_id=sophisticatedbackpacks mod_group_id=sophisticatedbackpacks -mod_version=3.18.65 +mod_version=3.18.66 sonar_project_key=sophisticatedbackpacks:SophisticatedBackpacks github_package_url=https://maven.pkg.github.com/P3pp3rF1y/SophisticatedBackpacks 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..28faa58a 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()), 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",