PlayerDeathEvent not working :3

Discussion in 'Plugin Development' started by demondking23, Feb 20, 2016.

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

    demondking23

    Hi! The title might be wrong, but I need help with my plugin!
    Code:
    package me.demonicrin;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin {
       
       
        @Override
        public void onEnable(){
            getLogger().info("Killheal has been enabled!");
                new PlayerListener(this);
               
        }
    
        @Override
        public void onDisable(){
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            if (cmd.getName().equalsIgnoreCase("heal") && sender instanceof Player){
               
                Player player = (Player)sender;
               
                player.getMaxHealth();
               
                player.sendMessage(ChatColor.AQUA + "You have been healed!");
               
            }
            return false;
        }
    }
    
    here is the player listener!
    Code:
    package me.demonicrin;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class PlayerListener implements Listener {
                                                         
           
            public PlayerListener(main plugin){                                                       
                plugin.getServer().getPluginManager().registerEvents(this, plugin);  
    
        }
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
            Player player = e.getPlayer();
           
            player.sendMessage(ChatColor.BLUE + "Welcome, " + player.getName() + "! " + "Hope you enjoy your stay!" );
        }                                                                                                 
       
        @EventHandler
        public void OnKill(PlayerDeathEvent e){
            Player k = e.getKiller();
           
            k.setHealth(k.getHealth + 6);
    
    
        }
    }

    idk the problem, but it's not working!
     
  2. Offline

    Lightspeed

  3. Offline

    BobTheHamster9

    You don't need this
    Bukkit logs this for you.

    You have nothing in your onDisable, remove it.
    There is no e.getKiller() method in PlayerDeathEvent that I'm aware of.
    If you want to get the killer you would have to do what is below
    Code:
     
    Player killed = e.getEntity();
    Player killer = killed.getKiller();
    
     
  4. Offline

    Lightspeed

    Eclipse came up with Player p = ((LivingEntity) e).getKiller(); even if theres no method in e o.o I don't know if it works or not. XD

    Also if you want to stop deaths cause it looks like a plugin for pvp just listen for EntityDamageByEntityEvent and check if a player gets another players health below zero if so tp to spawn so you got no death menu(Cause they glitch sometimes) ;D

    I gave you the idea but, I would like you to make it yourself if you want to use it.
     
  5. Offline

    elian1203

    I would recommend use EntityDamageByEntityEvent, check that both entities are players and the entity's health is less than or equal to 0. Right now you are not sure if the killer is a player, as well as you are setting the killer's health to its health plus 6. If the killer's health is anything above 14.0, it will throw an error. Make sure to safely add the health.
     
    Lightspeed and Xerox262 like this.
Thread Status:
Not open for further replies.

Share This Page