Solved Cancel a task from another class.

Discussion in 'Plugin Development' started by VinexAx789, Jun 16, 2016.

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

    VinexAx789

    Problem: Cannot cancel task from other class.

    Class:
    Code:
    package com.venomsurge.dtp.timer;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.venomsurge.dtp.DTP;
    import com.venomsurge.dtp.DTP.GameState;
    import com.venomsurge.dtp.utils.C;
    import com.venomsurge.dtp.utils.TabUtils;
    
    public class GameEndTimer extends BukkitRunnable {
    
        int timer;
    
        public DTP plugin;
    
        public GameEndTimer(DTP plugin, int timer) {
            this.timer = timer;
            this.plugin = plugin;
        }
    
        @SuppressWarnings({ "deprecation", "static-access" })
        @Override
        public void run() {
            if (timer == 0) {
                plugin.gstate = GameState.ENDED;
                for (Player player : Bukkit.getOnlinePlayers()) {
                    TabUtils.sendTitle(player, 10, 10, 10, ChatColor.GREEN + "Game over" + ChatColor.WHITE + "!");
                    plugin.endGame.gameOver(1, player);
                    plugin.presidentStillAlive(player);
                }
                Bukkit.broadcastMessage(ChatColor.GREEN + "Game over" + ChatColor.WHITE + "!");
                this.cancel();
            } else if (timer == 600) {
                Bukkit.broadcastMessage(
                        ChatColor.GREEN + "Game ending in " + ChatColor.WHITE + "10" + ChatColor.GREEN + " minutes");
                for (Player player : Bukkit.getOnlinePlayers()) {
                    player.playSound(player.getLocation(), Sound.NOTE_PLING, 1, 1);
                }
            } else if (timer == 300) {
                Bukkit.broadcastMessage(
                        ChatColor.GREEN + "Game ending in " + ChatColor.WHITE + "5" + ChatColor.GREEN + " minutes");
                for (Player player : Bukkit.getOnlinePlayers()) {
                    player.playSound(player.getLocation(), Sound.NOTE_PLING, 1, 1);
                }
            } else if (timer == 180) {
                Bukkit.broadcastMessage(
                        ChatColor.GREEN + "Game ending in " + ChatColor.WHITE + "3" + ChatColor.GREEN + " minutes");
                for (Player player : Bukkit.getOnlinePlayers()) {
                    player.playSound(player.getLocation(), Sound.NOTE_PLING, 1, 1);
                }
                for (String player : plugin.attackers) {
                    ItemStack presidentFinder = new ItemStack(Material.COMPASS, 1);
                    ItemMeta presMeta = presidentFinder.getItemMeta();
                    presMeta.setDisplayName(
                            C.red + "President Tracker " + C.dgray + "(" + C.yellow + "Right Click" + C.dgray + ")");
                    presidentFinder.setItemMeta(presMeta);
                    Bukkit.getPlayer(player).getInventory().addItem(presidentFinder);
                }
            } else if (timer == 60 || timer == 30 || timer == 20 || timer == 10 || timer <= 5) {
                Bukkit.broadcastMessage(ChatColor.GREEN + "Game ending in " + ChatColor.WHITE + timer + ChatColor.GREEN
                        + " second" + (timer != 1 ? "s" : ""));
                for (Player player : Bukkit.getOnlinePlayers()) {
                    TabUtils.sendTitle(player, 10, 10, 10, ChatColor.GREEN + "Game ending in " + ChatColor.WHITE + timer
                            + ChatColor.GREEN + " second" + (timer != 1 ? "s" : ""));
                    player.playSound(player.getLocation(), Sound.NOTE_PLING, 1, 1);
                }
            }
            timer--;
        }
    }
    I'm trying to cancel the task like this:

    Code:
    plugin.endTimer.cancel();
    But that didn't work it gave me a null pointer but then I did this:

    Code:
    plugin.endTimer.runTaskTimer(plugin, 20L, 20L).cancel();
    Then it worked but it doesn't end the task.

    If you can please help me that would be much appreciated.
     
  2. Offline

    Zombie_Striker

    @VinexAx789
    The reason you're getting an npe is because the timer most likely is null. Are you sure you nullchecked before you canceled the task.
     
  3. Offline

    VinexAx789

    @Zombie_Striker No I didn't null check before. That explains it. I'll try it.

    @Zombie_Striker Ok but what can I use the .cancel? Or plugin.endTimer.runTaskTimer(plugin, 20L, 20L).cancel(); I tried them both still didn't work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 17, 2016
  4. Offline

    Zombie_Striker

    @VinexAx789
    Have you tried using Scheduler#cancelTask(task's id)?
     
  5. Offline

    VinexAx789

    @Zombie_Striker I've tried to make an int = getTaskId();
    Then use Bukkit.getScheduler.cancelTask(endTimer.task);

    But that didn't work before.
     
  6. Offline

    Irantwomiles

    it needs to cancel your int.
     
  7. Offline

    VinexAx789

    @Irantwomiles

    I've tried this:

    Code:
    public int taskId = this.getTaskId();
    
    Then I've tried this:
    Code:
                if (!(plugin.endTimer.equals(null))) {
                    Bukkit.getScheduler().cancelTask(plugin.endTimer.taskId);
                }
    
    Didn't work here's the log.

    PHP:
    18.06 00:12:58 [ServerERROR Could not load 'plugins/DTP.jar' in folder 'plugins'
    18.06 00:12:58 [ServerINFO org.bukkit.plugin.InvalidPluginExceptionjava.lang.IllegalStateExceptionNot scheduled yet
    18.06 00
    :12:58 [ServerINFO at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot45.jar:git-Spigot-5f38d38-12698ea]
    18.06 00:12:58 [ServerINFO at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot45.jar:git-Spigot-5f38d38-12698ea]
    18.06 00:12:58 [ServerINFO at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    18.06 00:12:58 [ServerINFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    18.06 00:12:58 [ServerINFO at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    18.06 00:12:58 [ServerINFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    18.06 00:12:58 [ServerINFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_66]
    18.06 00:12:58 [ServerINFO Caused byjava.lang.IllegalStateExceptionNot scheduled yet
    18.06 00
    :12:58 [ServerINFO at org.bukkit.scheduler.BukkitRunnable.getTaskId(BukkitRunnable.java:134) ~[spigot45.jar:git-Spigot-5f38d38-12698ea]
    18.06 00:12:58 [ServerINFO at com.venomsurge.dtp.timer.GameEndTimer.<init>(GameEndTimer.java:21) ~[?:?]
    18.06 00:12:58 [ServerINFO at com.venomsurge.dtp.game.EndGame.<init>(EndGame.java:29) ~[?:?]
    18.06 00:12:58 [ServerINFO at com.venomsurge.dtp.events.JoinNQuitEvent.<init>(JoinNQuitEvent.java:41) ~[?:?]
    18.06 00:12:58 [ServerINFO at com.venomsurge.dtp.DTP.<init>(DTP.java:125) ~[?:?]
    18.06 00:12:58 [ServerINFO at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_66]
    18.06 00:12:58 [ServerINFO at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_66]
    18.06 00:12:58 [ServerINFO at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_66]
    18.06 00:12:58 [ServerINFO at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[?:1.8.0_66]
    18.06 00:12:58 [ServerINFO at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_66]
    18.06 00:12:58 [ServerINFO at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot45.jar:git-Spigot-5f38d38-12698ea]
    18.06 00:12:58 [ServerINFO at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot45.jar:git-Spigot-5f38d38-12698ea]
    18.06 00:12:58 [ServerINFO ... 6 more
     
  8. Offline

    Zombie_Striker

    It seems you are trying to cancel a task that has not started yet. Are you sure it is currently running? are you sure "taskId" is actually equal to the id?
     
  9. Offline

    VinexAx789

    @Zombie_Striker I've never tried to use extends BukkitRunnable normally I just use methods but the thing is it is indeed running, taskId variable is this public int taskId = this.getTaskId();
     
  10. Offline

    Irantwomiles

    @VinexAx789 I usually have my int = taskID; in my global space, then where I use my scheduler I do this.

    Code:
    taskID = Bukkit.getScheduler().get..(what ever task)
    
    then when I want to cancel my task I do this

    Code:
    Bukkit.getScheduler().cancelTask(taskID);
    
    not sure if its cancelTask() or cancelTaskID(), one of those.
     
  11. Offline

    VinexAx789

  12. Offline

    Irantwomiles

    thats where it would be a repeating task or delayed task, i dont remember what its called.
     
  13. Instead of using
    Code:java
    1. this.getTaskId();

    Use
    Code:java
    1. public int taskID = (define bukkitrunnable here)

    That's how I've done it in the past and it works perfectly fine.
     
  14. Offline

    VinexAx789

  15. Offline

    ArsenArsen

    When you schedule it it'll return a number, I thing it's int, and then save it and call cancelTask in BukkitScheduler. I am typing on my phone so function names are unchecked. Just parse the int to the function.
     
    VinexAx789 likes this.
  16. Offline

    VinexAx789

    @ArsenArsen So you're telling me to make an int? Then when I goto cancel it put that int as the id?
     
  17. Offline

    ArsenArsen

    Exactly :)
    Code:
        public static int id; // In main, without it you cant access it.
    
            id = getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {public void run() {}}, 20, 20);//In your main aswell
            Bukkit.getServer().getScheduler().cancelTask(Main.id); // Other class
    But I would still make a TaskManager which has a HashMap<String, Integer> and functions like scheduleRepeat(String id), and cancelTask(String id), and just put and remove from the map. Just more dev friendly and expandable.

    EDIT: It should fix the ISE because it returns after scheduling is done. Also
     
  18. Offline

    VinexAx789

    @ArsenArsen This will work with a class that is Extending BukkitRunnable?
     
  19. Offline

    ArsenArsen

    Yes, in theory. Because it doesn't care about the runnable, just adds it to the scheduler and tells the id. Still I havent done any tests. And by the way I think BukkitRunnable is deprecated, so I just use runnables all the time.
     
  20. Offline

    I Al Istannen

    @ArsenArsen
    BukkitRunnable is not deprecated.
    You can just call BukkitRunnable#cancel() instead of trying to get an id and also use BukkitRunnable#getTaskId() to get the id.

    Comparing Runnables and BukkitRunnables is pointless. One is the generic Java interface and one is a class, specifically written for using with the BukkitScheduler. Which do you think has more features and maybe be the better choice?
    (I mean, the class description is:
    )

    It all depends on the usecase and personal preference. I love BukkitRunnables and only use the SchedulerMethods if I have a short runnable and can easily use lambdas for it. Just when I feel it makes the code more tidy.

    You can find the JavaDoc here.
     
  21. Offline

    VinexAx789

  22. You don't need to worry about the class extending BukkitRunnable. Just use
    Code:java
    1. BukkitScheduler#cancelTask(id);
     
  23. Offline

    VinexAx789

    @Astrophylite thanks but the ways above didn't work and I've fixed this by just cancelling the task if it equaled the game state that I needed to cancel it.
     
Thread Status:
Not open for further replies.

Share This Page