Solved Entity Damage and Respawn event issue

Discussion in 'Plugin Development' started by JaySN, Oct 18, 2017.

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

    JaySN

    In short, I tried to clear my players when they die but it did not work.
    No stacktrace showed up either.
    Code:
    @EventHandler
        public void onHealthChange(EntityDamageEvent event) {
    
            if (event.getEntity() instanceof Player) {
                if (((Player) event.getEntity()).getPlayer().getHealth() < 0) {
    
                    ((Player) event.getEntity()).getPlayer().getInventory().clear();
                }
            }
    }
    I have also tried it with > 1 but it still didnt work

    Second issue.
    I tried to make it so that when a player respawns, he spawns with armor, but it didnt work

    Code:
    @EventHandler
        public void respawnEvent(PlayerRespawnEvent event) {
    
            ItemStack sword = new ItemStack(Material.WOOD_SWORD, 1);
            ItemStack leatherhelm = new ItemStack(Material.LEATHER_HELMET, 1);
            ItemStack leatherchest = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
            ItemStack leatherpant = new ItemStack(Material.LEATHER_LEGGINGS, 1);
            ItemStack leatherboot = new ItemStack(Material.LEATHER_BOOTS, 1);
    
            for (Player player : Bukkit.getOnlinePlayers()) {
                player.setWalkSpeed(.3f);
                player.getInventory().addItem(sword);
                player.getInventory().setHelmet(leatherhelm);
                player.getInventory().setChestplate(leatherchest);
                player.getInventory().setLeggings(leatherpant);
                player.getInventory().setBoots(leatherboot);
                player.updateInventory();
            }
    
        }
    
    }
    
     
  2. Offline

    ItsComits

    @JaySN Use the PlayerDeathEvent. This event will only happen if a player dies. Also to clear the items when the player dies you can clear the drops(.getDrops().clear()). Hope this helps. :D

    Also for the PlayerRespawnEvent. Do not loop through all the players online as everything inside the loop will execute for every player.
     
  3. Offline

    JaySN

    th
    there is no getDrops()?
     
  4. Offline

    ItsComits

    Are you using the correct event?
     
  5. Offline

    mine2012craft

    Really depends on if you are doing AutoRespawn (Setting max HP on death) or having the player manually respawn. I believe you would use the respawn event for manual respawn to set armor and weapons. For that event, get the player and set their weapons.

    As for clearing the player's inventory on death, use the PlayerDeathEvent. Get their inventory, do your checks, and then clear their inventory.
     
  6. Offline

    JaySN

    [Update]

    I got the items in respawn event working(thanks @mine2012craft for explaining and not gonna put the code since it might result in spoonfeeding for others with the same issue I am facing)
    but the item drops on death is still not clearing.
    Code:
    @EventHandler
    public void removeDrops(PlayerDeathEvent event){
    
    event.getDrops().clear();
    
    }
    
    I am soo confused with this. Would someone care to explain
    Do what checks?

    [UPDATE2]

    I am such a dumb*** . Well I forgot to put @EventHandler in my actual code but I have done it right in here for some reason lmao.

    -
    Thanks and Solved
     
    Last edited: Oct 20, 2017
Thread Status:
Not open for further replies.

Share This Page