starting/stopping tasktimer via command(s)

Discussion in 'Spigot Plugin Help' started by Michae, Mar 15, 2020.

  1. I'm making a particle system for my plugin. Long story short I can get the task to run with a normal
    Code (Text):
        public Map<CommandSender, BukkitTask> tasks = new HashMap<>();

        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {
            if ((sender instanceof Player)) {
                Player player = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("turnon")) {
                    BukkitTask t = tasks.put(sender, Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
                     
                        @Override
                        public void run() {
                         
                            Location location = player.getLocation();
                             player.spawnParticle(Particle.FLAME, location.getX(), location.getY(), location.getZ(), 15, 0.3,
                                    0.9, 0.3, 0.05, true);
                         
                        }
                    }, 0l, 0l));
                    if (t != null)
                        t.cancel();
                } else if (cmd.getName().equalsIgnoreCase("turnoff")) {
                    BukkitTask t = tasks.get(sender);
                    if (t != null) {
                        t.cancel();
                        player.sendMessage(ChatColor.RED + "scheduler stopped!");
                    }
                }
            }
            return false;
        }

    but if i introduce

    Code (Text):
        public Map<CommandSender, BukkitTask> tasks = new HashMap<>();

        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {
            if ((sender instanceof Player)) {
                Player player = (Player) sender;
                if (args.length >= 1) {
                    if (cmd.getName().equalsIgnoreCase("ssj")) {
             
                        if (args[0].equalsIgnoreCase("1")) {
                            BukkitTask t = tasks.put(sender, Bukkit.getScheduler().runTaskTimer(this, new Runnable() {

                                @Override
                                public void run() {
                                    Location location = player.getLocation();
                                    player.spawnParticle(Particle.FLAME, location.getX(), location.getY(), location.getZ(),
                                            15, 0.3, 0.9, 0.3, 0.05, true);
                                }
                            }, 0l, 0l));
                            if (t != null)
                                t.cancel();
                        }
                    }
                }
            }
            return false;
        }
    I get this error
    Code (Text):
     Task #9 for testsaiyan v1 generated an exception
    java.lang.IllegalArgumentException: data should be class java.lang.Void got class java.lang.Boolean
            at org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer.spawnParticle(CraftPlayer.java:1638) ~[spigot-1.15.2.jar:git-Spigot-6de3d4b-fc24934]
            at maven.testsaiyan.Main$1.run(Main.java:45) ~[?:?]
            at org.bukkit.craftbukkit.v1_15_R1.scheduler.CraftTask.run(CraftTask.java:81) ~[spigot-1.15.2.jar:git-Spigot-6de3d4b-fc24934]
            at org.bukkit.craftbukkit.v1_15_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:394) [spigot-1.15.2.jar:git-Spigot-6de3d4b-fc24934]
            at net.minecraft.server.v1_15_R1.MinecraftServer.b(MinecraftServer.java:1036) [spigot-1.15.2.jar:git-Spigot-6de3d4b-fc24934]
            at net.minecraft.server.v1_15_R1.DedicatedServer.b(DedicatedServer.java:406) [spigot-1.15.2.jar:git-Spigot-6de3d4b-fc24934]
            at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:984) [spigot-1.15.2.jar:git-Spigot-6de3d4b-fc24934]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:824) [spigot-1.15.2.jar:git-Spigot-6de3d4b-fc24934]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]

    I feel like I'm missing something simple. I'm open to making this code better and criticism so don't be soft on me.
     
  2. After a bit of research, I'm sure that making this a BukkitRunnable class would help out even better. If this is giving me an error stating this needs to a void instead of boolean, then i'm sure that's what this means.
     
  3. No beans, I'm having a real hard time here. Could someone point me in the right direction?
     
  4. Not 100% sure, but I'd think storing the task id would be easier compared to storing the runnable.

    Then just call
    Code (Java):
    Bukkit.getSchedularSomething.cancel(taskId);
    So have a separate class for your runnable, create an instance of it, put the task id in your map and run it.

    I also wouldn't put sender as your key, UUID would be much better (or the Player object itself)
     
  5. That might work, will do.