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
This commit is contained in:
Andreas Troelsen 2021-07-07 00:14:29 +02:00
parent 903752d23a
commit b99713f1ec
4 changed files with 111 additions and 0 deletions

View File

@ -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.

View File

@ -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

View File

@ -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.
* <p>
* Use this event to re-register components that need to be set up and in
* place <i>before</i> MobArena dips into its config-file. This is mostly
* relevant for plugins that <code>loadbefore</code> MobArena to register
* {@link com.garbagemule.MobArena.things.ThingParser}s and such.
* <p>
* This event is <i>not</i> 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;
}
}

View File

@ -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.
* <p>
* The primary goal of this event is to allow extension plugins to "ride
* along" with the <code>/ma reload</code> command so they don't have to
* implement their own commands. <i>One command to rule them all.</i>
* <p>
* 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.
* <p>
* This event is <i>not</i> suitable for re-registering components that
* need to be in place <i>before</i> 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;
}
}