Minecraft – The repeating command block gives me a new bow when I try to shoot. How to fix that

minecraft-commandsminecraft-java-edition

I'm making a Minecraft map and I don't want the player to be able loose his bow. So I wrote this command into a repeating command block:

/replaceitem entity @p hotbar.0 bow{display:{Name:"{"text":"Bow of Shadows","color":"green","italic":"false","bold":"false"}"},"Enchantments":[{id:"minecraft:infinity",lvl:1}]} 1.

But if I try to shoot an arrow the bow reloads instantly because the command block gives me a new bow and I can't shoot. I have same problem with a shield.

I'm playing Vanilla 1.13.2

Best Answer

The problem is that the bow is being replaced every tick.

Instead of constantly replacing their first hotbar slot, simply give them a bow if they are missing one.

Instead of the nearest player, we're going to run this as all players for multiplayer support.

/execute as @a...

First, we need to check if the player has the bow in their inventory:

...unless entity @p[nbt={Inventory:[{id:"minecraft:bow", tag:{INSERT NBT}}]}]...

Then give them the bow.

...run give @p minecraft:bow{INSERT NBT} 1

Combine the three lines like this:

/execute as @a unless entity @s[nbt={Inventory:[{id:"minecraft:bow", tag:{INSERT NBT}}]}] run give @s minecraft:bow{INSERT NBT}]} 1

This will give every player a Bow of Shadows, if they do not already have a matching bow in their inventory. (replace "INSERT NBT" with the bow's display and enchantment data.)

Hope it helps!