Solved Event not running (It is registered)

Discussion in 'Plugin Development' started by Monkey_Swag, Sep 11, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Monkey_Swag

    So I want to give a player a book that when he right clicks will open up a Kit GUI, it works when I actually have the book, but... well, let's see this first:
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent e){
    3. Player p = (Player) e.getEntity();
    4. if(!(p.getKiller() instanceof Player)){
    5. return;
    6. }
    7. Player killer = (Player) p.getKiller();
    8.  
    9. API.addDeaths(p, 1);
    10.  
    11. API.addKills(killer, 1);
    12. API.addPoints(killer, 5);
    13.  
    14.  
    15. p.getInventory().clear();
    16. p.getActivePotionEffects().clear();
    17. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
    18. ItemMeta meta = book.getItemMeta();
    19. meta.setDisplayName("§6Kits");
    20. List<String> lore = new ArrayList<String>();
    21. lore.add("§aChoose a kit!");
    22. meta.setLore(lore);
    23. book.setItemMeta(meta);
    24. p.getInventory().addItem(book);
    25. }


    As you can see, when the player dies he should get the book, but he doesn't actually get it for some reason. Events are registered, but it still won't work. Help is very appreciated!
     
  2. Offline

    mythbusterma

    Monkey_Swag

    You might have better luck with the PlayerRespawnEvent, also in Java naming conventions, "API" should be "api" or "Api."
     
  3. Offline

    Anfanzer

    Monkey_Swag
    What mythbusterma said. You must remember that the whole "onPlayerDeath" method will be completed in near a thousandth of a second. This means that there is no possible way the player could respond in time to recieve the book because dead players aren't an actual entity with an inventory. However if you use PlayerRespawnEvent... well then it will do exactly what it sounds like. Wraping your head around the timings can be quite difficult at times, I know it was for me as well, and still is.
     
  4. Offline

    Monkey_Swag

    mythbusterma Alright, that has been changed, thanks!
    Anfanzer This may be the problem, but I have an auto re-spawning class, and the book still won't go into the player's inventory when they die.
     
  5. Offline

    Anfanzer

    Monkey_Swag When a player dies it stops being an entity and thus no longer has an inventory. You must give the book to the player after they respawn. In addition, you cannot make a player respawn from the server side (as far as I'm aware), the client must click the respawn button, this is why you must use the PlayerRespawnEvent.

    Btw, could you show me your code. I'm interested in your "auto-respawing" method? Also, make sure that your way of giving the book actually even works (test it out in a command method or something).
     
  6. Offline

    PePsiGam3r

    Monkey_Swag Have you tried schedulers? when a player dies wait for your auto-respawn time plus a few ticks like 1 or 2, then give anything you want to the dead player, because the most of auto-respawn codes is a PacketPlayInClientCommand packet it does not call PlayerRespawnEvent. Hope that helpful!
     
  7. Offline

    Monkey_Swag

    Anfanzer It's not mine, I found it on a recources thread, will show it when I get home. PePsiGam3r that sounds like a good idea, will try it when I get home, thank you!
     
  8. Offline

    PePsiGam3r

  9. Offline

    nivek1212

    Try PlayerRespawnEvent instead of PlayerDeathEvent, because schedulers doesn't work, if the Player needs hours to respawn

    //Oops: Haven't seen that it has already said, sry
     
  10. Offline

    Monkey_Swag

    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page