2

A unique quirk about the /clear command is that it can clear items from the player's cursor, i.e. the one that is picked up and moving from slot to slot. Using this and conditional command blocks, I can detect when someone clicks on an item slot to pick it up. This is useful when I want to prevent a user from removing items from the chest.

A method that I used to detect this is shown here.

This method doesn't work if the chest's contents are constantly changing. So I had this idea, to give each item a custom NBT tag like LockedInChest:1b and use my earlier method to detect if items with this tag were removed.

Now I've run into a roadblock. It was all smooth sailing until I realized that you can't specify NBT in the /clear command without specifying an item. For example, I can match "stone with my NBT tag", or "stone with any NBT tag", but I can't mention "any item with my NBT tag".

How can I specify NBT only in the /clear command, and have it work for any item?

Past trials (all failures)

What if I could just remove the item name and keep the NBT?

/clear @s minecraft:stone{NBT}

/clear @s {NBT}

Can I just specify the namespace? minecraft:

/clear @s minecraft:stone{NBT}

/clear @s minecraft:{NBT}

and

/clear @s minecraft{NBT}

Is there a keyword for any item?

/clear @s minecraft:stone{NBT}

/clear @s minecraft:any{NBT}

and

/clear @s minecraft:all{NBT}

Related: How to remove specific NBT items from player inventory without data packs

0

1 Answer 1

1

Solution

Use data pack tags. Data packs allow you to define a tag which you can apply to different types of items, blocks, entities, fluids and functions. A tag is defined by a .json file containing a list of ids. You can use these tags to select multiple types of something at the same time.

Example of the solution

food.json:

{
    "values":[
        "minecraft:cake",
        "minecraft:apple",
        "minecraft:carrot",
        "minecraft:potato"
    ]
}

Every item listed in the file above can be selected using the food tag.

You want to clear all items with a specific NBT tag so you would need to create a tag that applies to every item in the game. This is how you would use that tag:

/clear @s #items_tag:all{NBT}

"#" means you are using a tag. "items_tag" is the namespace (folder) where the .json file is stored. And "all" is the name of the .json file (which is the name of the tag).

The clear command will go through the player's inventory and clear any items from the list defined in all.json if the NBT data matches.

How to implement the solution

These tag files are a part of the data pack system so you need to create a data pack before you can use it. Create a new folder and name it whatever you want. I'm going to name it "items tag". Then, create the following files are subfolders:

items tag
│   pack.mcmeta
│
└───data
    └───items_tag
        └───tags
            └───items
                    all.json

pack.mcmeta is a file that tells Minecraft that this is a data pack and it provides some information about the data pack.

items_tag is a namespace folder. Namespaces are used to organize files in data packs.

all.json is the file that defines the all tag.

Now all that's left to do is to populate the all.json file with item ids. You can copy the list from here and paste it into all.json. I extracted the list from a wiki page, so I'm not sure if it contains every single item. There are 973 ids in the list.

This is the bare minimum required to use tags in data packs. The wiki has a detailed explanation of the entire structure of data packs.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.