Server Clock

Discussion in 'Plugin Development' started by lpoulter, Jul 14, 2012.

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

    lpoulter

    Hi I would like to know the best way to implement a server clock. what I would like to do is have a clock running from the moment the server starts untill it is reset so i can write that can reference the clock time. e.g

    at 2 mins from server start
    do this
    at 10 mins from server start
    do this
    etc

    I've had a look at this and am unsure what would be the best method to use can I use the server ticks as my clock? Any suggests would be greatfully recived.
     
  2. you can see what 2 min is in ticks, and use the schedular to wait that delay
     
  3. Offline

    lpoulter

    thanks, if i want to make players invincible until a certain time am I right in thinking that i would need to schedule a Synchronous task as I would need to access methods such as player.setHealth()? Can I place a listner in a runnable block? to cancal player damage events.
     
  4. yes, but ook out whit long duration tasks and storing player names
     
  5. Offline

    lpoulter

    Sorry I dont understand this?
     
  6. if ou store players for a long time, they may disconnect earlier, meaning they never see the message, and because you storing a player instance, it will create an memory leak until the task is over
     
    lpoulter likes this.
  7. Offline

    lpoulter

    Ok thanks for your help.
     
  8. Offline

    pzxc

    If you want to reference the ACTUAL time the server has been online, make a global variable long serverStart = null, then in your onEnable() function set serverStart = new Date().getTime();
    Then when you need to check how long the server has been up, use long secondsOnline = (new Date().getTime() - serverStart)/1000;

    If you want to reference the number of ticks the server has been online, then use a global variable long serverTicks = 0, and in your onEnable() function schedule a syncrepeatingtask (delay 1L) that does nothing but serverTicks++
     
  9. Something similar to pzxc, but this time with System.currentMillis(); <-- store that in a final variable
    then whenever you wanna check how much time has passed take the currentMillis(); again and minus it with the stored milliseconds, the difference * 1000 = the seconds that have passed. (the difference * 60000 = the minutes that have passed)
     
  10. Offline

    pzxc

    you mean the difference / 1000 = the seconds that have passed
     
  11. Ye, sorry. ;)
     
  12. Offline

    Cowboys1919

    Im getting the feeling that this is another hunger games plugin..
     
  13. Offline

    Kodfod

    Code:JAVA
    1.  
    2. myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
    3.  
    4. public void run() {
    5. //2 min Do something
    6. }
    7. }, 2400L);
    8.  
    9. public void run1() {
    10. //10 min do something
    11. }
    12. }, 12000L);
    13.  
    14.  
     
  14. Offline

    Scizzr

    Just an idea (and something that I use on my plugins): Bukkit has a built-in scheduler. I use this code to do different things based on the time. Here's a more elaborate version (which I use) to do something either in terms of every n ticks or based on what time it is:
    Code:
    public class Main extends JavaPlugin {
        int lastTick = 0;
        public void onEnable() {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this,
                new Runnable() {
                    public void run() {
                        Calendar cal = Calendar.getInstance();
                        calY = cal.get(Calendar.YEAR);
                        calM = cal.get(Calendar.MONTH) + 1;
                        calD = cal.get(Calendar.DAY_OF_MONTH);
                        calH = cal.get(Calendar.HOUR);
                        calI = cal.get(Calendar.MINUTE);
                        calS = cal.get(Calendar.SECOND);
                        calA = cal.get(Calendar.AM_PM) == 0 ? "AM" : "PM";
                        
                        if (lastTick % 20 == 0) {
                            lastTick = 0;
                            
                            if (calS == 0) {
                                //do something every time the second is = 0
                                //ex: 12:00:00, 12:01:00, 12:02:00
                            }
                            if (calI == 0 && calS == 0) {
                                //do something every hour on the hour
                                //ex: 12:00:00, 13:00:00, 14:00:00
                            }
                            if (calH == 12 && calI == 0 && calS == 0) {
                                //do something every day at 12:00:00
                            }
                            if (calY == 2012 && calM == 12 && calD == 25 && calH == 0 && calI == 0 && calS == 0) {
                                //do something when the clock strikes midnight on Christmas morning
                            }
                            
                            //do something every second, regardless of what the clock says
                            //this is based on server ticks
                        }
                        
                        if (lastTick % 10 == 0) {
                            //do something every 1/2 second (10 ticks), regardless of what the clock says
                            //this is based on server ticks
                        }
                        
                        if (lastTick % 5 == 0) {
                            //do something every 1/4 second (5 ticks), regardless of what the clock says
                            //this is based on server ticks
                        }
                        
                        if (lastTick % 2 == 0) {
                            //do something every 1/10 second (2 ticks), regardless of what the clock says
                            //this is based on server ticks
                        }
                        
                        if (lastTick % 1 == 0) {
                            //do something every 1/20 second (1 tick), regardless of what the clock says
                            //this is based on server ticks
                        }
                        
                        lastTick++;
                    }
                }, 0L, 1L);
        }
    }
    
    A simplified version of this to do what you want is here:
    Code:
    public class Main extends JavaPlugin {
        int tickCount = 0;
        public void onEnable() {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this,
                new Runnable() {
                    public void run() {
                        if (tickCount == 2*60*20) {
                            //every 2 minutes
                        }
                        if (tickCount == 10*60*20) {
                            //every 10 minutes
                        }
                        tickCount++;
                    }
                }, 0L, 1L);
        }
    }
    
    Either way would work, but I prefer the former code because it allows for real-life time control (ie: the code can be run at specific intervals such as starting an event every hour, copying the plugins folder every half hour, or stopping the server every morning, or giving everyone cookies and milk on Christmas morning).

    -Scizzr
     
  15. Offline

    Kodfod

    Scizzr:
    I like that code. i May use it in my up-coming event plugin =)
    You don't mind do you?
     
  16. Offline

    Scizzr

    Nope. Sharing is caring. :D
     
  17. Offline

    lpoulter

    Hi when i write this code I get an error, "tickCount" cannot be refered to inside an inner class defined in a different method." Any ideas what this means and how I can get round it?

    Code:
        public void onEnable(){
            int tickCount = 0;
            Bukkit.getScheduler().scheduleAsyncDelayedTask(this, new Runnable(){
                public void run() {
     
                    Bukkit.getScheduler().scheduleAsyncRepeatingTask(plugin,
                        new Runnable(){
                            public void run() {
                                Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "until game starts");
                                tickCount++;
                        }                   
                    }, 0, 1L);
                    Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "The games have now begun");
                    Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "You are invincible for 2 minutes");
                    gameStarted = true;
                    Plugin plugin = new Kits();
                    Bukkit.getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable(){
                        public void run() {
                            Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "Invinciblity now over");
                            playersInvincible = false;                           
                        }
     
                    }, TIMEINVINICBLEMINS * 60 * 20);
                }
     
            }, TIMETOSTARTMINS * 60 * 20);
     
  18. Offline

    Scizzr

    Define it before the onEnable() method. Sorry about that confusion. Original post has been edited.

    Edit: As a side note, I noticed your logic is a bit flawed here. What happens with your code is:
    We'll say the server started at 15:17:30 and TimeInvincibleMins = 2, TimeToStartMins = 5
    1) Server starts at 15:17:30
    2) At 15:22:30
    —a) The server says "until game starts"
    —b) The server says "The games have now begin"
    —c) The server says "You are invincible for 2 minutes"
    —d) The server spams the message "until game starts" every tick
    3) At 15:24:30
    —a) The server says "Invincibility now over"
    —b) The server continues to spam "until game starts" every tick
    4) At 15:27:30
    —repeat 2)
    5) At 15:29:30
    —repeat 3)
    6) ...
    7) ...

    If you need help with any of this, feel free to shoot me a PM and we can hop on Skype or something. :)
     
Thread Status:
Not open for further replies.

Share This Page