Real Time Plugin Concept

Discussion in 'Bukkit Discussion' started by CyberSoul, Jul 13, 2016.

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

    CyberSoul

    I've looked around for a way to get the time to set to real time and I came up with this method on my own (Keep in mind that this is getting the real time for the computer the server runs on, not the player). You would put the following code in a runnable and using the scheduler, running it about every 100 ticks seems to work.
    The code assumes 1000 ticks to be an hour, 10 ticks to be a minute, and 1 tick to be 10 seconds, this works quite well for me.
    Code:
    public void run() {
           
            LocalDateTime now = LocalDateTime.now();
           
            int hour = now.getHour();
            int minute = now.getMinute();
            int second = now.getSecond();
           
            String strSecond;
           
            //I only want the tens digit of the seconds number
            if (Integer.toString(second).length() == 1) {
                strSecond = "0";
            } else {
                strSecond = Integer.toString(second).substring(0, 1);
            }
           
            String time;
           
            if (hour < 6) {
                hour += 24;
            }
           
            hour -= 6;
           
            if (Integer.toString(hour).length() == 1) {
                time = "0" + Integer.toString(hour);
            } else {
                time = Integer.toString(hour);
            }
           
            if (Integer.toString(minute).length() == 1) {
                time = time + "0" + Integer.toString(minute);
            } else {
                time = time + Integer.toString(minute);
            }
           
            time = time + strSecond;
           
            long mcTime = Long.parseLong(time);
           
            for (World world : plugin.getServer().getWorlds()) {
               
                world.setTime(mcTime);
               
            }
           
        }
    I'm interested in all suggestions for it and any comments about it. I've seen real time done before but nobody seems to have taken such a simple method.
     
Thread Status:
Not open for further replies.

Share This Page