Simple Timer?

Discussion in 'Plugin Development' started by Mattkx4, Mar 22, 2014.

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

    Mattkx4

    My current problem is:

    I'm trying to make a timer that will last 30 seconds! No more and no less! I would also like a countdown at the end! So the entire timer in chat would be like this:

    1.) The game will begin in 30 seconds!
    2.) 15 seconds until the game begins!
    3.) 10 seconds until the game begins!
    4.) 5 seconds
    5.) 4 seconds
    6.) 3 seconds
    7.) 2 seconds
    8.) 1 second
    *End Timer*

    I would appreciate if someone could either post some code that could help me, or point me to a proper place to find my answer!

    Thanks in advance!
     
  2. Offline

    MRPS

    use plugin.getServer().getScheduler().scheduleASyncDelayedTask()

    Mattkx4
     
  3. Offline

    Mattkx4

    MRPS
    I know that if this was in my main class then I would use:
    THIS.getServer()....
    But I want this timer in my "GameManagement" class, which is not my main class!

    What do I put in the plugin place?
     
  4. Offline

    MRPS


    yes

    getServer() is an inherited method from JavaPlugin
     
  5. Offline

    willeb96

    You could pass your plugin instance in the constructor of your GameManagment
     
  6. Offline

    Konkz

    PHP:
    public void startCountdown() {
          new 
    BukkitRunnable() {
                
    int time 30// time in seconds
     
                
    public void run() {
                    if (
    time 0) {
                        if (
    time 10 == || time 11) {
                            
    Bukkit.broadcastMessage("");
                            
    Bukkit.broadcastMessage("Game is starting in " ChatColor.GOLD
                                    
    time ChatColor.GREEN " seconds!");
                            
    Bukkit.broadcastMessage("");
     
                        }
                        
    time--;
                    } else {
                        
    startGame();
                        
    this.cancel();
                    }
                }
            }.
    runTaskTimer(this2020);
        }
    This will broadcast a message every 10 seconds, when it gets to 0 you have to make a method which determines what happens in the game.

    Eg:

    PHP:
    public void startGame() {
      for(
    Player pBukkit.getOnlinePlayers()) {
      
    p.teleport(LOCATION);
      }
    }
    Something like that :p

    Also, when you want the countdown to start you simply do startCountDown();

    You'd have to add boolean checks so it does not start multiple times, but I will leave that one to you.

    Mattkx4
     
  7. Offline

    paully104

    To try and hijack this thread a little bit I need to make a timer for the plugin i'm working on and I need to it to keep track of time since I issue the original command. So unlike a countdown its just infinitely keeping track of time until my ondeathevent where it outputs the time they died since the game started. Could you lead me in the right direction?
     
  8. Offline

    MRPS

    paully104

    Do what he's doing there, except increment up using a returnable field.
     
  9. Offline

    nlthijs48

    paully104 When the original command is executed save the time by using something like this:
    Code:java
    1. Long time = Calendar.getInstance().getTimeInMillis();

    Then in your onDeathEvent you get the current time again, substract the saved time and then you have the milliseconds of time between the two.
     
  10. Offline

    paully104

    Thanks for the tip however i have 1 issue void methods cannot return a value, so how would you go about returning the value so that my ondeathevent can use my gameplaytime that is being generated in the public void run?
     
  11. Offline

    MRPS

    paully104

    void methods cannot return a value, but you can pseudoreturn a value by doing something like this, buddy:

    Code:
    // ...in your runnable class
    run(){
      PluginMainClass.static_field++;
    }
    in your main class (as a field, above your onDisable and onEnable)
    Code:
    public static int static_field;
     
    @Override
    public void onEnable(){
    // ... do some stuff
    static_field = 0; // initialization at 0 [seconds], to be incremented only by a repeating timer task
    // ... more stuff
    }
    
    And finally in your event
    Code:
    @EventPriority...
    public void onWhatever(PlayerDeathEvent event){
      int seconds;
    // some stuff here
    // call your runnable timer thing here
     
    seconds = PluginMainClass.static_field;
     
    // do some more stuff here with them seconds
    }
     
  12. Offline

    GameplayJDK

  13. Offline

    MRPS

  14. Offline

    GameplayJDK

    MRPS
    That's true :)
     
  15. Offline

    paully104

    Might have to just message you im still getting an error trying this method im using it pretty much verbatim and im getting static_field cannot be resolved.
     
  16. Offline

    MRPS

    paully104
    Jesus christ. Give me a minute to boot up Eclipse and i'll get the exact code for you.

    paully104
    Code:
    package unit.test;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MainClass extends JavaPlugin {
     
        public static int s_variable;
        public int ns_variable;
        @Override
        public void onDisable(){
           
        }
       
        @Override
        public void onEnable(){
           
            s_variable = 0;
            ns_variable = 99;
           
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
     
                @Override
                public void run() {
                    // This is technically part of a different class than MainClass, so you have to access outside variables a different way
                    MainClass.s_variable++;
                        // increment up by 5?
                        MainClass.s_variable+=5;
                        // decrement down by 99
                        MainClass.s_variable-=99;
                   
                    // But what if i dont want to use static access!!!!1!
                    MainClass.this.ns_variable++;
                        // increment up by 5?
                        MainClass.this.ns_variable+=5;
                        // decrement down by 99
                        MainClass.this.ns_variable-=99;
                }
               
               
            });
           
           
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  17. Offline

    paully104

    Got it, thank you. Turns out I made a really simple mistake, I blame not having coffee yet.
     
Thread Status:
Not open for further replies.

Share This Page