[SOLVED] Event was called twice?! (Again?)

Discussion in 'Spigot Plugin Development' started by xxxCheeseproxxX, May 16, 2015.

  1. A while ago, I have posted a thread about the same problem right here, and it was solved.

    The problem is that whenever I call any of the abilities, the code will run twice when it shouldn't be.

    But for some reasons, the double event call or whatever the error was happened again... the weird thing was that I didn't even modify any of the listener classes that would cause double calling...

    So, I assume that the error is caused by the modification of the main class? However, the double calling was solved last time by removing all the listeners registers in the main class, thus, my main class does not contains any registering...

    I believe you are kind of confused, if not I am...

    Classes
    Reality.java (Main Class)
    Abilities.java (Abilities Interface)
    AbilityExplode.java (One of my ability class)

    ScreenShot(s)
    This was captured when I only activate the explode ability once. But the event was actually called twice.
    [​IMG]

    Please reply if you do know what is going on here, thank you :).

    *I will post out more codes if needed.
     
    #1 xxxCheeseproxxX, May 16, 2015
    Last edited: May 20, 2015
  2. Are you calling the constructor for the same listener twice in your plugin? Because listeners are supposed to only have one instance.
     
  3. Also note the use of /reload. I have found that sometimes after a few times Listeners are not automatically unregistered thus registered twice.
    A restart fixed it in those situations.
     
    • Agree Agree x 1
  4. Sorry for not having a respond for such a long time, I was on a vacation. Anyways, I for sure know that this problem wasn't caused by the /reload thing because it occurs when I start the server. Also, I am not calling the constructor twice, for a fact that the listeners weren't even registered in the main class. (I did this to fix the double event calling last time, even though I did't know why.)

    Does anyone else have a solution? It will be extremely helpful, since I really can't start to develop anymore without fixing this weird error. Thank you :).
     
  5. You could try to add debug messages to try to trace the listeners and events.
     
    • Like Like x 1
  6. I did tried to add some debug messages in different places in this class and other abilities classes. But all the debug messages printed twice... which means that the event was called twice, I assume.
    Code (Text):
        @EventHandler
        public void onPlayerUse(PlayerInteractEvent event) {
            //I HAD ADDED A DEBUG HERE
            final Player p = event.getPlayer();
            Map<String, List<String>> mapCache = ranks.get(playersINFO.get(p.getUniqueId()).get("rank"));
            if(mapCache.get("abilities")!=null){
                //I HAD ADDED A DEBUG HERE
                List<String> listCache = mapCache.get("abilities");
                if (listCache.contains("EXPLODE")) {
                    //I HAD ADDED A DEBUG HERE
                    if (p.getItemInHand().getType() == Material.getMaterial(abilitiesOptions.get("EXPLODE").get("item")) && tools.isHoldingCorrectItem(p, abilitiesOptions.get("EXPLODE").get("item"))) {
                        //I HAD ADDED A DEBUG HERE
                        if (cooldownTime.containsKey(p)) {
                            msg.send(p, "c", "You must wait for " + ChatColor.GREEN + cooldownTime.get(p) + ChatColor.RED + " seconds, before using "+ChatColor.LIGHT_PURPLE + "Explode" + ChatColor.RED + " again!");
                            return;
                        }
                        World world = p.getWorld();
                        world.spawn(p.getLocation(), TNTPrimed.class).setFuseTicks(30);
                        world.spawn(p.getLocation(), TNTPrimed.class).setFuseTicks(30);
                        world.spawn(p.getLocation(), TNTPrimed.class).setFuseTicks(30);
                        p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 20*4+10, 8));

                        cooldownTime.put(p, Integer.parseInt(abilitiesOptions.get("EXPLODE").get("cooldown")));
                        cooldownTask.put(p, new BukkitRunnable() {
                            public void run() {
                                cooldownTime.put(p, cooldownTime.get(p) - 1);
                                if (cooldownTime.get(p) == 0) {
                                    cooldownTime.remove(p);
                                    cooldownTask.remove(p);
                                    cancel();
                                }
                            }
                        });
                        cooldownTask.get(p).runTaskTimer(plugin, 20, 20);
                    }
                }
            }
        }
     
  7. Try to add a debug message into the constructor of the class to see if its being called twice.
     
    • Like Like x 1
  8. Yes it is being called twice, so does this means that the error is not caused by the abilities classes? But something else?

    Thank you for your tips :)
     
  9. Yes it is outside the abilities class. It means somewhere in your project you are creating two instances of that same class on accident.

    You're very welcome :)

    *Edit*

    Also, i was looking at your AbilityExplode class. There is a possible bug that may arise in the future, there's no if statement checking what type of interaction it is. So a player could be holding the correct tool but step on a pressure plate, which would cause the explosion to occur.
     
    • Like Like x 1
  10. If you're talking about me.cheesepro.reality.abilities.Abilites, that class has no existing code in it so it could not have caused this error. Unless in one of the functions that you've overridden has code in it with the ability to create another instance of AbilityExplode.
     
    • Like Like x 1
  11. Ohhh I got it! Solved the problem! The problem was that I was calling this class twice :). And most importantly this class registers all the abilities.

    Big thank you to @Rokl
     
    #11 xxxCheeseproxxX, May 20, 2015
    Last edited: May 20, 2015
    • Like Like x 1
  12. Thank you for your help again :), thanks for actully looking in to my code and trying to find possible bugs. Anyways, I have added this line of code to fix the bug you mentioned :)

    Code (Text):
    if (event.getAction()== Action.RIGHT_CLICK_AIR || event.getAction()== Action.RIGHT_CLICK_BLOCK)
     
     
    • Like Like x 1
  13. use HandlerList.unregisterAll(this); in onDisable()