import { system, world } from '@minecraft/server';


world.beforeEvents.worldInitialize.subscribe(initEvent => { initEvent.itemComponentRegistry.registerCustomComponent('test:death_potion', {
  onConsume: e => { 
let spawn = e.source.getDynamicProperty("deathpoint");
      if (!spawn) {
        return;
      }

      e.source.teleport({
          x: spawn.x,
          y: spawn.y,
          z: spawn.z
      });
      
      if (e.itemStack.amount > 1) {
        e.itemStack.amount -= 1;
        e.source.getComponent("minecraft:inventory").container.setItem(e.source.selectedSlotIndex, e.itemStack);
      } else {
        e.source.getComponent("minecraft:inventory").container.setItem(e.source.selectedSlotIndex, null);
      }
 },
});
 });
world.afterEvents.entityDie.subscribe((e) => {
    const dead = e.deadEntity;

    if (dead.typeId === "minecraft:player") {
        dead.setDynamicProperty("deathpoint", dead.location);
    }
});
