Solved Help with EntityDeathEvent

Discussion in 'Plugin Development' started by zakattack361, Feb 4, 2021.

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

    zakattack361

    I'm trying to create a plugin which records how many of each type of mob have been killed. Here's my code where it isn't working:
    Code:
    @EventHandler
        public void mobslayerEvent(EntityDeathEvent e) {
            Entity killed = e.getEntity();
            Entity killer = e.getEntity().getKiller();
            if(killer instanceof Player) {
                if (killed.getType().equals(EntityType.COW)) {
                    cows = cows + 1;
                    Bukkit.broadcastMessage(String.valueOf(cows));
                }
            }
        }
    When I go into the game and kill a cow nothing happens.
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    zakattack361

    Code:
    public class Main extends JavaPlugin{
    
        @Override
        public void onEnable() {
            new skillboardListener(this);
        }
        @Override
        public void onDisable() {
            getLogger().info("BYE!");
        }
    }
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    zakattack361

    Here:
    Code:
    package me.coding.skillboard.listeners;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    
    import me.coding.skillboard.Main;
    
    public class skillboardListener implements Listener{
    
        private Main plugin;
       
        public skillboardListener(Main plugin) {
            this.plugin = plugin;
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
       
    
        public static int cows = 0;
    
       
        @EventHandler
        public void mobslayerEvent(EntityDeathEvent e) {
            Entity killed = e.getEntity();
            Entity killer = e.getEntity().getKiller();
            if(killer instanceof Player) {
                if (killed.getType().equals(EntityType.COW)) {
                    cows = cows + 1;
                    Bukkit.broadcastMessage(String.valueOf(cows));
                }
            }
        }
    }
    
     
  6. Offline

    timtower Administrator Administrator Moderator

    @zakattack361
    killed.getType().equals(EntityType.COW)
    Turn that into this:
    killed.getType() == EntityType.COW
     
  7. Offline

    zakattack361

    Still nothing happening
     
  8. Offline

    timtower Administrator Administrator Moderator

    @zakattack361 Then add a print statement in the first line of the event.
    See if it gets called.
     
  9. Offline

    zakattack361

    Tried that and it isn't called, but I can't see anything wrong with my listener. Is Bukkit.broadcastMessage() the right way to do this or am I doing it wrong? I want the message to be sent to all players as the amount of kills is shared.
     
    Last edited: Feb 4, 2021
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    zakattack361

    I'm sure the server is working because the plugin has a command for something else and that works. I'll send the server log anyway: https://pastebin.com/RtuWXP1g
     
  12. Offline

    timtower Administrator Administrator Moderator

  13. Offline

    zakattack361

    Actually in my main I have 2 commands before it
    Code:
    public class Main extends JavaPlugin{
    
        @Override
        public void onEnable() {
            new skillboardShop(this);
            new skillboardChallenges(this);
            new skillboardListener(this);
        }
        @Override
        public void onDisable() {
            getLogger().info("BYE!");
        }
    }
    
    skillboardShop and skillboardChallenges. Do I need to have it in a certain order with skillboardListener first? I've never done a plugin with multiple files before
     
  14. Offline

    timtower Administrator Administrator Moderator

    @zakattack361 Order is not important for this.
    Can you add a logger line in the onEnable to make sure that your plugin gets updated correctly?
     
  15. Offline

    zakattack361

    It's being updated correctly because the skillboardShop and skillboardChallenges work but I'll add it anyway
    Edit: The logger isn't working either when I do this:
    Code:
    package me.coding.skillboard;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.coding.skillboard.commands.skillboardChallenges;
    import me.coding.skillboard.commands.skillboardShop;
    import me.coding.skillboard.listeners.skillboardListener;
    
    public class Main extends JavaPlugin{
    
        @Override
        public void onEnable() {
            new skillboardShop(this);
            new skillboardChallenges(this);
            new skillboardListener(this);
            getLogger().info("testing");
        }
        @Override
        public void onDisable() {
            getLogger().info("BYE!");
        }
    }
     
  16. Offline

    timtower Administrator Administrator Moderator

    @zakattack361 Then I think that you are exporting to the wrong file.
     
    zakattack361 likes this.
  17. Offline

    zakattack361

    @timtower Yes I think you're correct because in my bin folder nothing is being updated. How can I fix this?
    Edit: I was exporting directly into the server folder and that may have messed it up

    Thank you so much I've managed to fix it. I'd accidently changed my default execution environment to 15 instead of 1.8 while my complier level was 1.8.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 5, 2021
Thread Status:
Not open for further replies.

Share This Page