BukkitRunnable stops without a reason

Discussion in 'Spigot Plugin Development' started by OfirTIM, May 29, 2016.

  1. soo i've tried running BukkitRunnable and its seems like i got a problem with it, it is stopping randomly when i spam or in chat or just type in chat or regulary moving around... idk whats the bug or if its something to do with my bukkit version.

    Code (Text):
            final List<String> InvitedPlayers = Config.getConfig("ClansData").getStringList(ClanName + ".InvitedPlayers");
            new BukkitRunnable() {
             
                int confirmcount = 61;
             
                @Override
                public void run() {
                    try {
                    if (Config.getConfig("ClansData").getStringList(ClanName + ".InvitedPlayers").contains(invited.getUniqueId().toString())) {
                        confirmcount--;
                        System.out.println(confirmcount);
                        if (confirmcount == 0) {
                            for(int i=0;i<228;i++)
                                invited.sendMessage(" ");
                            invited.sendMessage("§6§lClan §7you expired your Invitation!");
                            Inviter.sendMessage("§6§lClan §c" + invited.getName() + " §7expired your Invitation request!");
                            InvitedPlayers.remove(invited.getUniqueId().toString());
                            Config.getConfig("ClansData").set(ClanName + ".InvitedPlayers", InvitedPlayers);
                            this.cancel();
                        }
                    } else {
                        this.cancel();
                    }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }.runTaskTimer(Main.getInstance(), 20, 20L);
    Edit:
    i found out that my ChatEvent was on Highest level and thats what completely done it wrong... but i still dont know what's the problem!


    Code (Text):
        @EventHandler (priority = EventPriority.HIGHEST)
        public void onChat(AsyncPlayerChatEvent event) {
            Player e = event.getPlayer();
            PlayerController PC = new PlayerController(e);
            ClanController CC = new ClanController(PC.getClanName());
             String format = event.getFormat();
             if (PC.getClanName() != null) {
                 if ((boolean) CC.getDataSection("ShowNickname"))
                 event.setFormat("§2[" + CC.getNickname() + "§2] §r" + format);
                 else event.setFormat("§7[" + PC.getClanName() + "§7] §r" + format);
             }
        }
     
    #1 OfirTIM, May 29, 2016
    Last edited: May 29, 2016
  2. -bump- sorry i just need it quickly done
     
  3. -Bump- Edit^^
     
  4. You should use normal Runnables
     
  5. soo you say i should use new Default Runnable?
     
  6. Well, either:

    Code (Java):
    Config.getConfig("ClansData").getStringList(ClanName + ".InvitedPlayers").contains(invited.getUniqueId().toString())
    Is returning false, or, confirmCount is 0. You call #cancel in your code, so obviously that's the point it's reaching.

    Has absolutely nothing to do with the problem. Nor is there any reason to here.
     
    • Agree Agree x 2
  7. its automaticly adding him to a invite, in the config list... after it its makes a cooldown and if he accepted before its will cancel, if he didn't its will cancel its self with deleting him from the invites
     
  8. Where is the code for that? I don't see anyone added to a list in the config anywhere.
     
  9. Code (Text):
                        final Player invited = Bukkit.getPlayer(args[1]);
                        if (invited != p) {
                        if (invited != null) {
                            PlayerController PCInvited = new PlayerController(invited);
                            if (PCInvited.getPlayerData().getBoolean("allowClanInvites")) {
                                final List<String> InvitedPlayers = (Config.getConfig("ClansData").getStringList(CC.getName() + ".InvitedPlayers"));
                                if(!PCInvited.inClan()) {
                                    if (!Config.getConfig("ClansData").getStringList(CC.getName() + ".InvitedPlayers").contains(invited.getUniqueId().toString())) {
                                InvitedPlayers.add(invited.getUniqueId().toString());
                                Config.getConfig("ClansData").set(CC.getName() + ".InvitedPlayers", InvitedPlayers);
                                Config.getConfig("ClansData").saveConfig();
                                for (String UUIDmembers : CC.getMembers()) {
                                    for (Player Allplayers : Bukkit.getOnlinePlayers()) {
                                        if(UUIDmembers.contains(Allplayers.getUniqueId().toString())) {
                                            Allplayers.sendMessage("§6§lClan §e" + p.getName() + " §3invited §c" + invited.getName() + " §3to your clan!");
                                        }
                                    }
                                }
                                new BukkitRunnable() {
                                   
                                    int confirmcount = 20;
                                   
                                    @Override
                                    public void run() {
                                        if (Config.getConfig("ClansData").getStringList(CC.getName() + ".InvitedPlayers").contains(invited.getUniqueId().toString())) {
                                            confirmcount--;
                                            System.out.println(confirmcount);
                                            if (confirmcount == 0) {
                                                for(int i=0;i<228;i++)
                                                    invited.sendMessage(" ");
                                                invited.sendMessage("§6§lClan §7you expired your Invitation!");
                                                p.sendMessage("§6§lClan §c" + invited.getName() + " §7expired your Invitation request!");
                                                InvitedPlayers.remove(invited.getUniqueId().toString());
                                                Config.getConfig("ClansData").set(CC.getName() + ".InvitedPlayers", InvitedPlayers);
                                                Config.getConfig("ClansData").saveConfig();
                                                System.out.println("Debugged");
                                                this.cancel();
                                            }
                                        } else this.cancel();
                                    }
                                }.runTaskTimer(Main.getInstance(), 20, 20L);
    Chat:

    Code (Text):
        @EventHandler (priority = EventPriority.HIGHEST)
        public void onChat(AsyncPlayerChatEvent event) {
            Player e = event.getPlayer();
            PlayerController PC = new PlayerController(e);
            ClanController CC = new ClanController(PC.getClanName());
             String format = event.getFormat();
             if (PC.getClanName() != null) {
                 if ((boolean) CC.getDataSection("ShowNickname"))
                 event.setFormat("§2[" + CC.getNickname() + "§2] §r" + format);
                 else event.setFormat("§7[" + PC.getClanName() + "§7] §r" + format);
             }
        }
    I found out its todo with the event priority
     
  10. Event priority has nothing to do with it. That would only change the order you're called in respective to other AsyncPlayerChatEvent listeners.

    What does have to do with it, is the fact that your chat event listener is Asynchronous (being called on a different thread). Not to mention you're recreating the PlayerController/ClanController objects every time. If you're expecting any state from those classes to persist through new objects, they'd be static which would already make them even more unsafe.

    Then again, the chat event has nothing to do with your runnable to begin with as far as I'm aware. Figure out which #cancel is being called, and debug your code from there.