EventHandler not working?

Discussion in 'Spigot Plugin Development' started by PretMetInternet, Jun 22, 2018.

  1. Hello everyone, I just made a simple plugin for a kill counter which counts the player's deaths and kills. I'm storing the information(kills + deaths), in an yml file which is generated for each player who has ever joined the server.
    The only thing is that the EventHandler for PlayerDeathEvent is not working, as well as the PlayerRespawnEvent. I have no idea why, but whenever I type something like this:

    Code (Text):
    @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event){
            CustomPlugin.getInstance().getLogger().info("dd");
            Player p = event.getEntity();
               Player killer = p.getKiller();
               File fK = new File("plugins/CustomPlugin/PlayerData/" + killer.getUniqueId() + ".yml");
            YamlConfiguration ymlK = YamlConfiguration.loadConfiguration(fK);
            int kills = ymlK.getInt("KitPvP.Kills");
            File fP = new File("plugins/CustomPlugin/PlayerData/" + p.getUniqueId() + ".yml");
            YamlConfiguration ymlP = YamlConfiguration.loadConfiguration(fP);
            int deaths = ymlP.getInt("KitPvP.Deaths");
            System.out.println("test test test");
            ymlK.set("KitPvP.Kills", kills + 1);
            ymlP.set("KitPvP.Deaths", deaths + 1);
            try {
                ymlK.save(fK);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                ymlP.save(fP);
            } catch(IOException e) {
                e.printStackTrace();
            }
            p.sendMessage(ChatUtil.format(ChatUtil.message("&cYou've been killed!")));
            killer.sendMessage(ChatUtil.format(ChatUtil.message("&aYou've killed someone!")));
        }
    It just doesn't do anything whenever I kill someone.
    I tried sending a message into the console, but there's no message sent either. All the other events in this class are working perfectly, so there must be something wrong with this method.
    By the way, I imported the event from
    org.bukkit.event.entity.PlayerDeathEvent, which is pretty much the only thing I can import it with.

    Please respond, I really need help right now! Thank you for helping.
     
  2. Did you register it in main class?
     
  3. I have. All the other events in the class you just saw are working perfectly fine
     
  4. If you created another class for your listener, you have to register it with an instance of this class instead of the main class. Did you do that?
     
    • Agree Agree x 1
  5. Yeah I did that. This is the code:
    getServer().getPluginManager().registerEvents(new EventsClass(), this);
     
  6. I fixed it! I have no idea HOW I fixed it, but the only thing I know what that I was exporting the file to the wrong direction every time, so one of the fixes that I tried must have worked! Thank you for trying to help me out! :D Have a nice day/morning/evening! :D
     
  7. Can you set this thread as solved? :)