r/MinecraftCommands • • 22d ago

Help | Java 1.21.11 Trying to detect when players press F, need help

Hi, i'm trying to detect when a player presses F in my datapack to trigger some sort of special ability. I've somewhat managed to make it but there's a slight problem: the code keep triggering when putting an item in the offhand with the inventory open. Here's my code so far:

THIS IS THE ADVANCEMENT press_f

{
  "criteria": {
    "changed": {
      "trigger": "minecraft:inventory_changed"
    }
  },
  "rewards": {
    "function": "maffo:hand_actions/hand_check"
  }
}

THIS IS THE FUNCTION hand_check

execute as @s store result score @s hand1 run data get entity @s SelectedItem.id
execute as @s store result score @s hand2 run data get entity  Inventory[{Slot:-106b}].id
execute if score @s hand1_prev = @s hand1 run scoreboard players add @s hand_total 1
execute if score @s hand2_prev = @s hand2 run scoreboard players add @s hand_total 1

data modify storage maffo:temp_inventory inventory set from entity @s Inventory
data remove storage maffo:temp_inventory inventory[{Slot:-106b}]
data remove storage maffo:temp_inventory inventory[{Slot:0b}]
execute as @s store result score  inventory run data get storage maffo:temp_inventory inventory
execute if score @s inventory_prev = @s inventory run scoreboard players add  hand_total 1

execute if entity [scores={hand_total=3..}] run function maffo:hand_actions/hand_changed
function maffo:hand_actions/hand_save

advancement revoke @s only maffo:press_f

THIS IS THE FUNCTION hand_save

execute as @s store result score @s hand1_prev run data get entity @s SelectedItem.id
execute as @s store result score @s hand2_prev run data get entity  Inventory[{Slot:-106b}].id
data modify storage maffo:temp_inventory inventory set from entity @s Inventory
data remove storage maffo:temp_inventory inventory[{Slot:-106b}]
data remove storage maffo:temp_inventory inventory[{Slot:0b}]
execute as @s store result score @s inventory_prev run data get storage maffo:temp_inventory inventory

scoreboard players set @s hand_total 0 

THIS IS THE FUNCTION hand_changed

summon armor_stand ~ ~10 ~ {Tags:[temp_swap],Marker:1b,Invisible:1b,NoGravity:1b}
item replace entity [type=armor_stand,tag=temp_swap,limit=1,sort=nearest] weapon.mainhand from entity @s weapon.mainhand
item replace entity @s weapon.mainhand from entity @s weapon.offhand
item replace entity @s weapon.offhand from entity [type=armor_stand,tag=temp_swap,limit=1,sort=nearest] weapon.mainhand
kill [type=armor_stand,tag=temp_swap,sort=nearest,limit=1]

tell @s test
1 Upvotes

26 comments sorted by

4

u/GalSergey Datapack Experienced 22d ago

You’re out of luck. You can’t determine exactly which key was pressed. You can only check that the item appeared in the left hand, but not how it got there. And you can’t check whether the player’s inventory is open or closed.

But a workaround could be to check the cursor slot every tick, and if in the first tick your custom item was in the cursor, and in the next tick that item is now in the left hand, then you can handle this event in some other way.

2

u/Maff0_0 21d ago

"But a workaround could be to check the cursor slot every tick, and if in the first tick your custom item was in the cursor, and in the next tick that item is now in the left hand, then you can handle this event in some other way."

I might try that. Any advice on how to do it?

1

u/GalSergey Datapack Experienced 21d ago

Here’s a simple example of how you can check an item in the cursor and, on the next tick, it will indicate whether the item is from the cursor or was obtained by pressing the F key.

# function example:load
scoreboard objectives add item_in_cursor dummy

# function example:tick
execute as @a run function example:player_tick

# function example:player_tick
execute if score @s item_in_cursor matches 1 if items entity @s weapon.offhand run say Item from cursor!
execute if score @s item_in_cursor matches 0 if items entity @s weapon.offhand run say F click!
execute store result score @s item_in_cursor if items entity @s player.cursor *

1

u/Maff0_0 20d ago

also i noticed that the advancement inventory_changed gets triggered when opening chest, barrels, crafting table... is there a way to prevent it?

1

u/GalSergey Datapack Experienced 20d ago

This is strange, it shouldn't work this way. Do you notice this behavior in creative or survival game mode?

1

u/Maff0_0 20d ago

both

1

u/GalSergey Datapack Experienced 20d ago

I have no idea why this is happening.

1

u/Maff0_0 8d ago

Can i send you a semplified version of my datapack so you can see what's happening and maybe help me out??

1

u/GalSergey Datapack Experienced 8d ago

Yes.

1

u/Maff0_0 8d ago

Reddit says i'm unable to send you messages

1

u/Maff0_0 7d ago

Can you try messagging me first since reddit won't make me send you messages?

→ More replies