Avoid race conditions in multiple load attempts.

This commit is contained in:
t00thpick1 2014-07-31 12:59:36 -04:00
parent 71d6cb661f
commit f809bca47b

View File

@ -1,5 +1,7 @@
package com.gmail.nossr50.runnables.player; package com.gmail.nossr50.runnables.player;
import java.util.concurrent.locks.ReentrantLock;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@ -18,6 +20,8 @@ public class PlayerProfileLoadingTask extends BukkitRunnable {
private static final int MAX_TRIES = 5; private static final int MAX_TRIES = 5;
private final Player player; private final Player player;
private int attempt = 0; private int attempt = 0;
private ReentrantLock lock = new ReentrantLock();
private boolean cancelled = false;
public PlayerProfileLoadingTask(Player player) { public PlayerProfileLoadingTask(Player player) {
this.player = player; this.player = player;
@ -27,10 +31,16 @@ public class PlayerProfileLoadingTask extends BukkitRunnable {
// DO NOT MODIFY THE McMMOPLAYER FROM THIS CODE // DO NOT MODIFY THE McMMOPLAYER FROM THIS CODE
@Override @Override
public void run() { public void run() {
lock.lock();
if (this.cancelled) {
return;
}
// Quit if they logged out // Quit if they logged out
if (!player.isOnline()) { if (!player.isOnline()) {
mcMMO.p.getLogger().info("Aborting profile loading recovery for " + player.getName() + " - player logged out"); mcMMO.p.getLogger().info("Aborting profile loading recovery for " + player.getName() + " - player logged out");
this.cancel(); this.cancel();
cancelled = true;
lock.unlock();
return; return;
} }
@ -46,6 +56,8 @@ public class PlayerProfileLoadingTask extends BukkitRunnable {
if (profile.isLoaded()) { if (profile.isLoaded()) {
new ApplySuccessfulProfile(profile).runTask(mcMMO.p); new ApplySuccessfulProfile(profile).runTask(mcMMO.p);
this.cancel(); this.cancel();
cancelled = true;
lock.unlock();
return; return;
} }
@ -55,8 +67,11 @@ public class PlayerProfileLoadingTask extends BukkitRunnable {
mcMMO.p.getServer().broadcast(LocaleLoader.getString("Profile.Loading.AdminFailureNotice", player.getName() ), Server.BROADCAST_CHANNEL_ADMINISTRATIVE); mcMMO.p.getServer().broadcast(LocaleLoader.getString("Profile.Loading.AdminFailureNotice", player.getName() ), Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
player.sendMessage(LocaleLoader.getString("Profile.Loading.Failure").split("\n")); player.sendMessage(LocaleLoader.getString("Profile.Loading.Failure").split("\n"));
this.cancel(); this.cancel();
cancelled = true;
lock.unlock();
return; return;
} }
lock.unlock();
} }
private class ApplySuccessfulProfile extends BukkitRunnable { private class ApplySuccessfulProfile extends BukkitRunnable {