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); getPlayerManager().addPlayerToCache(jPlayer);
} }
if (!getPlayerManager().getPlayersCache().isEmpty()) 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() { 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.ActionBar.CMIActionBar;
import net.Zrips.CMILib.Items.CMIItemStack; import net.Zrips.CMILib.Items.CMIItemStack;
import net.Zrips.CMILib.Logs.CMIDebug; import net.Zrips.CMILib.Logs.CMIDebug;
import net.Zrips.CMILib.Messages.CMIMessages;
import net.Zrips.CMILib.NBT.CMINBT; import net.Zrips.CMILib.NBT.CMINBT;
import net.Zrips.CMILib.Version.Version; import net.Zrips.CMILib.Version.Version;
@ -121,6 +122,27 @@ public class PlayerManager {
} }
public void addPlayerToMap(PlayerInfo info) { 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); playerUUIDMap.put(info.getUuid(), info);
playerIdMap.put(info.getID(), info); playerIdMap.put(info.getID(), info);
playerNameMap.put(info.getName().toLowerCase(), info); playerNameMap.put(info.getName().toLowerCase(), info);
@ -388,7 +410,6 @@ public class PlayerManager {
*/ */
public JobsPlayer getJobsPlayer(String playerName) { public JobsPlayer getJobsPlayer(String playerName) {
playerName = playerName.toLowerCase(); playerName = playerName.toLowerCase();
JobsPlayer jPlayer = players.get(playerName); JobsPlayer jPlayer = players.get(playerName);
return jPlayer != null ? jPlayer : playersCache.get(playerName); return jPlayer != null ? jPlayer : playersCache.get(playerName);
} }
@ -419,13 +440,22 @@ public class PlayerManager {
for (JobsDAOData jobdata : jobs) { for (JobsDAOData jobdata : jobs) {
Job job = Jobs.getJob(jobdata.getJobName()); Job job = Jobs.getJob(jobdata.getJobName());
if (job != null) { 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.progression.add(new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience()));
jPlayer.reloadMaxExperience(); jPlayer.reloadMaxExperience();
jPlayer.reloadLimits(); jPlayer.reloadLimits();
} }
} }
} }
if (points != null) if (points != null)
jPlayer.setPoints(points); jPlayer.setPoints(points);

View File

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

View File

