How to use exclamation marks in Minecraft commands?
I know that you can use exclamation marks in Minecraft commands to check for an entity that does not have a specific tag. For example, you can use /kill @e[type=!Player]
to kill all entities except for the player. I have, however, encountered two problems with using exclamation marks that I can't find a solution for.
For my first problem, I've set up a series of command blocks to summon shootable fireballs above the player’s head when a certain item is being held;
/scoreboard players add @a Fireball 1 {SelectedItem:{tag:{display:{Name:"Summon Fireball"}}}}
/execute @a[score_Fireball_min=1,score_Fireball=1] ~ ~ ~ /summon Fireball ~ ~2.5 ~ {direction:[0.0,0.0,0.0]}
/scoreboard players set @a Fireball 0
I used a comparator and a redstone torch so that the last command would activate when the first command would deactivate, that is, when the player is not holding the item. The problem is, that when I move far away from the setup, the redstone stops working, leaving me unable to summon any new fireballs. Instead, I'm trying to use a command block to check when anything except for item is being held;
/scoreboard players set @a Fireball 0 {SelectedItem:{tag:{display:{Name:!"Summon Fireball"}}}}
I’ve either put it at the wrong place, or you simply can’t use exclamation marks in NBT tags. Could someone tell me where I should put the exclamation mark, or in the latter case, how I can check for anything except for a specific item?
Further, I’ve tried using exclamation marks to set the score of all entities, including the ones with a blank score, but except for the ones with a score of 2 or higher, to 0;
/scoreboard players set @e[score_arrows_min!=2] Arrows 0
/scoreboard players set @a Arrows 1 {SelectedItem:{tag:{display:{Name:"Rain of Arrows"}}}}
/scoreboard players set @e[type=Arrow] Arrows 2 {inGround:1b}
The code does change the scores of entities without scores to 0, but it doesn’t exclude the arrows with a score of 2. Could anyone tell me what I’m doing wrong here?
Solution 1:
or you simply can’t use exclamation marks in NBT tags
This is the case unfortunately. Using !
for negation only works in selectors, and even then only for certain arguments (type
, name
, tag
, team
, m
).
The problem is, that when I move far away from the setup, the redstone stops working, leaving me unable to summon any new fireballs. Instead, I'm trying to use a command block to check when anything except for item is being held;
If I'm understanding correctly, this problem won't be solved by changing your commands if they already do the correct thing when you're near them.
You should instead move your contraption to spawn chunks (which are always loaded) or keep the chunks your contraption is in loaded with a chunk loader.
how I can check for anything except for a specific item?
Your original commands will leave players not holding the item with a score of 0, meaning you can do something like:
/scoreboard players add @a Fireball 1 {SelectedItem:{tag:{display:{Name:"Summon Fireball"}}}}
/execute @a[score_Fireball_min=1,score_Fireball=1] ~ ~ ~ /summon Fireball ~ ~2.5 ~ {direction:[0.0,0.0,0.0]}
/execute @a[score_Fireball_min=0,score_Fireball=0] ~ ~ ~ /say I'm not holding the fireball summon item
/scoreboard players set @a Fireball 0
You can rearrange these commands slightly to have players be left with 0 or 1 at the end of the tick depending on whether they're holding the fireball, if you want:
/scoreboard players set @a Fireball 0
/scoreboard players add @a Fireball 1 {SelectedItem:{tag:{display:{Name:"Summon Fireball"}}}}
/execute @a[score_Fireball_min=1,score_Fireball=1] ~ ~ ~ /summon Fireball ~ ~2.5 ~ {direction:[0.0,0.0,0.0]}
/execute @a[score_Fireball_min=0,score_Fireball=0] ~ ~ ~ /say I'm not holding the fireball summon item
Further, I’ve tried using exclamation marks to set the score of all entities, including the ones with a blank score, but except for the ones with a score of 2 or higher, to 0;
The score_...
argument also unfortunately doesn't accept !
for negation. You'll instead need to do this in multiple steps:
- Add 0 to all scores, turning null scores to 0:
/scoreboard players add @e[type=Arrow] Arrows 0
- Do the command for arrows with
Arrows
score less than 2:@e[score_Arrows=1]
- Do the command for arrows with Arrows score more than 2:
@e[score_Arrows_min=3]
Solution 2:
Minecraft 1.13 merged NBT tags into the selector syntax, making this easy:
/scoreboard players set @a[nbt=!{SelectedItem:{tag:{display:{Name:"\"Summon Fireball\""}}}}] Fireball 0
(1.13 also changed the syntax for item display names, so that they now need to be quoted, hence the \"
in the selector.)