This is a simple matter of our friend, the scoreboard. First, setup a scoreboard for mob kills:
/scoreboard objectives add Kills stat.mobKills
This will automatically keep track of a player's mob kills. We can also manipulate it manually. We'll get into that. Now, we want to detect (on a clock) when a player has a Kills score of 1, and give them 1 experience level, and then set their Kills score to 0
/xp @a[score_Kills_min=1] 1L
/scoreboard players set @a[score_Kills_min=1] Kills 0
These two commands should run at the same time to work right, though if redstone quirks cause it to not work right, you can run /testfor @p[score_Kills_min=1]
on a clock, and send the output of that command block to the /xp
and /scoreboard
commands in sequence.
Note this will also count passive mobs; chickens etc. There's no real way to avoid this, unless you want to keep track of each entity killed with /scoreboard objectives add KillEntity stat.killEntity.<entity>
(e.g. /scoreboard objectives add KillZombie stat.killEntity.Zombie
). Which you can do, but you'll need a set of /xp
and /scoreboard
reset commands for each one.
Since automated scoreboard statistics (namely stat.damageTaken
) only increments for players, you will need to rely on NBT data for non-player entities.
The HurtTime
tag will be set to 10 when a mob is struck, decreasing by 1 per tick until it reaches 0 again.
Adding the label for mobs that were struck.
/scoreboard players tag @e[type=Creeper,tag=!Hurt] add Hurt {HurtTime:10s}
Remove the label when the value is 9.
/scoreboard players tag @e[type=Creeper,tag=Hurt] remove Hurt {HurtTime:9s}
Target the mob based on the "Hurt" tag.
/execute @e[type=Creeper,tag=Hurt] ~ ~ ~ /effect @a minecraft:slowness
Best Answer
There are 3 main ways to do this:
1.) Simply test if the mob/boss in question is still present, if it's not do the stuff you want to do. The main disadvantage is that you can't directly target the player who killed the mob, but in some situations that's not really a problem. If it is a problem however:
2.) There are statistics for killing any mob that has a spawn egg which can be used to create a scoreboard objective. You can use this to test if a player has killed a certain kind of mob and target that player.
For example:
This will create a scoreboard which increases the score of a player when they kill a zombie. @a[score_zombieKills_min=1] will then target those players. Just make sure to reset the score afterwards.
The main disadvantage of this method is that you can only use it for mobs which have a spawn egg and you can't specify anything else. If you want more control and (for example) only detect when a player kills a "custom mob" which has altered NBT data:
3.) Advancements can use the "player_killed_entity" trigger to run a function from the player who killed that entity. The advantage of this method is that it has way more options. More information of all the options can be found here: https://minecraft.gamepedia.com/Advancements#minecraft:player_killed_entity
For example:
This will trigger and run the function "example:test" (located at "data\functions\example\test.mcfunction) when a player kills a powered creeper (in a desert with any kind of slowness effect and a weakness effect with a minimal amplifier of 2) with a wooden sword. This is of course really specific, but it shows how much control you have.
The function is run through the player who completed the advancement, which means you can use @s in the function to only target the player who triggered it.
Make sure to revoke the advancement in the function, since advancements can only be completed once without revoking them.