[Bukkit Scheduler] Extend task delay if something happens

Discussion in 'Plugin Development' started by Dudemister1999, Oct 27, 2014.

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

    Dudemister1999

    Hello! I've got some code:

    Code:java
    1. public static void addUndeadKill(final UUID player)
    2. {
    3. Player p = Bukkit.getPlayer(player);
    4. Scoreboard scoreboard = p.getScoreboard();
    5.  
    6. if(scoreboard.getObjective(DisplaySlot.SIDEBAR) != null)
    7. {
    8. Objective obj = scoreboard.getObjective(DisplaySlot.SIDEBAR);
    9. obj.setDisplayName(ChatColor.YELLOW + "+1 Kill!");
    10. obj.getScore(PacketUtil.SIDEBAR_KILLTYPE).setScore(0);
    11.  
    12. Team t = scoreboard.getTeam(PacketUtil.SIDEBAR_KILLTYPE);
    13. t.setSuffix(PacketUtil.KILLTYPE_NORMALZOMBIE);
    14.  
    15. Bukkit.getScheduler().runTaskLaterAsynchronously(DeadworksAPI.instance, new BukkitRunnable()
    16. {
    17. @Override
    18. public void run()
    19. {
    20. Player p = Bukkit.getPlayer(player);
    21. Scoreboard scoreboard = p.getScoreboard();
    22.  
    23. if(scoreboard.getObjective(DisplaySlot.SIDEBAR) != null)
    24. {
    25. scoreboard.clearSlot(DisplaySlot.SIDEBAR);
    26. }
    27. }
    28. }, (20 * 3));
    29. }
    30. }


    But I need to know, let's say for instance another thing is updated on the scoreboard. I need to know how to delay that task by even more. Any ideas?

    -Hydroxocobalamin (OCHbl)
     
  2. Offline

    Jaaakee224

    Dudemister1999
    Create an int and then replace the (20 * 3) with the int name.

    int example = 100;

    }, (int));

    and then you can use the int to extend task delay by adding to int.
     
  3. Offline

    Dudemister1999

    Jaaakee224 But with that, it would just create the same timer, and if it gets called again it'll just create another timer with an extended delay.
     
  4. Offline

    fireblast709

    Dudemister1999
    • You shouldn't be using non thread-safe methods in an async task (and I'm pretty sure Scoreboards are not thread-safe)
    • You shouldn't use BukkitRunnables in the scheduler. Either use a Runnable, or use a BukkitRunnable standalone (as in, without directly using the scheduler)
      Code:java
      1. new BukkitRunnable()
      2. {
      3. @Override
      4. public void run()
      5. {
      6. // TODO: code
      7. }
      8. }.runTask<whatever>(plugin, ...);
     
  5. Offline

    Dudemister1999

    fireblast709 It was originally a Runnable, I just changed it to BukkitRunnable for testing purposes.
     
Thread Status:
Not open for further replies.

Share This Page