Minecraft – How to give players 1 experience level per mob kill

minecraft-commandsminecraft-java-edition

I'm making a map and every time someone kills a mob I want them to get 1 experience level but I'm not sure how to do it. It should only use command blocks, no mods or plugins.

Best Answer

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.