diff --git a/com/gamingmesh/jobs/Jobs.java b/com/gamingmesh/jobs/Jobs.java index 87fee3ba..1ea7dfe3 100644 --- a/com/gamingmesh/jobs/Jobs.java +++ b/com/gamingmesh/jobs/Jobs.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Map.Entry; import java.util.WeakHashMap; import java.util.logging.Logger; @@ -52,6 +53,7 @@ import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.JobInfo; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; +import com.gamingmesh.jobs.container.PlayerInfo; import com.gamingmesh.jobs.container.FastPayment; import com.gamingmesh.jobs.dao.JobsDAO; import com.gamingmesh.jobs.economy.BufferedEconomy; @@ -430,36 +432,40 @@ public class Jobs { * Executes startup * @throws IOException */ - public static void startup() throws IOException { - reload(); - - // add all online players - if (!Jobs.getGCManager().MultiServerCompatability() && Jobs.getGCManager().PreLoadUse) { - int i = 0; - long time = System.currentTimeMillis(); - for (OfflinePlayer offline : Bukkit.getServer().getOfflinePlayers()) { - - try { - if (offline.isOnline()) - continue; - - long lastPlayed = offline.getLastPlayed(); - int dif = (int) ((time - lastPlayed) / 1000 / 60 / 60 / 24); - if (dif >= 7) - continue; - - JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayerOffline(offline); - if (jPlayer == null) - continue; - Jobs.getJobsDAO().loadLog(jPlayer); - Jobs.getPlayerManager().getPlayersCache().put(offline.getName().toLowerCase(), jPlayer); - } catch (Exception e) { - } - i++; - } - Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Preloaded " + i + " players data from last week"); + public static void startup(JobsPlugin plugin) { + try { + reload(); + } catch (IOException e1) { + e1.printStackTrace(); } + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + int i = 0; + int y = 0; + int total = Jobs.getPlayerManager().getPlayerMap().size(); + long time = System.currentTimeMillis(); + for (Entry one : Jobs.getPlayerManager().getPlayerMap().entrySet()) { + try { + JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayerOffline(one); + if (jPlayer == null) + continue; + Jobs.getPlayerManager().getPlayersCache().put(one.getValue().getName().toLowerCase(), jPlayer); + } catch (Exception e) { + } + i++; + y++; + if (y >= 1000) { + Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loaded " + i + "/" + total + " players data"); + y = 0; + } + } + Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Preloaded " + i + " players data in " + ((int) (((System.currentTimeMillis() - time) + / 1000d) * 100) / 100D)); + return; + } + }); // add all online players for (Player online : Bukkit.getServer().getOnlinePlayers()) { Jobs.getPlayerManager().playerJoin(online); diff --git a/com/gamingmesh/jobs/JobsPlugin.java b/com/gamingmesh/jobs/JobsPlugin.java index 5c6f044c..e8e58adc 100644 --- a/com/gamingmesh/jobs/JobsPlugin.java +++ b/com/gamingmesh/jobs/JobsPlugin.java @@ -18,7 +18,6 @@ package com.gamingmesh.jobs; -import java.io.IOException; import java.lang.reflect.InvocationTargetException; import org.bukkit.Bukkit; import org.bukkit.command.ConsoleCommandSender; @@ -96,7 +95,7 @@ public class JobsPlugin extends JavaPlugin { this.getCommand("jobs").setTabCompleter(new TabComplete()); - Jobs.startup(); + Jobs.startup(this); // register the listeners getServer().getPluginManager().registerEvents(new JobsListener(this), this); @@ -132,7 +131,7 @@ public class JobsPlugin extends JavaPlugin { Jobs.getJobsDAO().loadExplore(); Jobs.getCommandManager().fillCommands(); - } catch (IOException e) { + } catch (Exception e) { System.out.println("There was some issues when starting plugin. Please contact dev about this. Plugin will be disabled."); this.setEnabled(false); e.printStackTrace(); diff --git a/com/gamingmesh/jobs/PermissionHandler.java b/com/gamingmesh/jobs/PermissionHandler.java index 39750a3d..01c82646 100644 --- a/com/gamingmesh/jobs/PermissionHandler.java +++ b/com/gamingmesh/jobs/PermissionHandler.java @@ -48,10 +48,9 @@ public class PermissionHandler { if (jPlayer == null) return; - Player player = this.plugin.getServer().getPlayer(jPlayer.getPlayerUUID()); - if (player == null) { - return; - } + Player player = jPlayer.getPlayer(); + if (player == null) + return; boolean changed = false; diff --git a/com/gamingmesh/jobs/PlayerManager.java b/com/gamingmesh/jobs/PlayerManager.java index 507db09f..7a641876 100644 --- a/com/gamingmesh/jobs/PlayerManager.java +++ b/com/gamingmesh/jobs/PlayerManager.java @@ -49,6 +49,7 @@ import com.gamingmesh.jobs.dao.JobsDAO; import com.gamingmesh.jobs.dao.JobsDAOData; import com.gamingmesh.jobs.economy.PointsData; import com.gamingmesh.jobs.stuff.ChatColor; +import com.gamingmesh.jobs.stuff.Debug; import com.gamingmesh.jobs.stuff.PerformCommands; import com.gamingmesh.jobs.stuff.Perm; @@ -118,10 +119,13 @@ public class PlayerManager { public void playerJoin(Player player) { JobsPlayer jPlayer = this.playersCache.get(player.getName().toLowerCase()); if (jPlayer == null) { - jPlayer = JobsPlayer.loadFromDao(Jobs.getJobsDAO(), player); - JobsPlayer.loadLogFromDao(jPlayer); + Debug.D("not in cache"); + jPlayer = Jobs.getJobsDAO().loadFromDao(player); + jPlayer.loadLogFromDao(); this.playersCache.put(player.getName().toLowerCase(), jPlayer); - } + } else + + Debug.D("in cache"); this.players.put(player.getName().toLowerCase(), jPlayer); jPlayer.setPlayer(player); @@ -140,7 +144,7 @@ public class PlayerManager { if (Jobs.getGCManager().saveOnDisconnect()) { JobsPlayer jPlayer = this.players.remove(player.getName().toLowerCase()); if (jPlayer != null) { - jPlayer.save(Jobs.getJobsDAO()); + jPlayer.save(); jPlayer.onDisconnect(); } } else { @@ -155,8 +159,6 @@ public class PlayerManager { * Save all the information of all of the players in the game */ public void saveAll() { - JobsDAO dao = Jobs.getJobsDAO(); - /* * Saving is a three step process to minimize synchronization locks when called asynchronously. * @@ -168,7 +170,7 @@ public class PlayerManager { list = new ArrayList(this.players.values()); for (JobsPlayer jPlayer : list) { - jPlayer.save(dao); + jPlayer.save(); } Iterator iter = this.players.values().iterator(); @@ -239,10 +241,48 @@ public class PlayerManager { Jobs.getJobsDAO().loadPoints(jPlayer); - JobsPlayer.loadLogFromDao(jPlayer); + jPlayer.loadLogFromDao(); + return jPlayer; } + + /** + * Get the player job info for specific player + * @param player - the player who's job you're getting + * @return the player job info of the player + */ + public JobsPlayer getJobsPlayerOffline(Entry info) { + + if (info == null) + return null; + if (info.getValue().getName() == null) + return null; + + JobsPlayer jPlayer = new JobsPlayer(info.getValue().getName(), null); + jPlayer.setPlayerUUID(UUID.fromString(info.getKey())); + jPlayer.setUserId(info.getValue().getID()); + + List list = Jobs.getJobsDAO().getAllJobs(info.getValue().getName(), jPlayer.getPlayerUUID()); + for (JobsDAOData jobdata : list) { + if (Jobs.getJob(jobdata.getJobName()) == null) + continue; + Job job = Jobs.getJob(jobdata.getJobName()); + if (job == null) + continue; + JobProgression jobProgression = new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience()); + jPlayer.progression.add(jobProgression); + jPlayer.reloadMaxExperience(); + jPlayer.reloadLimits(); + } + + Jobs.getJobsDAO().loadPoints(jPlayer); + + jPlayer.loadLogFromDao(); + + return jPlayer; + } + /** * Causes player to join their job * @param jPlayer @@ -253,7 +293,7 @@ public class PlayerManager { if (jPlayer.isInJob(job)) return; // let the user join the job - if (!jPlayer.joinJob(job, jPlayer)) + if (!jPlayer.joinJob(job)) return; // JobsJoin event @@ -324,7 +364,7 @@ public class PlayerManager { */ public void transferJob(JobsPlayer jPlayer, Job oldjob, Job newjob) { // synchronized (jPlayer.saveLock) { - if (!jPlayer.transferJob(oldjob, newjob, jPlayer)) + if (!jPlayer.transferJob(oldjob, newjob)) return; JobsDAO dao = Jobs.getJobsDAO(); @@ -332,7 +372,7 @@ public class PlayerManager { oldjob.updateTotalPlayers(); dao.joinJob(jPlayer, newjob); newjob.updateTotalPlayers(); - jPlayer.save(dao); + jPlayer.save(); // } } @@ -344,8 +384,8 @@ public class PlayerManager { */ public void promoteJob(JobsPlayer jPlayer, Job job, int levels) { // synchronized (jPlayer.saveLock) { - jPlayer.promoteJob(job, levels, jPlayer); - jPlayer.save(Jobs.getJobsDAO()); + jPlayer.promoteJob(job, levels); + jPlayer.save(); Jobs.getSignUtil().SignUpdate(job.getName()); Jobs.getSignUtil().SignUpdate("gtoplist"); @@ -361,7 +401,7 @@ public class PlayerManager { public void demoteJob(JobsPlayer jPlayer, Job job, int levels) { // synchronized (jPlayer.saveLock) { jPlayer.demoteJob(job, levels); - jPlayer.save(Jobs.getJobsDAO()); + jPlayer.save(); Jobs.getSignUtil().SignUpdate(job.getName()); Jobs.getSignUtil().SignUpdate("gtoplist"); // } @@ -382,7 +422,7 @@ public class PlayerManager { if (prog.addExperience(experience)) performLevelUp(jPlayer, job, oldLevel); - jPlayer.save(Jobs.getJobsDAO()); + jPlayer.save(); Jobs.getSignUtil().SignUpdate(job.getName()); Jobs.getSignUtil().SignUpdate("gtoplist"); // } @@ -401,7 +441,7 @@ public class PlayerManager { return; prog.addExperience(-experience); - jPlayer.save(Jobs.getJobsDAO()); + jPlayer.save(); Jobs.getSignUtil().SignUpdate(job.getName()); Jobs.getSignUtil().SignUpdate("gtoplist"); // } diff --git a/com/gamingmesh/jobs/commands/list/.gitignore b/com/gamingmesh/jobs/commands/list/.gitignore index 53e3466a..2f5c209e 100644 --- a/com/gamingmesh/jobs/commands/list/.gitignore +++ b/com/gamingmesh/jobs/commands/list/.gitignore @@ -45,3 +45,4 @@ /editpoints.class /blockinfo.class /test.class +/purge.class diff --git a/com/gamingmesh/jobs/commands/list/convert.java b/com/gamingmesh/jobs/commands/list/convert.java index 4be891bc..9cd6329a 100644 --- a/com/gamingmesh/jobs/commands/list/convert.java +++ b/com/gamingmesh/jobs/commands/list/convert.java @@ -20,7 +20,7 @@ public class convert implements Cmd { @JobCommand(2600) public boolean perform(JobsPlugin plugin, final CommandSender sender, final String[] args) { - if (!(sender instanceof Player)) { + if (sender instanceof Player) { sender.sendMessage(Jobs.getLanguage().getMessage("general.error.fromconsole")); return false; } diff --git a/com/gamingmesh/jobs/config/GeneralConfigManager.java b/com/gamingmesh/jobs/config/GeneralConfigManager.java index 8278bd9b..44189178 100644 --- a/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -141,7 +141,6 @@ public class GeneralConfigManager { public Parser maxPointEquation; public boolean DisabledWorldsUse; - public boolean PreLoadUse; public List DisabledWorldsList = new ArrayList(); public List BoostSchedule = new ArrayList(); @@ -441,11 +440,7 @@ public class GeneralConfigManager { "Only commands can be performed from disabled worlds with jobs.disabledworld.commands permission node"); DisabledWorldsUse = c.get("Optimizations.DisabledWorlds.Use", false); DisabledWorldsList = c.getStringList("Optimizations.DisabledWorlds.List", Arrays.asList(Bukkit.getWorlds().get(0).getName())); - - - c.getW().addComment("Optimizations.PreLoad.Use", "By setting this to true, Jobs plugin will preload some of recent players data to be used instead of loading them from data base on players join"); - PreLoadUse = c.get("Optimizations.PreLoad.Use", false); - + // c.getW().addComment("Optimizations.Purge.Use", "By setting this to true, Jobs plugin will clean data base on startup from all jobs with level 1 and at 0 exp"); // PurgeUse = c.get("Optimizations.Purge.Use", false); diff --git a/com/gamingmesh/jobs/container/JobsPlayer.java b/com/gamingmesh/jobs/container/JobsPlayer.java index 80648e43..2286a935 100644 --- a/com/gamingmesh/jobs/container/JobsPlayer.java +++ b/com/gamingmesh/jobs/container/JobsPlayer.java @@ -30,7 +30,6 @@ import org.bukkit.entity.Player; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.dao.JobsDAO; -import com.gamingmesh.jobs.dao.JobsDAOData; import com.gamingmesh.jobs.resources.jfep.Parser; import com.gamingmesh.jobs.stuff.ChatColor; import com.gamingmesh.jobs.stuff.Perm; @@ -39,7 +38,7 @@ public class JobsPlayer { // the player the object belongs to private String userName; // progression of the player in each job - private UUID playerUUID; + public UUID playerUUID; public ArrayList progression = new ArrayList(); private HashMap> boostCounter = new HashMap>(); @@ -73,75 +72,15 @@ public class JobsPlayer { public JobsPlayer(String userName, OfflinePlayer player) { this.userName = userName; this.OffPlayer = player; - } - - public static JobsPlayer loadFromDao(JobsDAO dao, OfflinePlayer player) { - - JobsPlayer jPlayer = new JobsPlayer(player.getName(), player); - jPlayer.playerUUID = player.getUniqueId(); - List list = dao.getAllJobs(player); -// synchronized (jPlayer.saveLock) { - jPlayer.progression.clear(); - for (JobsDAOData jobdata : list) { - if (Jobs.getJob(jobdata.getJobName()) == null) - continue; - // add the job - Job job = Jobs.getJob(jobdata.getJobName()); - if (job == null) - continue; - - // create the progression object - JobProgression jobProgression = new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience()); - // calculate the max level - // add the progression level. - jPlayer.progression.add(jobProgression); - - } - jPlayer.reloadMaxExperience(); - jPlayer.reloadLimits(); - jPlayer.setUserId(Jobs.getPlayerManager().getPlayerMap().get(player.getUniqueId().toString()).getID()); - Jobs.getJobsDAO().loadPoints(jPlayer); -// } - return jPlayer; - } - - public static JobsPlayer loadFromDao(JobsDAO dao, Player player) { - - JobsPlayer jPlayer = new JobsPlayer(player.getName(), player); - jPlayer.playerUUID = player.getUniqueId(); - List list = new ArrayList(); - list = dao.getAllJobs(player); -// synchronized (jPlayer.saveLock) { - jPlayer.progression.clear(); - for (JobsDAOData jobdata : list) { - if (Jobs.getJob(jobdata.getJobName()) == null) - continue; - // add the job - Job job = Jobs.getJob(jobdata.getJobName()); - if (job == null) - continue; - - // create the progression object - JobProgression jobProgression = new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience()); - // calculate the max level - // add the progression level. - jPlayer.progression.add(jobProgression); - - } - jPlayer.reloadMaxExperience(); - jPlayer.reloadLimits(); - jPlayer.setUserId(Jobs.getPlayerManager().getPlayerMap().get(player.getUniqueId().toString()).getID()); - Jobs.getJobsDAO().loadPoints(jPlayer); -// } - return jPlayer; + this.player = Bukkit.getPlayer(userName); } public void setPlayer(Player p) { this.player = p; } - public static void loadLogFromDao(JobsPlayer jPlayer) { - Jobs.getJobsDAO().loadLog(jPlayer); + public void loadLogFromDao() { + Jobs.getJobsDAO().loadLog(this); } public synchronized List getUpdateBossBarFor() { @@ -388,16 +327,16 @@ public class JobsPlayer { * Player joins a job * @param job - the job joined */ - public boolean joinJob(Job job, JobsPlayer jPlayer) { + public boolean joinJob(Job job) { // synchronized (saveLock) { if (!isInJob(job)) { int level = 1; int exp = 0; - if (Jobs.getJobsDAO().checkArchive(jPlayer, job).size() > 0) { - List info = Jobs.getJobsDAO().checkArchive(jPlayer, job); + if (Jobs.getJobsDAO().checkArchive(this, job).size() > 0) { + List info = Jobs.getJobsDAO().checkArchive(this, job); level = info.get(0); //exp = info.get(1); - Jobs.getJobsDAO().deleteArchive(jPlayer, job); + Jobs.getJobsDAO().deleteArchive(this, job); } progression.add(new JobProgression(job, this, level, exp)); @@ -448,7 +387,7 @@ public class JobsPlayer { * @param job - the job being promoted * @param levels - number of levels to promote */ - public void promoteJob(Job job, int levels, JobsPlayer player) { + public void promoteJob(Job job, int levels) { // synchronized (saveLock) { JobProgression prog = getJobProgression(job); if (prog == null) @@ -459,7 +398,7 @@ public class JobsPlayer { int maxLevel = job.getMaxLevel(); - if (player.havePermission("jobs." + job.getName() + ".vipmaxlevel") && job.getVipMaxLevel() != 0) + if (this.havePermission("jobs." + job.getName() + ".vipmaxlevel") && job.getVipMaxLevel() != 0) maxLevel = job.getVipMaxLevel(); if (maxLevel > 0 && newLevel > maxLevel) { @@ -513,7 +452,7 @@ public class JobsPlayer { * @param oldjob - the old job * @param newjob - the new job */ - public boolean transferJob(Job oldjob, Job newjob, JobsPlayer jPlayer) { + public boolean transferJob(Job oldjob, Job newjob) { // synchronized (saveLock) { if (!isInJob(newjob)) { for (JobProgression prog : progression) { @@ -523,7 +462,7 @@ public class JobsPlayer { prog.setJob(newjob); int maxLevel = 0; - if (jPlayer.havePermission("jobs." + newjob.getName() + ".vipmaxlevel")) + if (this.havePermission("jobs." + newjob.getName() + ".vipmaxlevel")) maxLevel = newjob.getVipMaxLevel(); else maxLevel = newjob.getMaxLevel(); @@ -649,9 +588,10 @@ public class JobsPlayer { * Performs player save * @param dao */ - public void save(JobsDAO dao) { + public void save() { // synchronized (saveLock) { if (!isSaved()) { + JobsDAO dao = Jobs.getJobsDAO(); dao.save(this); dao.saveLog(this); dao.savePoints(this); diff --git a/com/gamingmesh/jobs/dao/JobsDAO.java b/com/gamingmesh/jobs/dao/JobsDAO.java index 7bbb17e8..4537dd2e 100644 --- a/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/com/gamingmesh/jobs/dao/JobsDAO.java @@ -162,24 +162,53 @@ public abstract class JobsDAO { if (conn == null) return jobs; + PreparedStatement prest = null; ResultSet res = null; try { - PreparedStatement prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "jobs` WHERE `userid` = ?;"); + prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "jobs` WHERE `userid` = ?;"); prest.setInt(1, id); res = prest.executeQuery(); while (res.next()) { - jobs.add(new JobsDAOData(uuid, res.getString(1), res.getInt(2), res.getInt(3))); + jobs.add(new JobsDAOData(res.getString(1), res.getInt(2), res.getInt(3))); } - prest.close(); } catch (SQLException e) { e.printStackTrace(); } finally { - if (res != null) - try { - res.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + close(res); + close(prest); + } + return jobs; + } + + /** + * Get all jobs the player is part of. + * @param playerUUID - the player being searched for + * @return list of all of the names of the jobs the players are part of. + */ + public List getAllJobs(JobsPlayer jPlayer) { + + ArrayList jobs = new ArrayList(); + + int id = jPlayer.getUserId(); + + JobsConnection conn = getConnection(); + if (conn == null) + return jobs; + + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "jobs` WHERE `userid` = ?;"); + prest.setInt(1, id); + res = prest.executeQuery(); + while (res.next()) { + jobs.add(new JobsDAOData(res.getString(1), res.getInt(2), res.getInt(3))); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); } return jobs; } @@ -196,26 +225,31 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return; + PreparedStatement prestt = null; try { - PreparedStatement prest = conn.prepareStatement("INSERT INTO `" + prefix + "users` (`player_uuid`, `username`) VALUES (?, ?);"); - prest.setString(1, uuid.toString()); - prest.setString(2, playerName); - prest.executeUpdate(); - prest.close(); + prestt = conn.prepareStatement("INSERT INTO `" + prefix + "users` (`player_uuid`, `username`) VALUES (?, ?);"); + prestt.setString(1, uuid.toString()); + prestt.setString(2, playerName); + prestt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(prestt); } + PreparedStatement prest = null; + ResultSet res = null; try { - PreparedStatement prest = conn.prepareStatement("SELECT `id` FROM `" + this.prefix + "users` WHERE `player_uuid` = ?;"); + prest = conn.prepareStatement("SELECT `id` FROM `" + this.prefix + "users` WHERE `player_uuid` = ?;"); prest.setString(1, uuid.toString()); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); res.next(); int id = res.getInt("id"); Jobs.getPlayerManager().getPlayerMap().put(uuid.toString(), new PlayerInfo(playerName, id)); - res.close(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } } @@ -229,24 +263,20 @@ public abstract class JobsDAO { if (conn == null) return 0; int count = 0; + PreparedStatement prest = null; ResultSet res = null; try { - PreparedStatement prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs` WHERE `job` = ?;"); + prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs` WHERE `job` = ?;"); prest.setString(1, JobName); res = prest.executeQuery(); while (res.next()) { count = res.getInt(1); } - prest.close(); } catch (SQLException e) { e.printStackTrace(); } finally { - if (res != null) - try { - res.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + close(res); + close(prest); } return count; } @@ -260,16 +290,19 @@ public abstract class JobsDAO { if (conn == null) return 0; int count = 0; + PreparedStatement prest = null; + ResultSet res = null; try { - PreparedStatement prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs`;"); - ResultSet res = prest.executeQuery(); + prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs`;"); + res = prest.executeQuery(); while (res.next()) { count = res.getInt(1); } - res.close(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } return count; } @@ -290,17 +323,20 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return jobs; + PreparedStatement prest = null; + ResultSet res = null; try { - PreparedStatement prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "jobs` WHERE `userid` = ?;"); + prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "jobs` WHERE `userid` = ?;"); prest.setInt(1, info.getValue().getID()); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); while (res.next()) { - jobs.add(new JobsDAOData(UUID.fromString(info.getKey()), res.getString(2), res.getInt(3), res.getInt(4))); + jobs.add(new JobsDAOData(res.getString(2), res.getInt(3), res.getInt(4))); } - prest.close(); - res.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } return jobs; } @@ -314,6 +350,7 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return; + PreparedStatement prest = null; try { int level = 1; int exp = 0; @@ -322,15 +359,16 @@ public abstract class JobsDAO { level = info.get(0); deleteArchive(jPlayer, job); } - PreparedStatement prest = conn.prepareStatement("INSERT INTO `" + prefix + "jobs` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);"); + prest = conn.prepareStatement("INSERT INTO `" + prefix + "jobs` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);"); prest.setInt(1, jPlayer.getUserId()); prest.setString(2, job.getName()); prest.setInt(3, level); prest.setInt(4, exp); prest.execute(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(prest); } } @@ -346,16 +384,19 @@ public abstract class JobsDAO { return null; List list = new ArrayList(); + PreparedStatement prest = null; + ResultSet res = null; try { - PreparedStatement prest = conn.prepareStatement("SELECT * FROM `" + prefix + table + "`"); - ResultSet res = prest.executeQuery(); + prest = conn.prepareStatement("SELECT * FROM `" + prefix + table + "`"); + res = prest.executeQuery(); while (res.next()) { list.add(new Convert(res.getInt("id"), res.getInt("userid"), res.getString("job"), res.getInt("level"), res.getInt("experience"))); } - res.close(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } try { @@ -371,10 +412,10 @@ public abstract class JobsDAO { if (conns == null) return; PreparedStatement insert = null; + Statement statement = null; int i = list.size(); try { - - Statement statement = conns.createStatement(); + statement = conns.createStatement(); if (Jobs.getGCManager().storageMethod.equalsIgnoreCase("sqlite")) { statement.executeUpdate("TRUNCATE `" + getPrefix() + table + "`"); } else { @@ -395,14 +436,9 @@ public abstract class JobsDAO { insert.executeBatch(); conns.commit(); conns.setAutoCommit(true); - statement.close(); } finally { - if (insert != null) { - try { - insert.close(); - } catch (SQLException e) { - } - } + close(statement); + close(insert); } } @@ -415,14 +451,16 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return; + PreparedStatement prest = null; try { - PreparedStatement prest = conn.prepareStatement("DELETE FROM `" + prefix + "jobs` WHERE `userid` = ? AND `job` = ?;"); + prest = conn.prepareStatement("DELETE FROM `" + prefix + "jobs` WHERE `userid` = ? AND `job` = ?;"); prest.setInt(1, jPlayer.getUserId()); prest.setString(2, job.getName()); prest.execute(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(prest); } } @@ -435,6 +473,7 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return; + PreparedStatement prest = null; try { int level = 1; int exp = 0; @@ -444,16 +483,16 @@ public abstract class JobsDAO { exp = (int) progression.getExperience(); } } - PreparedStatement prest = conn.prepareStatement("INSERT INTO `" + prefix - + "archive` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);"); + prest = conn.prepareStatement("INSERT INTO `" + prefix + "archive` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);"); prest.setInt(1, jPlayer.getUserId()); prest.setString(2, job.getName()); prest.setInt(3, level); prest.setInt(4, exp); prest.execute(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(prest); } } @@ -467,11 +506,13 @@ public abstract class JobsDAO { List info = new ArrayList(); if (conn == null) return info; + PreparedStatement prest = null; + ResultSet res = null; try { - PreparedStatement prest = conn.prepareStatement("SELECT `level`, `experience` FROM `" + prefix + "archive` WHERE `userid` = ? AND `job` = ?;"); + prest = conn.prepareStatement("SELECT `level`, `experience` FROM `" + prefix + "archive` WHERE `userid` = ? AND `job` = ?;"); prest.setInt(1, jPlayer.getUserId()); prest.setString(2, job.getName()); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); if (res.next()) { int level = (int) ((res.getInt(1) - (res.getInt(1) * (Jobs.getGCManager().levelLossPercentage / 100.0)))); if (level < 1) @@ -488,11 +529,11 @@ public abstract class JobsDAO { info.add(level); info.add(res.getInt(2)); } - res.close(); - prest.close(); - return info; } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } return info; } @@ -512,12 +553,14 @@ public abstract class JobsDAO { List names = new ArrayList(); if (conn == null) - return null; + return names; + PreparedStatement prest = null; + ResultSet res = null; try { - PreparedStatement prest = conn.prepareStatement("SELECT userid, COUNT(*) AS amount, sum(level) AS totallvl FROM `" + prefix + prest = conn.prepareStatement("SELECT userid, COUNT(*) AS amount, sum(level) AS totallvl FROM `" + prefix + "jobs` GROUP BY userid ORDER BY totallvl DESC LIMIT " + start + ",20;"); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); while (res.next()) { @@ -533,11 +576,11 @@ public abstract class JobsDAO { names.add(top); } - res.close(); - prest.close(); - return names; } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } return names; } @@ -551,11 +594,13 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return null; + List info = new ArrayList(); + PreparedStatement prest = null; + ResultSet res = null; try { - List info = new ArrayList(); - PreparedStatement prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "archive` WHERE `userid` = ?;"); + prest = conn.prepareStatement("SELECT `job`, `level`, `experience` FROM `" + prefix + "archive` WHERE `userid` = ?;"); prest.setInt(1, jPlayer.getUserId()); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); while (res.next()) { int level = (int) ((res.getInt(2) - (res.getInt(2) * (Jobs.getGCManager().levelLossPercentage / 100.0)))); @@ -579,13 +624,13 @@ public abstract class JobsDAO { info.add(res.getString(1) + ":" + res.getInt(2) + ":" + level + ":" + res.getInt(3)); } - res.close(); - prest.close(); - return info; } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } - return null; + return info; } public PlayerInfo loadPlayerData(UUID uuid) { @@ -594,24 +639,20 @@ public abstract class JobsDAO { if (conn == null) return pInfo; PreparedStatement prest = null; + ResultSet res = null; try { prest = conn.prepareStatement("SELECT * FROM `" + prefix + "users` WHERE `player_uuid` = ?;"); prest.setString(1, uuid.toString()); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); while (res.next()) { pInfo = new PlayerInfo(res.getString("username"), res.getInt("id")); Jobs.getPlayerManager().getPlayerMap().put(res.getString("player_uuid"), pInfo); } - res.close(); } catch (SQLException e) { e.printStackTrace(); } finally { - if (prest != null) - try { - prest.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + close(res); + close(prest); } return pInfo; } @@ -622,23 +663,70 @@ public abstract class JobsDAO { if (conn == null) return; PreparedStatement prest = null; + ResultSet res = null; try { prest = conn.prepareStatement("SELECT * FROM `" + prefix + "users`;"); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); while (res.next()) { Jobs.getPlayerManager().getPlayerMap().put(res.getString("player_uuid"), new PlayerInfo(res.getString("username"), res.getInt("id"))); } - res.close(); - return; } catch (SQLException e) { e.printStackTrace(); } finally { - if (prest != null) - try { - prest.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + close(res); + close(prest); + } + return; + } + + public JobsPlayer loadFromDao(OfflinePlayer player) { + + JobsPlayer jPlayer = new JobsPlayer(player.getName(), player); + jPlayer.playerUUID = player.getUniqueId(); + List list = getAllJobs(player); +// synchronized (jPlayer.saveLock) { + jPlayer.progression.clear(); + for (JobsDAOData jobdata : list) { + if (Jobs.getJob(jobdata.getJobName()) == null) + continue; + // add the job + Job job = Jobs.getJob(jobdata.getJobName()); + if (job == null) + continue; + + // create the progression object + JobProgression jobProgression = new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience()); + // calculate the max level + // add the progression level. + jPlayer.progression.add(jobProgression); + + } + jPlayer.reloadMaxExperience(); + jPlayer.reloadLimits(); + jPlayer.setUserId(Jobs.getPlayerManager().getPlayerMap().get(player.getUniqueId().toString()).getID()); + Jobs.getJobsDAO().loadPoints(jPlayer); +// } + return jPlayer; + } + + public void loadAllData() { + Jobs.getPlayerManager().getPlayerMap().clear(); + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + prefix + "users`;"); + res = prest.executeQuery(); + while (res.next()) { + Jobs.getPlayerManager().getPlayerMap().put(res.getString("player_uuid"), new PlayerInfo(res.getString("username"), res.getInt("id"))); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); } return; } @@ -652,14 +740,16 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return; + PreparedStatement prest = null; try { - PreparedStatement prest = conn.prepareStatement("DELETE FROM `" + prefix + "archive` WHERE `userid` = ? AND `job` = ?;"); + prest = conn.prepareStatement("DELETE FROM `" + prefix + "archive` WHERE `userid` = ? AND `job` = ?;"); prest.setInt(1, jPlayer.getUserId()); prest.setString(2, job.getName()); prest.execute(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(prest); } } @@ -671,8 +761,9 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return; + PreparedStatement prest = null; try { - PreparedStatement prest = conn.prepareStatement("UPDATE `" + prefix + prest = conn.prepareStatement("UPDATE `" + prefix + "jobs` SET `level` = ?, `experience` = ? WHERE `userid` = ? AND `job` = ?;"); for (JobProgression progression : player.getJobProgression()) { prest.setInt(1, progression.getLevel()); @@ -681,9 +772,10 @@ public abstract class JobsDAO { prest.setString(4, progression.getJob().getName()); prest.execute(); } - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(prest); } } @@ -691,24 +783,23 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return; + PreparedStatement prest = null; try { PlayerPoints pointInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(player.getPlayerUUID()); - String req = "UPDATE `" + prefix + "points` SET `totalpoints` = ?, `currentpoints` = ? WHERE `userid` = ?;"; if (pointInfo.isNewEntry()) { pointInfo.setNewEntry(false); req = "INSERT INTO `" + prefix + "points` (`totalpoints`, `currentpoints`, `userid`) VALUES (?, ?, ?);"; } - - PreparedStatement prest = conn.prepareStatement(req); - + prest = conn.prepareStatement(req); prest.setDouble(1, pointInfo.getTotalPoints()); prest.setDouble(2, pointInfo.getCurrentPoints()); prest.setInt(3, player.getUserId()); prest.execute(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(prest); } } @@ -716,20 +807,23 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return; + PreparedStatement prest = null; + ResultSet res = null; try { - PreparedStatement prest = conn.prepareStatement("SELECT `totalpoints`, `currentpoints` FROM `" + prefix + "points` WHERE `userid` = ?;"); + prest = conn.prepareStatement("SELECT `totalpoints`, `currentpoints` FROM `" + prefix + "points` WHERE `userid` = ?;"); prest.setInt(1, player.getUserId()); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); if (res.next()) { Jobs.getPlayerManager().getPointsData().addPlayer(player.getPlayerUUID(), res.getDouble("currentpoints"), res.getDouble("totalpoints")); } else { Jobs.getPlayerManager().getPointsData().addPlayer(player.getPlayerUUID()); } - res.close(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } } @@ -785,18 +879,8 @@ public abstract class JobsDAO { } catch (SQLException e) { e.printStackTrace(); } finally { - if (prest1 != null) - try { - prest1.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - if (prest2 != null) - try { - prest2.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + close(prest1); + close(prest2); } } @@ -809,32 +893,24 @@ public abstract class JobsDAO { if (conn == null) return; PreparedStatement prest = null; + ResultSet res = null; try { int time = TimeManage.timeInInt(); prest = conn.prepareStatement("SELECT * FROM `" + prefix + "log` WHERE `userid` = ? AND `time` = ? ;"); prest.setInt(1, player.getUserId()); prest.setInt(2, time); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); while (res.next()) { Jobs.getLoging().loadToLog(player, res.getString("action"), res.getString("itemname"), res.getInt("count"), res.getDouble("money"), res.getDouble("exp")); } - res.close(); } catch (Exception e) { - if (prest != null) - try { - prest.close(); - } catch (SQLException e1) { - e1.printStackTrace(); - } + close(res); + close(prest); this.dropDataBase("log"); this.createDefaultLogBase(); } finally { - if (prest != null) - try { - prest.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + close(res); + close(prest); } } @@ -849,9 +925,10 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return; + PreparedStatement prest2 = null; try { - PreparedStatement prest2 = conn.prepareStatement("INSERT INTO `" + prefix + "explore` (`worldname`, `chunkX`, `chunkZ`, `playerName`) VALUES (?, ?, ?, ?);"); + prest2 = conn.prepareStatement("INSERT INTO `" + prefix + "explore` (`worldname`, `chunkX`, `chunkZ`, `playerName`) VALUES (?, ?, ?, ?);"); conn.setAutoCommit(false); int i = 0; for (Entry worlds : Jobs.getExplore().getWorlds().entrySet()) { @@ -871,7 +948,6 @@ public abstract class JobsDAO { prest2.executeBatch(); conn.commit(); conn.setAutoCommit(true); - prest2.close(); if (i > 0) { String message = ChatColor.translateAlternateColorCodes('&', "&e[Jobs] Saved " + i + " new explorer entries."); @@ -880,6 +956,8 @@ public abstract class JobsDAO { } } catch (SQLException e) { e.printStackTrace(); + } finally { + close(prest2); } } @@ -894,17 +972,19 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return; + PreparedStatement prest = null; + ResultSet res = null; try { - - PreparedStatement prest = conn.prepareStatement("SELECT * FROM `" + prefix + "explore`;"); - ResultSet res = prest.executeQuery(); + prest = conn.prepareStatement("SELECT * FROM `" + prefix + "explore`;"); + res = prest.executeQuery(); while (res.next()) { Jobs.getExplore().ChunkRespond(res.getString("playerName"), res.getString("worldname"), res.getInt("chunkX"), res.getInt("chunkZ"), false); } - res.close(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } } @@ -918,21 +998,22 @@ public abstract class JobsDAO { List nameList = new ArrayList(); if (conn == null) return nameList; + PreparedStatement prest = null; + ResultSet res = null; try { - - PreparedStatement prest = conn.prepareStatement("SELECT `userid` FROM `" + prefix + "log` WHERE `time` >= ? AND `time` <= ? ;"); + prest = conn.prepareStatement("SELECT `userid` FROM `" + prefix + "log` WHERE `time` >= ? AND `time` <= ? ;"); prest.setInt(1, fromtime); prest.setInt(2, untiltime); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); while (res.next()) { if (!nameList.contains(res.getInt("userid"))) nameList.add(res.getInt("userid")); } - res.close(); - prest.close(); - return nameList; } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } return nameList; } @@ -947,11 +1028,13 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return jobs; + PreparedStatement prest = null; + ResultSet res = null; try { - PreparedStatement prest = conn.prepareStatement("SELECT DISTINCT(userid), `level`, `experience` FROM `" + prefix + prest = conn.prepareStatement("SELECT DISTINCT(userid), `level`, `experience` FROM `" + prefix + "jobs` WHERE `job` LIKE ? ORDER BY `level` DESC, LOWER(experience) DESC LIMIT " + limit + ", 15;"); prest.setString(1, jobsname); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); while (res.next()) { Entry info = Jobs.getPlayerManager().getPlayerInfoById(res.getInt("userid")); @@ -977,10 +1060,11 @@ public abstract class JobsDAO { jobs.add(new TopList(res.getInt("userid"), res.getInt("level"), res.getInt("experience"))); } } - res.close(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } return jobs; } @@ -995,17 +1079,20 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return slot; + PreparedStatement prest = null; + ResultSet res = null; try { - PreparedStatement prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs` WHERE `job` = ?;"); + prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + prefix + "jobs` WHERE `job` = ?;"); prest.setString(1, job.getName()); - ResultSet res = prest.executeQuery(); + res = prest.executeQuery(); if (res.next()) { slot = res.getInt(1); } - res.close(); - prest.close(); } catch (SQLException e) { e.printStackTrace(); + } finally { + close(res); + close(prest); } return slot; } @@ -1034,18 +1121,8 @@ public abstract class JobsDAO { } catch (NumberFormatException e) { e.printStackTrace(); } finally { - if (res != null) { - try { - res.close(); - } catch (SQLException e) { - } - } - if (prest != null) { - try { - prest.close(); - } catch (SQLException e) { - } - } + close(res); + close(prest); } return schema; } @@ -1076,12 +1153,7 @@ public abstract class JobsDAO { } catch (SQLException e) { e.printStackTrace(); } finally { - if (prest != null) { - try { - prest.close(); - } catch (SQLException e) { - } - } + close(prest); } } @@ -1092,14 +1164,12 @@ public abstract class JobsDAO { */ public void executeSQL(String sql) throws SQLException { JobsConnection conn = getConnection(); - Statement stmt = conn.createStatement(); + Statement stmt = null; try { + stmt = conn.createStatement(); stmt.execute(sql); } finally { - try { - stmt.close(); - } catch (SQLException e) { - } + close(stmt); } } @@ -1123,4 +1193,22 @@ public abstract class JobsDAO { public synchronized void closeConnections() { pool.closeConnection(); } + + private static void close(ResultSet res) { + if (res != null) + try { + res.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private static void close(Statement stmt) { + if (stmt != null) + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } } diff --git a/com/gamingmesh/jobs/dao/JobsDAOData.java b/com/gamingmesh/jobs/dao/JobsDAOData.java index d8ff9074..4506d494 100644 --- a/com/gamingmesh/jobs/dao/JobsDAOData.java +++ b/com/gamingmesh/jobs/dao/JobsDAOData.java @@ -18,8 +18,6 @@ package com.gamingmesh.jobs.dao; -import java.util.UUID; - /** * Container class to hold information out of the database. * @@ -30,53 +28,43 @@ import java.util.UUID; * */ public class JobsDAOData { - private UUID playerUUID; private String job; private int level; private int experience; - + /** * Constructor class for the DAO side of things. * @param job - the name of the job * @param level - the level of the job * @param experience - the experience of the job */ - public JobsDAOData(UUID playerUUID, String job, int level, int experience) { - this.playerUUID = playerUUID; - this.job = job; - this.level = level; - this.experience = experience; + public JobsDAOData(String job, int level, int experience) { + this.job = job; + this.level = level; + this.experience = experience; } - - /** - * Getter function for the playerUUID - * @return the job name - */ - public UUID getPlayerUUID() { - return playerUUID; - } - + /** * Getter function for the job name * @return the job name */ public String getJobName() { - return job; + return job; } - + /** * Getter function for the level * @return the level in the job */ public int getLevel() { - return level; + return level; } - + /** * Getter function for the experience. * @return the experience in the job */ public double getExperience() { - return experience; + return experience; } } diff --git a/com/gamingmesh/jobs/nmsUtil/v1_10.java b/com/gamingmesh/jobs/nmsUtil/v1_10.java index 286d1b4e..24222792 100644 --- a/com/gamingmesh/jobs/nmsUtil/v1_10.java +++ b/com/gamingmesh/jobs/nmsUtil/v1_10.java @@ -19,7 +19,6 @@ import org.bukkit.event.block.BlockPistonRetractEvent; import org.bukkit.inventory.ItemStack; import com.gamingmesh.jobs.NMS; -import com.gamingmesh.jobs.stuff.Debug; public class v1_10 implements NMS { @Override