[SOLVED] Need an example of onPlayerDeath and PlayerDeathEvent

Discussion in 'Plugin Development' started by Dussed, Feb 9, 2012.

Thread Status:
Not open for further replies.
  1. Hey there.

    Just started coded in java alongside the bukkit api, got a bit of experience in php (so not a total programmer noob) and I just need a bit of help along the way. So would anyone people able to provide the skeleton for capturing a player dying for me?

    I've had a quick search around the forums + wiki, and checked the documentation for a few hints, everything I've tried so far is failed, so if anyone can provide a clear example, you'd be a lifesaver!

    Thanks in advance! :)

    Dussed
     
  2. Code:
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event)
    {
      //do whatever here
    }
    and don't forget to register the listener!
     
  3. Code:
     pm.registerEvent(Type.ENTITY_DEATH, entityListener, Priority.High, this);
    I'm assuming this is the listener and should work? :)
     
  4. Offline

    Seadragon91

  5. So having:
    Code:
     
    public class myPlayerListener implements Listener {
            @EventHandler
            public void onPlayerDeath(PlayerDeathEvent event)
            {
                //do whatever here 
            }
    }
    should in theory work?
     
  6. Offline

    Seadragon91

    Yes if you register it in your main class.
     
  7. It's in my main class, doesn't seem to be triggering when I die.
    This is the code, compiles fine + no warnings or anything.
    Code:
            public class myPlayerListener implements Listener {
            @EventHandler
              public void onPlayerDeath(PlayerDeathEvent event)
              {
              Player player = (Player)event;
              Location wheretheyare = player.getLocation();
              String worldname = wheretheyare.getWorld().getName();
              getConfig().set("back."+player.getName()+".back.used", false);
              getConfig().set("back."+player.getName()+".back.worldname", worldname);
              double x = wheretheyare.getX();
              getConfig().set("back."+player.getName()+".back.x", x);
              double y = wheretheyare.getY();
              getConfig().set("back."+player.getName()+".back.y", y);
              double z = wheretheyare.getZ();
              getConfig().set("back."+player.getName()+".back.z", z);
              double yaw = wheretheyare.getYaw();
              getConfig().set("back."+player.getName()+".back.yaw", yaw);
              double pitch = wheretheyare.getPitch();
              getConfig().set("back."+player.getName()+".back.pitch", pitch);
              getServer().broadcastMessage("Type /back to return to your location!");
              }
            }
     
  8. Offline

    ItsHarry

    In your onEnable, put:

    getServer().registerEvents(this, this);
     
  9. It said the code was invalid, but I had a quick search and tried this:
    Code:
            getServer().getPluginManager().registerEvents(this, this);
    
    Which came up as valid, but it still isn't registering the death.
     
  10. Have you updated the external resource to 1.1-R3?
     
  11. I have indeed, something that just crossed my mind though, I haven't updated the actual server that's running to 1.1-R3, I don't know if that'll affect it, but I'm gonna go do it now.

    Tried that, still nothing.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  12. PlayerDeathEvent doesn't work for me either so use EntityDeathEvent and check if the entity is an instance of a Player.
     
  13. I've amended it to this now:
    Code:
    public class myEntityListener implements Listener {
            @EventHandler
              public void onEntityDeath(EntityDeathEvent event)
              {
              if(event instanceof Player){
              Player player = (Player)event;
              Location wheretheyare = player.getLocation();
              String worldname = wheretheyare.getWorld().getName();
              getConfig().set("back."+player.getName()+".back.used", false);
              getConfig().set("back."+player.getName()+".back.worldname", worldname);
              double x = wheretheyare.getX();
              getConfig().set("back."+player.getName()+".back.x", x);
              double y = wheretheyare.getY();
              getConfig().set("back."+player.getName()+".back.y", y);
              double z = wheretheyare.getZ();
              getConfig().set("back."+player.getName()+".back.z", z);
              double yaw = wheretheyare.getYaw();
              getConfig().set("back."+player.getName()+".back.yaw", yaw);
              double pitch = wheretheyare.getPitch();
              getConfig().set("back."+player.getName()+".back.pitch", pitch);
              getServer().broadcastMessage("Type /back to return to your location!");
              }
              }
            }
    Sorry for dodgy indenting, still doesn't work.
     
  14. Offline

    Seadragon91

    Change:
    Code:
    if(event instanceof Player){
    to:
    Code:
    if(event.getEntity() instanceof Player){
     
  15. Offline

    nisovin

    You actually want to to do this:

    getServer().getPluginManager().registerEvents(entityListener, this);

    You can only pass in "this" both times if your event methods are in your main class. If you have a separate listener class you need to put the listener in there.
     
  16. Tried that, no worky.
    Getting validation errors on the "entityListener" part for that, no auto fixes or it either.
     
  17. Offline

    nisovin

    Well put whatever your listener is called in there.
     
  18. Tried that, still getting validation errors.


    Should I try one of these?
    [​IMG]
     
  19. Offline

    nisovin

    I meant an actual instance of your class, not just the class name. Try this:

    getServer().getPluginManager().registerEvents(new MyEntityListener(), this);
     
  20. Still not having it. Here's my onEnable and EntityListener just in case I've done something noobishly wrong and you can spot it:

    Code:
        public void onEnable() {
                loadConfiguration();
                reloadConfig();
                System.out.print("[Dussed] Enabled Successfully.");   
               
                PluginManager pm = this.getServer().getPluginManager();
                pm.registerEvents(new MyEntityListener(), this);
     
        }
     
           
            public class MyEntityListener implements Listener {
                @EventHandler
                public void onEntityDeath(EntityDeathEvent event)
                {
                  if(event.getEntity() instanceof Player)
                  {
                      Player player = (Player)event;
                      Location wheretheyare = player.getLocation();
                      String worldname = wheretheyare.getWorld().getName();
                      getConfig().set("back."+player.getName()+".back.used", false);
                      getConfig().set("back."+player.getName()+".back.worldname", worldname);
                      double x = wheretheyare.getX();
                      getConfig().set("back."+player.getName()+".back.x", x);
                      double y = wheretheyare.getY();
                      getConfig().set("back."+player.getName()+".back.y", y);
                      double z = wheretheyare.getZ();
                      getConfig().set("back."+player.getName()+".back.z", z);
                      double yaw = wheretheyare.getYaw();
                      getConfig().set("back."+player.getName()+".back.yaw", yaw);
                      double pitch = wheretheyare.getPitch();
                      getConfig().set("back."+player.getName()+".back.pitch", pitch);
                      getServer().broadcastMessage("Type /back to return to your location!");
                  }
              }
            }
     
  21. There might be a problem as well. You'll need to change that also to event.getEntity().
     
    Dussed likes this.
  22. I've changed it to
    Code:
    Player player = (Player) event.getEntity();
    and guess what. You're a legend. It works. :D

    Thank you everyone who helped, really appreciate it :)
     
  23. Offline

    OneViGOR

    Hey guys! I know this is an old thread (well, kinda) but I've been following this with the same problem Dussed had, and I can't seem to find a solution. My project is here: <Edit by Moderator: Redacted mediafire url>

    This plugin is intended to run in the background of a game of Vareide's Survival Games. I know it's basic, but it's my first plugin :p
     
    Last edited by a moderator: Nov 11, 2016
Thread Status:
Not open for further replies.

Share This Page