Compare commits

..

2 commits

3 changed files with 67 additions and 8 deletions

View file

@ -42,6 +42,8 @@ public final class BuildConfiguration {
// Verbose logging
public static final boolean provideVerboseLoggingForSpellingHelpers = false;
public static final boolean provideVerboseLoggingForSpellingCompletions = false;
public static final boolean announceSchedulingActionsInLogIfPlayers = true;
public static final boolean announceSchedulingActionsInLogIfAlone = true;
// Command configuration
// -> /msg & /reply

View file

@ -18,7 +18,9 @@
*/
package de.pickshadow.plugin;
import de.pickshadow.plugin.classes.Configuration;
import de.pickshadow.plugin.classes.logger.LoggerInstanceImpl;
import de.pickshadow.plugin.utils.Miscellaneous;
import org.bukkit.Bukkit;
import java.util.UUID;
@ -26,18 +28,68 @@ 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;
public void initializeScheduler() {
logger = ObjHolder.logger.getInstance(getClass());
logger.verb("Scheduling tasks");
Bukkit.getScheduler().runTaskLater(ObjHolder.main, this::runMinuteTasks, 1200L);
logger.verb("Initializing scheduler");
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runMinuteTasks(execsMinutely), 1200L);
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runHourTasks(execsHourly), 72000L);
}
public void runMinuteTasks() {
logger.diag("Running minutely tasks");
public void runMinuteTasks(int execs) {
// If execution count does not match return
if (execs != execsMinutely) return;
// 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, this::runMinuteTasks, 1200L);
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runMinuteTasks(execsMinutely), 1200L);
}
public void runHourTasks(int execs) {
// If execution count does not match return
if (execs != execsHourly) return;
// Print announcement
printTaskAnnouncement("hourly");
// Increase execution count to prevent additional calls
execsHourly++;
// Reload configuration file
Configuration newConfig = ObjHolder.configLoader.loadConfig();
if (newConfig == null) {
logger.error("Unable to reload configuration file automatically");
} else {
ObjHolder.config = newConfig;
}
// Run garbage collection (old gen gc)
Miscellaneous.threadedGc();
// Schedule
Bukkit.getScheduler().runTaskLater(ObjHolder.main, () -> runHourTasks(execsHourly), 72000L);
}
private void printTaskAnnouncement(String timeframe) {
if (Bukkit.getOnlinePlayers().isEmpty() && !BuildConfiguration.announceSchedulingActionsInLogIfAlone) return;
if (!Bukkit.getOnlinePlayers().isEmpty() && !BuildConfiguration.announceSchedulingActionsInLogIfPlayers) return;
logger.diag("Running " + timeframe + " tasks");
}
}

View file

@ -52,13 +52,18 @@ public class Miscellaneous {
// Ensures garbage collection
// Stolen from JLibs
// https://github.com/santhosh-tekuri/jlibs/blob/e001fb7286c84f456125d1be00fc9a7e3b128881/core/src/main/java/jlibs/core/lang/RuntimeUtil.java#L148
public static void gc(){
public static void threadedGc(){
Thread gcThread = new Thread(Miscellaneous::gc);
gcThread.start();
}
public static void gc() {
ObjHolder.logger.warn(Miscellaneous.class, "Forcing garbage collection");
Object obj = new Object();
WeakReference<Object> ref = new WeakReference<>(obj);
obj = null;
while(ref.get() != null)
System.gc();
while(ref.get() != null) System.gc();
ObjHolder.logger.warn(Miscellaneous.class, "Garbage collector finished");
}
// Data type conversion