From 27bc14f21463ef0db67640fe60dfe1f6556f1513 Mon Sep 17 00:00:00 2001 From: Zrips Date: Mon, 11 May 2020 16:10:19 +0300 Subject: [PATCH] Correct way to remove old explore data --- .../jobs/container/ExploreChunk.java | 21 +++++++----- .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 34 ++++++++++++++----- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/container/ExploreChunk.java b/src/main/java/com/gamingmesh/jobs/container/ExploreChunk.java index a55ee023..ba2c94c0 100644 --- a/src/main/java/com/gamingmesh/jobs/container/ExploreChunk.java +++ b/src/main/java/com/gamingmesh/jobs/container/ExploreChunk.java @@ -12,9 +12,9 @@ public class ExploreChunk { private int x; private int z; private Set playerIds = new HashSet<>(); - private boolean full = false; - private Integer dbId = null; - private boolean updated = false; + private Boolean full; + private Integer dbId; + private Boolean updated; public ExploreChunk(int playerId, int x, int z) { this(x, z); @@ -28,7 +28,7 @@ public class ExploreChunk { } public ExploreRespond addPlayer(int playerId) { - if (full) { + if (isFullyExplored()) { return new ExploreRespond(Jobs.getExplore().getPlayerAmount() + 1, false); } boolean newChunkForPlayer = false; @@ -50,13 +50,13 @@ public class ExploreChunk { } public boolean isAlreadyVisited(int playerId) { - if (full) + if (isFullyExplored()) return true; return playerIds.contains(playerId); } public int getCount() { - if (full) + if (isFullyExplored()) return Jobs.getExplore().getPlayerAmount(); return playerIds.size(); } @@ -131,14 +131,17 @@ public class ExploreChunk { } public boolean isUpdated() { - return updated; + return updated == null ? false : updated; } public void setUpdated(boolean updated) { - this.updated = updated; + if (!updated) + this.updated = null; + else + this.updated = true; } public boolean isFullyExplored() { - return full; + return full == null ? false : full; } } diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index dc7ce20e..90383ddb 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -6,8 +6,10 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map.Entry; +import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -1748,7 +1750,7 @@ public abstract class JobsDAO { String uuid = res.getString(UserTableFields.player_uuid.getCollumn()); if (uuid == null || uuid.isEmpty()) { PreparedStatement ps = conn.prepareStatement("DELETE FROM `" + DBTables.UsersTable.getTableName() - + "` WHERE `" + UserTableFields.player_uuid.getCollumn() + "` = ?;"); + + "` WHERE `" + UserTableFields.player_uuid.getCollumn() + "` = ?;"); ps.setString(1, uuid); ps.execute(); continue; @@ -2469,18 +2471,25 @@ public abstract class JobsDAO { try { prest = conn.prepareStatement("SELECT * FROM `" + DBTables.ExploreDataTable.getTableName() + "`;"); res = prest.executeQuery(); + Set missingWorlds = new HashSet(); while (res.next()) { - String worldName = res.getString(ExploreDataTableFields.worldname.getCollumn()); - if (worldName == null || Bukkit.getWorld(worldName) == null) { - PreparedStatement prest2 = null; - prest2 = conn.prepareStatement("DELETE FROM `" + DBTables.ExploreDataTable.getTableName() + "` WHERE `" + ExploreDataTableFields.worldname.getCollumn() + "` = ?;"); - prest2.setString(1, worldName); - prest2.execute(); - close(prest2); + int worldId = res.getInt(ExploreDataTableFields.worldid.getCollumn()); + JobsWorld jworld = Util.getJobsWorld(worldId); + if (jworld == null || jworld.getWorld() == null) { + missingWorlds.add(worldId); } else { Jobs.getExplore().load(res); } } + + for (Integer one : missingWorlds) { + PreparedStatement prest2 = null; + prest2 = conn.prepareStatement("DELETE FROM `" + DBTables.ExploreDataTable.getTableName() + "` WHERE `" + ExploreDataTableFields.worldid.getCollumn() + "` = ?;"); + prest2.setInt(1, one); + prest2.execute(); + close(prest2); + } + } catch (SQLException e) { e.printStackTrace(); } finally { @@ -2677,6 +2686,15 @@ public abstract class JobsDAO { } } + protected static void close(PreparedStatement stmt) { + if (stmt != null) + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + public HashMap> getMap() { return map; }