PlayerProfileLoadingTask.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.gmail.nossr50.runnables.player;
  2. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  3. import com.gmail.nossr50.datatypes.player.PlayerProfile;
  4. import com.gmail.nossr50.mcMMO;
  5. import com.gmail.nossr50.runnables.commands.ScoreboardKeepTask;
  6. import com.gmail.nossr50.util.Misc;
  7. import org.bukkit.Server;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.scheduler.BukkitRunnable;
  10. public class PlayerProfileLoadingTask extends BukkitRunnable {
  11. private final mcMMO pluginRef;
  12. private final Player player;
  13. private int attempt = 0;
  14. public PlayerProfileLoadingTask(mcMMO pluginRef, Player player) {
  15. this.pluginRef = pluginRef;
  16. this.player = player;
  17. }
  18. private PlayerProfileLoadingTask(mcMMO pluginRef, Player player, int attempt) {
  19. this.pluginRef = pluginRef;
  20. this.player = player;
  21. this.attempt = attempt;
  22. }
  23. // WARNING: ASYNC TASK
  24. // DO NOT MODIFY THE McMMOPLAYER FROM THIS CODE
  25. @Override
  26. public void run() {
  27. if (Misc.isNPCIncludingVillagers(player)) {
  28. return;
  29. }
  30. // Quit if they logged out
  31. if (!player.isOnline()) {
  32. pluginRef.getLogger().info("Aborting profile loading recovery for " + player.getName() + " - player logged out");
  33. return;
  34. }
  35. PlayerProfile profile = pluginRef.getDatabaseManager().loadPlayerProfile(player.getName(), player.getUniqueId(), true);
  36. // If successful, schedule the apply
  37. if (profile.isLoaded()) {
  38. new ApplySuccessfulProfile(new McMMOPlayer(player, profile, pluginRef)).runTask(pluginRef);
  39. return;
  40. }
  41. // Print errors to console/logs if we're failing at least 2 times in a row to load the profile
  42. if (attempt >= 3) {
  43. //Log the error
  44. pluginRef.getLogger().severe(pluginRef.getLocaleManager().getString("Profile.Loading.FailureNotice",
  45. player.getName(), String.valueOf(attempt)));
  46. //Notify the admins
  47. pluginRef.getServer().broadcast(pluginRef.getLocaleManager().getString("Profile.Loading.FailureNotice", player.getName()), Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
  48. //Notify the player
  49. player.sendMessage(pluginRef.getLocaleManager().getString("Profile.Loading.FailurePlayer", String.valueOf(attempt)).split("\n"));
  50. }
  51. // Increment attempt counter and try
  52. attempt++;
  53. new PlayerProfileLoadingTask(pluginRef, player, attempt).runTaskLaterAsynchronously(pluginRef, (100 + (attempt * 100)));
  54. }
  55. private class ApplySuccessfulProfile extends BukkitRunnable {
  56. private final McMMOPlayer mcMMOPlayer;
  57. private ApplySuccessfulProfile(McMMOPlayer mcMMOPlayer) {
  58. this.mcMMOPlayer = mcMMOPlayer;
  59. }
  60. // Synchronized task
  61. // No database access permitted
  62. @Override
  63. public void run() {
  64. if (!player.isOnline()) {
  65. pluginRef.getLogger().info("Aborting profile loading recovery for " + player.getName() + " - player logged out");
  66. return;
  67. }
  68. mcMMOPlayer.setupPartyData();
  69. pluginRef.getUserManager().track(mcMMOPlayer);
  70. mcMMOPlayer.actualizeRespawnATS();
  71. if (pluginRef.getScoreboardSettings().getScoreboardsEnabled()) {
  72. pluginRef.getScoreboardManager().setupPlayer(player);
  73. if (pluginRef.getScoreboardSettings().getShowStatsAfterLogin()) {
  74. pluginRef.getScoreboardManager().enablePlayerStatsScoreboard(player);
  75. new ScoreboardKeepTask(pluginRef, player).runTaskLater(pluginRef, Misc.TICK_CONVERSION_FACTOR);
  76. }
  77. }
  78. if (pluginRef.getConfigManager().getConfigNotifications().isShowProfileLoadedMessage()) {
  79. player.sendMessage(pluginRef.getLocaleManager().getString("Profile.Loading.Success"));
  80. }
  81. }
  82. }
  83. }