Related: Detect Player Deaths
In Java this is easy, as there is a whole lot of scoreboard objectives criteria for every entity you can kill in the game, using the format minecraft.killed:minecraft.<entity>
, where <entity>
is a valid type of entity.
This method for command blocks cannot check any NBT data, including the tag of the killed mob.
But this can be done using advancement using the player_killed_entity
advancement trigger, if you’re using a datapack that looks like this:
{
"criteria": {
"requirement": {
"trigger": "minecraft:player_killed_entity",
"conditions": {
"entity": {
"type": "minecraft:pig",
"nbt": "{Tags:[\"example\"]}"
}
}
}
},
"rewards": {
"function": "example:kill_entity"
}
}
The advancement from above will check if the entity that the player killed has that NBT (so it has the tag example
in this case) if you don’t want to check NBT you can just use:
{
"criteria": {
"requirement": {
"trigger": "minecraft:player_killed_entity",
"conditions": {
"entity": {
"type": "minecraft:pig"
}
}
}
},
"rewards": {
"function": "example:kill_entity"
}
}
When the player kills a pig, they will run the funcion example:kill_entity
as it is set as a reward.
In Bedrock this is much more tricky, as there is only the dummy objective type.
The best way this subreddit has come up with so far is to use a modified loot table, making the entity drop a certain item on death that they otherwise wouldn’t drop. This also works for players, but only if the keepInventory
gamerule is false. To change an entities loot table, you will have to modify their behavior file and change the loot table to be your custom one or replace their default loot table if they have one. Find the minecraft:loot
component (or add it if it doesn’t exist) and give it your custom loottable.
"minecraft:loot": {
"table": "loot_tables/<your/loot/table>.json"
},
Now, you of course need to create said loot table as well, here is an example of a loot table that will always drop 1 dragon egg if the entity was killed by a player:
{
"pools": [
{
"conditions": [
{
"condition": "killed_by_player_or_pets"
}
],
"rolls": 1,
"entries": [
{
"type": "item",
"name": "minecraft:dragon_egg",
"weight": 1
}
]
}
]
}
This will overwrite the normal loottable for the entity. If you want the item to be dropped in addition to the normal loot, you will have to modify the entities standard loot table.
From here you can go two ways:
Credit to /u/DanRileyCG, for the original thread here
If you don’t have access to the files and are thus unable to create a behavior pack for this, there is a toned down version for player kills only that you can do.
It involves giving the player the special item into their inventory and making sure they drop it on death. To make it fool-proof / non-abuseable, there are a few more systems required (don’t repeatedly replace it or they could farm kills, only replace it if it’s empty, don’t award a kill if the player does have a bees nest in their inventory, etc.). Too much to go into in this wiki article, but here is a video where they show off one such system: https://www.youtube.com/watch?v=zuhd3qEOJ1I