From 8eed14b765b3456ec9e0a162c9e79655cbed7dfd Mon Sep 17 00:00:00 2001 From: Zrips Date: Tue, 18 Jul 2017 19:16:23 +0300 Subject: [PATCH] Data base conversion with one command. More data are being converted --- src/main/java/com/gamingmesh/jobs/Jobs.java | 17 +- .../com/gamingmesh/jobs/PlayerManager.java | 34 ++- .../jobs/commands/list/convert.java | 28 +-- .../jobs/config/ExploreManager.java | 9 + .../jobs/config/GeneralConfigManager.java | 1 - .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 197 ++++++++++++------ .../com/gamingmesh/jobs/dao/JobsManager.java | 14 +- 7 files changed, 216 insertions(+), 84 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index bcead591..b848a96c 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -150,7 +150,7 @@ public class Jobs extends JavaPlugin { private static NMS nms; private static ActionBar actionbar; - private boolean running = false; + private static boolean running = false; protected static VersionChecker versionCheckManager; @@ -523,7 +523,15 @@ public class Jobs extends JavaPlugin { e1.printStackTrace(); } - Bukkit.getScheduler().runTaskAsynchronously(this, new Runnable() { + loadAllPlayersData(); + // add all online players + for (Player online : Bukkit.getServer().getOnlinePlayers()) { + Jobs.getPlayerManager().playerJoin(online); + } + } + + public static void loadAllPlayersData() { + Bukkit.getScheduler().runTaskAsynchronously(Jobs.getInstance(), new Runnable() { @Override public void run() { int i = 0; @@ -533,6 +541,7 @@ public class Jobs extends JavaPlugin { // Cloning to avoid issues HashMap temp = new HashMap(Jobs.getPlayerManager().getPlayersInfoUUIDMap()); Iterator> it = temp.entrySet().iterator(); + Jobs.consoleMsg("map size: " + temp.size()); while (it.hasNext()) { Entry one = it.next(); if (!running) @@ -557,10 +566,6 @@ public class Jobs extends JavaPlugin { return; } }); - // add all online players - for (Player online : Bukkit.getServer().getOnlinePlayers()) { - Jobs.getPlayerManager().playerJoin(online); - } } /** diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index 2a53241e..8c6a2ed5 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -88,6 +88,13 @@ public class PlayerManager { PlayerNameMap.clear(); } + public void clearCache() { + playersCache.clear(); + playersUUIDCache.clear(); + players.clear(); + playersUUID.clear(); + } + public void addPlayerToMap(PlayerInfo info) { this.PlayerUUIDMap.put(info.getUuid(), info); this.PlayerIDMap.put(info.getID(), info); @@ -211,13 +218,34 @@ public class PlayerManager { } } } - + /** * Save all the information of all of the players */ - public void saveEntireChacheOfPlayers() { + public void convertChacheOfPlayers(boolean resetID) { + int y = 0; + int i = 0; + int total = playersUUIDCache.size(); for (Entry one : playersUUIDCache.entrySet()) { - one.getValue().save(); + JobsPlayer jPlayer = one.getValue(); + if (resetID) + jPlayer.setUserId(-1); + JobsDAO dao = Jobs.getJobsDAO(); + dao.updateSeen(jPlayer); + if (jPlayer.getUserId() == -1) + continue; + for (JobProgression oneJ : jPlayer.getJobProgression()) { + dao.insertJob(jPlayer, oneJ); + } + dao.saveLog(jPlayer); + dao.savePoints(jPlayer); + dao.recordPlayersLimits(jPlayer); + i++; + y++; + if (y >= 1000) { + Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Saved " + i + "/" + total + " players data"); + y = 0; + } } } 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 4826e401..41ec8766 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/convert.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/convert.java @@ -11,6 +11,7 @@ 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; import com.gamingmesh.jobs.stuff.ChatColor; public class convert implements Cmd { @@ -29,29 +30,34 @@ public class convert implements Cmd { return true; } - List list = null; List archivelist = null; try { - list = Jobs.getJobsDAO().convertDatabase("jobs"); archivelist = Jobs.getJobsDAO().convertDatabase("archive"); } catch (SQLException e) { e.printStackTrace(); sender.sendMessage(ChatColor.RED + "Can't read data from data base, please send error log to dev's."); return true; } - + Jobs.ChangeDatabase(); - - if (list == null & archivelist == null) + + if (archivelist == null) return false; try { - Jobs.getJobsDAO().continueConvertions(list, "jobs"); + Jobs.getJobsDAO().truncateAllTables(); + Jobs.getPlayerManager().convertChacheOfPlayers(true); + Jobs.getJobsDAO().continueConvertions(archivelist, "archive"); - Jobs.getJobsDAO().transferUsers(); + Jobs.getPlayerManager().clearMaps(); + Jobs.getPlayerManager().clearCache(); + + Jobs.getJobsDAO().saveExplore(false); + Jobs.getJobsDAO().saveBlockProtection(); + Jobs.loadAllPlayersData(); } catch (SQLException e) { e.printStackTrace(); - sender.sendMessage(ChatColor.RED + "Can't write data to data base, please send error log to dev's."); + Jobs.consoleMsg("&cCan't write data to data base, please send error log to dev's."); return true; } @@ -64,14 +70,12 @@ public class convert implements Cmd { String from = "MysSQL"; String to = "SqLite"; - if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) { + if (Jobs.getDBManager().getDbType().equals(DataBaseType.SqLite)) { from = "SqLite"; to = "MySQL"; } - sender.sendMessage(ChatColor.GOLD + "Data base was converted from " + ChatColor.GREEN + from + ChatColor.GOLD + " to " + ChatColor.GREEN + to + ChatColor.GOLD - + "! Now you can stop the server, change storage-method to " + ChatColor.GREEN + to + ChatColor.GOLD - + " in general config file and start server again on your new database system."); + Jobs.consoleMsg("&eData base was converted from &2" + from + " &eto &2" + to + "&e!"); return true; } diff --git a/src/main/java/com/gamingmesh/jobs/config/ExploreManager.java b/src/main/java/com/gamingmesh/jobs/config/ExploreManager.java index 7980c9bd..5851cba2 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ExploreManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ExploreManager.java @@ -1,6 +1,7 @@ package com.gamingmesh.jobs.config; import java.util.HashMap; +import java.util.Map.Entry; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -49,6 +50,14 @@ public class ExploreManager { return worlds; } + public int getSize() { + int i = 0; + for (Entry one : this.getWorlds().entrySet()) { + i += one.getValue().getChunks().size(); + } + return i; + } + public ExploreRespond ChunkRespond(Player player, Chunk chunk, boolean isNew) { return ChunkRespond(player.getName(), chunk.getWorld().getName(), chunk.getX(), chunk.getZ(), isNew); } diff --git a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java index d0500b43..085e3585 100644 --- a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -112,7 +112,6 @@ public class GeneralConfigManager { public double TaxesAmount; public String SoundLevelupSound, SoundTitleChangeSound, ServerAcountName, ServertaxesAcountName; public ArrayList keys; - public String storageMethod; public boolean hideJobsInfoWithoutPermission; public boolean UseTaxes; public boolean TransferToServerAccount; diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index e33f34fc..02fc750c 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -513,6 +513,12 @@ public abstract class JobsDAO { return true; } + public void truncateAllTables() { + for (DBTables one : DBTables.values()) { + this.truncate(one.getTableName()); + } + } + public DataBaseType getDbType() { return dbType; } @@ -625,33 +631,33 @@ public abstract class JobsDAO { } } - private void loadAllSavedJobs() { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + prefix + "jobs`;"); - res = prest.executeQuery(); - while (res.next()) { - int id = res.getInt("userid"); - ArrayList list = map.get(id); - if (list == null) { - list = new ArrayList(); - list.add(new JobsDAOData(res.getString("job"), res.getInt("level"), res.getInt("experience"))); - map.put(id, list); - } else { - list.add(new JobsDAOData(res.getString("job"), res.getInt("level"), res.getInt("experience"))); - } - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - } +// private void loadAllSavedJobs() { +// JobsConnection conn = getConnection(); +// if (conn == null) +// return; +// PreparedStatement prest = null; +// ResultSet res = null; +// try { +// prest = conn.prepareStatement("SELECT * FROM `" + prefix + "jobs`;"); +// res = prest.executeQuery(); +// while (res.next()) { +// int id = res.getInt("userid"); +// ArrayList list = map.get(id); +// if (list == null) { +// list = new ArrayList(); +// list.add(new JobsDAOData(res.getString("job"), res.getInt("level"), res.getInt("experience"))); +// map.put(id, list); +// } else { +// list.add(new JobsDAOData(res.getString("job"), res.getInt("level"), res.getInt("experience"))); +// } +// } +// } catch (SQLException e) { +// e.printStackTrace(); +// } finally { +// close(res); +// close(prest); +// } +// } public void recordNewPlayer(Player player) { recordNewPlayer((OfflinePlayer) player); @@ -870,6 +876,33 @@ public abstract class JobsDAO { } } + /** + * Join a job (create player-job entry from storage) + * @param player - player that wishes to join the job + * @param job - job that the player wishes to join + */ + public synchronized void insertJob(JobsPlayer jPlayer, JobProgression prog) { + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest = null; + try { + int exp = (int) prog.getExperience(); + if (exp < 0) + exp = 0; + prest = conn.prepareStatement("INSERT INTO `" + prefix + "jobs` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);"); + prest.setInt(1, jPlayer.getUserId()); + prest.setString(2, prog.getJob().getName()); + prest.setInt(3, prog.getLevel()); + prest.setInt(4, exp); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } + } + /** * Join a job (create player-job entry from storage) * @param player - player that wishes to join the job @@ -914,7 +947,7 @@ public abstract class JobsDAO { int i = list.size(); try { statement = conns.createStatement(); - if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) { + if (Jobs.getDBManager().getDbType().toString().equalsIgnoreCase("sqlite")) { statement.executeUpdate("TRUNCATE `" + getPrefix() + table + "`"); } else { statement.executeUpdate("DELETE from `" + getPrefix() + table + "`"); @@ -940,38 +973,38 @@ public abstract class JobsDAO { } } - public void transferUsers() throws SQLException { - JobsConnection conns = this.getConnection(); - if (conns == null) - return; - PreparedStatement insert = null; - Statement statement = null; - try { - statement = conns.createStatement(); - if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) { - statement.executeUpdate("TRUNCATE `" + getPrefix() + "users`"); - } else { - statement.executeUpdate("DELETE from `" + getPrefix() + "users`"); - } - - insert = conns.prepareStatement("INSERT INTO `" + getPrefix() + "users` (`id`, `player_uuid`, `username`, `seen`) VALUES (?, ?, ?, ?);"); - conns.setAutoCommit(false); - - for (Entry oneUser : Jobs.getPlayerManager().getPlayersCache().entrySet()) { - insert.setInt(1, oneUser.getValue().getUserId()); - insert.setString(2, oneUser.getValue().getPlayerUUID().toString()); - insert.setString(3, oneUser.getValue().getUserName()); - insert.setLong(4, oneUser.getValue().getSeen() == null ? System.currentTimeMillis() : oneUser.getValue().getSeen()); - insert.addBatch(); - } - insert.executeBatch(); - conns.commit(); - conns.setAutoCommit(true); - } finally { - close(statement); - close(insert); - } - } +// public void transferUsers() throws SQLException { +// JobsConnection conns = this.getConnection(); +// if (conns == null) +// return; +// PreparedStatement insert = null; +// Statement statement = null; +// try { +// statement = conns.createStatement(); +// if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) { +// statement.executeUpdate("TRUNCATE `" + getPrefix() + "users`"); +// } else { +// statement.executeUpdate("DELETE from `" + getPrefix() + "users`"); +// } +// +// insert = conns.prepareStatement("INSERT INTO `" + getPrefix() + "users` (`id`, `player_uuid`, `username`, `seen`) VALUES (?, ?, ?, ?);"); +// conns.setAutoCommit(false); +// +// for (Entry oneUser : Jobs.getPlayerManager().getPlayersCache().entrySet()) { +// insert.setInt(1, oneUser.getValue().getUserId()); +// insert.setString(2, oneUser.getValue().getPlayerUUID().toString()); +// insert.setString(3, oneUser.getValue().getUserName()); +// insert.setLong(4, oneUser.getValue().getSeen() == null ? System.currentTimeMillis() : oneUser.getValue().getSeen()); +// insert.addBatch(); +// } +// insert.executeBatch(); +// conns.commit(); +// conns.setAutoCommit(true); +// } finally { +// close(statement); +// close(insert); +// } +// } /** * Quit a job (delete player-job entry from storage) @@ -1320,6 +1353,10 @@ public abstract class JobsDAO { } public void updateSeen(JobsPlayer player) { + if (player.getUserId() == -1) { + insertPlayer(player); + return; + } JobsConnection conn = getConnection(); if (conn == null) return; @@ -1336,6 +1373,40 @@ public abstract class JobsDAO { } } + private void insertPlayer(JobsPlayer player) { + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prestt = null; + try { + prestt = conn.prepareStatement("INSERT INTO `" + prefix + "users` (`player_uuid`, `username`, `seen`) VALUES (?, ?, ?);"); + prestt.setString(1, player.getPlayerUUID().toString()); + prestt.setString(2, player.getUserName()); + prestt.setLong(3, player.getSeen()); + prestt.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prestt); + } + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT `id` FROM `" + prefix + "users` WHERE `player_uuid` = ?;"); + prest.setString(1, player.getPlayerUUID().toString()); + res = prest.executeQuery(); + res.next(); + int id = res.getInt("id"); + player.setUserId(id); + Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo(player.getUserName(), id, player.getPlayerUUID(), player.getSeen())); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + } + public void savePoints(JobsPlayer jPlayer) { JobsConnection conn = getConnection(); if (conn == null) @@ -1670,6 +1741,10 @@ public abstract class JobsDAO { * @param jobexplore - the information getting saved */ public void saveExplore() { + saveExplore(true); + } + + public void saveExplore(boolean ignoreOld) { if (!Jobs.getExplore().isExploreEnabled()) return; @@ -1687,7 +1762,7 @@ public abstract class JobsDAO { for (Entry worlds : temp.entrySet()) { for (Entry oneChunk : worlds.getValue().getChunks().entrySet()) { - if (!oneChunk.getValue().isNew()) + if (!oneChunk.getValue().isNew() && ignoreOld) continue; for (String oneuser : oneChunk.getValue().getPlayers()) { prest2.setString(1, worlds.getKey()); diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsManager.java b/src/main/java/com/gamingmesh/jobs/dao/JobsManager.java index fe8bcd30..6c586afe 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsManager.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsManager.java @@ -1,6 +1,7 @@ package com.gamingmesh.jobs.dao; import java.io.File; +import java.io.IOException; import org.bukkit.configuration.file.YamlConfiguration; @@ -24,7 +25,7 @@ public class JobsManager { return dao; } - public void switchDataBase() { + public void switchDataBase() { if (dao != null) dao.closeConnections(); switch (DbType) { @@ -39,6 +40,17 @@ public class JobsManager { dao.setDbType(DbType); break; } + + File f = new File(plugin.getDataFolder(), "generalConfig.yml"); + YamlConfiguration config = YamlConfiguration.loadConfiguration(f); + + config.set("storage.method", DbType.toString()); + try { + config.save(f); + } catch (IOException e) { + e.printStackTrace(); + } + Jobs.setDAO(dao); }