DeathEvent help

Discussion in 'Plugin Development' started by CullanP, Jan 7, 2015.

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

    CullanP

    Here's the code to my event. I'm using NMS code to respawn a player automatically without having them have to hit respawn, but I've run into an issue. If they're inside of a minecart it will respawn them back inside of the minecart or in the minecart location. I've tried using p.getvehicle, but that didn't work.

    Code:
    @EventHandler
        public void onSCDeath(PlayerDeathEvent e) {
            if (e.getEntity() instanceof Player) {
                ((CraftPlayer)e.getEntity()).getHandle().playerConnection.a(new PacketPlayInClientCommand(EnumClientCommand.PERFORM_RESPAWN));
                Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "tm msg " + e.getEntity().getName() + " &cWASTED!");
            }                            
        }
     
  2. Offline

    nverdier

    @CullanP You could also just use EntityDamageEvent and then check if the health is smaller than the damage, then cancel and do stuff...
     
  3. Offline

    Danteland

    cant you just do entity.leaveVehicle() to leave the minecart?
     
  4. Offline

    CullanP

    Tried. It leaves, but still respawns the player back in the location they died, which is an issue.

    That works to a degree, but shouldn't be necessary, also wouldn't apply to players drowning in a cart (or would it...). There has to be a way to handle this in the death event.
     
  5. set his health back to max, clear inventory, armorset, levels and after that teleport him on spawnlocation

    but i don't really know in which event you should use that :/ mhm
     
    Last edited: Jan 7, 2015
  6. Try with
    Code:
    @EventHandler
    public void onDeath(PlayerDeathEvent event) {
    Player player = event.getPlayer();
    player.setHealth(20.0);
    }
    
    If that not works you can tp him to the spawn ...
     
  7. Offline

    PiggerCast

    but make sure you dont clear the inventory, but rather, drop the contents of it.
    to do this, put this, before you teleport him
    Code:
                        for(ItemStack i : p.getInventory().getContents()){
                            Bukkit.getWorld(p.getWorld().getName()).dropItemNaturally(p.getLocation(), i);
                        }
     
  8. you could also use
    Code:
            Damageable d = (Damageable) p;
            d.setHealth(d.getMaxHealth());
    
    to get the maximum health of the player because if you us something like healthbar it wouldn't heal him up to max health

    i don't know if this will also clear the armorcontents but if not you could use
    Code:
            for(ItemStack i : p.getInventory().getArmorContents()){
                Bukkit.getWorld(p.getWorld().getName()).dropItem(p.getLocation(), i);
            }
    
    to clear the armorcontents too
     
  9. I don't know if him is using healthbar .
     
  10. yep i just wanted to tell u :)

    cause i have my own health plugin an there it would be a problem
    didn't want to correct you just to let you know :)

    cheers
    sn1cko
     
    MaTaMoR_ likes this.
  11. Offline

    PiggerCast


    True, but what you put there is just the armor. so he would need to put them both, in order to drop both the armor and the items. then after that he would have to clear the inventory, because this doesnt take it from the inv

    Code would look like this:

    Code:
    for(ItemStack i : player.getInventory().getContents(){
    bukkit.getWorld(player.getWorld().getName()).dropItemNaturally(player.getLocation(), i);
    }
    for(ItemStack a : player.getInventory().getArmorContents(){
    bukkit.getWorld(player.getWorld().getName()).dropItemNaturally(player.getLocation(), a);
    }
    player.getInventory().clear();
    player.getInventory().setHelmet(new ItemStack(Material.AIR));
    player.getInventory().setChestplate(new ItemStack(Material.AIR));
    player.getInventory().setLeggings(new ItemStack(Material.AIR));
    player.getInventory().setBoots(new ItemStack(Material.AIR));
     
Thread Status:
Not open for further replies.

Share This Page