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:
JeremyStar™ 2024-04-30 22:30:44 +02:00
parent 0fb317038a
commit d776de46c7
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
3 changed files with 24 additions and 24 deletions

View file

@ -20,6 +20,7 @@ package de.pickshadow.plugin.base;
import de.pickshadow.plugin.BuildConfiguration; import de.pickshadow.plugin.BuildConfiguration;
import de.pickshadow.plugin.classes.Configuration; import de.pickshadow.plugin.classes.Configuration;
import de.pickshadow.plugin.classes.Types;
import de.pickshadow.plugin.classes.logger.LoggerInstanceImpl; import de.pickshadow.plugin.classes.logger.LoggerInstanceImpl;
import de.pickshadow.plugin.utils.Miscellaneous; import de.pickshadow.plugin.utils.Miscellaneous;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -29,48 +30,46 @@ import java.util.UUID;
public class Scheduler { public class Scheduler {
LoggerInstanceImpl logger; LoggerInstanceImpl logger;
// Keeps track on how many times the scheduler has been run // Keeps track of running bukkit scheduler tasks
// Normally this isn't required, this however prevents duplicate private int taskIdMinutely = 0;
// scheduler calls in case /pssp scheduler <timeframe> is used. private int taskIdHourly = 0;
public int execsMinutely = 0;
public int execsHourly = 0;
public void initializeScheduler() { public void initializeScheduler() {
logger = ObjHolder.logger.getInstance(getClass()); logger = ObjHolder.logger.getInstance(getClass());
logger.verb("Initializing scheduler"); logger.verb("Initializing scheduler");
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runMinuteTasks(execsMinutely), 1200L); taskIdMinutely = Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runMinuteTasks, 1200L).getTaskId();
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runHourTasks(execsHourly), 72000L); taskIdHourly = Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runHourTasks, 72000L).getTaskId();
} }
public void runMinuteTasks(int execs) { public void executeScheduler(Types.SchedulerType type) {
// If execution count does not match return switch (type) {
if (execs != execsMinutely) return; case MINUTELY:
Bukkit.getScheduler().cancelTask(taskIdMinutely);
runMinuteTasks();
break;
case HOURLY:
Bukkit.getScheduler().cancelTask(taskIdHourly);
runHourTasks();
}
}
public void runMinuteTasks() {
// Print announcement // Print announcement
printTaskAnnouncement("minutely"); printTaskAnnouncement("minutely");
// Increase execution count to prevent additional calls
execsMinutely++;
// Save player data // Save player data
for (UUID uuid : ObjHolder.playerDataLoader.getLoadedUUIDs()) ObjHolder.playerDataLoader.savePlayerData(uuid); for (UUID uuid : ObjHolder.playerDataLoader.getLoadedUUIDs()) ObjHolder.playerDataLoader.savePlayerData(uuid);
// Schedule // Schedule
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runMinuteTasks(execsMinutely), 1200L); taskIdMinutely = Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runMinuteTasks, 1200L).getTaskId();
} }
public void runHourTasks(int execs) { public void runHourTasks() {
// If execution count does not match return
if (execs != execsHourly) return;
// Print announcement // Print announcement
printTaskAnnouncement("hourly"); printTaskAnnouncement("hourly");
// Increase execution count to prevent additional calls
execsHourly++;
// Reload configuration file // Reload configuration file
Configuration newConfig = ObjHolder.configLoader.loadConfig(); Configuration newConfig = ObjHolder.configLoader.loadConfig();
if (newConfig == null) { if (newConfig == null) {
@ -85,7 +84,7 @@ public class Scheduler {
// Schedule // Schedule
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runHourTasks(execsHourly), 72000L); taskIdHourly = Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runHourTasks, 72000L).getTaskId();
} }
private void printTaskAnnouncement(String timeframe) { private void printTaskAnnouncement(String timeframe) {

View file

@ -22,4 +22,5 @@ public class Types {
public enum VersionType { RELEASE, RELEASECANDIDATE, BETA, ALPHA } public enum VersionType { RELEASE, RELEASECANDIDATE, BETA, ALPHA }
public enum LoggerLevel { ERROR, WARN, INFO, VERB, DIAG } public enum LoggerLevel { ERROR, WARN, INFO, VERB, DIAG }
public enum FormatType { NONE, ERROR, NORMAL } public enum FormatType { NONE, ERROR, NORMAL }
public enum SchedulerType { MINUTELY, HOURLY }
} }

View file

@ -95,13 +95,13 @@ public class PluginCommand extends CommandBase {
case "minutely": case "minutely":
case "minute": case "minute":
sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER.replace("%timeframe%", "minutely"))); 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"))); sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER_DONE.replace("%timeframe%", "minutely")));
break; break;
case "hourly": case "hourly":
case "hour": case "hour":
sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER.replace("%timeframe%", "hourly"))); 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"))); sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_SCHEDULER_DONE.replace("%timeframe%", "hourly")));
break; break;
default: sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_INVALID_ARGUMENT.replace("%usage%", command.getUsage()), Types.FormatType.ERROR)); default: sender.sendMessage(Miscellaneous.format(Translation.GLOBAL_INVALID_ARGUMENT.replace("%usage%", command.getUsage()), Types.FormatType.ERROR));