Skip to content

Commit

Permalink
Add ability to stay in the same selected hotbar slot when using pick …
Browse files Browse the repository at this point in the history
…block

resolves #18
  • Loading branch information
Sjouwer committed Jul 29, 2023
1 parent 18119a5 commit f7128b4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/pickblockpro/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f7128b4

Please sign in to comment.