Sync save time less dependent on tick time
This commit is contained in:
parent
628dd2827e
commit
329d5993fe
|
|
@ -18,8 +18,6 @@ import org.bukkit.Bukkit;
|
|||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class VillagerUI extends JavaPlugin {
|
||||
|
||||
public static VillagerUI instance;
|
||||
|
|
@ -73,17 +71,12 @@ public class VillagerUI extends JavaPlugin {
|
|||
private void scheduleTasks() {
|
||||
new SaveTask().runTaskTimerAsynchronously(
|
||||
this,
|
||||
getSecondsToNextXMinutes(Config.SAVE_TIME) * 20,
|
||||
TimeUnit.MINUTES.toSeconds(Config.SAVE_TIME) * 60 * 20L);
|
||||
0,
|
||||
20);
|
||||
logInOut.runTaskTimerAsynchronously(
|
||||
this,
|
||||
getSecondsToNextXMinutes(Config.LOG_TIME) * 20,
|
||||
TimeUnit.MINUTES.toSeconds(Config.LOG_TIME) * 20L);
|
||||
}
|
||||
|
||||
public static long getSecondsToNextXMinutes(int minutes) {
|
||||
long seconds = TimeUnit.MINUTES.toSeconds(minutes);
|
||||
return seconds - (TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) % seconds);
|
||||
0,
|
||||
20);
|
||||
}
|
||||
|
||||
private void registerEvents() {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ public class LogInOut extends BukkitRunnable {
|
|||
private final HashMap<String, Double> map;
|
||||
private int day;
|
||||
private File file;
|
||||
private long nextExecution;
|
||||
|
||||
public LogInOut() {
|
||||
createLogsDir();
|
||||
|
|
@ -29,6 +30,7 @@ public class LogInOut extends BukkitRunnable {
|
|||
map = loadFile(file);
|
||||
else
|
||||
map = new HashMap<>();
|
||||
this.nextExecution = Utilities.getNextXMinuteTime(Config.LOG_TIME);
|
||||
}
|
||||
|
||||
public void log(String material, double cost) {
|
||||
|
|
@ -61,6 +63,9 @@ public class LogInOut extends BukkitRunnable {
|
|||
public void run() {
|
||||
if (this.isCancelled())
|
||||
return;
|
||||
if (System.currentTimeMillis() < nextExecution)
|
||||
return;
|
||||
nextExecution = Utilities.getNextXMinuteTime(Config.LOG_TIME);
|
||||
int new_day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
|
||||
if (!file.exists()) {
|
||||
boolean success = false;
|
||||
|
|
|
|||
|
|
@ -5,8 +5,18 @@ import com.alttd.objects.EconUser;
|
|||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class SaveTask extends BukkitRunnable {
|
||||
|
||||
private long nextExecution;
|
||||
|
||||
public SaveTask() {
|
||||
this.nextExecution = Utilities.getNextXMinuteTime(Config.SAVE_TIME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (System.currentTimeMillis() < nextExecution)
|
||||
return;
|
||||
nextExecution = Utilities.getNextXMinuteTime(Config.SAVE_TIME);
|
||||
if (Config.DEBUG)
|
||||
Logger.info("Syncing users.");
|
||||
EconUser.getEconUsers().forEach(econUser -> {
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ import com.alttd.objects.Price;
|
|||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Utilities {
|
||||
/**
|
||||
|
|
@ -146,4 +146,10 @@ public class Utilities {
|
|||
return str.toUpperCase();
|
||||
return str.substring(0, 1).toUpperCase() + str.substring(1);
|
||||
}
|
||||
|
||||
public static long getNextXMinuteTime(int minutes) {
|
||||
long millis = TimeUnit.MINUTES.toMillis(minutes);
|
||||
long currentMillis = System.currentTimeMillis();
|
||||
return currentMillis + (currentMillis - (currentMillis % millis));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user