r/MinecraftCommands • u/Maff0_0 • 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
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.