mcMMO/src/main/java/com/gmail/nossr50/runnables/player/PlayerProfileLoadingTask.java

104 lines
3.9 KiB
Java

package com.gmail.nossr50.runnables.player;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.runnables.commands.ScoreboardKeepTask;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
public class PlayerProfileLoadingTask extends BukkitRunnable {
private final mcMMO pluginRef;
private final Player player;
private int attempt = 0;
public PlayerProfileLoadingTask(mcMMO pluginRef, Player player) {
this.pluginRef = pluginRef;
this.player = player;
}
private PlayerProfileLoadingTask(mcMMO pluginRef, Player player, int attempt) {
this.pluginRef = pluginRef;
this.player = player;
this.attempt = attempt;
}
// WARNING: ASYNC TASK
// DO NOT MODIFY THE McMMOPLAYER FROM THIS CODE
@Override
public void run() {
if (pluginRef.getMiscTools().isNPCIncludingVillagers(player)) {
return;
}
// Quit if they logged out
if (!player.isOnline()) {
pluginRef.getLogger().info("Aborting profile loading recovery for " + player.getName() + " - player logged out");
return;
}
PlayerProfile profile = pluginRef.getDatabaseManager().loadPlayerProfile(player.getName(), player.getUniqueId(), true);
// If successful, schedule the apply
if (profile.isLoaded()) {
new ApplySuccessfulProfile(new McMMOPlayer(player, profile, pluginRef)).runTask(pluginRef);
return;
}
// Print errors to console/logs if we're failing at least 2 times in a row to load the profile
if (attempt >= 3) {
//Log the error
pluginRef.getLogger().severe(pluginRef.getLocaleManager().getString("Profile.Loading.FailureNotice",
player.getName(), String.valueOf(attempt)));
//Notify the admins
pluginRef.getServer().broadcast(pluginRef.getLocaleManager().getString("Profile.Loading.FailureNotice", player.getName()), Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
//Notify the player
player.sendMessage(pluginRef.getLocaleManager().getString("Profile.Loading.FailurePlayer", String.valueOf(attempt)).split("\n"));
}
// Increment attempt counter and try
attempt++;
new PlayerProfileLoadingTask(pluginRef, player, attempt).runTaskLaterAsynchronously(pluginRef, (100 + (attempt * 100)));
}
private class ApplySuccessfulProfile extends BukkitRunnable {
private final McMMOPlayer mcMMOPlayer;
private ApplySuccessfulProfile(McMMOPlayer mcMMOPlayer) {
this.mcMMOPlayer = mcMMOPlayer;
}
// Synchronized task
// No database access permitted
@Override
public void run() {
if (!player.isOnline()) {
pluginRef.getLogger().info("Aborting profile loading recovery for " + player.getName() + " - player logged out");
return;
}
mcMMOPlayer.setupPartyData();
pluginRef.getUserManager().track(mcMMOPlayer);
mcMMOPlayer.actualizeRespawnATS();
if (pluginRef.getScoreboardSettings().getScoreboardsEnabled()) {
pluginRef.getScoreboardManager().setupPlayer(player);
if (pluginRef.getScoreboardSettings().getShowStatsAfterLogin()) {
pluginRef.getScoreboardManager().enablePlayerStatsScoreboard(player);
new ScoreboardKeepTask(pluginRef, player).runTaskLater(pluginRef, pluginRef.getMiscTools().TICK_CONVERSION_FACTOR);
}
}
if (pluginRef.getConfigManager().getConfigNotifications().isShowProfileLoadedMessage()) {
player.sendMessage(pluginRef.getLocaleManager().getString("Profile.Loading.Success"));
}
}
}
}