Public Run() ?

Discussion in 'Spigot Plugin Development' started by jamesd5, Jan 14, 2019.

  1. So I am trying to cancel a Public run() to where it only runs once.
    Code (Text):
    public run () {
    //code
    cancel();
    }
    This Should work right? However it still runs every tick. Any suggestions?
     
  2. use Bukkit#getScheduler()#runTask or runTaskLater and it will run only once, no need for BukkitRunnable if you don't want to run it in timer.
     
  3. Show the code for your BukkitRunnable.
     
  4. Sorry I did not give all the information but ty :>
    Code (Text):

    Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                    @Override
                        public void run() {
                            for(Player p : sm.getWorld().getPlayers()) {
                                if(sm.getHealth() > 0) {
                                if(!(sm.getHealth() == 0)) {
                                final Sheep bomb = sm.getWorld().spawn(p.getLocation(), Sheep.class);
                                BukkitRunnable runnable = new BombTask(bomb.getWorld(), bomb);
                                runnable.runTaskTimer(MainclassName.plugin, 0L, 0L);
                                cancel();
                                } else {
                                    cancel();
                                }
                                } else {
                                    cancel();
                                }
                            }
                        }
                }, 100L, 100L);
     
    What it is doing is summoning a sheep every tick, but I want it to only summon it once. :>
     
  5. Maybe don't use scheduleSyncRepeatingTask()?
     
    • Funny Funny x 2
  6. No offense but it's a great mess. And it's not clear what the purpose of the whole thing. Could you paste the whole method?
     
  7. here's your code with proper indents:
    Code (Java):
    Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
            @Override
            public void run() {
            for(Player p : sm.getWorld().getPlayers()) {
                if(sm.getHealth() > 0) {
                    if(!(sm.getHealth() == 0)) {
                        final Sheep bomb = sm.getWorld().spawn(p.getLocation(), Sheep.class);
                        BukkitRunnable runnable = new BombTask(bomb.getWorld(), bomb);
                        runnable.runTaskTimer(MainclassName.plugin, 0L, 0L);
                        cancel();
                    } else {
                        cancel();
                    }
                } else {
                    cancel();
                }
            }
        }
    }, 100L, 100L);
    Also, What is your "BombTask" object? If that is calling this task, that would absolutely cause it to continue infinitely and worse yet create unnecessary objects and tasks over and over...

    You're using a repeating task for something you'd want to run only one time, so fix that up by using a delayed task instead, and get rid of redundant if statements:
    Code (Java):
    Bukkit.getScheduler().runTaskLater(plugin, () -> {
                for (Player p : sm.getWorld().getPlayers()) {
                    if (sm.getHealth() > 0) {
                        final Sheep bomb = sm.getWorld().spawn(p.getLocation(), Sheep.class);
                        BukkitRunnable runnable = new BombTask(bomb.getWorld(), bomb);
                        runnable.runTaskTimer(MainclassName.plugin, 0L, 0L);
                    }
                }
            }, 100L);
    Second, assuming your "BombTask" is meant to be a countdown until the sheep "explodes," You again don't want a task timer. You want a delayed task.
     
    #7 GreatThane, Jan 14, 2019
    Last edited: Jan 14, 2019
    • Useful Useful x 1
  8. There is no need to cancel a task at the end.
    Do as suggested and use BukkitRunnable#runTaskLater or BukkitRunnable#runTask OR use Scheduler#runTask
     
    • Agree Agree x 1
  9. Ok ty for all the resposnses. My code
    Code (Text):
    public void run() {
    Bukkit.getScheduler().runTaskLater(plugin, () -> {
                for (Player p : sm.getWorld().getPlayers()) {
                    if (sm.getHealth() > 0) {
                        final Sheep bomb = sm.getWorld().spawn(p.getLocation(), Sheep.class);
                        BukkitRunnable runnable = new BombTask(bomb.getWorld(), bomb);
                        runnable.runTaskTimer(MainclassName.plugin, 0L, 0L);
                    }
                }
            }, 100L);
    }
     
    Ty to @GreatThane, @Alex_qp, @ItzSomebody

    Sorry I am not that well at explaining.
    But what @GreatThane assumed was mostly correct. With a delay, a sheep is summoned. In the BombTask, it customizes the sheep, and with another delay it explodes.
    With
    Code (Text):
     public void run() {
    at the top, It repeats every tick, so I tried to cancel this with
    Code (Text):
     cancel();
    . And it did not work. How would I summon 1 Sheep, still using public void run()? ty :D
     
  10. Is there any reason you don't want to use lambda expressions?
     
  11. Leave this old dinosaur and switch to Java 8 finally, you don't need to use anonymous class if you can use simple lambda.
     
  12. I think that's working:
    Code (Java):
    cancel();
    return;
     
  13. By the sounds of it, you want to start another delayed task and NOT a repeating task.
    Assuming all the methods (of sm, for example) exist:
    Code (Java):
    public void run() {
    Bukkit.getScheduler().runTaskLater(plugin, () -> {
                for (Player p : sm.getWorld().getPlayers()) {
                    if (sm.getHealth() > 0) {
                        final Sheep bomb = sm.getWorld().spawn(p.getLocation(), Sheep.class);
                        BukkitRunnable runnable = new BombTask(bomb.getWorld(), bomb);
                        runnable.runTaskLater(MainclassName.plugin, 1 * 20L);  // 1 second delay, change as necessary - run only once
                    }
                }
            }, 100L);
    }
    Only if he uses a BukkitRunnable, not for Runnable or a lambda expression.
     
    • Useful Useful x 1
  14. I have tried this, however it still spawns the Sheep every tick.
    Code (Text):

    public void run() {
        for(int i=0; i<1; i++) {
             Bukkit.getScheduler().runTaskLater(plugin, () -> {
                  for(Player p : sm.getWorld().getPlayers()) {
                       while (sm.getHealth() > 0) {
                            final Sheep bomb = sm.getWorld().spawn(p.getLocation(), Sheep.class);
                            BukkitRunnable runnable = new BombTask(bomb.getWorld(), bomb);
                            runnable.runTaskLater(MainClassName.plugin, 1 * 20L);
                        }
                    }
             }, 100L);
         }
    }
     
    Any suggestions?
     
  15. How's your server not crashing with that code?
    Code (Java):
               for(Player p : sm.getWorld().getPlayers()) {
                       while (sm.getHealth() > 0) {
    Also, what's the point of for(int i=0; i<1; i++) {?
     
  16. I just confused. :I I am trying to find methods of limiting the code to only spawn One Sheep. Have any suggestions?
     
  17. Do you only want to do it once or do it once but with a delay?
     
  18. try this:

    Code (Text):
    for(int i = 0, i = bukkit.getServer().getOnlinePlayers(), i++) {

    }
     
    • Like Like x 1
  19. Once with a delay
     
  20. And what do you want to do exactly with this? Spawn a sheep on every player online?
     

Share This Page