3

I have made a Minecraft PVP game and currently, when a player gets a kill they receive one emerald, and anyone who dies' inventory is wiped.

I would like to change it so that players also get half of the person they killed's emeralds before that person's inventory wipes by respawning. To do this I would imagine you would have to do it in two parts:

  1. Create a score out of half the emeralds a dead player had
  2. Give this number of emeralds to the player that killed them

I couldn't find anything on tracking which player killed which and how to turn a number of items someone has into a score. Does anyone know how you can do both of these?

0

1 Answer 1

2

I solved this with a datapack: http://www.mediafire.com/file/x2njlcqr5qgdpwg/file

There are several parts to this datapack. The first, getting the amount of emeralds someone has:

execute as @a store result score @s emeralds run clear @s minecraft:emerald 0

This runs every tick and stores how many emeralds every player has in a scoreboard called emeralds.

Then, I need to detect a player getting killed (the victim) and the player that killed them (the killer). I can do this with 2 scoreboards; one for detecting deaths and one for detecting kills:

execute as @a[scores={deathCount=1},limit=1] run function...
execute as @a[scores={playerKillCount=1},limit=1] run function...

There is one downside: if 2 players are killed in the same tick, the killers/victims may get mixed up.

Once a victim is detected I reset the victim's death count, assign them a tag, and divide their emerald count in half:

scoreboard players reset @s deathCount
tag @s add victim
scoreboard players operation @s emeralds /= #2 emeralds 

Once a killer is detected I reset the killer's kill count and give them the right amount of emeralds:

scoreboard players reset @s playerkillCount

function emerald:give

The killer will run a recursive function that will give them an amount of emeralds equal to the victim's emerald score:

scoreboard players remove @a[tag=victim,limit=1] emeralds 1
give @s emerald 1
execute if score @a[tag=victim,limit=1] emeralds matches 1.. run function emerald:give

It decreases the victim's score, gives 1 emerald, and runs again if the victim's score is not 0. In this way, I can avoid hardcoding the amount of emeralds to give.

Finally, I clear the tags.

tag @a[tag=victim,limit=1] remove victim

To download the datapack, use the link and extract the zip file into the datapacks folder. I could not test this datapack due to not having friends, but it should work in theory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .