From b99713f1ec3326f0149683079a7baca58f0c3e4c Mon Sep 17 00:00:00 2001 From: Andreas Troelsen Date: Wed, 7 Jul 2021 00:14:29 +0200 Subject: [PATCH] Add reload events. This commit introduces two reload events, MobArenaPreReloadEvent and MobArenaReloadEvent. Both events are fired when MobArena reloads, the former right before the reload, and the latter right after. The reason for two events is to allow plugins to reload either entirely or partially along with MobArena, if it makes sense for them to do so, _when_ it makes sense for them to do so. Some plugins need to reload and re-register themselves with MobArena before the config-file itself is loaded (e.g. ThingParsers), while others either require MobArena to be fully loaded or are more decoupled and thus don't depend on anything in MobArena's innards to function. The "pre" event is for the former, while the other event is for the latter. As for naming, the choice of "Pre" and no prefix was made for the sake of consistency with the event names in the Bukkit API, which has just one example of such a pair (PlayerPreLoginEvent and PlayerLoginEvent). Some event naming conventions (e.g. in the .NET world) seem to favor present and past tense (reloading, reloaded), but this would be wildly inconsistent with the rest of the event names, so it might be better to just stay consistent. Names may change before an actual release, but for now, this is what we're rolling with. Closes #677 --- changelog.md | 1 + .../com/garbagemule/MobArena/MobArena.java | 8 +++ .../events/MobArenaPreReloadEvent.java | 50 ++++++++++++++++++ .../MobArena/events/MobArenaReloadEvent.java | 52 +++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 src/main/java/com/garbagemule/MobArena/events/MobArenaPreReloadEvent.java create mode 100644 src/main/java/com/garbagemule/MobArena/events/MobArenaReloadEvent.java diff --git a/changelog.md b/changelog.md index abf44c7..5713ceb 100644 --- a/changelog.md +++ b/changelog.md @@ -13,6 +13,7 @@ These changes will (most likely) be included in the next version. ## [Unreleased] ### Added - (API) MobArena's internal command handler now supports registering pre-instantiated subcommand instances. This should make it easier for extensions to avoid the Singleton anti-pattern for command dependencies. +- (API) MobArena now fires MobArenaPreReloadEvent and MobArenaReloadEvent before and after, respectively, reloading its config-file. This should allow extensions and other plugins to better respond to configuration changes. ### Changed - The regex pattern for the player list command is now less greedy, so it will only match on `/ma players`, `/ma playerlist`, and `/ma player-list`. The previous pattern matched on anything that starts with `player`, which rendered the `/ma player-stats` command in MobArenaStats impossible to invoke. diff --git a/src/main/java/com/garbagemule/MobArena/MobArena.java b/src/main/java/com/garbagemule/MobArena/MobArena.java index ae6c1ff..6bd10e4 100644 --- a/src/main/java/com/garbagemule/MobArena/MobArena.java +++ b/src/main/java/com/garbagemule/MobArena/MobArena.java @@ -2,6 +2,8 @@ package com.garbagemule.MobArena; import com.garbagemule.MobArena.commands.CommandHandler; import com.garbagemule.MobArena.config.LoadsConfigFile; +import com.garbagemule.MobArena.events.MobArenaPreReloadEvent; +import com.garbagemule.MobArena.events.MobArenaReloadEvent; import com.garbagemule.MobArena.formula.FormulaMacros; import com.garbagemule.MobArena.formula.FormulaManager; import com.garbagemule.MobArena.framework.Arena; @@ -181,6 +183,9 @@ public class MobArena extends JavaPlugin } public void reload() { + MobArenaPreReloadEvent pre = new MobArenaPreReloadEvent(this); + getServer().getPluginManager().callEvent(pre); + try { reloadConfig(); reloadGlobalMessenger(); @@ -192,6 +197,9 @@ public class MobArena extends JavaPlugin setLastFailureCauseAndRethrow(e); } lastFailureCause = null; + + MobArenaReloadEvent post = new MobArenaReloadEvent(this); + getServer().getPluginManager().callEvent(post); } @Override diff --git a/src/main/java/com/garbagemule/MobArena/events/MobArenaPreReloadEvent.java b/src/main/java/com/garbagemule/MobArena/events/MobArenaPreReloadEvent.java new file mode 100644 index 0000000..0e26437 --- /dev/null +++ b/src/main/java/com/garbagemule/MobArena/events/MobArenaPreReloadEvent.java @@ -0,0 +1,50 @@ +package com.garbagemule.MobArena.events; + +import com.garbagemule.MobArena.MobArena; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +/** + * Called right before MobArena begins reloading. + *

+ * Use this event to re-register components that need to be set up and in + * place before MobArena dips into its config-file. This is mostly + * relevant for plugins that loadbefore MobArena to register + * {@link com.garbagemule.MobArena.things.ThingParser}s and such. + *

+ * This event is not suitable for working with the "current" state + * of arenas, classes, etc., because MobArena's state at the time of this + * event is about to become stale. To work with the "current" state after + * a reload, use the {@link MobArenaReloadEvent} instead. + * + * @see MobArenaReloadEvent + */ +public class MobArenaPreReloadEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + private final MobArena plugin; + + public MobArenaPreReloadEvent(MobArena plugin) { + this.plugin = plugin; + } + + /** + * Get the current MobArena plugin instance. + * + * @return the MobArena plugin instance + */ + public MobArena getPlugin() { + return plugin; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + +} diff --git a/src/main/java/com/garbagemule/MobArena/events/MobArenaReloadEvent.java b/src/main/java/com/garbagemule/MobArena/events/MobArenaReloadEvent.java new file mode 100644 index 0000000..56d96f5 --- /dev/null +++ b/src/main/java/com/garbagemule/MobArena/events/MobArenaReloadEvent.java @@ -0,0 +1,52 @@ +package com.garbagemule.MobArena.events; + +import com.garbagemule.MobArena.MobArena; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +/** + * Called right after MobArena has reloaded. + *

+ * The primary goal of this event is to allow extension plugins to "ride + * along" with the /ma reload command so they don't have to + * implement their own commands. One command to rule them all. + *

+ * This event is useful if you need to work with arenas, classes, etc. in + * their "current" state. This is typical of plugins that (soft)depend on + * MobArena and use its API after the initialization phase. + *

+ * This event is not suitable for re-registering components that + * need to be in place before MobArena reloads. For that, see the + * {@link MobArenaPreReloadEvent}. + * + * @see MobArenaPreReloadEvent + */ +public class MobArenaReloadEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + private final MobArena plugin; + + public MobArenaReloadEvent(MobArena plugin) { + this.plugin = plugin; + } + + /** + * Get the current MobArena plugin instance. + * + * @return the MobArena plugin instance + */ + public MobArena getPlugin() { + return plugin; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + +}