Canceling Tasks

Discussion in 'BungeeCord Plugin Development' started by Lazertx, Nov 25, 2014.

  1. I am working on a AntiBot plugin and I recently added a reload command that calls my reload method.
    Code (Text):
        public void reload() {
            reloadConfig();

            unregisterChecks();
            unregisterCommands();

            p = this;

            registerChecks();
            registerCommands();
        }
    The method unregisterChecks() is suppose to cancel all tasks running by my plugin.
    Code (Text):
        private void unregisterChecks() {
            getProxy().getPluginManager().unregisterListeners(this);
            System.out.println(getProxy().getScheduler().cancel(this));
        }
    getProxy().getScheduler().cancel(Plugin) returns the amount of tasks canceled by the scheduler and my console prints out 0 which means that nothing is being cancel so something may be wrong with the way I scheduled my task.

    The task that I want to be canceled.
    Code (Text):
        private void registerChecks() {
            if (configuration.getBoolean(ConfigPaths.CHECKS_SAME_IP_ENABLED)) {
                getProxy().getScheduler().schedule(this, new Runnable() {
                    @Override
                    public void run() {
                        getProxy().getPluginManager().registerListener(AntiBot.p, new SameIPCheck());
                        for (ProxiedPlayer proxiedPlayer : AntiBot.p.getProxy().getPlayers()) {
                            if (proxiedPlayer.hasPermission("antibot.admin")) {
                                proxiedPlayer.sendMessage(new ComponentBuilder("ยป ").color(ChatColor.DARK_RED).append("Same IP check enabled.").color(ChatColor.RED).create());
                            }
                        }
                    }
                }, configuration.getInt(ConfigPaths.CHECKS_SAME_IP_ENABLE_DELAY), TimeUnit.SECONDS);
            }
        }
    Whole class if needed http://pastebin.com/xD3haQ5v

    Note: All of this code is inside of AntiBot.java which extends net.md_5.bungee.api.plugin.Plugin

    I may be missing something obvious so any help would be greatly appreciated.
     
    #1 Lazertx, Nov 25, 2014
    Last edited: Nov 25, 2014
  2. (Typing from phone so keeping it short)
    Have you tried BukkitRunnable and calling its runTask something method to start it. Should be able to cancel those.
     
    • Optimistic Optimistic x 1
  3. I am using the BungeeCord API for my plugin and don't want to have to compile bukkit with my plugin for people to run a BungeeCord plugin as it would make my plugin a big download and it just seems wrong to have to add a dependency for something like this.
     
  4. My bad, didn't notice this was in the BungeeCord department. I don't know shit about developing for BungeeCord so I'll leave it to someone else to try and help.

    Good luck!
     
  5. xD Regardless I appreciate your attempt to help.
     
    • Friendly Friendly x 1
  6. Maybe store ScheduledTask object in variable? and cancel it on reload
     
  7. Yeah it looks like that's what I have to do if you look here
    [​IMG]
    I am using the first method and for some reason they don't add it to the tasks Object Map and remove it when it runs.

    Edit: Do you think it's worth submitting a pull request for this?

    Edit 2: Actually the first method just calls the second method but with the argument of 0 so it won't repeat anyways I am just going to store the task and cancel it that way.
     
  8. Well yes, but it's executing after a delay so you don't need period.
     
  9. If you want to cancel them all, try and store the ids or like ^^ said, the scheduled tasks in a set or list.
     
  10. Tux

    Tux

    Try using the latest BungeeCord build, it fixes your original code.