Death Event - Give item after death

Discussion in 'Spigot Plugin Development' started by DidactCraft, May 26, 2015.

  1. Ok, so I'm making a custom kit pvp server right now (Coding it) and I was wondering about something. I have been trying to give a player the kit selector item (compass) after they died. Can someone give me a pointer other than ' go search it up' as I have already tried that <3


    Thanks in advance, this community is legendary :)
     
  2. You cant give a dead player an item. Give it to him when he respawns. Theres a PlayerRespawnEvent for that :)
     
  3. Here's my code. Is this right?
    @EventHandler
    public void onPlayerDeath(PlayerRespawnEvent e) {

    PlayerInventory pi = ((HumanEntity) e).getInventory();
    ItemStack kitselector = new ItemStack(Material.COMPASS);
    ArrayList<String> kitselectorLore = new ArrayList<String>();
    ItemMeta kitselectorMeta = kitselector.getItemMeta();
    kitselectorMeta.setDisplayName("§a§lKit Selector");
    kitselectorLore.add("§8-----");
    kitselectorLore.add("§aSelect you're kit!");
    kitselectorMeta.setLore(kitselectorLore);
    kitselector.setItemMeta(kitselectorMeta);

    pi.addItem(kitselector);



    }
     
  4. Why are you casting an event to a HumanEntity? Use PlayerRespawnEvent#getPlayer().
     
  5. You must use a delayed task or it won'
    PlayerRespawnEvent it's called before the player respawns so you must use a delayed task.
     
  6. @EventHandler
    public void onPlayerDeath(PlayerRespawnEvent e) {

    final PlayerInventory pi = e.getPlayer().getInventory();
    final ItemStack kitselector = new ItemStack(Material.COMPASS);
    ArrayList<String> kitselectorLore = new ArrayList<String>();
    ItemMeta kitselectorMeta = kitselector.getItemMeta();
    kitselectorMeta.setDisplayName("§a§lKit Selector");
    kitselectorLore.add("§8-----");
    kitselectorLore.add("§aSelect you're kit!");
    kitselectorMeta.setLore(kitselectorLore);
    kitselector.setItemMeta(kitselectorMeta);
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    public void run() {
    pi.addItem(kitselector);
    }
    }, 100);



    }
     
  7. Ok, this should work. Btw.

    Select you're kit! - means Select you are kit!

    Did you mean "Select your kit!" ?
     
  8. Lel, oops my grammer <3 And yeah it worked, thanks
     
  9. Never give a player an item on death. Insted, give it to them when they respawn.
     
  10. I know this is an old thread, But if the issue is solved change the title to Solved, Thank you.
     
    • Funny Funny x 1
  11. MiniDigger

    Supporter

    What a might necro, almost 3 years :LOL:
     
    • Agree Agree x 1
    • Funny Funny x 1
  12. Optic_Fusion1

    Resource Staff

    DidactCraft was last seen:
    1y 38w ago