Countdown

Discussion in 'Plugin Development' started by thatdubstepgamer, Sep 25, 2013.

Thread Status:
Not open for further replies.
  1. Hello and I want to add a countdown timer to my plugin heres my code so far
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract11(PlayerInteractEvent event) {
    3.  
    4. Player player = event.getPlayer();
    5. if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    6. if (event.getClickedBlock().getType() == Material.GOLD_BLOCK) {
    7. event.setCancelled(true);
    8. Bukkit.broadcastMessage(player.getName()+ " Has just armed the Red teams bomb!");
    9. event.getClickedBlock().setType(Material.REDSTONE_BLOCK);
    10. //Timer goes here and displays messages as it counts down from 15 to 0
    11.  
    12. }}}
     
  2. Offline

    chasechocolate

    Use a runnable:
    Code:java
    1. new BukkitRunnable(){
    2. int timeLeft = 10;
    3.  
    4. @Override
    5. public void run(){
    6. if(timeLeft > 0){
    7. timeLeft -= 1;
    8. } else {
    9. //The timer is done
    10.  
    11. this.cancel();
    12. return;
    13. }
    14. }
    15. }.runTaskTimer(plugin, 0L, 20L);
     
    Skyost likes this.
  3. And how do I add messages so I says how much time is left every second.
     
  4. Offline

    Zettelkasten

    As you could image, you would use a loop (properly for-loop) to count down.
    With Thread.sleep(time) you can pause it for 1000 milliseconds.
    If you would just do this, it will pause the whole server. You will have to use threads. A multithreading tutorial is over here: http://www.tutorialspoint.com/java/java_multithreading.htm
     
  5. Sorry still dont really know how to tie this in chasechocolate I got it but need to figure out how to say messages every second
     
  6. Offline

    chasechocolate

    thatdubstepgamer the run() method gets called every second with the code I showed. You can broadcast a message using Bukkit.broadcastMessage("Some message").
     
  7. Ok I put it in my code and when I insert }.runTaskTimer(plugin, 0L, 20L); it underlines plugin in red
     
  8. Offline

    chasechocolate

    thatdubstepgamer import BukkitRunnable. Also, the "plugin" should be changed to the main class of your plugin. If you have the event method in your main class (the one that extends JavaPlugin), then change it to "this".
     
  9. Ok here we go and 1 last thing how do I make it say
    [Timer] 15 seconds remaing!
    and at 0 run a command sorry if its so much
     
  10. Offline

    WauloK

  11. Offline

    JPG2000

    thatdubstepgamer Do what chase said. Broadcast the message with your integer variable.
     
Thread Status:
Not open for further replies.

Share This Page