How to replace an item the player has in their inventory in 1.13?
I'm not very familiar with the new Minecraft 1.13 commands so I'm not exactly sure of how I would go about doing this. I would like to replace the item "Water Bottle" with a different item whenever a player has either filled a water bottle up or found one in the world. For example, the player walks up to a body of water with an empty glass bottle, they fill it up and immediately the water bottle is replaced with a fire resistance potion (just as an idea of what I'm trying to do). I had tried to use a repeat command block that clears the player's inventory of any water bottle that is in their inventory using the /clear command, and then if that is successful (I check with a comparator) I simply give the player my desired item using the /give command. However, the problem I'm having with this is that it seems to be very inconsistent. For example, if I was to click multiple times on a source of water relatively fast then the majority of them are not turned into my item and the player is left over with different Water Bottles.
Solution 1:
If you are willing to use a datapack (its pretty simple and easy) you can set it up to run a function every tick which checks for a player having a water bottle, and if they do have one you can give them something else. Unfortunately, this requires recursion if you want to handle the player putting multiple items in their inventory at once (so taking them out of chests), so if someone knows how to do recursion, or has another fix, please modify my answer to explain it. If you want to do the datapack, here are some instructions for setting it up. Your tick function might look like this:
execute as @a[nbt={Inventory:[{id:"minecraft:potion",Count:1b,tag:{Potion:"minecraft:water"}}]}] run give @s minecraft:dragon_breath 1
execute as @a[nbt={Inventory:[{id:"minecraft:potion",Count:1b,tag:{Potion:"minecraft:water"}}]}] run clear @s minecraft:potion{Potion:"minecraft:water"} 1
Because a tick is the smallest unit of game time, you couldn't outclick this function. Make sure your give command comes before your clear, and you can change the dragons breath to any item id you want.
Edit: Just realized the link doesn't explain how to make a function run every tick. Basically, you put the above function in a .mcfunction file in your functions folder in your namespace. However, in your data folder (which has your namespace) you make a folder called minecraft, and inside that a folder called tags, and inside that a folder called functions. That folder will get a JSON document with the name 'tick' and the following text:
{
"replace": false,
"values": ["<your namespace>:tick"]
}
This runs the function (which checks for water bottles) every tick.