Skip to content

Commit

Permalink
fix: 🐛 Fix refill upgrade to not delete item in hand when block is mi…
Browse files Browse the repository at this point in the history
…ddle click picked from backpack but there's no place for the item in player's hand in backpack or player's inventory
  • Loading branch information
P3pp3rF1y committed Oct 23, 2023
1 parent 2fec745 commit 822cba5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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.+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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()),
Expand Down Expand Up @@ -309,3 +335,4 @@ private static ItemStack refillSlot(Supplier<ItemStack> getSlotContents, ItemSta
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 822cba5

Please sign in to comment.