I want to make a carrots on sticks run commands, but I want different carrots on sticks to have different names and run different commands. Is this possible?
Minecraft – How to make items run commands
minecraft-commandsminecraft-java-edition
Related Solutions
If you want to be exact, you need to use raycasting. But there is a simpler way to approximate it: Summon a dummy entity at the player that is rotated towards the target, then check whether the dummy entity's head rotation is similar to the player's head rotation.
A problem with this is that the X portion of the player's head rotation ranges from -360 to 360, not just from -180 to 180, and that a rotation of -179° and 179° would not be considered close to each other. That can both be solved by simply checking for a rotation 360° higher and 360° lower as well.
Here is an example function implementing this. It only works for the closest player to where the function is executed and assumes that only one zombie exists in the world. It can be adjusted to more players and more targets, but that requires a more complex setup.
# Place a dummy armour at the player, facing the zombie.
summon armor_stand ~ ~ ~ {Tags:["rotation_helper"],Marker:1,NoAI:1,NoGravity:1,Invisible:1,Small:1,NoBaseplate:1,Invulnerable:1,Silent:1,DisabledSlots:2039583}
execute at @p run tp @e[type=armor_stand,tag=rotation_helper] ~ ~ ~ facing entity @e[type=zombie,limit=1]
# Store the rotation in scoreboards
execute as @e[type=armor_stand,tag=rotation_helper] store result score @s x_rot run data get entity @s Rotation[0]
execute as @e[type=armor_stand,tag=rotation_helper] store result score @s y_rot run data get entity @s Rotation[1]
execute as @p store result score @s x_rot run data get entity @s Rotation[0]
execute as @p store result score @s y_rot run data get entity @s Rotation[1]
# Calculate the difference between the rotations
scoreboard players operation @p x_rot -= @e[type=armor_stand] x_rot
scoreboard players operation @p y_rot -= @e[type=armor_stand] y_rot
# Check if the rotations are similar, ±360°.
execute if score @p x_rot matches -2..2 if score @p y_rot matches -2..2 run tag @p add looking_at_zombie
execute if score @p x_rot matches -362..-358 if score @p y_rot matches -2..2 run tag @p add looking_at_zombie
execute if score @p x_rot matches 358..362 if score @p y_rot matches -2..2 run tag @p add looking_at_zombie
# Output
execute if entity @p[tag=looking_at_zombie] run say looking at zombie
execute as @p if entity @s[tag=!looking_at_zombie] run say not looking at zombie
# Reset
kill @e[type=armor_stand,tag=rotation_helper]
tag @p remove looking_at_zombie
The invisible zombie will attack you on survival, and if you're in creative the collision will move you. the thing you may first need it's the zombie (this time will be no invisible, but silent):
/summon minecraft:zombie ~ ~ ~ {Silent:1}
After, you will need add a team for
/team add zombie
After, you will need to modify the team and erase the teammate collision
/team modify zombie collisionRule pushOtherTeams
And then (optional), FriendlyFire
/team modify zombie FriendlyFire false
After, you will need add the zombie (and you). it's easy, you had to stay near to the zombie and put:
/team join zombie @e[type=zombie,distance=..3]
For you, only /team join zombie
Final step, you will need the zombie does not stay near of your camera (player vision) Put a repetitive command block, in it:
/execute at <your name> as @r run teleport @e[type=zombie,team=zombie] ^ ^ ^-1
Best Answer
For running commands if you're holding the item, you can use
as
orat
the player, and use thenbt
target selector to check for theSelectedItem
tag of the player.Here's a quick rundown:
as
changes the executor of the command as the entity, meaning you can use@s
to target the executor's self just after declaring who to execute it as.at
changes the positional context of the command, meaning it will run the command relative to the entity.@a[...]
indicates players, meaning it will only select players.nbt
checks for the entity's NBT, which in this case, we're checking for the player'sSelectedItem
tag{SelectedItem: {id: "minecraft:carrot_on_a_stick", tag: {custom_tag: 1b}}}
refers to the item about to be checked. We'll set theid
string to"minecraft:carrot_on_a_stick"
since we'll be checking for that item. As fortag
, it refers to the item's NBT. In this case, we'll be checking for thecustom_tag: 1b
NBT in the item.The item in the provided example can be obtained by running this command:
Now, if you want to check if a player has interacted with that specific carrot on a stick, you would need a scoreboard with the
minecraft.used:minecraft.carrot_on_a_stick
criterion. As for why, it's a bug that would be useful in our case.After creating the objective, you would check if a player has a score in the scoreboard objective, using the
scores
target selector argument. So the whole command would look like this:<objective>
being the name of your newly created scoreboard objective.1..
is a range that would check if the player has 1 or more value in the said objectiveAfterwards, you would reset the score of the player in the objective by running a
/scoreboard players
command: