1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +01:00

Resolving duplicated player job records

This commit is contained in:
Zrips 2021-07-18 14:52:53 +03:00
parent bbdacf8e03
commit 8fb7cd2edf
5 changed files with 80 additions and 31 deletions

View File

@ -572,7 +572,7 @@ public final class Jobs extends JavaPlugin {
getPlayerManager().addPlayerToCache(jPlayer);
}
if (!getPlayerManager().getPlayersCache().isEmpty())
consoleMsg("&e[Jobs] Preloaded " + getPlayerManager().getPlayersCache().size() + " players data in " + ((int) ((System.currentTimeMillis() - time) / 1000.0D * 100.0D) / 100.0D));
consoleMsg("&e[Jobs] Preloaded &6" + getPlayerManager().getPlayersCache().size() + " &eplayers data in &6" + ((int) ((System.currentTimeMillis() - time) / 1000.0D * 100.0D) / 100.0D));
}
public static void convertDatabase() {

View File

@ -64,6 +64,7 @@ import com.gamingmesh.jobs.stuff.Util;
import net.Zrips.CMILib.ActionBar.CMIActionBar;
import net.Zrips.CMILib.Items.CMIItemStack;
import net.Zrips.CMILib.Logs.CMIDebug;
import net.Zrips.CMILib.Messages.CMIMessages;
import net.Zrips.CMILib.NBT.CMINBT;
import net.Zrips.CMILib.Version.Version;
@ -121,6 +122,27 @@ public class PlayerManager {
}
public void addPlayerToMap(PlayerInfo info) {
// Checking duplicated UUID's which usually is a cause of previous bugs
if (playerUUIDMap.containsKey(info.getUuid()) && playerUUIDMap.get(info.getUuid()).getID() != info.getID()) {
int id = playerUUIDMap.get(info.getUuid()).getID();
CMIMessages.consoleMessage("&7Duplicate! &5" + info.getName() + " &7same UUID for 2 entries in dabase. Please remove of one them from users table id1: &2" + id + " &7id2: &2" + info.getID());
if (id < info.getID()) {
return;
}
}
// Checking duplicated names which usually is a cause of previous bugs
if (playerNameMap.containsKey(info.getName().toLowerCase()) && playerNameMap.get(info.getName().toLowerCase()).getID() != info.getID()) {
int id = playerNameMap.get(info.getName().toLowerCase()).getID();
CMIMessages.consoleMessage("&7Name Duplicate! &5" + info.getName() + " &7same UUID for 2 entries in dabase. Please remove of one them from users table id1: &2" + id + " &7id2: &2" + info
.getID());
if (id < info.getID()) {
return;
}
}
playerUUIDMap.put(info.getUuid(), info);
playerIdMap.put(info.getID(), info);
playerNameMap.put(info.getName().toLowerCase(), info);
@ -388,7 +410,6 @@ public class PlayerManager {
*/
public JobsPlayer getJobsPlayer(String playerName) {
playerName = playerName.toLowerCase();
JobsPlayer jPlayer = players.get(playerName);
return jPlayer != null ? jPlayer : playersCache.get(playerName);
}
@ -419,6 +440,15 @@ public class PlayerManager {
for (JobsDAOData jobdata : jobs) {
Job job = Jobs.getJob(jobdata.getJobName());
if (job != null) {
// Fixing issue with doubled jobs. Picking bigger job by level or exp
JobProgression oldProg = jPlayer.getJobProgression(job);
if (oldProg != null && (oldProg.getLevel() > jobdata.getLevel() || oldProg.getLevel() == jobdata.getLevel() && oldProg.getExperience() > jobdata.getExperience())) {
Jobs.getDBManager().getDB().removeSpecificJob(jPlayer, jobdata.getJobName(), jobdata.getLevel());
CMIMessages.consoleMessage("Cleaned up duplicated jobs record for " + jPlayer.getName() + " Job:" + jobdata.getJobName() + " Level:" + jobdata.getLevel());
continue;
}
jPlayer.progression.add(new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience()));
jPlayer.reloadMaxExperience();
jPlayer.reloadLimits();

View File

@ -1,7 +1,5 @@
package com.gamingmesh.jobs.commands.list;
import java.util.concurrent.CompletableFuture;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -23,10 +21,8 @@ public class convert implements Cmd {
return true;
}
CompletableFuture.supplyAsync(() -> {
Jobs.convertDatabase();
return true;
}).thenAccept(e -> {
String from = "MySQL";
String to = "SQLite";
@ -36,7 +32,6 @@ public class convert implements Cmd {
}
Jobs.consoleMsg("&eData base was converted from &2" + from + " &eto &2" + to + "&e!");
});
return true;
}

View File

@ -634,6 +634,7 @@ public abstract class JobsDAO {
res = prest.executeQuery();
while (res.next()) {
int id = res.getInt(JobsTableFields.userid.getCollumn());
List<JobsDAOData> ls = map.get(id);
if (ls == null)
ls = new ArrayList<>();
@ -644,14 +645,14 @@ public abstract class JobsDAO {
converted = false;
} else {
// This should be removed when we switch over to id only method
if (converted)
if (res.getString(JobsTableFields.job.getCollumn()) == null || res.getString(JobsTableFields.job.getCollumn()).isEmpty())
if (converted && res.getString(JobsTableFields.job.getCollumn()) == null || res.getString(JobsTableFields.job.getCollumn()).isEmpty())
converted = false;
Job job = Jobs.getJob(jobId);
if (job != null)
if (job != null) {
ls.add(new JobsDAOData(job.getName(), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn())));
}
}
map.put(id, ls);
}
@ -1588,6 +1589,33 @@ public abstract class JobsDAO {
return done;
}
/**
* Quit a job (delete player-job entry from storage)
* @param player - player that wishes to quit the job
* @param job - job that the player wishes to quit
*/
public synchronized boolean removeSpecificJob(JobsPlayer jPlayer, String jobName, int level) {
JobsConnection conn = getConnection();
if (conn == null)
return false;
PreparedStatement prest = null;
boolean done = true;
try {
prest = conn.prepareStatement("DELETE FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.userid.getCollumn() + "` = ? AND `" + JobsTableFields.job.getCollumn()
+ "` = ? AND `" + JobsTableFields.level.getCollumn() + "` = ?;");
prest.setInt(1, jPlayer.getUserId());
prest.setString(2, jobName);
prest.setInt(3, level);
prest.execute();
} catch (SQLException e) {
e.printStackTrace();
done = false;
} finally {
close(prest);
}
return done;
}
/**
* Record job to archive
* @param player - player that wishes to quit the job

View File

@ -163,8 +163,7 @@ public class JobsListener implements Listener {
if (!Jobs.getGCManager().MultiServerCompatability())
Jobs.getPlayerManager().playerJoin(event.getPlayer());
else {
plugin.getServer().getScheduler().runTaskLater(plugin, () ->
Jobs.getPlayerManager().playerJoin(event.getPlayer()), 10L);
plugin.getServer().getScheduler().runTaskLater(plugin, () -> Jobs.getPlayerManager().playerJoin(event.getPlayer()), 10L);
}
}
@ -182,10 +181,7 @@ public class JobsListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event) {
java.util.concurrent.CompletableFuture.supplyAsync(() -> {
Jobs.getPlayerManager().playerQuit(event.getPlayer());
return true;
});
}
@EventHandler(priority = EventPriority.MONITOR)