From 75ca0929df76e1e6ed93ca100a8c02e8c9ef5854 Mon Sep 17 00:00:00 2001 From: Zrips Date: Mon, 25 Sep 2023 16:36:00 +0300 Subject: [PATCH] Option to disable explorer database saving --- src/main/java/com/gamingmesh/jobs/Jobs.java | 18 +- .../jobs/config/ExploreManager.java | 164 +++++++++--------- .../jobs/config/GeneralConfigManager.java | 7 +- .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 36 ++-- 4 files changed, 117 insertions(+), 108 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index d2652eab..e6f55167 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -365,13 +365,13 @@ public final class Jobs extends JavaPlugin { */ public static ScheduleManager getScheduleManager() { if (scheduleManager == null) - scheduleManager = new ScheduleManager(getInstance()); + scheduleManager = new ScheduleManager(getInstance()); return scheduleManager; } public static NameTranslatorManager getNameTranslatorManager() { - if (nameTranslatorManager == null) - nameTranslatorManager = new NameTranslatorManager(); + if (nameTranslatorManager == null) + nameTranslatorManager = new NameTranslatorManager(); return nameTranslatorManager; } @@ -383,7 +383,7 @@ public final class Jobs extends JavaPlugin { public static JobsCommands getCommandManager() { if (cManager == null) - cManager = new JobsCommands(getInstance()); + cManager = new JobsCommands(getInstance()); return cManager; } @@ -410,8 +410,8 @@ public final class Jobs extends JavaPlugin { * @return the sign manager */ public static SignUtil getSignUtil() { - if (signManager == null) - signManager = new SignUtil(getInstance()); + if (signManager == null) + signManager = new SignUtil(getInstance()); return signManager; } @@ -586,7 +586,8 @@ public final class Jobs extends JavaPlugin { getPlayerManager().clearMaps(); getPlayerManager().clearCache(); - dao.saveExplore(); + if (Jobs.getGeneralConfigManager().ExploreSaveIntoDatabase) + dao.saveExplore(); // Do we really need to convert Block protection? // Jobs.getJobsDAO().saveBlockProtection(); } catch (SQLException e) { @@ -890,9 +891,8 @@ public final class Jobs extends JavaPlugin { CMIMessages.consoleMessage(prefix); HandlerList.unregisterAll(this); - if (dao != null) { + if (dao != null && Jobs.getGeneralConfigManager().ExploreSaveIntoDatabase) dao.saveExplore(); - } blockOwnerShipsMaterial.values().forEach(BlockOwnerShip::save); ToggleBarHandling.save(); diff --git a/src/main/java/com/gamingmesh/jobs/config/ExploreManager.java b/src/main/java/com/gamingmesh/jobs/config/ExploreManager.java index f9cf4e75..1b0d62fd 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ExploreManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ExploreManager.java @@ -26,140 +26,140 @@ public class ExploreManager { private int playerAmount = 1; public int getPlayerAmount() { - return playerAmount; + return playerAmount; } public void setPlayerAmount(int amount) { - if (playerAmount < amount) - playerAmount = amount; + if (playerAmount < amount) + playerAmount = amount; } public boolean isExploreEnabled() { - return exploreEnabled; + return exploreEnabled; } public void setExploreEnabled() { - if (!exploreEnabled) { - exploreEnabled = true; - } + exploreEnabled = true; } public void load() { - if (!exploreEnabled) - return; + if (!exploreEnabled) + return; - CMIMessages.consoleMessage("&eLoading explorer data"); - Long time = System.currentTimeMillis(); - Jobs.getJobsDAO().loadExplore(); - int size = getSize(); - CMIMessages.consoleMessage("&eLoaded explorer data" + (size != 0 ? " (&6" + size + "&e)" : " ") + " in " + (System.currentTimeMillis() - time) + " ms"); + if (Jobs.getGeneralConfigManager().ExploreSaveIntoDatabase) { + CMIMessages.consoleMessage("&eLoading explorer data"); + Long time = System.currentTimeMillis(); + Jobs.getJobsDAO().loadExplore(); + int size = getSize(); + CMIMessages.consoleMessage("&eLoaded explorer data" + (size != 0 ? " (&6" + size + "&e)" : " ") + " in " + (System.currentTimeMillis() - time) + " ms"); + } } public Map> getWorlds() { - return worlds; + return worlds; } public int getSize() { - int i = 0; - for (Map one : worlds.values()) { - for (Entry chunks : one.entrySet()) { - i += chunks.getValue().getChunks().size(); - } - } - return i; + int i = 0; + for (Map one : worlds.values()) { + for (Entry chunks : one.entrySet()) { + i += chunks.getValue().getChunks().size(); + } + } + return i; } public ExploreRespond chunkRespond(Player player, Chunk chunk) { - return chunkRespond(Jobs.getPlayerManager().getJobsPlayer(player).getUserId(), chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); + return chunkRespond(Jobs.getPlayerManager().getJobsPlayer(player).getUserId(), chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); } public ExploreRespond chunkRespond(int playerId, Chunk chunk) { - return chunkRespond(playerId, chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); + return chunkRespond(playerId, chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); } public ExploreRespond chunkRespond(int playerId, String world, int x, int z) { - Map eRegions = worlds.getOrDefault(world, new HashMap()); + Map eRegions = worlds.getOrDefault(world, new HashMap()); - int RegionX = (int) Math.floor(x / 32D); - int RegionZ = (int) Math.floor(z / 32D); + int RegionX = (int) Math.floor(x / 32D); + int RegionZ = (int) Math.floor(z / 32D); - ExploreRegion region = eRegions.get(RegionX + ":" + RegionZ); - if (region == null) { - region = new ExploreRegion(RegionX, RegionZ); - } + ExploreRegion region = eRegions.get(RegionX + ":" + RegionZ); + if (region == null) { + region = new ExploreRegion(RegionX, RegionZ); + } - int chunkRelativeX = (RegionX * 32) - x; - int chunkRelativeZ = (RegionZ * 32) - z; + int chunkRelativeX = (RegionX * 32) - x; + int chunkRelativeZ = (RegionZ * 32) - z; - ExploreChunk chunk = region.getChunk(chunkRelativeX, chunkRelativeZ); - if (chunk == null) { - chunk = new ExploreChunk(); - region.addChunk(chunkRelativeX, chunkRelativeZ, chunk); - } + ExploreChunk chunk = region.getChunk(chunkRelativeX, chunkRelativeZ); + if (chunk == null) { + chunk = new ExploreChunk(); + region.addChunk(chunkRelativeX, chunkRelativeZ, chunk); + } - eRegions.put(RegionX + ":" + RegionZ, region); + eRegions.put(RegionX + ":" + RegionZ, region); - worlds.put(world, eRegions); + worlds.put(world, eRegions); - return chunk.addPlayer(playerId); + return chunk.addPlayer(playerId); } public void load(ResultSet res) { - try { - String worldName = res.getString(ExploreDataTableFields.worldname.getCollumn()); + try { + String worldName = res.getString(ExploreDataTableFields.worldname.getCollumn()); - JobsWorld jobsWorld = Util.getJobsWorld(worldName); - if (jobsWorld == null) - jobsWorld = Util.getJobsWorld(res.getInt(ExploreDataTableFields.worldid.getCollumn())); + JobsWorld jobsWorld = Util.getJobsWorld(worldName); + if (jobsWorld == null) + jobsWorld = Util.getJobsWorld(res.getInt(ExploreDataTableFields.worldid.getCollumn())); - if (jobsWorld == null) - return; + if (jobsWorld == null) + return; - int x = res.getInt(ExploreDataTableFields.chunkX.getCollumn()); - int z = res.getInt(ExploreDataTableFields.chunkZ.getCollumn()); - String names = res.getString(ExploreDataTableFields.playerNames.getCollumn()); - int id = res.getInt("id"); + int x = res.getInt(ExploreDataTableFields.chunkX.getCollumn()); + int z = res.getInt(ExploreDataTableFields.chunkZ.getCollumn()); + String names = res.getString(ExploreDataTableFields.playerNames.getCollumn()); + int id = res.getInt("id"); - Map eRegions = worlds.getOrDefault(jobsWorld.getName(), new HashMap()); + Map eRegions = worlds.getOrDefault(jobsWorld.getName(), new HashMap()); - int RegionX = (int) Math.floor(x / 32D); - int RegionZ = (int) Math.floor(z / 32D); + int RegionX = (int) Math.floor(x / 32D); + int RegionZ = (int) Math.floor(z / 32D); - int chunkRelativeX = RegionX * 32 - x; - int chunkRelativeZ = RegionZ * 32 - z; + int chunkRelativeX = RegionX * 32 - x; + int chunkRelativeZ = RegionZ * 32 - z; - ExploreRegion region = eRegions.get(RegionX + ":" + RegionZ); - if (region == null) { - region = new ExploreRegion(RegionX, RegionZ); - } - ExploreChunk chunk = region.getChunk(chunkRelativeX, chunkRelativeZ); - if (chunk == null) { - chunk = new ExploreChunk(); - region.addChunk(chunkRelativeX, chunkRelativeZ, chunk); - } - chunk.deserializeNames(names); - chunk.setDbId(id); + ExploreRegion region = eRegions.get(RegionX + ":" + RegionZ); + if (region == null) { + region = new ExploreRegion(RegionX, RegionZ); + } + ExploreChunk chunk = region.getChunk(chunkRelativeX, chunkRelativeZ); + if (chunk == null) { + chunk = new ExploreChunk(); + region.addChunk(chunkRelativeX, chunkRelativeZ, chunk); + } + chunk.deserializeNames(names); + chunk.setDbId(id); - eRegions.put(RegionX + ":" + RegionZ, region); - worlds.put(jobsWorld.getName(), eRegions); + eRegions.put(RegionX + ":" + RegionZ, region); + worlds.put(jobsWorld.getName(), eRegions); - } catch (SQLException e) { - e.printStackTrace(); - } + } catch (SQLException e) { + e.printStackTrace(); + } } public void resetRegion(String worldname) { - CMIMessages.consoleMessage("&eReseting explorer data. World: " + worldname); + CMIMessages.consoleMessage("&eReseting explorer data. World: " + worldname); - Map> worlds = getWorlds(); - worlds.put(worldname, new HashMap()); + Map> worlds = getWorlds(); + worlds.put(worldname, new HashMap()); - boolean r = Jobs.getJobsDAO().deleteExploredWorld(worldname); - if (!r) { - CMIMessages.consoleMessage("&eFailed in DAO."); - return; - } + boolean r = Jobs.getJobsDAO().deleteExploredWorld(worldname); + if (!r) { + CMIMessages.consoleMessage("&eFailed in DAO."); + return; + } - CMIMessages.consoleMessage("&eCompleted to reset explorer data."); + CMIMessages.consoleMessage("&eCompleted to reset explorer data."); } } diff --git a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java index 981ff700..42e636a2 100644 --- a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -107,7 +107,7 @@ public class GeneralConfigManager { DisableJoiningJobThroughGui, FireworkLevelupUse, UseRandom, UsePerPermissionForLeaving, EnableConfirmation, jobsInfoOpensBrowse, MonsterDamageUse, useMaxPaymentCurve, blockOwnershipTakeOver, hideJobsInfoWithoutPermission, UseTaxes, TransferToServerAccount, TakeFromPlayersPayment, AutoJobJoinUse, AllowDelevel, RomanNumbers, - BossBarEnabled = false, BossBarShowOnEachAction = false, BossBarsMessageByDefault = false, ExploreCompact, DBCleaningJobsUse, DBCleaningUsersUse, + BossBarEnabled = false, BossBarShowOnEachAction = false, BossBarsMessageByDefault = false, ExploreCompact, ExploreSaveIntoDatabase = false, DBCleaningJobsUse, DBCleaningUsersUse, DisabledWorldsUse, UseAsWhiteListWorldList, MythicMobsEnabled, LoggingUse, payForCombiningItems, BlastFurnacesReassign = false, SmokerReassign = false, payForStackedEntities, payForAbove = false, payForEachVTradeItem, allowEnchantingBoostedItems, bossBarAsync = false, preventShopItemEnchanting; @@ -427,6 +427,11 @@ public class GeneralConfigManager { "By setting this to true when there is max amount of players explored a chunk then it will be marked as fully explored and exact players who explored it will not be saved to save some memory"); ExploreCompact = c.get("Optimizations.Explore.Compact", true); + c.addComment("Optimizations.Explore.SaveIntoDatabase", + "While enabled explored chunk data will be saved into database and will persist over server restarts", + "While disabled expored chunk data resets on every server startup which will freeup memory and speedup server startups and stop in some cases"); + ExploreSaveIntoDatabase = c.get("Optimizations.Explore.SaveIntoDatabase", false); + c.addComment("Logging.Use", "With this set to true all players jobs actions will be logged to database for easy to see statistics", "This is still in development and in future it will expand"); LoggingUse = c.get("Logging.Use", false); diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 514a8349..b75b401a 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -2558,6 +2558,8 @@ public abstract class JobsDAO { PreparedStatement prest2 = null; try { + CMIMessages.consoleMessage("&e[Jobs] Preparing explorer data save."); + prest2 = conn.prepareStatement("INSERT INTO `" + DBTables.ExploreDataTable.getTableName() + "` (`" + ExploreDataTableFields.worldid.getCollumn() + "`, `" + ExploreDataTableFields.chunkX.getCollumn() + "`, `" + ExploreDataTableFields.chunkZ.getCollumn() @@ -2570,25 +2572,27 @@ public abstract class JobsDAO { Map> temp = new HashMap<>(Jobs.getExploreManager().getWorlds()); for (Entry> worlds : temp.entrySet()) { + JobsWorld jobsWorld = Util.getJobsWorld(worlds.getKey()); + + int id = jobsWorld == null ? 0 : jobsWorld.getId(); + if (id == 0) + continue; + for (Entry region : worlds.getValue().entrySet()) { - JobsWorld jobsWorld = Util.getJobsWorld(worlds.getKey()); + for (Entry oneChunk : region.getValue().getChunks().entrySet()) { + ExploreChunk chunk = oneChunk.getValue(); + if (chunk.getDbId() != -1) + continue; - int id = jobsWorld == null ? 0 : jobsWorld.getId(); - if (id != 0) - for (Entry oneChunk : region.getValue().getChunks().entrySet()) { - ExploreChunk chunk = oneChunk.getValue(); - if (chunk.getDbId() != -1) - continue; + prest2.setInt(1, id); + prest2.setInt(2, region.getValue().getChunkGlobalX(oneChunk.getKey())); + prest2.setInt(3, region.getValue().getChunkGlobalZ(oneChunk.getKey())); + prest2.setString(4, chunk.serializeNames()); + prest2.setString(5, jobsWorld != null ? jobsWorld.getName() : ""); + prest2.addBatch(); - prest2.setInt(1, id); - prest2.setInt(2, region.getValue().getChunkGlobalX(oneChunk.getKey())); - prest2.setInt(3, region.getValue().getChunkGlobalZ(oneChunk.getKey())); - prest2.setString(4, chunk.serializeNames()); - prest2.setString(5, jobsWorld != null ? jobsWorld.getName() : ""); - prest2.addBatch(); - - i++; - } + i++; + } } } prest2.executeBatch();