Skip to content

Commit

Permalink
Merge pull request #899 from P3pp3rF1y/1.18.x-dev
Browse files Browse the repository at this point in the history
fix: 🐛 Fix refill upgrade to not delete item in hand when block is mi…
  • Loading branch information
P3pp3rF1y authored Nov 11, 2023
2 parents d4d6eaf + ab23b1f commit eddfcb5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 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.18.2
forge_version=40.1.30
mod_version=3.18.65
mod_version=3.18.66
jei_mc_version=1.18.2
jei_version=9.7.0+
curios_version=1.18.2-5.0.6.+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.nbt.StringTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.LivingEntity;
Expand All @@ -16,11 +17,9 @@
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;
import net.p3pp3rf1y.sophisticatedcore.inventory.InventoryHandler;
import net.p3pp3rf1y.sophisticatedcore.upgrades.FilterLogic;
import net.p3pp3rf1y.sophisticatedcore.upgrades.IFilteredUpgrade;
import net.p3pp3rf1y.sophisticatedcore.upgrades.ITickableUpgrade;
Expand Down Expand Up @@ -145,19 +144,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(new TranslatableComponent("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 @@ -166,6 +173,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 @@ -311,3 +337,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 eddfcb5

Please sign in to comment.