Minecraft – How to make a fireball shooting sword on vanilla Minecraft

minecraft-commandsminecraft-java-edition

I'm trying to make a command block setup that will spawn a fireball above the player when a specific named sword is used. I'd also like to copy the players direction to the fireball. Finally, I'd also like to add some potion effects. I've got the following commands:

/scoreboard objectives add IsHolding dummy
/scoreboard objectives add InfinityBlade stat.useitem.minecraft.golden_sword    

/scoreboard players set @a IsHolding 0
/scoreboard players set @a IsHolding 1 {SelectedItem:{tag:{display:{Name:"Infinity Blade"}}}}
/effect @a[score_IsHolding_min=1] minecraft:regeneration 1 1
/effect @a[score_IsHolding_min=1] minecraft:strength 1 1
/execute @a[score_IsHolding_min=1,score_InfinityBlade_min=1] ~ ~ ~ /summon Fireball ~ ~2 ~ 
/scoreboard players set @a InfinityBlade 0

The problems are that it's necessary to hit a mob in order to spawn the fireball, and that the fireball doesn't go in the direction that the sword is used.

Best Answer

What you could do to test if you left-click the sword is have some mob (ex. an slime) that is made invisible, then teleported constantly to your head. For example, you could use this command:

summon Slime ~ ~1.4 ~ {NoAI:1,CustomName:"LeftClick",CustomNameVisible:0,Size:0,ActiveEffects:[{Id:14,Duration:1000000,Ambient:1}],Attributes:[{Name:generic.maxHealth,Base:1}]}

Basically, this command summons a Slime named LeftClick which is invisible and has half a heart of health. Then, you want to make a redstone clock attached to this command:

execute @p[score_IsHolding_min=1] ~ ~ ~ tp @e[name=LeftClick] ~ ~1.4 ~

This will constantly teleport that slime to your head. So, whenever you left click, you will hit the slime, which will add one to your InfinityBlade objective score count. Additionally, remember that you have to set up some sort of command to spawn a new LeftClick slime every time you left-click, since the previous one will be killed.

The only problem with this system is that you can't actually hit anything else with the sword, since the slime is constantly being teleported to you, so whenever you left-click, you hit the slime. Unfortunately, there's no way to get around this, so if this isn't what you want, you have to find another way to detect when you want to shoot a fireball.

However, if this idea works for you, you then have to be able to detect which way the player is facing to summon a fireball in that direction. To do this, you have to use an MCEdit filter called CPDirections. This video explains how to use the filter in great detail.

Hopefully I answered all of your questions.