From f7128b4c4b73c8fec70d728b0bfb0c6580027833 Mon Sep 17 00:00:00 2001 From: Sjouwer Date: Sat, 29 Jul 2023 15:22:35 +0200 Subject: [PATCH] Add ability to stay in the same selected hotbar slot when using pick block resolves #18 --- gradle.properties | 2 +- .../pickblockpro/config/ModConfig.java | 6 +++ .../pickblockpro/util/InventoryManager.java | 39 ++++++++++++++++--- .../assets/pickblockpro/lang/en_us.json | 2 + 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/gradle.properties b/gradle.properties index 20949d0..93463c8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,6 +17,6 @@ cloth_config_version=11.1.106 # Mod Properties - mod_version = 1.7.20-beta + mod_version = 1.7.20-beta.2 maven_group = io.github.sjouwer archives_base_name = Pick-Block-Pro diff --git a/src/main/java/io/github/sjouwer/pickblockpro/config/ModConfig.java b/src/main/java/io/github/sjouwer/pickblockpro/config/ModConfig.java index 2d3956d..2d18d94 100644 --- a/src/main/java/io/github/sjouwer/pickblockpro/config/ModConfig.java +++ b/src/main/java/io/github/sjouwer/pickblockpro/config/ModConfig.java @@ -135,6 +135,8 @@ static class LockedSlots { static class Inventory { @Tooltip private boolean searchThroughContainers = true; + @Tooltip + private boolean stayInSameSlot = false; @CollapsibleObject(startExpanded=true) @Tooltip private LockedSlots lockedSlots = new LockedSlots(); } @@ -326,6 +328,10 @@ public boolean searchContainers() { return inventory.searchThroughContainers; } + public boolean stayInSameSlot() { + return inventory.stayInSameSlot; + } + public boolean isSlotLocked(int slot) { return switch (slot) { case 0 -> inventory.lockedSlots.slot1; diff --git a/src/main/java/io/github/sjouwer/pickblockpro/util/InventoryManager.java b/src/main/java/io/github/sjouwer/pickblockpro/util/InventoryManager.java index 1f664af..683e5d1 100644 --- a/src/main/java/io/github/sjouwer/pickblockpro/util/InventoryManager.java +++ b/src/main/java/io/github/sjouwer/pickblockpro/util/InventoryManager.java @@ -6,6 +6,7 @@ import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.screen.slot.SlotActionType; import net.minecraft.text.Text; import static net.minecraft.entity.player.PlayerInventory.MAIN_SIZE; @@ -24,13 +25,27 @@ private InventoryManager(){ * @param item Item to give the player */ public static void pickOrPlaceItemInInventory(ItemStack item) { - if (client.interactionManager == null || client.player == null) { - PickBlockPro.LOGGER.error("Unable to place item inside inventory; no player and/or interaction manager"); + if (client.player == null) { + PickBlockPro.LOGGER.error("Unable to place item inside inventory; no player found"); return; } - boolean isCreative = client.player.getAbilities().creativeMode; PlayerInventory inventory = client.player.getInventory(); + int selectedSlot = inventory.selectedSlot; + pickOrPlaceItemInInventory(item, inventory); + int newSelectedSlot = inventory.selectedSlot; + + if (config.stayInSameSlot() && + selectedSlot != newSelectedSlot && + !isHotbarSlotLocked(selectedSlot) && + !isHotbarSlotLocked(newSelectedSlot)) { + swapSlot(selectedSlot, newSelectedSlot); + inventory.selectedSlot = selectedSlot; + } + } + + private static void pickOrPlaceItemInInventory(ItemStack item, PlayerInventory inventory) { + boolean isCreative = client.player.getAbilities().creativeMode; int stackSlot = inventory.getSlotWithStack(item); //Item is not inside the inventory, if in survival search through item-containers to see if they contain the item @@ -51,7 +66,7 @@ public static void pickOrPlaceItemInInventory(ItemStack item) { //Item is already inside the inventory, need to swap it to the hotbar if (stackSlot >= HOTBAR_SIZE) { - client.interactionManager.pickFromInventory(stackSlot); + swapSlot(inventory.selectedSlot, stackSlot); return; } @@ -67,6 +82,20 @@ public static void pickOrPlaceItemInInventory(ItemStack item) { } } + private static void swapSlot(int firstSlot, int secondSlot) { + if (client.interactionManager == null || client.player == null) { + PickBlockPro.LOGGER.error("Unable to swap inventory slot; no player and/or interaction manager"); + return; + } + + client.interactionManager.clickSlot( + client.player.playerScreenHandler.syncId, + MAIN_SIZE + firstSlot, + secondSlot, + SlotActionType.SWAP, + client.player); + } + /** * Searches through item-containers like Shulkers * @param inventory Player's inventory @@ -100,9 +129,7 @@ private static boolean setOptimalSlot(PlayerInventory inventory) { return false; } - //tick to update new selected slot inventory.selectedSlot = slot; - client.interactionManager.tick(); return true; } diff --git a/src/main/resources/assets/pickblockpro/lang/en_us.json b/src/main/resources/assets/pickblockpro/lang/en_us.json index fd0a1da..bade710 100644 --- a/src/main/resources/assets/pickblockpro/lang/en_us.json +++ b/src/main/resources/assets/pickblockpro/lang/en_us.json @@ -119,6 +119,8 @@ "text.autoconfig.pickblockpro/config.category.inventory_settings": "Inventory", "text.autoconfig.pickblockpro/config.option.inventory.searchThroughContainers": "Search through item-containers", "text.autoconfig.pickblockpro/config.option.inventory.searchThroughContainers.@Tooltip": "Also search through item-containers \nlike shulkers and bundles to find \nthe picked item (survival only)", + "text.autoconfig.pickblockpro/config.option.inventory.stayInSameSlot": "Stay in same hotbar slot", + "text.autoconfig.pickblockpro/config.option.inventory.stayInSameSlot.@Tooltip": "Tries to keep the selected hotbar slot \nthe same when using pick block. \nThis won't work with locked slots", "text.autoconfig.pickblockpro/config.option.inventory.lockedSlots": "Locked hotbar slots", "text.autoconfig.pickblockpro/config.option.inventory.lockedSlots.@Tooltip": "Items in a locked slot will not be \nswitched out or replaced, instead \nthe next available slot will be used", "text.autoconfig.pickblockpro/config.option.inventory.lockedSlots.slot1": "Lock 1st slot",