Thread.sleep() In Bukkit?

Discussion in 'Plugin Development' started by AXCoding, Jul 23, 2014.

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

    AXCoding

    Hi, I'm making a plugin that must wait before sending players more messages. Example.
    Code:java
    1. p.sendMessage("Hi There");
    2. //wait 20 ticks
    3. p.sendMessage("Thiscmessage is 20 ticks later")
    4. //wait 100 ticks
    5. p.sendMessage("Thiscmessage is 100 ticks later")
    6.  


    Anyone know how to do this? Any help is greatly appreciated :D
     
  2. Offline

    Gater12

  3. Offline

    xTigerRebornx

    xTrollxDudex and Garris0n like this.
  4. Offline

    AXCoding

    I'm kind of a visual learner... If anyone could show me an example of how to use this..
    Code:java
    1. import org.bukkit.event.EventHandler;
    2. import org.bukkit.event.Listener;
    3. import org.bukkit.event.player.PlayerJoinEvent;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5. import org.bukkit.scheduler.BukkitRunnable;
    6. import org.bukkit.scheduler.BukkitTask;
    7.  
    8. public final class ExamplePlugin extends JavaPlugin {
    9.  
    10. @Override
    11. public void onEnable() {
    12. new ExampleListener(this);
    13. }
    14. }
    15.  
    16. class ExampleListener implements Listener {
    17.  
    18. private final ExamplePlugin plugin;
    19.  
    20. public ExampleListener(ExamplePlugin plugin) {
    21. this.plugin = plugin;
    22. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    23. }
    24.  
    25. @EventHandler
    26. public void onJoin(PlayerJoinEvent event) {
    27. // Create the task and schedule to run it once, after 20 ticks
    28. BukkitTask task = new ExampleTask(this.plugin).runTaskLater(this.plugin, 20);
    29. }
    30.  
    31. }


    Lets say when a player times in the command /sendelayedmessages
    It sends them Hi
    then 20 ticks later
    Whats going on
    then 20 ticks later
    last thing?

    Thanks, sorry for giving you soo much work :(
    Gater12 xTigerRebornx
     
  5. Offline

    MCForger

    AXCoding
    On the end of one runnable just call another runnable with a delay and another message. Or make the runnable just run a repeating task every certain amount of seconds with set messages. Once it completed the amount of runs you want, cancel the runnable.
     
  6. Offline

    Gater12

    AXCoding
    In your case you want a repeating task running at a 20 tick interval that cancels itself out after 2 runs. You could use anonymous BukkitRunnable if you're just doing something small.

    Along-the-lines-of:

    Code:java
    1. new BukkitRunnable(){
    2. counter field
    3. public void run(){
    4. if counter is something do something
    5. other checks if necessary
    6. increment/decrement counter
    7. if counter is at some value cancel the task
    8. cancel();
    9. }
    10. }.runTaskTimer(pluginInstance, delayLong, intervalLong);
     
  7. Offline

    xTrollxDudex

  8. Offline

    xize

    Why not use a compair between the server time in milliseconds?, if its for a event with chat I kinda think a scheduler looks messy unless you want to perse listen to the server ticks.

    so you could do something like:

    Date toTime = new Date(System.currentTimeMillies());
    toTime.setSeconds(toTime.getSeconds()+20);

    Long time = toTime.getTime()

    then when somebody chats you could check if System.current.... Is higher than the value :)

    But since the setSeconds is kinda unsafe because it sometimes creates a negative integer you may want to look in the TimeUnit system in the javadocs/google.
     
Thread Status:
Not open for further replies.

Share This Page