diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index 1cc9a422..33984c01 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -133,8 +133,26 @@ public class PlayerManager { public void addPlayerToCache(JobsPlayer jPlayer) { playersUUIDCache.putIfAbsent(jPlayer.playerUUID, jPlayer); - if (jPlayer.getName() != null) + if (jPlayer.getName() == null) + return; + + // On data load check for name duplication + if (!Jobs.fullyLoaded) { + // Checking for existing record by same name + JobsPlayer oldRecord = playersNameCache.get(jPlayer.getName().toLowerCase()); + if (oldRecord != null) { + CMIMessages.consoleMessage("&cDuplicate in database for (&f" + jPlayer.getName() + "&c) -> (&f" + jPlayer.getUniqueId() + "&c) <-> (&f" + oldRecord.getUniqueId() + "&c)"); + // Using newest record + if (jPlayer.getSeen() > oldRecord.getSeen()) + playersNameCache.put(jPlayer.getName().toLowerCase(), jPlayer); + return; + } playersNameCache.putIfAbsent(jPlayer.getName().toLowerCase(), jPlayer); + } else { + // Add player to cache independent if there was other record by this name, we should use latest record + playersNameCache.put(jPlayer.getName().toLowerCase(), jPlayer); + } + } public void addPlayer(JobsPlayer jPlayer) { @@ -426,6 +444,7 @@ public class PlayerManager { jPlayer.setUserId(info.getID()); jPlayer.setDoneQuests(info.getQuestsDone()); jPlayer.setQuestProgressionFromString(info.getQuestProgression()); + jPlayer.setSeen(info.getSeen()); if (jobs != null) { for (JobsDAOData jobdata : jobs) { diff --git a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java index 9ceb157d..2a1972b4 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java @@ -928,30 +928,32 @@ public class JobsPlayer { */ public void save(boolean async) { // synchronized (saveLock) { - if (!isSaved) { - JobsDAO dao = Jobs.getJobsDAO(); - if (!async) { - dao.save(this); - dao.saveLog(this); - dao.savePoints(this); - dao.recordPlayersLimits(this); - dao.updateSeen(this); - } else { - dao.saveAsync(this); - dao.saveLogAsync(this); - dao.savePointsAsync(this); - dao.recordPlayersLimitsAsync(this); - dao.updateSeenAsync(this); - } + if (isSaved) + return; - setSaved(true); - - Player player = getPlayer(); - if (player == null || !player.isOnline()) { - Jobs.getPlayerManager().addPlayerToCache(this); - Jobs.getPlayerManager().removePlayer(player); - } + JobsDAO dao = Jobs.getJobsDAO(); + if (async) { + dao.saveAsync(this); + dao.saveLogAsync(this); + dao.savePointsAsync(this); + dao.recordPlayersLimitsAsync(this); + dao.updateSeenAsync(this); + } else { + dao.save(this); + dao.saveLog(this); + dao.savePoints(this); + dao.recordPlayersLimits(this); + dao.updateSeen(this); } + + setSaved(true); + + Player player = getPlayer(); + if (player == null || !player.isOnline()) { + Jobs.getPlayerManager().addPlayerToCache(this); + Jobs.getPlayerManager().removePlayer(player); + } + // } }