From 4edc1c5c62c767c35ccce87e0f728d8aea805ee4 Mon Sep 17 00:00:00 2001 From: BONNe1704 Date: Tue, 18 Dec 2018 10:58:56 +0200 Subject: [PATCH] Create WorldSaveEvent listener that stores challenges, when event occur. Partial fix of issue #21. --- .../addon/challenges/ChallengesAddon.java | 3 ++ .../challenges/listeners/SaveListener.java | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/main/java/bentobox/addon/challenges/listeners/SaveListener.java diff --git a/src/main/java/bentobox/addon/challenges/ChallengesAddon.java b/src/main/java/bentobox/addon/challenges/ChallengesAddon.java index e006dfb..a0abe0b 100644 --- a/src/main/java/bentobox/addon/challenges/ChallengesAddon.java +++ b/src/main/java/bentobox/addon/challenges/ChallengesAddon.java @@ -5,6 +5,7 @@ import org.bukkit.Bukkit; import bentobox.addon.challenges.commands.ChallengesCommand; import bentobox.addon.challenges.commands.admin.Challenges; import bentobox.addon.challenges.listeners.ResetListener; +import bentobox.addon.challenges.listeners.SaveListener; import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.commands.CompositeCommand; @@ -73,6 +74,8 @@ public class ChallengesAddon extends Addon { } // Register the reset listener this.registerListener(new ResetListener(this)); + // Register the autosave listener. + this.registerListener(new SaveListener(this)); // Done } diff --git a/src/main/java/bentobox/addon/challenges/listeners/SaveListener.java b/src/main/java/bentobox/addon/challenges/listeners/SaveListener.java new file mode 100644 index 0000000..2e1a821 --- /dev/null +++ b/src/main/java/bentobox/addon/challenges/listeners/SaveListener.java @@ -0,0 +1,36 @@ +package bentobox.addon.challenges.listeners; + + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.world.WorldSaveEvent; + +import bentobox.addon.challenges.ChallengesAddon; + + +/** + * This is Simple World Save event listener. On each world save, this method + * asks challenge manager to save its data. + */ +public class SaveListener implements Listener +{ + public SaveListener(ChallengesAddon addon) { + this.addon = addon; + } + + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onWorldSave(WorldSaveEvent e) + { + this.addon.getChallengesManager().save(e.isAsynchronous()); + } + + +// --------------------------------------------------------------------- +// Section: Variables +// --------------------------------------------------------------------- + + + private ChallengesAddon addon; +}