1

This question already has an answer here:

Is it possible to make a weapon in Minecraft that applies status effects when it hits an entity, for example a Sword that poisons and slows whoever's hit by it.

Also can you give a weapon a custom durability that would make it break after it's first hit?

marked as duplicate by Virusbomb, galacticninja, DrFish, Joachim, Timmy Jim Aug 16 at 11:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Why the down votes? This seems like a valid question. – SlayerDiAngelo Jul 18 at 12:01
  • 1
    @SlayerDiAngelo It is, but it on Arqade it is generally discouraged to ask a question with the open-endedness of "Is it possible to do this thing" because if it involves commands then it seems like you're asking someone to write commands for you, which is strongly discouraged. Of course, in this case, the answer is not obvious and quite complicated, along with the question being a question many people can relate to, which makes it fine. – Nik3141 Jul 18 at 20:47
  • Additionally, this question, this question, and this question are all open ended too, but they are fine for the site because they aren't easily solved and have interesting and useful solutions. – Nik3141 Jul 18 at 20:49
1

Firstly, you can give a sword with custom durability pretty easily. Use the Damage tag, which represents how much damage the item has taken. Since a diamond sword has 1562 durability, a Damage of 1561 will leave it with one durability left. Damage can be found on this website in the first row of the chart. See the bottom of this answer to learn more about NBT tags.

Secondly, you can have weapons inflict status effects, but it is a little difficult. You need to first set up a scoreboard which tracks the uses of a particular weapon (let's say that your poison sword is a diamond sword named "Poison Sword" with a dark_green name, given with /give @s minecraft:diamond_sword{display:{Name:"{\"text\":\"Poison Sword\",\"color\":\"dark_green\"}"}}) so in this case a diamond sword. Using the linked wiki, we would use /scoreboard objectives add swordUsed minecraft.used:minecraft.diamond_sword. Then, in a tick function in a datapack (we need the datapack so we can raycast) we need to check for any players that have used their sword and run a function that raycasts from them:

execute as @a[scores={swordUsed=1..},nbt={SelectedItem:{tag:{display:{Name:"{\"text\":\"Poison Sword\",\"color\":\"dark_green\"}"}}}}] at @s anchored eyes positioned ^ ^ ^0.1 run function namespace:raycasting_function

The raycasting function that is run will need to do two things: count the amount of blocks that it moves forward (so it doesn't go on indefinitely), and give the first entity it encounters the poison effect. Here is an example function:

#use the swordUsed scoreboard to keep track of function iterations:
scoreboard players add @s swordUsed 1

#if there is no entity and the block is air, water, or lava, run the function but positioned 0.1 blocks in front of where it was run this time
#in this way it will move forwards by .1 blocks each repetition, until it either hits a block or an entity, or has gone 5 blocks (.1 blocks times 50 repetitions max = 5 blocks)
execute if score @s swordUsed matches ..50 if block ~ ~ ~ minecraft:air unless entity @e[dx=0.1,dy=0.1,dz=0.1] positioned ^ ^ ^0.1 run function namespace:raycasting_function
execute if score @s swordUsed matches ..50 if block ~ ~ ~ minecraft:water unless entity @e[dx=0.1,dy=0.1,dz=0.1] positioned ^ ^ ^0.1 run function namespace:raycasting_function
execute if score @s swordUsed matches ..50 if block ~ ~ ~ minecraft:lava unless entity @e[dx=0.1,dy=0.1,dz=0.1] positioned ^ ^ ^0.1 run function namespace:raycasting_function

#if there is an entity where the function is, give it an effect
execute as entity @e[dx=0.1,dy=0.1,dz=0.1] run effect give @s minecraft:poison [<seconds>] [<amplifier>] [<hideParticles>]

#reset the scoreboard if there is an entity
execute if entity @e[dx=0.1,dy=0.1,dz=0.1] run scoreboard players set @s swordUsed 0

The reason we use dx=, dy=, and dz= rather than distance= while checking for entities is because of this post, where I learned that distance only selects the entity's coordinate location while dx, dy, and dz select their hitbox.

I haven't tested this yet, but I intend to do so within a week. This is a pretty good source on writing datapacks. Hope this helps.

-2

No, you can't add a status effect on a sword, besides flame, which you get by enchanting; but for a bow you can get dipped arrows which can cause different types of status effects.

  • That's not true. I've played on many servers where the swords poisoned you or other stuff along those lines. He's not trying to do it through enchantments. – Robbie Jul 18 at 15:28

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