There is no way to remove items if they are moved with the cursor, as far as I am aware, due to the fact that it doesn't have a slot id.
However clearing all other slots of a specific item and killing all dropped items is possible, using /replaceitem
. This would require 36 command blocks to clear items from every slot in the inventory apart from armor, 37 if you include offhand.
Inventory
/replaceitem entity @a slot.inventory.# minecraft:air
Replace #
with a number from 0 to 26, with 0 being top-left slot and 26 being bottom right slot of the inventory. This does not include the hotbar
Hotbar
/replaceitem entity @a slot.hotbar.# minecraft:air
Replace #
with a number from 0 to 8, 0 being far left hotbar slot, 8 being far right. This does not include offhand
Offhand
/replaceitem entity @a slot.weapon.offhand minecraft:air
To clear the entire inventory, use /clear <player>
I know its a late answer, but this will work. It may cause a bit of lag. You will not be able to use any other items, unless you find a way of specifying what item to replace. I have tested /clear
for its maxCount
parameter, but it specifies the maximum amount to be cleared, not the maximum items left.
Good luck!
Apart from the NBT error (Tags
needs to be in EntityTag
), this is caused by the bug MC-123289: Snowball entities don't copy the NBT tags of their item when thrown, so the tag you have given it simply gets deleted when the snowball is thrown.
A workaround for this would be to tag the snowball based on a nearby player who matches conditions. Be careful to only apply that to new snowballs (not yet tagged), otherwise it could change from passing by other players. Even with that, it could still happen that a player throws a snowball in the direction of another player and that player gets selected instead, but only if they are really close, which shouldn't be a problem for your usecase.
Best Answer
You cannot do it directly with NBT detection because the slot simply does not exist when empty. What you can do instead is initially label the player as having the slot empty first, and then remove the label if they actually do have a snowball in that slot.
For example, the following would be run in the presented order, assuming a dummy objective called "NoSnowball" was created:
Label them as having no snowball.
Change their score to 0 if they actually do have a snowball in Slot 0.
Players with a resulting score of 1 did not have a snowball in slot 0:
For 1.9+, you can use scoreboard tags instead of requiring an objective.
Apply label by default.
Remove the label if they do have a snowball in slot 0.
Players that still have a "NoSnowball" tag will be those without snowballs in slot 0.