I try to make a pvp server that players are able to attack each other by using a crossbow loaded with firework rocket.The problem is if the player fires his crossbow too close to themself, they will be damaged by the rocket due to explosion damage.

I tried to solve this issue by adding the players into a team that I have created and turn off friendly fire. It works nicely but it also prevents the players from attacking their enemies since they are in the same team.

Besides, another idea is to give the player resistance 255 for 1 second after they have fired the crossbow so even if the rocket explodes near them they will not be hurt. This is by far the best solution I can come out with. However, if the player spams this, they will always be immune to all kind of damage. As a result of that, their enemies totally cannot defeat him.

So, I wonder is there another way to protect the players from being damaged by the firework rocket that is fired by themself BUT they will still be able to hurt their enemies with the same rocket.

*the version that I am using is java edition 1.16.3


Solution 1:

You can set the firework entity to be on the same team as the player who fired it. Make two kinds of fireworks (for example, one with a red explosion and one with blue). Have two command blocks (set to Repeating; Always Active) with the commands below.

For red:

/team join <team> @e[type=firework_rocket{Fireworks:{Explosions:[{Colors:[I;11743532]}]}}

For blue:

/team join <team> @e[type=firework_rocket{Fireworks:{Explosions:[{Colors:[I;2437522]}]}}

If you're using colors other than red and blue, please refer to this list of firework NBT colors to change the number. However, please make sure to keep the I; in front of the color's number in the string.

With FriendlyFire set to false for both teams, this will prevent the fireworks from damaging the team they came from.