diff --git a/src/main/java/com/gmail/nossr50/commands/database/ConvertDatabaseCommand.java b/src/main/java/com/gmail/nossr50/commands/database/ConvertDatabaseCommand.java index 7f85a8977..8f6400766 100644 --- a/src/main/java/com/gmail/nossr50/commands/database/ConvertDatabaseCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/database/ConvertDatabaseCommand.java @@ -61,7 +61,7 @@ public class ConvertDatabaseCommand implements CommandExecutor { mcMMO.getDatabaseManager().saveUser(profile); } - UserManager.addUser(player); + UserManager.track(player); } new DatabaseConversionTask(oldDatabase, sender, previousType.toString(), newType.toString()).runTaskAsynchronously(mcMMO.p); diff --git a/src/main/java/com/gmail/nossr50/commands/experience/ConvertExperienceCommand.java b/src/main/java/com/gmail/nossr50/commands/experience/ConvertExperienceCommand.java index 3c0a97f64..f93846518 100644 --- a/src/main/java/com/gmail/nossr50/commands/experience/ConvertExperienceCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/experience/ConvertExperienceCommand.java @@ -37,7 +37,7 @@ public class ConvertExperienceCommand implements CommandExecutor { new FormulaConversionTask(sender, newType).runTaskLater(mcMMO.p, 1); for (Player player : mcMMO.p.getServer().getOnlinePlayers()) { - UserManager.addUser(player); + UserManager.track(player); } return true; diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java index 148c802d7..38a14f0fc 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -93,13 +93,13 @@ public class McMMOPlayer { private boolean isUsingUnarmed; private final FixedMetadataValue playerMetadata; - public McMMOPlayer(Player player) { + public McMMOPlayer(Player player, PlayerProfile profile) { String playerName = player.getName(); UUID uuid = player.getUniqueId(); this.player = player; playerMetadata = new FixedMetadataValue(mcMMO.p, playerName); - profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, uuid, true); + this.profile = profile; party = PartyManager.getPlayerParty(playerName); ptpRecord = new PartyTeleportRecord(); @@ -130,70 +130,6 @@ public class McMMOPlayer { for (ToolType toolType : ToolType.values()) { toolMode.put(toolType, false); } - - if (!profile.isLoaded()) { - mcMMO.p.getLogger().warning("Unable to load the PlayerProfile for " + playerName + ". Will retry over the next several seconds."); - new RetryProfileLoadingTask().runTaskTimerAsynchronously(mcMMO.p, 11L, 31L); - } - } - - private class RetryProfileLoadingTask extends BukkitRunnable { - private static final int MAX_TRIES = 5; - private final String playerName = McMMOPlayer.this.player.getName(); - private final UUID uniqueId = McMMOPlayer.this.player.getUniqueId(); - private int attempt = 0; - - // WARNING: ASYNC TASK - // DO NOT MODIFY THE McMMOPLAYER FROM THIS CODE - @Override - public void run() { - // Quit if they logged out - if (!player.isOnline()) { - mcMMO.p.getLogger().info("Aborting profile loading recovery for " + playerName + " - player logged out"); - this.cancel(); - return; - } - - // Send the message that we're doing the recovery - if (attempt == 0) { - player.sendMessage(LocaleLoader.getString("Recovery.Notice")); - } - - // Increment attempt counter and try - attempt++; - PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(uniqueId, true); - // If successful, schedule the apply - if (profile.isLoaded()) { - new ApplySuccessfulProfile(profile).runTask(mcMMO.p); - player.sendMessage(LocaleLoader.getString("Recovery.Success")); - this.cancel(); - return; - } - - // If we've failed five times, give up - if (attempt >= MAX_TRIES) { - mcMMO.p.getLogger().severe("Giving up on attempting to load the PlayerProfile for " + playerName); - mcMMO.p.getServer().broadcast(LocaleLoader.getString("Recovery.AdminFailureNotice", playerName), Server.BROADCAST_CHANNEL_ADMINISTRATIVE); - player.sendMessage(LocaleLoader.getString("Recovery.Failure").split("\n")); - this.cancel(); - return; - } - } - } - - private class ApplySuccessfulProfile extends BukkitRunnable { - private final PlayerProfile profile; - - private ApplySuccessfulProfile(PlayerProfile profile) { - this.profile = profile; - } - - // Synchronized task - // No database access permitted - @Override - public void run() { - McMMOPlayer.this.profile = profile; - } } public AcrobaticsManager getAcrobaticsManager() { diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index 1eacb7fcb..771fe4f87 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -41,7 +41,7 @@ import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.party.ShareHandler; -import com.gmail.nossr50.runnables.commands.McScoreboardKeepTask; +import com.gmail.nossr50.runnables.player.PlayerProfileLoadingTask; import com.gmail.nossr50.runnables.skills.BleedTimerTask; import com.gmail.nossr50.skills.fishing.FishingManager; import com.gmail.nossr50.skills.herbalism.HerbalismManager; @@ -387,9 +387,7 @@ public class PlayerListener implements Listener { return; } - McMMOPlayer mcMMOPlayer = UserManager.addUser(player); - mcMMOPlayer.actualizeRespawnATS(); - ScoreboardManager.setupPlayer(player); + new PlayerProfileLoadingTask(player).runTaskLaterAsynchronously(mcMMO.p, 1); // 1 Tick delay to ensure the player is marked as online before we begin loading if (Config.getInstance().getMOTDEnabled() && Permissions.motd(player)) { Motd.displayAll(player); @@ -403,11 +401,6 @@ public class PlayerListener implements Listener { player.sendMessage(LocaleLoader.getString("UpdateChecker.Outdated")); player.sendMessage(LocaleLoader.getString("UpdateChecker.NewAvailable")); } - - if (Config.getInstance().getShowStatsAfterLogin()) { - ScoreboardManager.enablePlayerStatsScoreboard(player); - new McScoreboardKeepTask(player).runTaskLater(mcMMO.p, 1 * Misc.TICK_CONVERSION_FACTOR); - } } /** diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 2d496443f..5ca626b02 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -167,7 +167,7 @@ public class mcMMO extends JavaPlugin { holidayManager = new HolidayManager(); for (Player player : getServer().getOnlinePlayers()) { - UserManager.addUser(player); // In case of reload add all users back into UserManager + UserManager.track(player); // In case of reload add all users back into UserManager ScoreboardManager.setupPlayer(player); } diff --git a/src/main/java/com/gmail/nossr50/runnables/player/PlayerProfileLoadingTask.java b/src/main/java/com/gmail/nossr50/runnables/player/PlayerProfileLoadingTask.java new file mode 100644 index 000000000..fcc04c07d --- /dev/null +++ b/src/main/java/com/gmail/nossr50/runnables/player/PlayerProfileLoadingTask.java @@ -0,0 +1,93 @@ +package com.gmail.nossr50.runnables.player; + +import java.util.UUID; + +import org.bukkit.Server; +import org.bukkit.entity.Player; +import org.bukkit.metadata.FixedMetadataValue; +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.locale.LocaleLoader; +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; + +public class PlayerProfileLoadingTask extends BukkitRunnable { + private static final int MAX_TRIES = 5; + private final Player player; + private int attempt = 0; + + public PlayerProfileLoadingTask(Player player) { + this.player = player; + } + + // WARNING: ASYNC TASK + // DO NOT MODIFY THE McMMOPLAYER FROM THIS CODE + @Override + public void run() { + // Quit if they logged out + if (!player.isOnline()) { + mcMMO.p.getLogger().info("Aborting profile loading recovery for " + player.getName() + " - player logged out"); + this.cancel(); + return; + } + + // Send the message that we're doing the recovery + if (attempt == 0) { + player.sendMessage(LocaleLoader.getString("Profile.Loading.Starting")); + } + + // Increment attempt counter and try + attempt++; + PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(player.getUniqueId(), true); + // If successful, schedule the apply + if (profile.isLoaded()) { + new ApplySuccessfulProfile(profile).runTask(mcMMO.p); + this.cancel(); + return; + } + + // If we've failed five times, give up + if (attempt >= MAX_TRIES) { + mcMMO.p.getLogger().severe("Giving up on attempting to load the PlayerProfile for " + player.getName() ); + mcMMO.p.getServer().broadcast(LocaleLoader.getString("Profile.Loading.AdminFailureNotice", player.getName() ), Server.BROADCAST_CHANNEL_ADMINISTRATIVE); + player.sendMessage(LocaleLoader.getString("Profile.Loading.Failure").split("\n")); + this.cancel(); + return; + } + } + + private class ApplySuccessfulProfile extends BukkitRunnable { + private final PlayerProfile profile; + + private ApplySuccessfulProfile(PlayerProfile profile) { + this.profile = profile; + } + + // Synchronized task + // No database access permitted + @Override + public void run() { + if (!player.isOnline()) { + mcMMO.p.getLogger().info("Aborting profile loading recovery for " + player.getName() + " - player logged out"); + return; + } + McMMOPlayer mcMMOPlayer = new McMMOPlayer(player, profile); + UserManager.track(mcMMOPlayer); + player.sendMessage(LocaleLoader.getString("Profile.Loading.Success")); + mcMMOPlayer.actualizeRespawnATS(); + ScoreboardManager.setupPlayer(player); + if (Config.getInstance().getShowStatsAfterLogin()) { + ScoreboardManager.enablePlayerStatsScoreboard(player); + new McScoreboardKeepTask(player).runTaskLater(mcMMO.p, 1 * Misc.TICK_CONVERSION_FACTOR); + } + } + } +} + + diff --git a/src/main/java/com/gmail/nossr50/util/Misc.java b/src/main/java/com/gmail/nossr50/util/Misc.java index 1196fa63f..474231f9b 100644 --- a/src/main/java/com/gmail/nossr50/util/Misc.java +++ b/src/main/java/com/gmail/nossr50/util/Misc.java @@ -112,7 +112,7 @@ public final class Misc { if (player != null) { UserManager.remove(player); - UserManager.addUser(player); + UserManager.track(player); } } diff --git a/src/main/java/com/gmail/nossr50/util/player/UserManager.java b/src/main/java/com/gmail/nossr50/util/player/UserManager.java index 10923a2eb..61e704818 100644 --- a/src/main/java/com/gmail/nossr50/util/player/UserManager.java +++ b/src/main/java/com/gmail/nossr50/util/player/UserManager.java @@ -18,16 +18,12 @@ public final class UserManager { private UserManager() {} /** - * Add a new user. + * Track a new user. * - * @param player The player to create a user record for - * @return the player's {@link McMMOPlayer} object + * @param mcMMOPlayer the player profile to start tracking */ - public static McMMOPlayer addUser(Player player) { - McMMOPlayer mcMMOPlayer = new McMMOPlayer(player); - player.setMetadata(mcMMO.playerDataKey, new FixedMetadataValue(mcMMO.p, mcMMOPlayer)); - - return mcMMOPlayer; + public static void track(McMMOPlayer mcMMOPlayer) { + mcMMOPlayer.getPlayer().setMetadata(mcMMO.playerDataKey, new FixedMetadataValue(mcMMO.p, mcMMOPlayer)); } /** diff --git a/src/main/resources/locale/locale_en_US.properties b/src/main/resources/locale/locale_en_US.properties index 34f36eece..04729f5d5 100644 --- a/src/main/resources/locale/locale_en_US.properties +++ b/src/main/resources/locale/locale_en_US.properties @@ -960,7 +960,7 @@ Scoreboard.Misc.Cooldown=[[LIGHT_PURPLE]]Cooldown Scoreboard.Misc.Overall=[[GOLD]]Overall #DATABASE RECOVERY -Recovery.Notice=[[RED]]Notice: mcMMO was [[DARK_RED]]unable to load your data.[[RED]] Retrying 5 times... -Recovery.Success=[[GREEN]]Success! Your mcMMO data was loaded. -Recovery.Failure=[[RED]]mcMMO still cannot load your data. You may want to [[AQUA]]contact the server owner.\n[[YELLOW]]You can still play on the server, but you will have [[BOLD]]no mcMMO levels[[YELLOW]] and any XP you get [[BOLD]]will not be saved[[YELLOW]]. -Recovery.AdminFailureNotice=[[DARK_RED]][A][[RED]] mcMMO was unable to load the player data for [[YELLOW]]{0}[[RED]]. [[LIGHT_PURPLE]]Please inspect your database setup. +Profile.Loading.Start=[[GREEN]]Notice: mcMMO is now loading your profile. Stats and skills will not function until loaded... +Profile.Loading.Success=[[GREEN]]Success! Your mcMMO data was loaded. +Profile.Loading.Failure=[[RED]]mcMMO still cannot load your data. You may want to [[AQUA]]contact the server owner.\n[[YELLOW]]You can still play on the server, but you will have [[BOLD]]no mcMMO levels[[YELLOW]] and any XP you get [[BOLD]]will not be saved[[YELLOW]]. +Profile.Loading.AdminFailureNotice=[[DARK_RED]][A][[RED]] mcMMO was unable to load the player data for [[YELLOW]]{0}[[RED]]. [[LIGHT_PURPLE]]Please inspect your database setup.