1

I would like to run a function whenever a player is charging a bow, essentially using the bow as a held right click detector. I thought to use SelectedItem and some nbt tag to do this, but the wiki page for player.dat format doesn't mention bows, just crossbows, and only in terms of what projectile it is charged with. I'm not sure SelectedItem could work for this, but if it can that would be great. I know the command will be something like this:

execute as @a[nbt={SelectedItem:{"id":"minecraft:bow",tag:{?}}}] at @s run function namespace:function

Edit:

Any way of detecting a hold and click would be a good answer if using SelectedItem is impossible.

8
  • I'm pretty sure it's impossible. But you could maybe retexture a crossbow and do the arrow firing with commands. Jul 5, 2019 at 21:24
  • I was going to retexture the bow into a wand and have it detecting input while it was pulled back. This was because a carrot on a stick can't detect a hold really. Do you know of any other way to determine if someone is holding right click (or any other click) on an item?
    – Nik3141
    Jul 5, 2019 at 21:37
  • Probably not. Just detect clicks 20 times per second instead. Jul 6, 2019 at 3:00
  • When holding down right click with a carrot on a stick, the value of a scoreboard with the objective minecraft.used:minecraft.carrot_on_a_stick increments maybe every 4 ticks, rather than every tick. This makes it difficult to determine whether the value has gone up consistently because they are holding or spam clicking.
    – Nik3141
    Jul 6, 2019 at 3:47
  • Does the same happen if you take it away and re-give it every tick? Or just use snowballs instead? Of course use /replaceitem instead of /give, to avoid the visual and audio spam. Jul 6, 2019 at 4:59

1 Answer 1

3

You can't detect a right click with a bow, you can however use a carrot on a stick.

This would require 2 scoreboard objectives, one to detect when a carrot on a stick is used and one dummy objective that you can use as timer, because the carrot on the stick will only be used every 4-5 ticks:

/scoreboard objectives add carrots minecraft.used:minecraft.carrot_on_a_stick
/scoreboard objectives add timer dummy

You would then repeatedly have to run these commands in this order, in your case probably in a function:

#count the timer down
scoreboard players remove @a[scores={timer=1..}] timer 1

#reset the timer if the carrot on the stick was used
scoreboard players set @a[scores={carrots=1}] timer 5

#reset the scoreboard that detects if the carrot on the stick was used
scoreboard players set @a carrots 0

The timer will never reach 0 as long as the player holds right click, so your command would look like this:

execute as @a[scores={timer=1..}] at @s run function namespace:function

This will have a delay of a bit less than 5 ticks (0.25 seconds) after the player releases the right click, so it is possible to detect a right click even if the player already released it for a short period of time.

Use this command to give yourself a carrot on a stick that doesn't break:

/give @s minecraft:carrot_on_a_stick{Unbreakable:1}
4
  • So this works pretty well, but for my purposes I'd like something that doesn't involve anything actually being removed from the player's inventory, as well as the added difficulty of removing the snowball sounds. I'm going to try to use a carrot on a stick and maybe put that answer here too.
    – Nik3141
    Jul 5, 2019 at 23:57
  • 2
    @Nik3141 I understand the problem with the snowball being removed from the inventory. You can probably remove the sound of the snowball with /stopsound. I will try it and then edit the answer tomorrow. I will also try some other things that may be better for this and then add a new answer if it isn't too similar to your solution (or if I'm faster).
    – user232393
    Jul 6, 2019 at 0:13
  • 1
    @Nik3141 This now uses carrots on a stick, I didn't make a new answer, because this is very similar to how the other answer worked.
    – user232393
    Jul 6, 2019 at 10:50
  • Ok I think this will work perfectly, thanks a ton.
    – Nik3141
    Jul 6, 2019 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .