Solved BukkitRunnable won't cancel

Discussion in 'Plugin Development' started by Natank25, Nov 27, 2021.

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

    Natank25

    Hi,
    I am kind of new to plugins development and I created a BukkitRunnable wich inside of it I wrote "this.cancel()" at the end. The fact is when I want to start the same thread again, this error message show up in the console :

    "java.lang.IllegalStateException: Already scheduled as 9895"

    So, I don't know if I did something wrong or not

    Thanks in advance
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    Natank25

    Ok so, there is my code, i also tried to use a command to cancel the task with the task id that show up in the server console

    My Main class :

    Code:
    package io.github.Natank25.HideAndSeek;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Instrument;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Note;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    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.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener { // Extending JavaPlugin so that Bukkit knows its the main
                                                                // class...
    
        private static Plugin plugin;
        int timerHiding = 30;
        String timerHunting = "5";
        String maps[] = { "Jungle", "Desert" };
        String currentMap;
        boolean hunterReady = false;
        boolean hiderReady = false;
        List<Player> hiders = new ArrayList<Player>();
        List<Player> hunters = new ArrayList<Player>();
        List<Entity> huntersEntity = new ArrayList<Entity>();
        List<Entity> hidersEntity = new ArrayList<Entity>();
        Entity huntersPoint = Bukkit.getEntity(UUID.fromString("671d121a-0a1b-4276-9110-f858a89e9a41"));
        Entity hidersPoint = Bukkit.getEntity(UUID.fromString("b394db20-176c-4c18-b439-bb56907ed2a7"));
        Location hiderRedstoneBlock = new Location(Bukkit.getWorld("world"), -85, 163, -281);
        Location hunterRedstoneBlock = new Location(Bukkit.getWorld("world"), -78, 163, -285);
        Start start = new Start(this);
    
        public void onEnable() {
            plugin = this;
            this.getCommand("cancelTask").setExecutor(new cancelTaskExecutor(this));
            getServer().getPluginManager().registerEvents(this, this);
        }
    
        public void onDisable() {
            plugin = null;// To stop memory leeks
            Bukkit.getServer().getScheduler().cancelTasks(this);
        }
    
    
        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
            for (Listener listener : listeners) {
                Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
            }
        }
    
    //To access the plugin variable from other classes
        public static Plugin getPlugin() {
            return plugin;
        }
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
    
            Player player = event.getPlayer();
            Block b = event.getClickedBlock();
            Action action = event.getAction();
    
            if (action == Action.RIGHT_CLICK_BLOCK && b != null
                    && (b.getType() == Material.OAK_SIGN || b.getType() == Material.OAK_WALL_SIGN)) {
                Sign sign = (Sign) b.getState();
                String line2 = sign.getLine(1);
                String line3 = sign.getLine(2);
    
    //Set time to hide
                if (line2.equalsIgnoreCase("Set time to hide to")) {
                    if (timerHiding == 30 || timerHiding == 60 || timerHiding == 90) {
                        timerHiding += 30;
                    } else if (timerHiding == 120) {
                        timerHiding = 180;
                    } else {
                        timerHiding = 30;
                    }
                    sign.setLine(2, timerHiding + " secs");
                }
    
    //Set time to hunt
                if (line2.equalsIgnoreCase("Set time to hunt")) {
                    if (timerHunting == "5") {
                        timerHunting = "7.5";
    
                    } else if (timerHunting == "7.5") {
                        timerHunting = "10";
    
                    } else if (timerHunting == "10") {
                        timerHunting = "15";
    
                    } else if (timerHunting == "15") {
                        timerHunting = "5";
                    }
                    sign.setLine(2, "to " + timerHunting + " mins");
    
                }
    
                if (line2.equalsIgnoreCase("Start")) {
                    player.sendMessage("START");
                }
    
    //Choose map
                if (line3.equalsIgnoreCase("Map")) {
                    String mapChoosing = sign.getLine(1);
                    if (currentMap != null) {
                        if (currentMap.equals(mapChoosing)) {
                            player.sendMessage(mapChoosing + " map was already selected");
                        } else {
                            currentMap = mapChoosing;
                            Bukkit.broadcastMessage(mapChoosing + " map is now selected !");
                        }
                    } else {
                        currentMap = mapChoosing;
                        Bukkit.broadcastMessage(mapChoosing + " map is now selected !");
                    }
                }
    
    //Ready the team
                if (line2.equalsIgnoreCase("Hunter Team")) {
                    if (!hunterReady) {
                        sign.setLine(2, "Click to " + ChatColor.RED + "unready");
                        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(),
                                "fill -77 167 -282 -77 165 -284 minecraft:blue_stained_glass_pane");
                        huntersEntity = huntersPoint.getNearbyEntities(3.0, 3.5, 2.5);
                        if (huntersEntity != null) {
                            for (Entity e : huntersEntity) {
                                if (e.getType() == EntityType.PLAYER && e != null) {
                                    hunters.add((Player) e);
                                }
                            }
                            for (Player p : hunters) {
                                player.sendMessage(p.getName());
                            }
                        }
                        hunterRedstoneBlock.getBlock().setType(Material.REDSTONE_BLOCK);
                        hunterReady = true;
                    } else {
                        sign.setLine(2, "Click to " + ChatColor.GREEN + "ready");
                        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "fill -77 167 -282 -77 165 -284 air");
                        hunters.clear();
                        hunterRedstoneBlock.getBlock().setType(Material.AIR);
                        hunterReady = false;
                    }
                }
                if (line2.equalsIgnoreCase("Hider Team")) {
                    if (!hiderReady) {
                        sign.setLine(2, "Click to " + ChatColor.RED + "unready");
                        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(),
                                "fill -86 167 -284 -86 165 -282 minecraft:yellow_stained_glass_pane");
                        hidersEntity = hidersPoint.getNearbyEntities(3.0, 3.5, 2.5);
                        if (hidersEntity != null) {
                            for (Entity e : hidersEntity) {
                                if (e.getType() == EntityType.PLAYER && e != null) {
                                    hiders.add((Player) e);
                                }
                            }
                            for (@SuppressWarnings("unused")
                            Player p : hiders) {
                            }
                        }
                        hiderRedstoneBlock.getBlock().setType(Material.REDSTONE_BLOCK);
                        hiderReady = true;
                    } else {
                        sign.setLine(2, "Click to " + ChatColor.GREEN + "ready");
                        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "fill -86 167 -284 -86 165 -282 air");
                        hiders.clear();
                        hiderRedstoneBlock.getBlock().setType(Material.AIR);
                        hiderReady = false;
                    }
                }
                // while (hiderReady && hunterReady) {
                // Bukkit.broadcastMessage("BOTH READY");
    
                // }
    
                sign.update(true);
                player.playNote(player.getLocation(), Instrument.PLING, Note.natural(0, Note.Tone.B));
                if (hiderReady && hunterReady) {
    
                    start.runTaskAsynchronously(this);
                }
    
            }
    
        }
    
    }
    
    My Start class :

    Code:
    package io.github.Natank25.HideAndSeek;
    
    import org.bukkit.Bukkit;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class Start extends BukkitRunnable {
    
        //private Main main;
    
        public Start(Main mainCMD) {
            //this.main = mainCMD;
        }
    
        public void run() {
                Bukkit.broadcastMessage("MAN, I CAN'T believe IT'S WORKING");
    
          
            this.cancel();
        }
    
    }
    
    and finally my cancelTaskExecutor class :

    Code:
    package io.github.Natank25.HideAndSeek;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class cancelTaskExecutor implements CommandExecutor {
        private final Main plugin;
    
        public cancelTaskExecutor(Main plugin) {
            this.plugin = plugin;
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                sender.sendMessage(args);
                Bukkit.getScheduler().cancelTask(Integer.parseInt(args[0]));
                return true;
            }
            return false;
        }
    
    }
    
    Thank for your help

    Nevermind, i got it working

    I just replaced
    Code:
     if (hiderReady && hunterReady) {
         start.runTaskAsynchronously(this);
    }
    by
    Code:
     if (hiderReady && hunterReady) {
        new Start(this).runTaskAsynchronously(this);
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 28, 2021
Thread Status:
Not open for further replies.

Share This Page