From 6d37e0c8c67b799e939e613cdba01a81e24c0266 Mon Sep 17 00:00:00 2001 From: games647 Date: Mon, 18 Sep 2017 09:34:59 +0200 Subject: [PATCH] Added McMMOPlayerLoadedEvent firing after the profile was loaded from storage --- .../events/McMMOPlayerLoadedEvent.java | 41 +++++++++++++++++++ .../player/PlayerProfileLoadingTask.java | 19 ++++++--- 2 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/events/McMMOPlayerLoadedEvent.java diff --git a/src/main/java/com/gmail/nossr50/events/McMMOPlayerLoadedEvent.java b/src/main/java/com/gmail/nossr50/events/McMMOPlayerLoadedEvent.java new file mode 100644 index 000000000..869e65b99 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/events/McMMOPlayerLoadedEvent.java @@ -0,0 +1,41 @@ +package com.gmail.nossr50.events; + +import com.gmail.nossr50.datatypes.player.McMMOPlayer; + +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; + +/** + * Called after the mcMMO player profile was successful loaded from data storage. This happens if the player recently + * joined the server or the data was reloaded (e.g. database convert, reset stats, convert experience) + * while the player is online. + */ +public class McMMOPlayerLoadedEvent extends PlayerEvent { + + private final McMMOPlayer mcMMOPlayer; + + public McMMOPlayerLoadedEvent(McMMOPlayer mcMMOPlayer) { + super(mcMMOPlayer.getPlayer()); + + this.mcMMOPlayer = mcMMOPlayer; + } + + /** + * @return mcMMO player profile associated to the player + */ + public McMMOPlayer getMcMMOPlayer() { + return mcMMOPlayer; + } + + /** Rest of file is required boilerplate for custom events **/ + private static final HandlerList handlers = new HandlerList(); + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/com/gmail/nossr50/runnables/player/PlayerProfileLoadingTask.java b/src/main/java/com/gmail/nossr50/runnables/player/PlayerProfileLoadingTask.java index 811d6cfb0..c18145bb3 100644 --- a/src/main/java/com/gmail/nossr50/runnables/player/PlayerProfileLoadingTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/player/PlayerProfileLoadingTask.java @@ -1,19 +1,21 @@ package com.gmail.nossr50.runnables.player; -import org.bukkit.Server; -import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; - -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.PlayerProfile; +import com.gmail.nossr50.events.McMMOPlayerLoadedEvent; import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.runnables.commands.McScoreboardKeepTask; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.scoreboards.ScoreboardManager; +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + public class PlayerProfileLoadingTask extends BukkitRunnable { private static final int MAX_TRIES = 5; private final Player player; @@ -87,6 +89,13 @@ public class PlayerProfileLoadingTask extends BukkitRunnable { ScoreboardManager.enablePlayerStatsScoreboard(player); new McScoreboardKeepTask(player).runTaskLater(mcMMO.p, 1 * Misc.TICK_CONVERSION_FACTOR); } + + Bukkit.getScheduler().runTask(mcMMO.p, new Runnable() { + @Override + public void run() { + Bukkit.getPluginManager().callEvent(new McMMOPlayerLoadedEvent(mcMMOPlayer)); + } + }); } } }