Need help with player death event

Discussion in 'Plugin Development' started by anorexicPanda, Jul 19, 2012.

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

    anorexicPanda

    I'm trying to make a plugin that logs where the player died, and then displays that location by sending a message. I'm kinda a noob but this is what I have so far:
    Code:
    public void onEntityDeath(EntityDeathEvent e){
        if (e.getEntity() instanceof Player){
        }
    }
    I get a marker in Eclipse saying "EntityDeathEven cannot be resolved to a type". This sounds like it can't define "e" as EntityDeathEvent but I'm not sure how to fix that. Is there a different way I should be making this plugin? And I have everything else about the plugin working, It used to print a message whenever someone joined the server. Any help is appreciated!:)
     
  2. Offline

    Kodfod

    PlayerDeathEvent = your solution. =)
     
  3. Offline

    anorexicPanda

    Pardon my stupidity, I didn't realize I was supposed to replace entity with the entity I was calling :p Btw thanks for the fast reply! So my code is this now:

    Code:
    public void onPlayerDeath(PlayerDeathEvent e){
        if (e.getEntity() instanceof Player){
        }
    }
    But I still get a marker saying "EntityDeathEvent cannot be resolved to a type" for the first line and the second line has a marker saying "-Player cannot be resolved to a type, -event cannot be resolved
     
  4. Offline

    ZeusAllMighty11

    import them... lol

    Also, don't use hyphens. Player player = e.getPlayer()

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  5. Offline

    anorexicPanda

    I did import them but I never noticed that there is a warning next to entityDeathEvent that says it is not being used. here's my imprt line:
    Code:
    import org.bukkit.event.player.PlayerJoinEvent;
    Where am I using hyphens?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  6. Offline

    ZeusAllMighty11

    -Player cannot be resolved to a type, -event
     
  7. Offline

    anorexicPanda

    Sorry, I thought you meant i was using it in my code and Where should I put "Player player = e.getPlayer()"?
     
  8. Offline

    ZeusAllMighty11

    Anywhere where your method is. (inside method)
     
  9. Offline

    anorexicPanda

    I tried this but there is an error on line 2 sayin "The method getPlayer() is undefined for the type PlayerDeathEvent"
    Code:
    public void onEntityDeath(PlayerDeathEvent e){
        Player player = e.getPlayer();
        if (e.getEntity() instanceof Player){
        }
    }
     
  10. Offline

    russjr08

    Have you registered it and implemented it as a Listener?

    Also, you need to import that:
    import org.bukkit.event.player.PlayerDeathEvent;
     
  11. Offline

    anorexicPanda

    I have implemented it as a listener and when i put that at the top of my file it says it "can't be resolved" and when I hover over "PlayerDeathEvent" it says "No Javadoc could be found"
     
  12. Offline

    russjr08

    Have you added the Bukkit API jar to your Build Path / Library?
     
  13. Offline

    anorexicPanda

    Ya, all the other imports are working fine, thats the only one with an error
     
  14. Offline

    Kodfod

    okay, maybe this will help.

    [​IMG]

    Then

    [​IMG]

    If that doesn't help. make sure you have the following:

    Code:JAVA
    1. import org.bukkit.event.Listener;
    2. import org.bukkit.plugin.java.JavaPlugin;
    3. inport org.bukkit.event.PlayerDeathEvent;
     
  15. Offline

    anorexicPanda

    I did those things but none took away the error, here is my whole PlayerListener.java file, there might be something wrong that I haven't posted:
    Code:
    package me.zac.test;
     
    import org.bukkit.ChatColor;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerDeathEvent; //this gets a "cannot resolve" error
    import org.bukkit.event.event.PlayerDeathEvent;  //and so does this one
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class PlayerListener implements Listener {
       
        public Main plugin;
       
        public PlayerListener(Main instance){
            plugin = instance;
        }
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onEntityDeath(PlayerDeathEvent e){
            Player player = e.getPlayer();
            if (e.getEntity() instanceof Player){
            }
        }
    //    public void onPlayerJoin(PlayerJoinEvent e){
    //        e.getPlayer().sendMessage(ChatColor.DARK_BLUE+"Welcome to our server!");
    //    }
     
    }
    
     
  16. Offline

    Kodfod

    Code:java
    1. [FONT=Consolas]@EventHandler(priority = EventPriority.HIGHEST)[/FONT]


    1st off, never sent anything on HIGHEST, for this it needs to be normal.

    2nd it should be:
    Code:JAVA
    1. import org.bukkit.event.entity.PlayerDeathEvent;


    Sorry for the 1st wrong answer (my eclipse isn't working)
     
  17. Offline

    russjr08

    Also, if you're talking about the JavaDoc error, it is safe to ignore.
     
  18. Offline

    anorexicPanda

    lol crap thats the only error I was worried about this whole time. This is my code now but i tested it and it doesn't say anything when i die:
    Code:
    public void onEntityDeath(PlayerDeathEvent e){
        Player player = e.getPlayer();
        if (e.getEntity() instanceof Player){
            e.getPlayer().sendMessage(ChatColor.DARK_BLUE+"You died!");
        }
    }
     
  19. Offline

    russjr08

    You might need to change it to EntityDeathEvent, I don't think there is a playerdeathevent. You'll need @EventHandler at the top, and in EntityDeathEvent there isn't a getplayer method (Since it might not be a player is what I would assume). So in order to get that into a player you would need to cast the entity to a Player (Is that possible?)

    So it would be something like this.
    Code:Java
    1.  
    2. @EventHandler
    3. public void onEntityDeath(EntityDeathEvent e){
    4. if (e.getEntity() instanceof Player){
    5. Player player = (Player)e.getEntity;
    6. player.sendMessage(ChatColor.DARK_BLUE+"You died!");
    7. }
    8. }
    9.  

    That may not be exactly correct... I've never messed with that event. Worth a shot though!
     
  20. Offline

    Kodfod

    Well if you do:

    Code:JAVA
    1. public void onEntityDeath(PlayerDeathEvent e){
    Code:JAVA
    1.  
    2. [FONT=arial] Player player = e.getPlayer();[/FONT]
    3. [FONT=arial] if (e.getEntity() instanceof Player){[/FONT]
    4. [FONT=arial] e.getPlayer().sendMessage(ChatColor.DARK_BLUE+"You died!");[/FONT]
    5. [FONT=arial] } [/FONT]
    6. [FONT=arial]}[/FONT]


    No reason to check if it is a player. PlayerDeathEvent

    so what you would do is:

    Code:JAVA
    1. public void onEntityDeath(PlayerDeathEvent e){
    Code:JAVA
    1.  
    2.  
    3. [FONT=arial] e.getPlayer().sendMessage(ChatColor.DARK_BLUE+"You died!");[/FONT]
    4. [FONT=arial]}[/FONT]
    5. [FONT=arial][/FONT]
     
  21. Offline

    anorexicPanda

    I changed it to this but I just get a ton of errors whenever I die:
    Code:
    public void onEntityDeath(PlayerDeathEvent e){
        //Player player = e.getPlayer();
        e.getPlayer().sendMessage(ChatColor.DARK_BLUE+"You died!");
    }
    I tried that but it didn't work :( I got this error in the console:
    07:01:17 [SEVERE] Could not pass event EntityDeathEvent to test

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  22. Offline

    chaseoes

    Perhaps you should give us those errors?
     
  23. Offline

    Kodfod

    Like?
    Please post errors.
     
  24. Offline

    russjr08

    Kodfod
    Oops, I didn't even see that in the JavaDocs. Shame on me for not using the search lol

    anorexicPanda
    In the console, it should have printed more than that, could you post it, please?
     
  25. Offline

    Kodfod

    Okay, here is EVERYTHING you should have in this class.

    Code:JAVA
    1. package me.kodfod.PlayerDeath;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.EntityType;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.entity.PlayerDeathEvent;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin implements Listener {
    12.  
    13. @Override
    14. public void onDisable() {
    15. }
    16.  
    17. @Override
    18. public void onEnable() {
    19. getServer().getPluginManager().registerEvents(this, this);
    20. }
    21.  
    22. @EventHandler
    23. public void onDeath(PlayerDeathEvent e) {
    24. if (e.getEntityType() == EntityType.PLAYER) {
    25. Player player = e.getEntity();
    26. player.sendMessage(ChatColor.DARK_BLUE + "You Died");
    27. }
    28. }
    29.  
    30. }
     
  26. Offline

    anorexicPanda

    OOoh never mind it actually did work! :D Here is the code that works, I accidentaly didn't change one of the "player"s to "entity" (I'll mark the one i forgot):
    Code:
    @EventHandler(priority = EventPriority.NORMAL)
    public void onEntityDeath(EntityDeathEvent e){
        if (e.getEntity() instanceof Player){
            Player player = (Player)e.getEntity(); //This getEntity was PlayerEntity when i tested it, sorry!
            player.sendMessage(ChatColor.DARK_BLUE+"You died!");
        }
    }
    Now I'm onto my next task. I need to find a way to print the coordinates of the player. I think I'll look around for a bit and If I run into trouble/get it working I'll post it here.
     
  27. Offline

    Kodfod

    I was wrong, i say Player and ASSUMED that you wouldn't have to check entity type, but turns out you do.
     
  28. Offline

    anorexicPanda

    It's all good, I assumed that too :p but I got the location working. It prints out a bunch of un-needed stuff but because messaging the location isn't what I wan't the final product to do, I won't worry about it:
    Code:
    @EventHandler(priority = EventPriority.NORMAL)
    public void onEntityDeath(EntityDeathEvent e){
        if (e.getEntity() instanceof Player){
            Player player = (Player)e.getEntity();
            player.sendMessage(ChatColor.DARK_BLUE+"You died!");
            player.sendMessage((ChatColor.DARK_BLUE+"Location " + player.getLocation()));
        }
    }
    The next thing I would like to do is make it so that people can type a command to go back to that location, but I'm not sure how to start this. I already have a command that messages "This is a cool plugin!" So I can just edit that one, but how would I save that location so I can call it in the command?
     
  29. Offline

    Kodfod

    What i would do is this:

    Code:JAVA
    1. @EventHandler
    2. public void onDeath(PlayerDeathEvent e) {
    3. if (e.getEntityType() == EntityType.PLAYER) {
    4. Player player = e.getEntity();
    5. player.sendMessage(ChatColor.DARK_BLUE + "You Died");
    6. getConfig().set(player.getName() + ".X", player.getLocation().getBlockX());
    7. getConfig().set(player.getName() + ".Y", player.getLocation().getBlockY());
    8. getConfig().set(player.getName() + ".Z", player.getLocation().getBlockZ());
    9. getConfig().set(player.getName() + ".World", player.getLocation().getWorld());
    10. }
    11. }
     
  30. Offline

    anorexicPanda

    Thats awesome! Now I know how to just get the x,y,z or world. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page