diff --git a/src/main/java/world/bentobox/challenges/ChallengesAddon.java b/src/main/java/world/bentobox/challenges/ChallengesAddon.java index 0735838..59d3a38 100644 --- a/src/main/java/world/bentobox/challenges/ChallengesAddon.java +++ b/src/main/java/world/bentobox/challenges/ChallengesAddon.java @@ -220,6 +220,16 @@ public class ChallengesAddon extends Addon { this.registerRequestHandler(new LevelDataRequestHandler(this)); this.registerRequestHandler(new CompletedChallengesRequestHandler(this)); + + if (this.settings.getAutoSaveTimer() > 0) + { + this.getPlugin().getServer().getScheduler().runTaskTimerAsynchronously( + this.getPlugin(), + bukkitTask -> ChallengesAddon.this.challengesManager.save(), + this.settings.getAutoSaveTimer() * 60 * 20, + this.settings.getAutoSaveTimer() * 60 * 20 + ); + } } else { this.logError("Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!"); this.setState(State.DISABLED); diff --git a/src/main/java/world/bentobox/challenges/Settings.java b/src/main/java/world/bentobox/challenges/Settings.java index e5818a3..a1a5ccd 100644 --- a/src/main/java/world/bentobox/challenges/Settings.java +++ b/src/main/java/world/bentobox/challenges/Settings.java @@ -155,6 +155,12 @@ public class Settings implements ConfigObject @ConfigEntry(path = "title.title-showtime") private int titleShowtime = 70; + @ConfigComment("") + @ConfigComment("Long that represents how frequently (in minutes) challenges addon will save data to database.") + @ConfigComment("If this is set to 0, saving will not happen.") + @ConfigEntry(path = "auto-saver") + private long autoSaveTimer = 30; + @ConfigComment("") @ConfigComment("This list stores GameModes in which Challenges addon should not work.") @ConfigComment("To disable addon it is necessary to write its name in new line that starts with -. Example:") @@ -369,6 +375,27 @@ public class Settings implements ConfigObject } + /** + * This method returns the autoSaveTimer object. + * @return the autoSaveTimer object. + */ + public long getAutoSaveTimer() + { + return autoSaveTimer; + } + + + /** + * This method sets the autoSaveTimer object value. + * @param autoSaveTimer the autoSaveTimer object new value. + * + */ + public void setAutoSaveTimer(long autoSaveTimer) + { + this.autoSaveTimer = autoSaveTimer; + } + + /** * This method sets the titleShowtime object value. * @param titleShowtime the titleShowtime object new value. diff --git a/src/main/java/world/bentobox/challenges/listeners/SaveListener.java b/src/main/java/world/bentobox/challenges/listeners/SaveListener.java index cebf22b..ccb043a 100644 --- a/src/main/java/world/bentobox/challenges/listeners/SaveListener.java +++ b/src/main/java/world/bentobox/challenges/listeners/SaveListener.java @@ -22,21 +22,6 @@ public class SaveListener implements Listener } - /** - * This event listener handles world save event. - * @param e World Save event. - */ - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onWorldSave(WorldSaveEvent e) - { - // Save only for worlds where exist any challenge addon data. - if (this.addon.getChallengesManager().hasAnyChallengeData(e.getWorld())) - { - this.addon.getChallengesManager().save(); - } - } - - /** * This event listener handles player kick event. * If player is kicked, then remove it from player cache data. diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6056a2b..fb4b81c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -109,6 +109,10 @@ title: # Integer that represents how long title will be visible for player. title-showtime: 70 # +# Long that represents how frequently (in minutes) challenges addon will save data to database. +# If this is set to 0, saving will not happen. +auto-saver: 30 +# # This list stores GameModes in which Challenges addon should not work. # To disable addon it is necessary to write its name in new line that starts with -. Example: # disabled-gamemodes: