Can bukkit tell time on the computer it hosted on?

Discussion in 'Plugin Development' started by GRANTSWIM4, Dec 30, 2012.

?

if it does not would you like this added

  1. yes

    66.7%
  2. nope

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

    GRANTSWIM4

    I makeing a plugin that uses a lib that send email at a time can bukkit then when it rech mid night on the hosted computer?
     
  2. Offline

    KeybordPiano459

  3. Offline

    mastermustard

    although im not certain i think the answer is it goes off of minecraft time therefor no. although im not positive its worth researching google it maybe a more advanced plugin creator can give the answer

    nevermind then seems that there is one

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  4. Offline

    GRANTSWIM4

  5. Offline

    Ewe Loon

    since bucket plugins are written in java you have access to all the java libraries

    to get the time and date you have several options

    long t=System.currentTimeMillis();
    long d=new Date().getTime();

    these return the current time as a long

    Calendar cal=new GregorianCalendar();

    there are functions in cal to get the time / date etc
     
  6. Offline

    GRANTSWIM4

    I want it when it turns mid night it will do a event to the sever

    Sorry for being noob we were all one at some point xD @EweLoon @mastermustard KeybordPiano459

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  7. Offline

    fireblast709

    You would have to schedule a task checking for world time
     
  8. Offline

    GRANTSWIM4

    I want it when it reach 12:00 it will do a event
     
  9. Schedule a task that will check what time it is, every x minutes. If time = 12:00 => do you thing.
     
  10. Offline

    GRANTSWIM4

    Where do i schedule?
     
  11. Offline

    fireblast709

    onEnable() probably
     
  12. Offline

    KeybordPiano459

    Yeah, GRANTSWIM4 in your onEnable() method schedule a task with the time being how long there is until midnight (detected in your onEnable())
     
  13. Offline

    GRANTSWIM4

    This plugin going to be on 50 severs becuse im doing it for a sever host who puting it on there severs so if there reset there sever what would i do?
     
  14. Offline

    KeybordPiano459

    Uhm... the exact same thing? I said you should try to detect how much time there is left until midnight, so it doesn't matter when you start the server.
     
  15. Offline

    Ewe Loon

    Since long t=System.currentTimeMillis(); returns the time in milliseconds
    and there are 86400000 milliseconds per day

    first create a public long in your plugin called "tomorrow" inside your main class
    and also a public BukkitScheduler called scheduler

    public long tomorrow;
    public BukkitScheduler scheduler;


    in the onEnable() procedure

    initialize tomorrow, scheduler and start an async monitoring task
    Code:
    public void onEnable() {
    ccs=getServer().getConsoleSender();
    getServer().getPluginManager().registerEvents(this, this);
    config=getConfig();
     
    log("Enabled");
     
    tomorrow=(long)Math.floor(System.currentTimeMillis() / 86400000)+1;
    scheduler=getServer().getScheduler();
    scheduler.runTaskTimerAsynchronously(this, new Runnable() {
    @Override
    public void run() {
    long t=System.currentTimeMillis();
    long dat = (long)Math.floor(t / 86400000);
    if (dat>=tomorrow){
    tomorrow=dat+1;
     
    //   do your stuff here
    //   note as its async, dont access bukit API unless you know they are safe to use in ASync tasks
     
    }
    }
    }, 1, 1);
    }
    
    if you need to use API functions shedule a Sync task to do it
    add another public var
    public Plugin plugin=this;
    then add the following to your ASync Task
    Code:
                        scheduler.runTask(plugin, new Runnable() {
                            @Override
                            public void run() {
                                // Do api stuff here       
                            }
                        });                   
    
     
  16. Offline

    GRANTSWIM4

    Thank for the help as Ewe Loon sated
    Can i just get the time and when it hit midnight it will do the event


    Edit: posted b 4 what he said
     
  17. Offline

    Ewe Loon

    you might want to store the tomorrow var in the config, incase the server is not running at midnight
     
  18. Offline

    GRANTSWIM4

    ok can some one point me to a tut about config i never touch them yet
     
  19. Offline

    tommycake50

    use a dateformat and a date instance
    also inside a check like so
    Code:
    int taskID = Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable(){
    public void run(){
    SimpleDateFormat f = new SimpleDateFormat("HH:mm");
    String s = f.format(new Date());
    if(s.equals(00:00)){
    //thread safe code to start the event
    Bukkit.getServer().getScheduler().cancelTask(taskID);
    }
    }
    },0,200);
    then once the event is done restart the task.
    and if the server ain't running at midnight the event wont happen and in your onenable call the method that starts this task.
     
  20. Offline

    fireblast709

    Lightweight version
    Code:java
    1. Calendar midnight = Calendar.getInstance();
    2. midnight.set(Calendar.HOUR_OF_DAY, 0);
    3. midnight.set(Calendar.MINUTE, 0);
    4. midnight.set(Calendar.SECOND, 0);
    5. midnight.set(Calendar.MILLISECOND, 1);
    6. midnight.set(Calendar.DAY_OF_YEAR, midnight.get(Calendar.DAY_OF_YEAR) + 1);
    7. long tillMidnight = midnight.getTimeInMillis() - System.currentTimeMillis() - 1;
    8. long ticksTillMidnight = tillMidnight / 50;
    This code gets you the amount of ticks till the next midnight. Just run a sync scheduler with this in each onEnable().
    Also, no config is needed as it would reassign itself in each onEnable()
     
  21. Offline

    tommycake50

    nice method!
     
  22. Offline

    GRANTSWIM4

    Thanks
     
  23. Offline

    Ewe Loon

    why do people go through all that to get the number of millies till midnight when

    long tillmidnight = 86399999 - (system.currentTimeMillis() % 86400000);

    will do exactly the same thing and a lot more processor efficient

    also comparing a formated date with "00:00" will fail if the server lags or is not running

    full api docs http://jd.bukkit.org/apidocs/overview-summary.html

    public FileConfiguration =getConfig()

    FileConfiguration api is here http://jd.bukkit.org/apidocs/org/bukkit/configuration/file/FileConfiguration.html

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  24. Offline

    fireblast709

    Ewe Loon The code:
    Code:java
    1. public static void main(String[] args)
    2. {
    3. long time = System.nanoTime();
    4. Calendar midnight = Calendar.getInstance();
    5. midnight.set(Calendar.HOUR_OF_DAY, 0);
    6. midnight.set(Calendar.MINUTE, 0);
    7. midnight.set(Calendar.SECOND, 0);
    8. midnight.set(Calendar.MILLISECOND, 1);
    9. midnight.set(Calendar.DAY_OF_YEAR, midnight.get(Calendar.DAY_OF_YEAR) + 1);
    10. long tillMidnight = midnight.getTimeInMillis() - System.currentTimeMillis() - 1;
    11. long ticksTillMidnight = tillMidnight / 50;
    12. System.out.println(ticksTillMidnight);
    13.  
    14. System.out.println("nano:"+(System.nanoTime() - time));
    15. time = System.nanoTime();
    16. long tillmidnight = 86399999 - (System.currentTimeMillis() % 86400000);
    17. long ticks = tillmidnight / 50;
    18. System.out.println(ticks);
    19. System.out.println("nano: "+(System.nanoTime() - time));
    20.  
    21. }
    The output:
    Code:
    1646861 // The amount of ticks, my version
    nano:49165362 // Nanoseconds it took to run, my version
    1718861 // The amount of ticks, your version
    nano: 71375 // Nanoseconds it took to run, your version
    A quick calculation (1656000 = 23*3600*1000/50) shows that my amount of ticks was close, and yours was way off. Explain the difference please ;3
     
  25. Offline

    tommycake50

    if its not running you dont even need to start the event?
    and if it laggs it will just do it a little but later.
     
  26. Offline

    GRANTSWIM4

    Can i use a if satement
     
  27. Offline

    Sagacious_Zed Bukkit Docs

    You can use any control statement to implement your method.
     
  28. Offline

    GRANTSWIM4

    got it i have to ask one thing How do i tell if it first startup
     
  29. Offline

    mastermustard

    although other people came up with alot of ideas that are probably better you could have it do an event off ticks and since one full cycle of day/night=24000 ticks (17 minutes) you could have it enable off startup. there for you could have there be a huge amount of ticks inbetween each event.

    hypothetically speaking if those servers restarted every 12 hours have the event go inbetween those startups so in the 6th hour after each startup an event would happen. 6 hours = 508,235 ticks

    there methods are probably better but this is just an idea
     
  30. Offline

    GRANTSWIM4

    I need to know what a sync scheduler I am vary new with java I wacth some tuts and stuff
     
Thread Status:
Not open for further replies.

Share This Page