Minecraft – How to make a sword that when you hold it the mobs near it will die in 1 secs

minecraft-java-edition

I saw a video on YouTube where someone made a sword that using repeating command blocks, causes the mobs nearby to die. The sword in the video was created in Minecraft Bedrock Edition but I want it in Java Edition and in 1.16.

Best Answer

It in fact is possible using scoreboards and /execute commands, I'll try to explain everything as thoroughly as possible.

First off all, I'm assuming the sword has been renamed to "Spaghetti" and is a netherite sword, as this will be what the command block will be checking for (these are just examples and can of course be any item and name you want). You can rename a netherite sword in an anvil or obtain a renamed sword directly with the following give command:

/give @p netherite_sword{display:{Name:'{"text":"Spaghetti"}'}} 1

If you don't want a lot of text and just want the commands here they are:

/scoreboard objectives add os_timer dummy is first needed to create the scoreboard we need

Then the following 3 commands all need to be in 3 separate always active repeating command blocks 1

execute at @a[nbt={SelectedItem:{id:"minecraft:netherite_sword",tag:{display:{Name:'{"text":"Spaghetti"}'}}}}] run scoreboard players add @e[nbt=!{SelectedItem:{id:"minecraft:netherite_sword",tag:{display:{Name:'{"text":"Spaghetti"}'}}}},distance=..3] os_timer 1

kill @e[scores={os_timer=20}]

execute at @a run scoreboard players reset @e[distance=3..] os_timer

Explanations below:

/scoreboard objectives add os_timer dummy The scoreboard "os_timer" is needed as this is the countdown until a mob is killed, the name can be whatever you want, as long as you edit them in every other command as well.

The first command in a command block:

The first part of the command /execute at @a[nbt={SelectedItem:{id:"minecraft:netherite_sword",tag:{display:{Name:'{"text":"Spaghetti"}'}}}}] in short looks for a player that wields a netherite sword that is called "Spaghetti" and takes that player's location as its location where it runs.

The second part of the command run scoreboard players add @e[nbt=!{SelectedItem:{id:"minecraft:netherite_sword",tag:{display:{Name:'{"text":"Spaghetti"}'}}}},distance=..3] os_timer 1 in short adds 1 score to the "os_timer" scoreboard for every entity in a 3 block radius that is NOT wielding the sword (as to not kill yourself in the process).

Second command block:

kill @e[scores={os_timer=20}] simply kills all entities whose "os_timer" score is 20, 20 being chosen as a repeating command block runs 20 ticks per second.

Third command block:

execute at @a run scoreboard players reset @e[distance=3..] os_timer this one is not needed, however what it does is, it resets every entity's score when they go outside the 3 block range (of every player)