@ -634,6 +634,7 @@ public abstract class JobsDAO {
res = prest.executeQuery(); res = prest.executeQuery();
while (res.next()) { while (res.next()) {
int id = res.getInt(JobsTableFields.userid.getCollumn()); int id = res.getInt(JobsTableFields.userid.getCollumn());
List<JobsDAOData> ls = map.get(id); List<JobsDAOData> ls = map.get(id);
if (ls == null) if (ls == null)
ls = new ArrayList<>(); ls = new ArrayList<>();
@ -644,13 +645,13 @@ public abstract class JobsDAO {
converted = false; converted = false;
} else { } else {
// This should be removed when we switch over to id only method // This should be removed when we switch over to id only method
if (converted) if (converted && res.getString(JobsTableFields.job.getCollumn()) == null || res.getString(JobsTableFields.job.getCollumn()).isEmpty())
if (res.getString(JobsTableFields.job.getCollumn()) == null || res.getString(JobsTableFields.job.getCollumn()).isEmpty()) converted = false;
converted = false;
Job job = Jobs.getJob(jobId); 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()))); ls.add(new JobsDAOData(job.getName(), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn())));
}
} }
map.put(id, ls); map.put(id, ls);
@ -1588,6 +1589,33 @@ public abstract class JobsDAO {
return done; 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 * Record job to archive
* @param player - player that wishes to quit the job * @param player - player that wishes to quit the job
@ -1857,7 +1885,7 @@ public abstract class JobsDAO {
try { try {
prest = conn.prepareStatement("UPDATE `" + getJobsTableName() + "` SET `" + JobsTableFields.level.getCollumn() + "` = ?, `" + JobsTableFields.experience.getCollumn() prest = conn.prepareStatement("UPDATE `" + getJobsTableName() + "` SET `" + JobsTableFields.level.getCollumn() + "` = ?, `" + JobsTableFields.experience.getCollumn()
+ "` = ? WHERE `" + JobsTableFields.userid.getCollumn() + "` = ? AND `" + JobsTableFields.jobid.getCollumn() + "` = ? " + "` = ? WHERE `" + JobsTableFields.userid.getCollumn() + "` = ? AND `" + JobsTableFields.jobid.getCollumn() + "` = ? "
+ "OR `" + JobsTableFields.userid.getCollumn() + "` = ? AND `" + JobsTableFields.jobid.getCollumn() + "` = ?;"); + "OR `" + JobsTableFields.userid.getCollumn() + "` = ? AND `" + JobsTableFields.jobid.getCollumn() + "` = ?;");
for (JobProgression progression : player.getJobProgression()) { for (JobProgression progression : player.getJobProgression()) {
prest.setInt(1, progression.getLevel()); prest.setInt(1, progression.getLevel());
prest.setDouble(2, progression.getExperience()); prest.setDouble(2, progression.getExperience());

View File

@ -110,7 +110,7 @@ public class JobsListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockFromToEvent(BlockFromToEvent event) { public void onBlockFromToEvent(BlockFromToEvent event) {
if (!Jobs.getGCManager().useBlockProtection) if (!Jobs.getGCManager().useBlockProtection)
return; return;
if (!Jobs.getGCManager().ignoreOreGenerators) if (!Jobs.getGCManager().ignoreOreGenerators)
@ -119,7 +119,7 @@ public class JobsListener implements Listener {
return; return;
Jobs.getBpManager().remove(event.getToBlock()); Jobs.getBpManager().remove(event.getToBlock());
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onJoin(PlayerJoinEvent event) { public void onJoin(PlayerJoinEvent event) {
if (Jobs.getGCManager().isShowNewVersion() && event.getPlayer().hasPermission("jobs.versioncheck")) if (Jobs.getGCManager().isShowNewVersion() && event.getPlayer().hasPermission("jobs.versioncheck"))
@ -163,8 +163,7 @@ public class JobsListener implements Listener {
if (!Jobs.getGCManager().MultiServerCompatability()) if (!Jobs.getGCManager().MultiServerCompatability())
Jobs.getPlayerManager().playerJoin(event.getPlayer()); Jobs.getPlayerManager().playerJoin(event.getPlayer());
else { else {
plugin.getServer().getScheduler().runTaskLater(plugin, () -> plugin.getServer().getScheduler().runTaskLater(plugin, () -> Jobs.getPlayerManager().playerJoin(event.getPlayer()), 10L);
Jobs.getPlayerManager().playerJoin(event.getPlayer()), 10L);
} }
} }
@ -182,10 +181,7 @@ public class JobsListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
java.util.concurrent.CompletableFuture.supplyAsync(() -> { Jobs.getPlayerManager().playerQuit(event.getPlayer());
Jobs.getPlayerManager().playerQuit(event.getPlayer());
return true;
});
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
@ -221,7 +217,7 @@ public class JobsListener implements Listener {
} }
player.performCommand("jobs " + command + " " + CMIChatColor.stripColor(plugin.getComplement().getLine(sign, 2)) player.performCommand("jobs " + command + " " + CMIChatColor.stripColor(plugin.getComplement().getLine(sign, 2))
+ " " + CMIChatColor.stripColor(plugin.getComplement() + " " + CMIChatColor.stripColor(plugin.getComplement()
.getLine(sign, 3)).replace(" ", "")); // Replace trailing spaces at 3rd line to parse command .getLine(sign, 3)).replace(" ", "")); // Replace trailing spaces at 3rd line to parse command
} }