From 47280b04d021e2f167394d86f9d6d80ff9017e27 Mon Sep 17 00:00:00 2001 From: montlikadani Date: Mon, 13 Apr 2020 17:44:14 +0200 Subject: [PATCH] Improve database loading --- src/main/java/com/gamingmesh/jobs/Jobs.java | 66 ++++++++++--------- .../com/gamingmesh/jobs/PlayerManager.java | 3 +- .../jobs/commands/list/convert.java | 40 +---------- .../gamingmesh/jobs/config/ConfigManager.java | 8 ++- .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 43 +++++------- 5 files changed, 62 insertions(+), 98 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index f014db38..645ab239 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -59,6 +59,7 @@ import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.io.IOException; +import java.sql.SQLException; import java.util.*; import java.util.Map.Entry; import java.util.logging.Logger; @@ -510,9 +511,31 @@ public class Jobs extends JavaPlugin { /** * Executes close connections */ - public static void ChangeDatabase() { - getDBManager().switchDataBase(); - getPlayerManager().reload(); + public static void convertDatabase() { + try { + List archivelist = dao.convertDatabase(); + + getDBManager().switchDataBase(); + getPlayerManager().reload(); + + dao.truncateAllTables(); + getPlayerManager().convertChacheOfPlayers(true); + + dao.continueConvertions(archivelist); + getPlayerManager().clearMaps(); + getPlayerManager().clearCache(); + + dao.saveExplore(); +// Do we really need to convert Block protection? +// Jobs.getJobsDAO().saveBlockProtection(); + } catch (SQLException e) { + e.printStackTrace(); + Jobs.consoleMsg("&cCan't write data to data base, please send error log to dev's."); + return; + } + + reload(); + loadAllPlayersData(); } /** @@ -592,14 +615,6 @@ public class Jobs extends JavaPlugin { @Override public void onEnable() { instance = this; - setEnabled(true); - - if (instance == null) { - System.out.println("Plugin instance is null. Plugin will be disabled."); - System.out.println("Try restart your server completely. If this not work contact the developers."); - setEnabled(false); - return; - } // itemManager = new ItemManager(this); @@ -625,7 +640,6 @@ public class Jobs extends JavaPlugin { } try { - YmlMaker jobConfig = new YmlMaker(this, "jobConfig.yml"); jobConfig.saveDefaultConfig(); @@ -673,7 +687,6 @@ public class Jobs extends JavaPlugin { getCommandManager().fillCommands(); getDBManager().getDB().triggerTableIdUpdate(); - } catch (Throwable e) { e.printStackTrace(); System.out.println("There was some issues when starting plugin. Please contact dev about this. Plugin will be disabled."); @@ -727,11 +740,7 @@ public class Jobs extends JavaPlugin { getGCManager().reload(); getLanguage().reload(); - try { - getConfigManager().reload(); - } catch (IOException e) { - e.printStackTrace(); - } + getConfigManager().reload(); getDBManager().getDB().loadAllJobsWorlds(); getDBManager().getDB().loadAllJobsNames(); @@ -740,7 +749,7 @@ public class Jobs extends JavaPlugin { ToggleBarHandling.load(); usedSlots.clear(); for (Job job : jobs) { - usedSlots.put(job, dao.getSlotsTaken(job)); + usedSlots.put(job, getDBManager().getDB().getSlotsTaken(job)); } getPlayerManager().reload(); getPermissionHandler().registerPermissions(); @@ -755,7 +764,7 @@ public class Jobs extends JavaPlugin { paymentThread = new BufferedPaymentThread(getGCManager().getEconomyBatchDelay()); paymentThread.start(); - dao.loadPlayerData(); + getDBManager().getDB().loadPlayerData(); // Schedule if (getGCManager().enableSchedule) { @@ -771,17 +780,14 @@ public class Jobs extends JavaPlugin { if (instance == null) return; - try { -// GUIManager.CloseInventories(); -// shopManager.CloseInventories(); - dao.saveExplore(); +// GUIManager.CloseInventories(); +// shopManager.CloseInventories(); + dao.saveExplore(); + + getBpManager().saveCache(); + FurnaceBrewingHandling.save(); + ToggleBarHandling.save(); - getBpManager().saveCache(); - FurnaceBrewingHandling.save(); - ToggleBarHandling.save(); - } catch (Throwable e) { - e.printStackTrace(); - } shutdown(); instance = null; consoleMsg("&e[Jobs] &2Plugin has been disabled successfully."); diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index a590f7fa..76e6be19 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -114,8 +114,7 @@ public class PlayerManager { public void addPlayerToMap(PlayerInfo info) { PlayerUUIDMap.put(info.getUuid(), info); PlayerIDMap.put(info.getID(), info); - if (info.getName() != null) - PlayerNameMap.put(info.getName().toLowerCase(), info); + PlayerNameMap.put(info.getName().toLowerCase(), info); } public void addPlayerToCache(JobsPlayer jPlayer) { diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/convert.java b/src/main/java/com/gamingmesh/jobs/commands/list/convert.java index 78a2838f..77217134 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/convert.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/convert.java @@ -1,8 +1,5 @@ package com.gamingmesh.jobs.commands.list; -import java.sql.SQLException; -import java.util.List; - import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -10,7 +7,6 @@ import org.bukkit.entity.Player; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.commands.JobCommand; -import com.gamingmesh.jobs.container.Convert; import com.gamingmesh.jobs.dao.JobsManager.DataBaseType; public class convert implements Cmd { @@ -28,41 +24,7 @@ public class convert implements Cmd { return true; } - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { - @Override - public void run() { - List archivelist = null; - try { - archivelist = Jobs.getJobsDAO().convertDatabase(); - } catch (SQLException e) { - e.printStackTrace(); - Jobs.consoleMsg("&cCan't write data to data base, please send error log to dev's."); - return; - } - - Jobs.ChangeDatabase(); - - try { - Jobs.getJobsDAO().truncateAllTables(); - Jobs.getPlayerManager().convertChacheOfPlayers(true); - - Jobs.getJobsDAO().continueConvertions(archivelist); - Jobs.getPlayerManager().clearMaps(); - Jobs.getPlayerManager().clearCache(); - - Jobs.getJobsDAO().saveExplore(); -// Do we really need to convert Block protection? -// Jobs.getJobsDAO().saveBlockProtection(); - } catch (SQLException e) { - e.printStackTrace(); - Jobs.consoleMsg("&cCan't write data to data base, please send error log to dev's."); - return; - } - - Jobs.reload(); - Jobs.loadAllPlayersData(); - } - }); + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> Jobs.convertDatabase()); String from = "MySQL"; String to = "SQLite"; diff --git a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java index ad3b2b36..cfe1f287 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java @@ -269,9 +269,13 @@ public class ConfigManager { cfg.save(); } - public void reload() throws IOException { + public void reload() { // job settings - loadJobSettings(); + try { + loadJobSettings(); + } catch (IOException e) { + e.printStackTrace(); + } } public YamlConfiguration getJobConfig() { diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 79acdd67..577a1d29 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -649,21 +649,20 @@ public abstract class JobsDAO { res = prest.executeQuery(); while (res.next()) { int id = res.getInt(JobsTableFields.userid.getCollumn()); - List ls = map.get(id); - if (ls == null) - ls = new ArrayList<>(); + String jobName = res.getString(JobsTableFields.job.getCollumn()); + List ls = new ArrayList<>(); int jobId = res.getInt(JobsTableFields.jobid.getCollumn()); if (jobId == 0) { - ls.add(new JobsDAOData(res.getString(JobsTableFields.job.getCollumn()), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); + ls.add(new JobsDAOData(jobName, res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); 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 (jobName == null || jobName.isEmpty()) converted = false; - Job job = Jobs.getJob(jobId); + Job job = Jobs.getJob(jobName); if (job != null) ls.add(new JobsDAOData(job.getName(), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); } @@ -1479,13 +1478,7 @@ public abstract class JobsDAO { String jobName = res.getString(ArchiveTableFields.job.getCollumn()); int jobid = res.getInt(ArchiveTableFields.jobid.getCollumn()); - Job job = null; - if (jobid != 0) { - job = Jobs.getJob(jobid); - } else { - job = Jobs.getJob(jobName); - } - + Job job = jobid != 0 ? Jobs.getJob(jobid) : Jobs.getJob(jobName); if (job == null) continue; @@ -1740,27 +1733,26 @@ public abstract class JobsDAO { public void loadPlayerData() { Jobs.getPlayerManager().clearMaps(); + JobsConnection conn = getConnection(); if (conn == null) return; + PreparedStatement prest = null; ResultSet res = null; try { prest = conn.prepareStatement("SELECT * FROM `" + DBTables.UsersTable.getTableName() + "`;"); res = prest.executeQuery(); while (res.next()) { - long seen = System.currentTimeMillis(); - try { - seen = res.getLong(UserTableFields.seen.getCollumn()); - Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo( - res.getString(UserTableFields.username.getCollumn()), - res.getInt("id"), - UUID.fromString(res.getString(UserTableFields.player_uuid.getCollumn())), - seen, - res.getInt(UserTableFields.donequests.getCollumn()), - res.getString(UserTableFields.quests.getCollumn()))); - } catch (Exception e) { - } + long seen = res.getLong(UserTableFields.seen.getCollumn()); + + Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo( + res.getString(UserTableFields.username.getCollumn()), + res.getInt("id"), + UUID.fromString(res.getString(UserTableFields.player_uuid.getCollumn())), + seen, + res.getInt(UserTableFields.donequests.getCollumn()), + res.getString(UserTableFields.quests.getCollumn()))); } } catch (SQLException e) { e.printStackTrace(); @@ -1768,6 +1760,7 @@ public abstract class JobsDAO { close(res); close(prest); } + return; }