Video of what is happening I'm working on a medkit in Minecraft and I want it to work when I drop it. I need help on detecting dropped items with a specific tag. The base of the medkit is going to be a target block but I want for it to only work when I drop the item with the tag and not when I drop a normal item.
-
7Possible duplicate of Where do I put item tags on items inside a chest or your inventory?– ppperyJul 26, 2022 at 18:10
-
@pppery how is that relevant? The answers to that solve putting tags onto an item, not detecting when that item is dropped.– CorsakaJul 27, 2022 at 8:34
-
@Corsaka Because what the question is actually asking about is distinguishing items with custom tags from items without them, and the syntax for testing for items is the same as the syntax for giving them.– ppperyJul 27, 2022 at 13:23
-
@pppery But that's for items within chests. They're asking specifically how to detect dropped items.– CorsakaJul 27, 2022 at 13:30
-
@Corsaka That question already has a large number of duplicates not relating to chests, so it's clear the community does not agree with your narrow construction.– ppperyJul 27, 2022 at 13:31
2 Answers
To select entities with the 'REPLACEME' tag:
/execute at @e[type=item,nbt={Item:{tag:{tag:"REPLACEME"}}}] run COMMANDHERE
It doesn't have to be a target block as long as it has the correct NBT tag.
Replace 'REPLACEME' with the tag - in your case, 'medkit' - and 'COMMANDHERE' with whatever you want to happen after it's dropped. Note you are executing at the entity, not as it, so any commands run will be run from the command block but at the location of the dropped entity. If you instead want things to happen to the entity, use as.
-
@AlexanderUkrainskiy You'll need to run it after you drop the item - if it doesn't work, is the game showing you an error?– CorsakaJul 27, 2022 at 15:04
-
the command block is a repeat always active one the game version is 1.19 and when i run it there dont show up any errors in the command block output or in the game output window and it still doesnt work Jul 27, 2022 at 15:09
-
@AlexanderUkrainskiy Okay, wow, this is a really stupid bug. If someone more experienced than me could explain the necessity of two
tag:{tag:
declarations, that'd be nice; anyway, I've updated the command in the answer to fix it.– CorsakaJul 27, 2022 at 17:09
Another answer, as what Corsaka describes doesn't work for me either. Credit where credit is due, this answer is based on theirs. I've tried to add some additional info too.
I first gave myself a tagged item as like this. From what I can tell, OP's command does it like this as well.
/give @p minecraft:honey_bottle{Tags:["medpack"]}
Now, to create the actual test. By looking at the NBT data obtained from throwing the item and doing /data
, we gather the following info about how that dropped item is described. It's actually an entity of the type minecraft:item
.
{ # What are this entity's properties?
Item: { # What item does this entity describe?
tag: { # What extra tags does this item have?
Tags: ["medpack"], # What user-given Tags does this item have?
... # Other extra data
}, ... # End of extra tag, other item data
}, ... # End of item description, other entity tags
} # End of entity tags
Hence, I've successfully killed the item like this:
/kill @e[type=item,nbt={Item:{tag:{Tags:["medpack"]}}}]
Note that this works if the item has more tags than just "medpack", which is a bit weird but ok. You might also want to not kill the item but instead execute something as Corsaka described in their answer.
-
Ah. I think I get how the tagging system works - for some absurd reason, you cannot literally assign
tag:{}
to anything upon giving yourself an item. It instead uses anything in the{}
as a brand new tag that can be anything the user wants. (and, fwiw, my answer assumes the user uses<item>{tag:"tagname"}
)– CorsakaJul 28, 2022 at 8:44 -