3

I am working in 1.14 and I have this code: clear @a minecraft:wooden_sword 1 which is simply clearing the player of any wooden sword that they have in their inventory. However, I would like this to happen only to any wooden sword that doesn't have a custom name on it. So if I have a wooden sword called "Test" in the inventory and I run that command on a repeating command block, that particular wooden sword is not cleared from the inventory. But any sword with the default name Wooden Sword is cleared.

  • Could you not just clear wooden swords with the name "Wooden Sword"? Any otherwise named wooden sword would not be cleared and only the ones named with the default name would. Unless default swords don't have a name at all and just display the item name then this would not work I suppose. – Nik3141 Jun 6 at 15:16
  • @Nik3141 yes, I have tested this, but it seems that the default name cannot be targeted through commands. – Quijibo Jun 6 at 15:46
1

I recommend using a data pack for custom crafting. Here (archive) is the wiki article for custom recipes. These are made exactly for your usecase and can do everything a normal person would want from a custom crafting system.

I'll answer your question anyway: The problem is that you need to specify every slot. If you also want to replace the unnamed items with ones that have special properties, while keeping the count (useful for shift-clicking in custom crafting), you also need to specify every possible amount. That gives you a total of 2304 commands in the worst case. But you can mainly just copy-paste. There are also some ways to improve performance, if you need that, but it should be ok as it is.
This does not work for arbitrary banner patterns, because you would need to specify more possible combinations than you have hard drive space for.

Here are the commands that replace all wooden swords that are not named "a" with wooden swords that are named "b". You can just remove the »Name:"{\"text\":\"a\"}"« part if you want to replace all wooden swords that have no name and no lore.

execute as @p if data entity @s Inventory[{Slot:0b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.0 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:1b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.1 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:2b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.2 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:3b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.3 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:4b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.4 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:5b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.5 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:6b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.6 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:7b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.7 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:8b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s hotbar.8 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:9b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.0 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:10b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.1 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:11b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.2 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:12b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.3 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:13b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.4 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:14b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.5 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:15b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.6 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:16b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.7 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:17b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.8 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:18b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.9 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:19b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.10 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:20b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.11 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:21b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.12 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:22b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.13 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:23b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.14 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:24b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.15 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:25b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.16 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:26b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.17 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:27b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.18 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:28b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.19 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:29b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.20 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:30b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.21 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:31b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.22 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:32b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.23 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:33b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.24 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:34b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.25 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1
execute as @p if data entity @s Inventory[{Slot:35b,id:"minecraft:wooden_sword",Count:1b}] unless data entity @s Inventory[{Slot:0b,tag:{display:{Name:"{\"text\":\"a\"}"}}}] run replaceitem entity @s inventory.26 wooden_sword{display:{Name:"{\"text\":\"b\"}"}} 1

Explanation: For every slot, the corresponding command checks first if you have exactly 1 (in case you want to use different items, you can copy-paste this and change the amounts, otherwise you might as well leave out the »,Count:1b« part and the "1" at the end) wooden sword in that slot, then it checks if you have not a wooden sword with the name "a" in that slot, then it replaces the item. So if you have a wooden sword there which is named "a", nothing happens. And if you don't have a wooden sword there, nothing happens as well.
You can also chain multiple of these unless data parts to exclude multiple names.

  • I have ~39 custom crafting recipes, will I have to do this with each item since I'm planning for every item to have a custom name/lore? Will I have to have these 35 command blocks for each item? – Hydra Jun 6 at 19:33
  • 36 and yes. Or just use a data pack with recipes, much easier. – Fabian Röling Jun 6 at 20:30
  • What do you mean data packs with recipes? That's what I am doing to get the custom crafting to work in the first place. Is there a way to get NBT data to work with for custom recipes in a data pack because that would be perfect. – Hydra Jun 6 at 20:36
  • 1
    I thought you could, but I just looked it up and apparently you actually can't. That's weird. Then, in afraid, the best option is indeed to have such a system for every item. You can avoid having to list every possible stack number by letting the crafting output always be something unstackable. You can re-texture it if necessary. – Fabian Röling Jun 6 at 22:46
  • Yeah, it's really strange they haven't implemented that since the possibilities of what people could make with custom crafting supporting NBT data would be endless. But I guess this workaround is better than nothing. – Hydra Jun 6 at 22:55
2

I do not think that there is a way to do it and no easy workaround either.

If it is possible for you to tag every sword, then you can use this to give a player a sword that does not have a custom name:

/give <player> minecraft:wooden_sword{canBeDeleted:true}

Which you then can delete with:

/clear @a minecraft:wooden_sword{canBeDeleted:true} 1

This would only delete swords with a "canBeDeleted" tag, so if you create a sword with a custom name without that tag, then it will not be affected.

  • 1
    The "canBeDeleted"-tag would be invisible to most players, they could find it by using /data and they can see how many tags there are if they have advanced tool tips enabled (toggle with F3+H) – bearb001 Jun 6 at 14:50
  • The problem with this is that I am trying to make custom crafting using a datapack. Since NBT data does not work for custom crafting I am using this clearing method as a workaround. I'm pretty sure canBeDeleted is NBT data, which would not work, since the outcome of the crafting recipe will simply be a wooden sword. – Hydra Jun 6 at 17:04
  • 1
    I would have to look into custom crafting. There does not seem to be a simple way where you do not check every slot one by one. It would be nice if there is a way to check if a certain tag is not set, but there does not seem to be a way to do that. There is an easy way to clear your inventory from custom named items though. – bearb001 Jun 6 at 17:08
  • If you'd like to look into custom crafting I would suggest this video, since this is what I used to get an understanding of how it works. Basically, you can create custom recipes in JSON format, add them to a data pack, enable the data pack in game with /datapack enable datapackname. The item that the recipe outputs can't have NBT data on it since it hasn't been implemented yet, which is why I am trying to use this workaround to it. – Hydra Jun 6 at 17:24
  • 1
    So basically what you want to do is to change the crafting recipee of the wooden sword in a way, so that the sword has a custom name? – bearb001 Jun 6 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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