Skip to content

Commit aa7e2da

Browse files
Implement autosave feature with 5-minute recurring task
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
1 parent 81694af commit aa7e2da

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/main/java/dansplugins/simpleskills/SimpleSkills.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import java.util.ArrayList;
2929
import java.util.Arrays;
30+
import java.util.Timer;
31+
import java.util.TimerTask;
3032

3133
/**
3234
* @author Daniel Stephenson
@@ -44,6 +46,7 @@ public class SimpleSkills extends PonderBukkitPlugin {
4446
private final StorageService storageService = new StorageService(playerRecordRepository, skillRepository, messageService, configService, experienceCalculator, log);
4547
private final ChanceCalculator chanceCalculator = new ChanceCalculator(playerRecordRepository, configService, skillRepository, messageService, experienceCalculator, log);
4648
private final PlayerJoinEventListener playerJoinEventListener = new PlayerJoinEventListener(playerRecordRepository, log);
49+
private Timer autosaveTimer;
4750

4851

4952
/**
@@ -64,13 +67,15 @@ public void onEnable() {
6467
registerEventListeners();
6568
initializeCommandService();
6669
checkFilesVersion();
70+
startAutosave();
6771
}
6872

6973
/**
7074
* This runs when the server stops.
7175
*/
7276
@Override
7377
public void onDisable() {
78+
stopAutosave();
7479
log.debug("Saving files.");
7580
storageService.save();
7681
log.debug("Saving language files.");
@@ -208,4 +213,36 @@ private void initializeSkills() {
208213
skillRepository.addSkill(new Strength(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
209214
}
210215

216+
/**
217+
* Starts the autosave scheduler which saves data every 5 minutes.
218+
*/
219+
private void startAutosave() {
220+
log.debug("Starting autosave task.");
221+
autosaveTimer = new Timer(true);
222+
autosaveTimer.scheduleAtFixedRate(new TimerTask() {
223+
@Override
224+
public void run() {
225+
if (!isEnabled()) {
226+
autosaveTimer.cancel();
227+
return;
228+
}
229+
Bukkit.getScheduler().runTask(SimpleSkills.this, () -> {
230+
log.debug("Autosaving files.");
231+
storageService.save();
232+
});
233+
}
234+
}, 1000 * 60 * 5, 1000 * 60 * 5);
235+
}
236+
237+
/**
238+
* Stops the autosave scheduler.
239+
*/
240+
private void stopAutosave() {
241+
if (autosaveTimer != null) {
242+
log.debug("Stopping autosave task.");
243+
autosaveTimer.cancel();
244+
autosaveTimer = null;
245+
}
246+
}
247+
211248
}

0 commit comments

Comments
 (0)