Keep items on PlayerDeathEvent

Discussion in 'Plugin Development' started by ImPhantom, Jan 30, 2014.

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


    How would i implement that a player keep their items on death trough a PlayerDeathEvent such as this:

    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onPlayerDeath(PlayerDeathEvent event) {
    3. if(event.getEntity() instanceof Player)
    4. {
    5. Player player = (Player) event.getEntity();
    6."Name: " + player.getDisplayName());
    7. }
    8. }
  2. Offline


    Don't know if you can do it in here or would need to hook into the damage event and check if health gets 0

    What you probably need to do is get their inventory and save it. And then on re spawn if they are on the list give them the stuff again

    Edit: and clear the drops most likely
  3. Offline


    Or, if you don't care who keeps it just use GameRule :)
  4. Offline


    Doesn't that just keep it from falling into the world though?

    Place the player's inventory on death and retrieve when he respawns

    Edit: If you don't want to use a HashMap, use an ArrayDeque. Use add(...) and poll() to grab the inventory.
  5. Offline


    ImPhantom Store the items / armor in a Hash Map, listen for the PlayerRespawnEvent and give them back their armor / items.
  6. Offline


    I couldn't be bothered to do armour but I'm guessing this is how items are done. :)

    2. private HashMap<String, List<ItemStack>> items = new HashMap<String, List<ItemStack>>();
    4. @EventHandler
    5. public void onPlayerDeath(PlayerDeathEvent event) {
    7. // TODO: Amour
    8. items.put(event.getEntity().getName(), event.getDrops());
    10. event.getDrops().clear();
    11. }
    13. @SuppressWarnings("deprecation")
    14. @EventHandler
    15. public void onPlayerRespaw(PlayerRespawnEvent e) {
    17. if (!items.containsKey(e.getPlayer().getName())) {
    18. return;
    19. }
    21. for (ItemStack item : items.get(e.getPlayer().getName())) {
    22. e.getPlayer().getInventory().addItem(item);
    23. }
    24. e.getPlayer().updateInventory();
    26. items.remove(e.getPlayer().getName());
    27. }
  7. Offline


    What do you mean?
  8. Offline


    Normally, when you die, you drop your items. If you clear the drops, then nothing will drop out, but you won't keep your items.
  9. Offline


    xTrollxDudex read the rest of my post :p

  10. Offline


    If you said most likely then I'd assume that you just invalidated your first answer :p

    Besides so, wouldn't a Queue implementation be better for this purpose?
  11. Offline


    that's why I said 'and' :)

    anyways about the Queue implementation. I'm not sure.
    If you refere to this sort of Queue check the spoiler about my opinion. If you mean another sort of Queue then I dunno
    Show Spoiler

    If you have a queue with

    player1 - his Inventory as an entry
    player2 - his Inventory

    and so on

    Unless I might missunderstand on how you mean Queue implementation I'd say its not a good Idea.
    Queue works after the FiFo principle.
    And this makes it bad for this purpose. Why? If player1 dies you put him in the queue as I put above and give him the inventory on the respawn event again, every other player would need to wait for player1 to respawn before they can respawn and get their Inventory back. If he disconnects, what then? Or you go and iterate over it and check every entry then I guess there's no real need for a Queue.

    HOWEVER: If you force them to respawn, THEN a queue is probably a bit better
    xTrollxDudex likes this.
Thread Status:
Not open for further replies.

Share This Page