[SOLVED] OnPlayerDeath Listener Not Working

Discussion in 'Plugin Development' started by OneViGOR, Jun 4, 2012.

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

    OneViGOR

    Hey guys, OneViGOR here, and I need some programming assistance.

    I am attempting to create a plugin to run alongside Vareide's Survival Games that allows you to specify how many people are taking part with the command: /tributes <number of competitors>, and then have that number decrease by one every time someone dies until there is an eventual winner.

    However, the problem lies with the DeathListener. I have tried using a PlayerDeathEvent without success and currently have an EntityDeathEvent implemented, also without success. I was following a forum thread from February where someone had the same problem (http://forums.bukkit.org/threads/solved-need-an-example-of-onplayerdeath-and-playerdeathevent.59226/). His was solved, but mine still isn't.

    Please take a look at my plugin and let me know what's wrong. Feel free to fix it yourself if you wish ;) My project is here: <Edit by Moderator: Redacted mediafire url>

    Thanks a lot!
     
    Last edited by a moderator: Nov 10, 2016
  2. Offline

    BobbyD441

    Well first remove
    Code:
    public class DeathListener extends PvpPlugin{
    from the listener class, only
    Code:
     public class MyDeathListener implements Listener{
    is enough. Second you need to register the listener in your main class PvpPlugin like so:
    Code:
    onEnable(){
      getServer().getPluginManager().registerEvents(new MyDeathListener(), this);
    }
    That should make it work better =)
     
  3. Offline

    OneViGOR

    I now have the following as my main class:

    Code:
    public void onEnable(){
              getServer().getPluginManager().registerEvents(new MyDeathListener(), this);
        }
        public class MyDeathListener implements Listener{
            @EventHandler
            public void onEntityDeath(EntityDeathEvent event){
                Bukkit.broadcastMessage("There are now " + tributes);
                if(event.getEntity() instanceof Player){
                    Player player = (Player) event.getEntity();
                    tributes = tributes--;
                    Bukkit.broadcastMessage("There are now " + tributes);
                    if(tributes == 1){
                        Bukkit.broadcastMessage(ChatColor.RED + "" + player + " is the last to perish. We have a winner!");
                    } else if(tributes < 1){
                        Bukkit.broadcastMessage(ChatColor.RED + "" + player + "'s death does not count, as there is no game in progress.");
                    } else if(tributes > 1){
                        Bukkit.broadcastMessage(ChatColor.RED + "As " + player + "'s lifeless body hits the ground, you hear the sound of a distant cannon. " + tributes + " tributes remain.");
                    }
               
                }
            }
        }
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled.");
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("tributes")){
                if(args.length == 1){
                    tributes = Integer.parseInt(args[0]);
                    player.sendMessage(ChatColor.RED + "You have specified there are " + tributes + " competitors.");
                } else {
                    player.sendMessage(ChatColor.RED + "Please enter the number of competitors taking part.");
                }
            }
            return false;
        }
    }
     
     
    
    There seems to be a problem with the semi-colon at the end of PluginDescriptionFile pdfFile = this.getDescription(); now. It says a < was expected :confused:
     
  4. Offline

    LucasEmanuel

    Well to start you dont have it inside a method.
     
  5. Offline

    OneViGOR

    Sorry, I'm new to programming. What do I need to do in order to put it inside a method? Also, is that the only problem?
     
  6. Offline

    LucasEmanuel

    Put this:
    Code:java
    1. PluginDescriptionFile pdfFile = this.getDescription();
    2. this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled.");


    In your onEnable like this:
    Code:java
    1. public void onEnable() {
    2. PluginDescriptionFile pdfFile = this.getDescription();
    3. this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled.");
    4. }
     
  7. Offline

    OneViGOR

    Guess what, guys! Thanks to your help and a little fiddling on my part, it now works. The only problem I now face is that when someone dies, instead of saying their name like "OneViGOR died," it instead says "CraftPlayer{name=OneViGOR} died." Anyone know why this has happened?

    BTW, this was happening with the EntityDeathEvent, so I tried switching it back to a PlayerDeathEvent and I got the same result. Assisstance please?

    Are you able to help with the problem I described in the post above?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  8. place the player his name inside the message, not the player
     
  9. Offline

    OneViGOR

    Thank you guys so much! The plugin is now working just as I hoped it would! I cant thank you guys enough!
     
Thread Status:
Not open for further replies.

Share This Page