Numerous tweaks + failsafes added to Loading/Saving of players

This commit is contained in:
nossr50
2019-04-10 01:52:34 -07:00
parent f1204f8a2a
commit 426b1304e2
7 changed files with 67 additions and 19 deletions

View File

@ -13,6 +13,7 @@ import com.gmail.nossr50.runnables.player.PlayerProfileSaveTask;
import com.gmail.nossr50.skills.child.FamilyTree;
import com.gmail.nossr50.util.player.UserManager;
import com.google.common.collect.ImmutableMap;
import org.bukkit.Bukkit;
import java.util.HashMap;
import java.util.Map;
@ -29,6 +30,7 @@ public class PlayerProfile {
/* HUDs */
private MobHealthbarType mobHealthbarType;
private int scoreboardTipsShown;
private int saveAttempts = 0;
/* Skill Data */
private final Map<PrimarySkillType, Integer> skills = new HashMap<PrimarySkillType, Integer>(); // Skill & Level
@ -36,7 +38,7 @@ public class PlayerProfile {
private final Map<SuperAbilityType, Integer> abilityDATS = new HashMap<SuperAbilityType, Integer>(); // Ability & Cooldown
private final Map<UniqueDataType, Integer> uniquePlayerData = new HashMap<>(); //Misc data that doesn't fit into other categories (chimaera wing, etc..)
// Store previous XP gains for deminished returns
// Store previous XP gains for diminished returns
private DelayQueue<SkillXpGain> gainedSkillsXp = new DelayQueue<SkillXpGain>();
private HashMap<PrimarySkillType, Float> rollingSkillsXp = new HashMap<PrimarySkillType, Float>();
@ -94,8 +96,13 @@ public class PlayerProfile {
new PlayerProfileSaveTask(this).runTaskAsynchronously(mcMMO.p);
}
public void scheduleAsyncSaveDelay() {
new PlayerProfileSaveTask(this).runTaskLaterAsynchronously(mcMMO.p, 20);
}
public void save() {
if (!changed || !loaded) {
saveAttempts = 0;
return;
}
@ -104,8 +111,29 @@ public class PlayerProfile {
changed = !mcMMO.getDatabaseManager().saveUser(profileCopy);
if (changed) {
mcMMO.p.getLogger().warning("PlayerProfile saving failed for player: " + playerName + " " + uuid);
mcMMO.p.getLogger().severe("PlayerProfile saving failed for player: " + playerName + " " + uuid);
if(saveAttempts > 0)
{
mcMMO.p.getLogger().severe("Attempted to save profile for player "+getPlayerName()
+ " resulted in failure. "+saveAttempts+" have been made so far.");
}
if(saveAttempts < 10)
{
saveAttempts++;
scheduleAsyncSaveDelay();
return;
} else {
mcMMO.p.getLogger().severe("mcMMO has failed to save the profile for "
+getPlayerName()+" numerous times." +
" mcMMO will now stop attempting to save this profile." +
" Check your console for errors and inspect your DB for issues.");
}
}
saveAttempts = 0;
}
public String getPlayerName() {

View File

@ -484,7 +484,8 @@ public class PlayerListener implements Listener {
return;
}
new PlayerProfileLoadingTask(player).runTaskLaterAsynchronously(mcMMO.p, 1); // 1 Tick delay to ensure the player is marked as online before we begin loading
//Delay loading for 3 seconds in case the player has a save task running, its hacky but it should do the trick
new PlayerProfileLoadingTask(player).runTaskLaterAsynchronously(mcMMO.p, 60);
if (Config.getInstance().getMOTDEnabled() && Permissions.motd(player)) {
Motd.displayAll(player);

View File

@ -14,7 +14,6 @@ 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;
private int attempt = 0;
@ -37,9 +36,6 @@ public class PlayerProfileLoadingTask extends BukkitRunnable {
return;
}
// Increment attempt counter and try
attempt++;
PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(player.getName(), player.getUniqueId(), true);
// If successful, schedule the apply
if (profile.isLoaded()) {
@ -47,14 +43,24 @@ public class PlayerProfileLoadingTask extends BukkitRunnable {
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"));
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
mcMMO.p.getLogger().severe(LocaleLoader.getString("Profile.Loading.FailureNotice",
player.getName(), String.valueOf(attempt)));
//Notify the admins
mcMMO.p.getServer().broadcast(LocaleLoader.getString("Profile.Loading.FailureNotice", player.getName()), Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
//Notify the player
player.sendMessage(LocaleLoader.getString("Profile.Loading.FailurePlayer", String.valueOf(attempt)).split("\n"));
}
new PlayerProfileLoadingTask(player, attempt).runTaskLaterAsynchronously(mcMMO.p, 100 * attempt);
// Increment attempt counter and try
attempt++;
new PlayerProfileLoadingTask(player, attempt).runTaskLaterAsynchronously(mcMMO.p, 100);
}
private class ApplySuccessfulProfile extends BukkitRunnable {
@ -90,7 +96,6 @@ public class PlayerProfileLoadingTask extends BukkitRunnable {
player.sendMessage(LocaleLoader.getString("Profile.Loading.Success"));
}
}
}
}

View File

@ -12,6 +12,11 @@ public class PlayerProfileSaveTask extends BukkitRunnable {
@Override
public void run() {
playerProfile.save();
boolean saveSuccess = playerProfile.save();
if(!saveSuccess)
{
playerProfile.scheduleAsyncSaveDelay();
}
}
}