Skip to content

Commit

Permalink
Cancel scheduled tasks on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
SparklingComet committed Feb 19, 2021
1 parent 466c98c commit 32e17a8
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/org/shanerx/backup/AutoBackup.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.bstats.bukkit.Metrics;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.zeroturnaround.zip.ZipUtil;

import java.io.File;
Expand All @@ -36,6 +37,7 @@ public class AutoBackup extends JavaPlugin {
private static AutoBackup plugin;
private Set<BackupMode> backupModes, defaultModes;
private File root, backupsDir, logFile;
private Set<BukkitTask> tasks;

@Override
public void onEnable() {
Expand All @@ -58,7 +60,6 @@ public void onEnable() {
@Override
public void onDisable() {
getServer().getScheduler().cancelTasks(this);

}

public void loadSettings() {
Expand All @@ -76,20 +77,29 @@ public void loadSettings() {
}

public void scheduleAll() {
if (tasks == null) {
tasks = new HashSet<>();
}
else {
tasks.forEach(BukkitTask::cancel);
tasks.clear();
}

int period;
boolean log = getConfig().getBoolean("log-to-console");
for (BackupMode mode : getBackupModes()) {
if (mode.getSchedule() > 0) {
period = mode.getSchedule() * 60 * 20; // convert mins to ticks

new BukkitRunnable() {
BukkitTask task = new BukkitRunnable() {
@Override
public void run() {
if (log) getServer().getConsoleSender().sendMessage(Message.SCHEDULED_BACKUP_LOG.toConsoleString()
.replaceAll("%NAME%", getServer().getConsoleSender().getName()).replaceAll("%MODE%", mode.getName()));
performBackup(mode, true, getConfig().getBoolean("backup-log.enable") ? "CONSOLE" : null);
}
}.runTaskTimer(this, getConfig().getBoolean("immediate-backup") ? 0 : period, period);
tasks.add(task);
}
}
}
Expand Down

0 comments on commit 32e17a8

Please sign in to comment.