Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schedule HourChecker based on real system time #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
package de.alexanderritter.varo.timemanagment;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.GregorianCalendar;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldBorder;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scheduler.BukkitRunnable;

import de.alexanderritter.varo.ingame.PlayerManager;
import de.alexanderritter.varo.ingame.VaroPlayer;
import de.alexanderritter.varo.main.Varo;

public class PerHourChecker extends BukkitRunnable {
public class PerHourChecker extends TimerTask {

Varo plugin;
GregorianCalendar calendar;
int timeinterval; // seconds
ScheduledExecutorService executorService;

public PerHourChecker(Varo plugin) {
this.plugin = plugin;
timeinterval = 60*60; //60*60 seconds
calendar = new GregorianCalendar();
calendar.setFirstDayOfWeek(GregorianCalendar.MONDAY);
shrinkWorldborder();
this.runTaskTimer(plugin, 0, timeinterval*20);
executorService = Executors.newScheduledThreadPool(1);
scheduleNextRun();
}

@Override
Expand All @@ -38,6 +45,20 @@ public void run() {
updateDay();
checkCoordinatePost();
shrinkWorldborder();
scheduleNextRun();
}

private void scheduleNextRun(){
LocalDateTime localNow = LocalDateTime.now();
ZoneId zone = ZoneId.systemDefault();
ZonedDateTime nowWithZone = ZonedDateTime.of(localNow, zone);
ZonedDateTime nextTargetWithZone = nowWithZone.withHour(nowWithZone.getHour()).withMinute(0).withSecond(0).plusHours(1L);
Duration duration = Duration.between(nowWithZone, nextTargetWithZone);
if (duration.isNegative()){
duration = duration.plusHours(1L);
}
plugin.getLogger().info("Duration: " + duration.getSeconds());
executorService.schedule(this, duration.getSeconds(), TimeUnit.SECONDS);
}

public void updateDay() {
Expand Down Expand Up @@ -93,7 +114,7 @@ public void shrinkWorldborder() {
int endsize = plugin.getConfig().getInt("border.end-radius")*2;
double shrinkAmountPerHour = plugin.getSettings().getBorderShrinkPerHour();

plugin.getLogger().info("Worldborder diameter will be shrunken by " + (double) shrinkAmountPerHour + " blocks every " + timeinterval + " seconds (" + (double) timeinterval / 3600 + " hours).");
plugin.getLogger().info("Worldborder diameter will be shrunken by " + (double) shrinkAmountPerHour + " blocks every 3600 seconds (1 hour).");

String bordermsg = "";
for(World world : Bukkit.getWorlds()) {
Expand All @@ -108,7 +129,7 @@ public void shrinkWorldborder() {
border.setSize(border.getSize() - (instant * shrinkAmountPerHour));
}
bordermsg += "Worldborder is shrinking from " + border.getSize() + " to " + (border.getSize() - shrinkAmountPerHour) + " in the next hour";
border.setSize(border.getSize() - shrinkAmountPerHour, timeinterval);
border.setSize(border.getSize() - shrinkAmountPerHour, 3600);
}
instant = 0;
plugin.getLogger().info(bordermsg);
Expand Down