Solved While loop with scheduler doesn't work ?

Discussion in 'Plugin Development' started by ThunderWaffeMC, Feb 28, 2014.

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

    ThunderWaffeMC

    Hi. Is it possible to use a while statement combined with a scheduler? When I use the code below, it crashes my server:

    Code:java
    1.  
    2. @EventHandler
    3. public void toggleSneak(PlayerToggleSneakEvent event) {
    4. final Player player = event.getPlayer();
    5. player.sendMessage("Toggled sneaking");
    6. while (player.isSneaking()) {
    7. Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
    8. @Override
    9. public void run() {
    10. player.sendMessage("You're sneaking"); //send this every 2 seconds while the player is sneaking
    11. }
    12. }, 40L);
    13. }
    14. }
    15.  


    Any help is appreciated, thanks!
     
  2. Offline

    Jake6177

    It should be possible. Something is making the statement think the player is sneaking while they're not if it keeps going.
     
  3. Offline

    ThunderWaffeMC

    Jake6177
    Yeah that's what I was thinking. What can I do about this?
     
  4. Offline

    Jake6177

    Hmm...maybe at the top:

    player.sendMessage("toggled " + (player.isSneaking()) ? "on":"off");

    See if it correctly changes from on to off?
     
  5. Offline

    ThunderWaffeMC

    Well when I first hold the shift key down is displays the 'Toggled sneaking' message; then when I let go it displays it again. Any idea?
     
  6. Offline

    Jake6177

    idea!

    how about (psuedocode):

    if player is sneaking
    make a repeating runnable for every 2 seconds
    in runnable check if sneaking
    if no longer sneaking, cancel task
     
    ThunderWaffeMC likes this.
  7. Offline

    ThunderWaffeMC

    Alright I'll try that, thanks!

    Jake6177 This is very strange but I have another while loop with a scheduler in another plugin and it seems to lag out and crash as well. It certainly is very strange:

    Code:java
    1.  
    2. final int cooldownPlayer = plugin.getCustomConfig().getInt("Shoot Cooldown." + player.getName() + ".Time Remaining");
    3. while (cooldownPlayer != 0) {
    4. Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
    5. @Override
    6. public void run() {
    7. plugin.getCustomConfig().set("Shoot Cooldown." + player.getName() + ".Time Remaining", cooldownPlayer - 1);
    8. plugin.updateCustomConfig(true);
    9. player.sendMessage("2 - Looped: " + cooldownPlayer);
    10. }
    11. }, 20L);
    12. }
    13.  


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

    Jake6177

    Yeah I totally agree with you it's very odd..
     
  9. Offline

    werter318

    ThunderWaffeMC Jake6177 How is that weird? That while loop doesn't wait for the scheduled task to end, it keeps going until cooldownPlayer = 0, which happends in 1 second, AFTER ~5000 tasks are already generated.
     
  10. Offline

    ThunderWaffeMC

    I would have thought that the loop would have executed the full way before restarting? Otherwise the loop would execute in 1 hit and that isn't appropriate for my needs.
     
  11. Offline

    Lucariatias

    You don't need the while loop, that will continuously schedule tasks. You want to check, within the run method, if the player is sneaking. Also, only schedule the task once, so that it isn't scheduled every time a player toggles sneaking:
    Code:java
    1.  
    2. public class SomePlugin extends JavaPlugin implements Listener {
    3. @Override
    4. public void onEnable() {
    5.  
    6. getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    7. @Override
    8. public void run() {
    9. for (Player player : SomePlugin.this.getServer().getOnlinePlayers()) {
    10. if (player.isSneaking() {
    11. player.sendMessage("You're sneaking"); //send this every 2 seconds while the player is sneaking
    12. }
    13. }
    14. }
    15. }, 40L, 40L);
    16. }
    17.  
    18. @EventHandler
    19. public void toggleSneak(PlayerToggleSneakEvent event) {
    20. event.getPlayer().sendMessage("Toggled sneaking");
    21. }
    22.  
    23.  


    Same issue, do a repeating scheduler and include a check inside it. You're continuously scheduling tasks, which would overload the server.
     
    ThunderWaffeMC likes this.
Thread Status:
Not open for further replies.

Share This Page