Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void onEnable() {
- createRunnable().runTaskTimerAsynchronously(this,
- 20-(Bukkit.getWorld("world").getTime() % 20), 20);
- }
- private BukkitRunnable createRunnable() {
- long threshold = 18; //Incase a skip happens we have this.
- long activateAt = 16000; //What Minecraft time to activate this at.
- return (new BukkitRunnable() {
- long prevTime = -1; //Checking for time skips/sleeping
- @Override
- public void run() {
- if((Bukkit.getWorld("world").getTime() % 20) != 0) {//Check if it got out of sync (from a command or something)
- cancel();
- createRunnable().runTaskTimerAsynchronously(MainClassExtendsJavaPlugin.this,
- 20-(Bukkit.getWorld("world").getTime() % 20), 20);//Reschedule the task
- return;
- }
- if(!(prevTime == -1 || Bukkit.getWorld("world").getTime() - prevTime >= threshold)//Check if time jumped(from sleeping or other)
- || (Bukkit.getWorld("world").getTime() == activateAt)) {//Check if the time is the activation time
- onActivate();
- prevTime = -1;//Reset the skip check time
- return;
- }
- prevTime = Bukkit.getWorld("world").getTime(); //Update the skipChecktime
- }
- /**
- This is run whenever the worldtime is the same as ActivateAt time or when someone sleeps/time skips
- */
- private void onActivate() {
- //Code here
- System.out.println("Nighttime!");
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement