Replace buggy execution times system
I replaced that buggy execution time checking thingy for simply keeping track of the bukkit scheduler task id and cancelling the task if necessary. Much easier and simpler, with less bugs.
This commit is contained in:
parent
0fb317038a
commit
d776de46c7
3 changed files with 24 additions and 24 deletions
|
@ -20,6 +20,7 @@ package de.pickshadow.plugin.base;
|
|||
|
||||
import de.pickshadow.plugin.BuildConfiguration;
|
||||
import de.pickshadow.plugin.classes.Configuration;
|
||||
import de.pickshadow.plugin.classes.Types;
|
||||
import de.pickshadow.plugin.classes.logger.LoggerInstanceImpl;
|
||||
import de.pickshadow.plugin.utils.Miscellaneous;
|
||||
import org.bukkit.Bukkit;
|
||||
|
@ -29,48 +30,46 @@ import java.util.UUID;
|
|||
public class Scheduler {
|
||||
LoggerInstanceImpl logger;
|
||||
|
||||
// Keeps track on how many times the scheduler has been run
|
||||
// Normally this isn't required, this however prevents duplicate
|
||||
// scheduler calls in case /pssp scheduler <timeframe> is used.
|
||||
public int execsMinutely = 0;
|
||||
public int execsHourly = 0;
|
||||
// Keeps track of running bukkit scheduler tasks
|
||||
private int taskIdMinutely = 0;
|
||||
private int taskIdHourly = 0;
|
||||
|
||||
public void initializeScheduler() {
|
||||
logger = ObjHolder.logger.getInstance(getClass());
|
||||
logger.verb("Initializing scheduler");
|
||||
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runMinuteTasks(execsMinutely), 1200L);
|
||||
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runHourTasks(execsHourly), 72000L);
|
||||
taskIdMinutely = Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runMinuteTasks, 1200L).getTaskId();
|
||||
taskIdHourly = Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runHourTasks, 72000L).getTaskId();
|
||||
}
|
||||
|
||||
public void runMinuteTasks(int execs) {
|
||||
// If execution count does not match return
|
||||
if (execs != execsMinutely) return;
|
||||
public void executeScheduler(Types.SchedulerType type) {
|
||||
switch (type) {
|
||||
case MINUTELY:
|
||||
Bukkit.getScheduler().cancelTask(taskIdMinutely);
|
||||
runMinuteTasks();
|
||||
break;
|
||||
case HOURLY:
|
||||
Bukkit.getScheduler().cancelTask(taskIdHourly);
|
||||
runHourTasks();
|
||||
}
|
||||
}
|
||||
|
||||
public void runMinuteTasks() {
|
||||
// Print announcement
|
||||
printTaskAnnouncement("minutely");
|
||||
|
||||
// Increase execution count to prevent additional calls
|
||||
execsMinutely++;
|
||||
|
||||
|
||||
// Save player data
|
||||
for (UUID uuid : ObjHolder.playerDataLoader.getLoadedUUIDs()) ObjHolder.playerDataLoader.savePlayerData(uuid);
|
||||
|
||||
|
||||
// Schedule
|
||||
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runMinuteTasks(execsMinutely), 1200L);
|
||||
taskIdMinutely = Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runMinuteTasks, 1200L).getTaskId();
|
||||
}
|
||||
|
||||
public void runHourTasks(int execs) {
|
||||
// If execution count does not match return
|
||||
if (execs != execsHourly) return;
|
||||
|
||||
public void runHourTasks() {
|
||||
// Print announcement
|
||||
printTaskAnnouncement("hourly");
|
||||
|
||||
// Increase execution count to prevent additional calls
|
||||
execsHourly++;
|
||||
|
||||
// Reload configuration file
|
||||
Configuration newConfig = ObjHolder.configLoader.loadConfig();
|
||||
if (newConfig == null) {
|
||||
|
@ -85,7 +84,7 @@ public class Scheduler {
|
|||
|
||||
|
||||
// Schedule
|
||||
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runHourTasks(execsHourly), 72000L);
|
||||
taskIdHourly = Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runHourTasks, 72000L).getTaskId();
|
||||
}
|
||||
|
||||
private void printTaskAnnouncement(String timeframe) {
|
||||
|
|
|
@ -22,4 +22,5 @@ public class Types {
|
|||
public enum VersionType { RELEASE, RELEASECANDIDATE, BETA, ALPHA }
|
||||
public enum LoggerLevel { ERROR, WARN, INFO, VERB, DIAG }
|
||||
public enum FormatType { NONE, ERROR, NORMAL }
|
||||
public enum SchedulerType { MINUTELY, HOURLY }
|
||||
}
|
||||
|
|
|
@ -95,13 +95,13 @@ public class PluginCommand extends CommandBase {
|
|||
case "minutely":
|
||||
case "minute":
|
||||
sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER.replace("%timeframe%", "minutely")));
|
||||
ObjHolder.scheduler.runMinuteTasks(ObjHolder.scheduler.execsMinutely);
|
||||
ObjHolder.scheduler.executeScheduler(Types.SchedulerType.MINUTELY);
|
||||
sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER_DONE.replace("%timeframe%", "minutely")));
|
||||
break;
|
||||
case "hourly":
|
||||
case "hour":
|
||||
sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER.replace("%timeframe%", "hourly")));
|
||||
ObjHolder.scheduler.runHourTasks(ObjHolder.scheduler.execsHourly);
|
||||
ObjHolder.scheduler.executeScheduler(Types.SchedulerType.HOURLY);
|
||||
sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER_DONE.replace("%timeframe%", "hourly")));
|
||||
break;
|
||||
default: sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_INVALID_ARGUMENT.replace("%usage%", command.getUsage()), Types.FormatType.ERROR));
|
||||
|
|
Loading…
Reference in a new issue