#How can I make it so that if I kill a mob with the diamond sword, the diamond sword gets a kill coun

1 messages · Page 1 of 1 (latest)

obtuse valley
#

How can I make it so that if I kill a mob with the diamond sword, the diamond sword gets a kill count?

rustic totem
#

You can listen to the entities deaths with EntityDieAfterEvent

#

And set the count with ItemStack.setDynamicProperty() & ItemStack.setLore()

obtuse valley
#

Do you have any examples of what it should look like?

rustic totem
# obtuse valley Do you have any examples of what it should look like?
import { world } from "@minecraft/server";

world.afterEvents.entityDie.subscribe(({ deadEntity, damageSource: { damagingEntity } }) => {
  if (damagingEntity?.typeId !== 'minecraft:player') return;

  const equip = damagingEntity.getComponent('equippable');
  const hand = equi.getEquipment('Mainhand');
  if (hand?.typeId !== 'minecraft:diamond_sword') return;

  const count = (hand.getDynamicProperty('kill_count') ?? 0) + 1;
  hand.setDynamicProperty('kill_count', count);
  hand.setLore([`Kill count: ${count}`]);

  const hand = equi.setEquipment('Mainhand', hand);
});