Minecraft – how do i make it so that the diamond sword can give player potion effects

minecraft-commandsminecraft-java-edition

How do i put potion effects on my sword? like in factions when you hit a player they get wither, poision, slowness, weakness, blindness, etc. ive looked on the internet for commands. i also tried to put factions on my multiplayer server and failed

Best Answer

The way I know how to do this is to track who gave damage and is holding a poison sword, then execute that target and give poison to the nearest player who has taken damage.

To track who has given or taken damage, use the following scoreboard:

/scoreboard objectives add dealt minecraft.custom:minecraft.damage_dealt
/scoreboard objectives add taken minecraft.custom:minecraft.damage_taken

These scoreboards accumulate player scores for damage they give and take.

Then place these command blocks in a repeating chain:

execute as @a[scores={dealt=1..},nbt={SelectedItem:{id:"minecraft:diamond_sword",tag:{display:{Name:"\"Poison Sword\""}}}}] at @s run effect give @a[scores={taken=1..},limit=1,sort=nearest] poison

This lets players who have given damage and is holding a diamond "Poison Sword" to give poison to the nearest player who has taken damage.

scoreboard players reset @a taken
scoreboard players reset @a dealt

These two commands refresh the scoreboards so the machine can run again later.

Name a diamond sword "Poison Sword" and try it out. If you don't want the name to be specified, replace the first command with:

execute as @a[scores={dealt=1..},nbt={SelectedItem:{id:"minecraft:diamond_sword"}}] at @s run effect give @a[scores={taken=1..},limit=1,sort=nearest] poison

(NOTE: There might be a more efficient method than this out there, but I never had the need to learn such a method so I'm sure this will do well for now.)