diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index 98a1387a..dae17e93 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -1190,13 +1190,16 @@ public class PlayerManager { if (jPlayer == null || player.hasPermission("jobs.*")) return; - int confMaxJobs = Jobs.getGCManager().getMaxJobs(); - short playerMaxJobs = (short) jPlayer.progression.size(); + int playerMaxJobs = getMaxJobs(jPlayer); + int playerCurrentJobs = jPlayer.progression.size(); - if (confMaxJobs > 0 && playerMaxJobs >= confMaxJobs && !getJobsLimit(jPlayer, playerMaxJobs)) + if (playerMaxJobs <= 0 || playerCurrentJobs >= playerMaxJobs) return; for (Job one : Jobs.getJobs()) { + if (jPlayer.progression.size() >= playerMaxJobs) + return; + if (one.getMaxSlots() != null && Jobs.getUsedSlots(one) >= one.getMaxSlots()) continue; diff --git a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java index f0ec0501..e0978211 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java @@ -44,6 +44,7 @@ import net.Zrips.CMILib.ActionBar.CMIActionBar; import net.Zrips.CMILib.Colors.CMIChatColor; import net.Zrips.CMILib.Equations.Parser; import net.Zrips.CMILib.Items.CMIMaterial; +import net.Zrips.CMILib.Logs.CMIDebug; import net.Zrips.CMILib.Time.CMITimeManager; public class JobsPlayer { @@ -98,24 +99,24 @@ public class JobsPlayer { private Set blockOwnerShipInform = null; public JobsPlayer(OfflinePlayer player) { - this.userName = player.getName() == null ? "Unknown" : player.getName(); - this.playerUUID = player.getUniqueId(); + this.userName = player.getName() == null ? "Unknown" : player.getName(); + this.playerUUID = player.getUniqueId(); } public JobsPlayer(Player player) { - this.userName = player.getName() == null ? "Unknown" : player.getName(); - this.playerUUID = player.getUniqueId(); + this.userName = player.getName() == null ? "Unknown" : player.getName(); + this.playerUUID = player.getUniqueId(); } public JobsPlayer(String userName) { - this.userName = userName == null ? "Unknown" : userName; + this.userName = userName == null ? "Unknown" : userName; } /** * @return the cached or new instance of {@link PlayerPoints} */ public PlayerPoints getPointsData() { - return pointsData; + return pointsData; } /** @@ -124,8 +125,8 @@ public class JobsPlayer { * @param points the amount of points */ public void addPoints(double points) { - pointsData.addPoints(points); - this.setSaved(false); + pointsData.addPoints(points); + this.setSaved(false); } /** @@ -134,8 +135,8 @@ public class JobsPlayer { * @param points the amount of points */ public void takePoints(double points) { - pointsData.takePoints(points); - this.setSaved(false); + pointsData.takePoints(points); + this.setSaved(false); } /** @@ -144,8 +145,8 @@ public class JobsPlayer { * @param points the amount of points */ public void setPoints(double points) { - pointsData.setPoints(points); - this.setSaved(false); + pointsData.setPoints(points); + this.setSaved(false); } /** @@ -154,9 +155,9 @@ public class JobsPlayer { * @param points {@link PlayerPoints} */ public void setPoints(PlayerPoints points) { - pointsData.setPoints(points.getCurrentPoints()); - pointsData.setTotalPoints(points.getTotalPoints()); - pointsData.setDbId(points.getDbId()); + pointsData.setPoints(points.getCurrentPoints()); + pointsData.setTotalPoints(points.getTotalPoints()); + pointsData.setDbId(points.getDbId()); } /** @@ -166,14 +167,14 @@ public class JobsPlayer { * @return true if yes */ public boolean havePoints(double points) { - return pointsData.getCurrentPoints() >= points; + return pointsData.getCurrentPoints() >= points; } /** * @return the cached instance of {@link ArchivedJobs} */ public ArchivedJobs getArchivedJobs() { - return archivedJobs; + return archivedJobs; } /** @@ -183,38 +184,38 @@ public class JobsPlayer { * @return the given archived job progression */ public JobProgression getArchivedJobProgression(Job job) { - return archivedJobs.getArchivedJobProgression(job); + return archivedJobs.getArchivedJobProgression(job); } public void setArchivedJobs(ArchivedJobs archivedJob) { - this.archivedJobs = archivedJob; + this.archivedJobs = archivedJob; } /** * @return the total level of all jobs for this player */ public int getTotalLevels() { - int i = 0; - for (JobProgression job : progression) { - i += job.getLevel(); - } - return i; + int i = 0; + for (JobProgression job : progression) { + i += job.getLevel(); + } + return i; } public void setPaymentLimit(PaymentData paymentLimits) { - this.paymentLimits = paymentLimits; + this.paymentLimits = paymentLimits; } /** * @return the limit of {@link PaymentData} */ public PaymentData getPaymentLimit() { - if (paymentLimits == null) - paymentLimits = Jobs.getJobsDAO().getPlayersLimits(this); + if (paymentLimits == null) + paymentLimits = Jobs.getJobsDAO().getPlayersLimits(this); - if (paymentLimits == null) - paymentLimits = new PaymentData(); - return paymentLimits; + if (paymentLimits == null) + paymentLimits = new PaymentData(); + return paymentLimits; } /** @@ -225,50 +226,50 @@ public class JobsPlayer { * @return true if it is under */ public boolean isUnderLimit(CurrencyType type, double amount) { - if (amount == 0) - return true; + if (amount == 0) + return true; - Player player = getPlayer(); - if (player == null) - return true; + Player player = getPlayer(); + if (player == null) + return true; - CurrencyLimit limit = Jobs.getGCManager().getLimit(type); - if (!limit.isEnabled()) - return true; + CurrencyLimit limit = Jobs.getGCManager().getLimit(type); + if (!limit.isEnabled()) + return true; - PaymentData data = getPaymentLimit(); + PaymentData data = getPaymentLimit(); - if (data.isReachedLimit(type, getLimit(type))) { - String name = type.getName().toLowerCase(); + if (data.isReachedLimit(type, getLimit(type))) { + String name = type.getName().toLowerCase(); - if (!data.isInformed() && player.isOnline() && !data.isReseted(type)) { - if (Jobs.getGCManager().useMaxPaymentCurve) { - player.sendMessage(Jobs.getLanguage().getMessage("command.limit.output.reached" + name + "limit")); - player.sendMessage(Jobs.getLanguage().getMessage("command.limit.output.reached" + name + "limit2")); - player.sendMessage(Jobs.getLanguage().getMessage("command.limit.output.reached" + name + "limit3")); - } else { - player.sendMessage(Jobs.getLanguage().getMessage("command.limit.output.reached" + name + "limit")); - player.sendMessage(Jobs.getLanguage().getMessage("command.limit.output.reached" + name + "limit2")); - } + if (!data.isInformed() && player.isOnline() && !data.isReseted(type)) { + if (Jobs.getGCManager().useMaxPaymentCurve) { + player.sendMessage(Jobs.getLanguage().getMessage("command.limit.output.reached" + name + "limit")); + player.sendMessage(Jobs.getLanguage().getMessage("command.limit.output.reached" + name + "limit2")); + player.sendMessage(Jobs.getLanguage().getMessage("command.limit.output.reached" + name + "limit3")); + } else { + player.sendMessage(Jobs.getLanguage().getMessage("command.limit.output.reached" + name + "limit")); + player.sendMessage(Jobs.getLanguage().getMessage("command.limit.output.reached" + name + "limit2")); + } - data.setInformed(true); - } + data.setInformed(true); + } - if (data.isAnnounceTime(limit.getAnnouncementDelay()) && player.isOnline()) - CMIActionBar.send(player, Jobs.getLanguage().getMessage("command.limit.output." + name + "time", "%time%", CMITimeManager.to24hourShort(data.getLeftTime(type)))); + if (data.isAnnounceTime(limit.getAnnouncementDelay()) && player.isOnline()) + CMIActionBar.send(player, Jobs.getLanguage().getMessage("command.limit.output." + name + "time", "%time%", CMITimeManager.to24hourShort(data.getLeftTime(type)))); - if (data.isReseted(type)) - data.setReseted(type, false); + if (data.isReseted(type)) + data.setReseted(type, false); - return false; - } + return false; + } - data.addAmount(type, amount); - return true; + data.addAmount(type, amount); + return true; } public double percentOverLimit(CurrencyType type) { - return getPaymentLimit().percentOverLimit(type, getLimit(type)); + return getPaymentLimit().percentOverLimit(type, getLimit(type)); } /** @@ -278,49 +279,49 @@ public class JobsPlayer { */ @Deprecated public void loadLogFromDao() { - Jobs.getJobsDAO().loadLog(this); + Jobs.getJobsDAO().loadLog(this); } public List getUpdateBossBarFor() { - return updateBossBarFor; + return updateBossBarFor; } @Deprecated public void clearUpdateBossBarFor() { - updateBossBarFor.clear(); + updateBossBarFor.clear(); } public List getBossBarInfo() { - return barMap; + return barMap; } public void hideBossBars() { - for (BossBarInfo one : barMap) { - one.getBar().setVisible(false); - } + for (BossBarInfo one : barMap) { + one.getBar().setVisible(false); + } } public Map getLog() { - return logList; + return logList; } public void setLog(Map logList) { - this.logList = logList; + this.logList = logList; } public void setUserId(int userid) { - this.userid = userid; + this.userid = userid; } public int getUserId() { - return userid; + return userid; } /** * @return {@link Player} or null if not exist */ public Player getPlayer() { - return playerUUID != null ? plugin.getServer().getPlayer(playerUUID) : null; + return playerUUID != null ? plugin.getServer().getPlayer(playerUUID) : null; } /** @@ -332,7 +333,7 @@ public class JobsPlayer { * @return amount of boost */ public double getBoost(String jobName, CurrencyType type) { - return getBoost(jobName, type, false); + return getBoost(jobName, type, false); } /** @@ -344,110 +345,110 @@ public class JobsPlayer { * @return amount of boost */ public double getBoost(String jobName, CurrencyType type, boolean force) { - double boost = 0D; + double boost = 0D; - if (type == null || !isOnline()) - return boost; + if (type == null || !isOnline()) + return boost; - long time = System.currentTimeMillis(); + long time = System.currentTimeMillis(); - List counterList = boostCounter.get(jobName); - if (counterList != null) { - for (BoostCounter counter : counterList) { - if (counter.getType() != type) - continue; + List counterList = boostCounter.get(jobName); + if (counterList != null) { + for (BoostCounter counter : counterList) { + if (counter.getType() != type) + continue; - if (force || time - counter.getTime() > 1000 * 60) { - boost = getPlayerBoostNew(jobName, type); - counter.setBoost(boost); - counter.setTime(time); - return boost; - } + if (force || time - counter.getTime() > 1000 * 60) { + boost = getPlayerBoostNew(jobName, type); + counter.setBoost(boost); + counter.setTime(time); + return boost; + } - return counter.getBoost(); - } + return counter.getBoost(); + } - boost = getPlayerBoostNew(jobName, type); - counterList.add(new BoostCounter(type, boost, time)); - return boost; - } + boost = getPlayerBoostNew(jobName, type); + counterList.add(new BoostCounter(type, boost, time)); + return boost; + } - boost = getPlayerBoostNew(jobName, type); + boost = getPlayerBoostNew(jobName, type); - counterList = new ArrayList<>(); - counterList.add(new BoostCounter(type, boost, time)); + counterList = new ArrayList<>(); + counterList.add(new BoostCounter(type, boost, time)); - boostCounter.put(jobName, counterList); - return boost; + boostCounter.put(jobName, counterList); + return boost; } private Double getPlayerBoostNew(String jobName, CurrencyType type) { - Double v1 = Jobs.getPermissionManager().getMaxPermission(this, "jobs.boost." + jobName + "." + type.getName(), true, false); - Double boost = v1; + Double v1 = Jobs.getPermissionManager().getMaxPermission(this, "jobs.boost." + jobName + "." + type.getName(), true, false); + Double boost = v1; - v1 = Jobs.getPermissionManager().getMaxPermission(this, "jobs.boost." + jobName + ".all", false, false); - if (v1 != 0d && (v1 > boost || v1 < boost)) - boost = v1; + v1 = Jobs.getPermissionManager().getMaxPermission(this, "jobs.boost." + jobName + ".all", false, false); + if (v1 != 0d && (v1 > boost || v1 < boost)) + boost = v1; - v1 = Jobs.getPermissionManager().getMaxPermission(this, "jobs.boost.all.all", false, false); - if (v1 != 0d && (v1 > boost || v1 < boost)) - boost = v1; + v1 = Jobs.getPermissionManager().getMaxPermission(this, "jobs.boost.all.all", false, false); + if (v1 != 0d && (v1 > boost || v1 < boost)) + boost = v1; - v1 = Jobs.getPermissionManager().getMaxPermission(this, "jobs.boost.all." + type.getName(), false, false); - if (v1 != 0d && (v1 > boost || v1 < boost)) - boost = v1; + v1 = Jobs.getPermissionManager().getMaxPermission(this, "jobs.boost.all." + type.getName(), false, false); + if (v1 != 0d && (v1 > boost || v1 < boost)) + boost = v1; - return boost; + return boost; } public int getPlayerMaxQuest(String jobName) { - int m1 = (int) Jobs.getPermissionManager().getMaxPermission(this, "jobs.maxquest.all", false, true); - int m2 = (int) Jobs.getPermissionManager().getMaxPermission(this, "jobs.maxquest." + jobName, false, true); - return m1 > m2 ? m1 : m2; + int m1 = (int) Jobs.getPermissionManager().getMaxPermission(this, "jobs.maxquest.all", false, true); + int m2 = (int) Jobs.getPermissionManager().getMaxPermission(this, "jobs.maxquest." + jobName, false, true); + return m1 > m2 ? m1 : m2; } /** * Reloads max experience for all jobs for this player. */ public void reloadMaxExperience() { - progression.forEach(JobProgression::reloadMaxExperience); + progression.forEach(JobProgression::reloadMaxExperience); } /** * Reloads limit for this player. */ public void reload(CurrencyType type) { - Parser eq = Jobs.getGCManager().getLimit(type).getMaxEquation(); - eq.setVariable("totallevel", getTotalLevels()); + Parser eq = Jobs.getGCManager().getLimit(type).getMaxEquation(); + eq.setVariable("totallevel", getTotalLevels()); - maxJobsEquation = Jobs.getPlayerManager().getMaxJobs(this); - limits.put(type, (int) eq.getValue()); - setSaved(false); + maxJobsEquation = Jobs.getPlayerManager().getMaxJobs(this); + limits.put(type, (int) eq.getValue()); + setSaved(false); } public void reloadLimits() { - for (CurrencyType type : CurrencyType.values()) { - reload(type); - } + for (CurrencyType type : CurrencyType.values()) { + reload(type); + } } public int getLimit(CurrencyType type) { - return limits.getOrDefault(type, 0); + return limits.getOrDefault(type, 0); } public void resetPaymentLimit() { - if (paymentLimits == null) - getPaymentLimit(); - if (paymentLimits != null) - paymentLimits.resetLimits(); - setSaved(false); + if (paymentLimits == null) + getPaymentLimit(); + if (paymentLimits != null) + paymentLimits.resetLimits(); + setSaved(false); } /** * @return an unmodifiable list of job progressions */ public List getJobProgression() { - return Collections.unmodifiableList(progression); + return Collections.unmodifiableList(progression); } /** @@ -457,14 +458,14 @@ public class JobsPlayer { * @return the job progression or null if job not exists */ public JobProgression getJobProgression(Job job) { - if (job != null) { - for (JobProgression prog : progression) { - if (prog.getJob().isSame(job)) - return prog; - } - } + if (job != null) { + for (JobProgression prog : progression) { + if (prog.getJob().isSame(job)) + return prog; + } + } - return null; + return null; } /** @@ -473,28 +474,28 @@ public class JobsPlayer { */ @Deprecated public String getUserName() { - return getName(); + return getName(); } /** * @return the name of this player */ public String getName() { - Player player = getPlayer(); - if (player != null) - userName = player.getName(); - return userName; + Player player = getPlayer(); + if (player != null) + userName = player.getName(); + return userName; } public String getDisplayName() { - if (!this.isOnline()) - return getName(); - Player p = this.getPlayer(); + if (!this.isOnline()) + return getName(); + Player p = this.getPlayer(); - if (p == null) - return getName(); + if (p == null) + return getName(); - return p.getDisplayName() == null || p.getDisplayName().isEmpty() ? getName() : p.getDisplayName(); + return p.getDisplayName() == null || p.getDisplayName().isEmpty() ? getName() : p.getDisplayName(); } /** @@ -503,28 +504,28 @@ public class JobsPlayer { */ @Deprecated public UUID getPlayerUUID() { - return getUniqueId(); + return getUniqueId(); } /** * @return the {@link UUID} of this player */ public UUID getUniqueId() { - return playerUUID; + return playerUUID; } public void setPlayerUUID(UUID playerUUID) { - setUniqueId(playerUUID); + setUniqueId(playerUUID); } public void setUniqueId(UUID playerUUID) { - this.playerUUID = playerUUID; + this.playerUUID = playerUUID; } public String getDisplayHonorific() { - if (honorific == null) - reloadHonorific(); - return honorific; + if (honorific == null) + reloadHonorific(); + return honorific; } /** @@ -534,69 +535,69 @@ public class JobsPlayer { */ public boolean joinJob(Job job) { // synchronized (saveLock) { - if (!isInJob(job)) { - int level = 1; - double exp = 0; + if (!isInJob(job)) { + int level = 1; + double exp = 0; - JobProgression archived = getArchivedJobProgression(job); - if (archived != null) { - level = getLevelAfterRejoin(archived); - exp = getExpAfterRejoin(archived, level); - Jobs.getJobsDAO().deleteArchive(this, job); - } + JobProgression archived = getArchivedJobProgression(job); + if (archived != null) { + level = getLevelAfterRejoin(archived); + exp = getExpAfterRejoin(archived, level); + Jobs.getJobsDAO().deleteArchive(this, job); + } - progression.add(new JobProgression(job, this, level, exp)); - reloadMaxExperience(); - reloadLimits(); - reloadHonorific(); - Jobs.getPermissionHandler().recalculatePermissions(this); - return true; - } - return false; + progression.add(new JobProgression(job, this, level, exp)); + reloadMaxExperience(); + reloadLimits(); + reloadHonorific(); + Jobs.getPermissionHandler().recalculatePermissions(this); + return true; + } + return false; // } } public int getLevelAfterRejoin(JobProgression jp) { - if (jp == null) - return 1; + if (jp == null) + return 1; - int level = jp.getLevel(); + int level = jp.getLevel(); - level = (int) ((level - (level * (Jobs.getGCManager().levelLossPercentage / 100.0)))); - if (level < 1) - level = 1; + level = (int) ((level - (level * (Jobs.getGCManager().levelLossPercentage / 100.0)))); + if (level < 1) + level = 1; - if (jp.getLevel() == getMaxJobLevelAllowed(jp.getJob())) { - level = jp.getLevel(); + if (jp.getLevel() == getMaxJobLevelAllowed(jp.getJob())) { + level = jp.getLevel(); - if (!Jobs.getGCManager().fixAtMaxLevel) { - level = (int) (level - (level * (Jobs.getGCManager().levelLossPercentageFromMax / 100.0))); - if (level < 1) - level = 1; - } - } + if (!Jobs.getGCManager().fixAtMaxLevel) { + level = (int) (level - (level * (Jobs.getGCManager().levelLossPercentageFromMax / 100.0))); + if (level < 1) + level = 1; + } + } - return level; + return level; } public double getExpAfterRejoin(JobProgression jp, int level) { - if (jp == null) - return 1; + if (jp == null) + return 1; - int max = jp.getMaxExperience(level); - double exp = jp.getExperience(); - if (exp > max) - exp = max; + int max = jp.getMaxExperience(level); + double exp = jp.getExperience(); + if (exp > max) + exp = max; - if (exp > 0) { - if (jp.getLevel() == getMaxJobLevelAllowed(jp.getJob())) { - if (!Jobs.getGCManager().fixAtMaxLevel) - exp = (exp - (exp * (Jobs.getGCManager().levelLossPercentageFromMax / 100.0))); - } else - exp = (exp - (exp * (Jobs.getGCManager().levelLossPercentage / 100.0))); - } + if (exp > 0) { + if (jp.getLevel() == getMaxJobLevelAllowed(jp.getJob())) { + if (!Jobs.getGCManager().fixAtMaxLevel) + exp = (exp - (exp * (Jobs.getGCManager().levelLossPercentageFromMax / 100.0))); + } else + exp = (exp - (exp * (Jobs.getGCManager().levelLossPercentage / 100.0))); + } - return exp; + return exp; } /** @@ -605,14 +606,14 @@ public class JobsPlayer { */ public boolean leaveJob(Job job) { // synchronized (saveLock) { - if (progression.remove(getJobProgression(job))) { - reloadMaxExperience(); - reloadLimits(); - reloadHonorific(); - Jobs.getPermissionHandler().recalculatePermissions(this); - return true; - } - return false; + if (progression.remove(getJobProgression(job))) { + reloadMaxExperience(); + reloadLimits(); + reloadHonorific(); + Jobs.getPermissionHandler().recalculatePermissions(this); + return true; + } + return false; // } } @@ -622,11 +623,11 @@ public class JobsPlayer { */ public boolean leaveAllJobs() { // synchronized (saveLock) { - progression.clear(); - reloadHonorific(); - Jobs.getPermissionHandler().recalculatePermissions(this); - reloadLimits(); - return true; + progression.clear(); + reloadHonorific(); + Jobs.getPermissionHandler().recalculatePermissions(this); + reloadLimits(); + return true; // } } @@ -636,22 +637,22 @@ public class JobsPlayer { * @param levels - number of levels to promote */ public void promoteJob(Job job, int levels) { - if (levels <= 0) - return; + if (levels <= 0) + return; // synchronized (saveLock) { - JobProgression prog = getJobProgression(job); - if (prog == null) - return; + JobProgression prog = getJobProgression(job); + if (prog == null) + return; - int oldLevel = prog.getLevel(), - newLevel = oldLevel + levels, - maxLevel = job.getMaxLevel(this); + int oldLevel = prog.getLevel(), + newLevel = oldLevel + levels, + maxLevel = job.getMaxLevel(this); - if (maxLevel > 0 && newLevel > maxLevel) - newLevel = maxLevel; + if (maxLevel > 0 && newLevel > maxLevel) + newLevel = maxLevel; - setLevel(job, newLevel); + setLevel(job, newLevel); // } } @@ -661,19 +662,19 @@ public class JobsPlayer { * @param levels - number of levels to demote */ public void demoteJob(Job job, int levels) { - if (levels <= 0) - return; + if (levels <= 0) + return; // synchronized (saveLock) { - JobProgression prog = getJobProgression(job); - if (prog == null) - return; + JobProgression prog = getJobProgression(job); + if (prog == null) + return; - int newLevel = prog.getLevel() - levels; - if (newLevel < 1) - newLevel = 1; + int newLevel = prog.getLevel() - levels; + if (newLevel < 1) + newLevel = 1; - setLevel(job, newLevel); + setLevel(job, newLevel); // } } @@ -684,31 +685,31 @@ public class JobsPlayer { */ private void setLevel(Job job, int level) { // synchronized (saveLock) { - JobProgression prog = getJobProgression(job); - if (prog == null) - return; + JobProgression prog = getJobProgression(job); + if (prog == null) + return; - int oldLevel = prog.getLevel(); + int oldLevel = prog.getLevel(); - if (level != oldLevel) { - if (prog.setLevel(level)) { - JobsLevelUpEvent levelUpEvent = new JobsLevelUpEvent(this, job, prog.getLevel(), - Jobs.getTitleManager().getTitle(oldLevel, prog.getJob().getName()), - Jobs.getTitleManager().getTitle(prog.getLevel(), prog.getJob().getName()), - Jobs.getGCManager().SoundLevelupSound, - Jobs.getGCManager().SoundLevelupVolume, - Jobs.getGCManager().SoundLevelupPitch, - Jobs.getGCManager().SoundTitleChangeSound, - Jobs.getGCManager().SoundTitleChangeVolume, - Jobs.getGCManager().SoundTitleChangePitch); + if (level != oldLevel) { + if (prog.setLevel(level)) { + JobsLevelUpEvent levelUpEvent = new JobsLevelUpEvent(this, job, prog.getLevel(), + Jobs.getTitleManager().getTitle(oldLevel, prog.getJob().getName()), + Jobs.getTitleManager().getTitle(prog.getLevel(), prog.getJob().getName()), + Jobs.getGCManager().SoundLevelupSound, + Jobs.getGCManager().SoundLevelupVolume, + Jobs.getGCManager().SoundLevelupPitch, + Jobs.getGCManager().SoundTitleChangeSound, + Jobs.getGCManager().SoundTitleChangeVolume, + Jobs.getGCManager().SoundTitleChangePitch); - plugin.getServer().getPluginManager().callEvent(levelUpEvent); - } + plugin.getServer().getPluginManager().callEvent(levelUpEvent); + } - reloadHonorific(); - reloadLimits(); - Jobs.getPermissionHandler().recalculatePermissions(this); - } + reloadHonorific(); + reloadLimits(); + Jobs.getPermissionHandler().recalculatePermissions(this); + } // } } @@ -719,47 +720,47 @@ public class JobsPlayer { */ public boolean transferJob(Job oldjob, Job newjob) { // synchronized (saveLock) { - if (!isInJob(newjob)) { - for (JobProgression prog : progression) { - if (!prog.getJob().isSame(oldjob)) - continue; + if (!isInJob(newjob)) { + for (JobProgression prog : progression) { + if (!prog.getJob().isSame(oldjob)) + continue; - prog.setJob(newjob); + prog.setJob(newjob); - int maxLevel = getMaxJobLevelAllowed(newjob); + int maxLevel = getMaxJobLevelAllowed(newjob); - if (newjob.getMaxLevel() > 0 && prog.getLevel() > maxLevel) - prog.setLevel(maxLevel); + if (newjob.getMaxLevel() > 0 && prog.getLevel() > maxLevel) + prog.setLevel(maxLevel); - reloadMaxExperience(); - reloadLimits(); - reloadHonorific(); - Jobs.getPermissionHandler().recalculatePermissions(this); - return true; - } - } - return false; + reloadMaxExperience(); + reloadLimits(); + reloadHonorific(); + Jobs.getPermissionHandler().recalculatePermissions(this); + return true; + } + } + return false; // } } public int getMaxJobLevelAllowed(Job job) { - Player player = getPlayer(); + Player player = getPlayer(); - int maxLevel = 0; - if (player != null && (player.hasPermission("jobs." + job.getName() + ".vipmaxlevel") || player.hasPermission("jobs.all.vipmaxlevel"))) - maxLevel = job.getVipMaxLevel() > job.getMaxLevel() ? job.getVipMaxLevel() : job.getMaxLevel(); - else - maxLevel = job.getMaxLevel(); + int maxLevel = 0; + if (player != null && (player.hasPermission("jobs." + job.getName() + ".vipmaxlevel") || player.hasPermission("jobs.all.vipmaxlevel"))) + maxLevel = job.getVipMaxLevel() > job.getMaxLevel() ? job.getVipMaxLevel() : job.getMaxLevel(); + else + maxLevel = job.getMaxLevel(); - int tMax = (int) Jobs.getPermissionManager().getMaxPermission(this, "jobs." + job.getName() + ".vipmaxlevel"); - if (tMax > maxLevel) - maxLevel = tMax; + int tMax = (int) Jobs.getPermissionManager().getMaxPermission(this, "jobs." + job.getName() + ".vipmaxlevel"); + if (tMax > maxLevel) + maxLevel = tMax; - tMax = (int) Jobs.getPermissionManager().getMaxPermission(this, "jobs.all.vipmaxlevel"); - if (tMax > maxLevel) - maxLevel = tMax; + tMax = (int) Jobs.getPermissionManager().getMaxPermission(this, "jobs.all.vipmaxlevel"); + if (tMax > maxLevel) + maxLevel = tMax; - return maxLevel; + return maxLevel; } /** @@ -769,126 +770,143 @@ public class JobsPlayer { * @return true if this player is in the given job, otherwise false */ public boolean isInJob(Job job) { - return getJobProgression(job) != null; + return getJobProgression(job) != null; } /** * Function that reloads this player honorific */ public void reloadHonorific() { - StringBuilder builder = new StringBuilder(); + StringBuilder builder = new StringBuilder(); - if (progression.size() > 0) { - for (JobProgression prog : progression) { - DisplayMethod method = prog.getJob().getDisplayMethod(); - if (method == DisplayMethod.NONE) - continue; - if (builder.length() > 0) { - builder.append(Jobs.getGCManager().modifyChatSeparator); - } - processesChat(method, builder, prog.getLevel(), Jobs.getTitleManager().getTitle(prog.getLevel(), - prog.getJob().getName()), prog.getJob()); - } - } else { - Job nonejob = Jobs.getNoneJob(); - if (nonejob != null) { - processesChat(nonejob.getDisplayMethod(), builder, -1, null, nonejob); - } - } + if (progression.size() > 0) { + for (JobProgression prog : progression) { + DisplayMethod method = prog.getJob().getDisplayMethod(); + if (method == DisplayMethod.NONE) + continue; + if (builder.length() > 0) { + builder.append(Jobs.getGCManager().modifyChatSeparator); + } + processesChat(method, builder, prog.getLevel(), Jobs.getTitleManager().getTitle(prog.getLevel(), + prog.getJob().getName()), prog.getJob()); + } + } else { + Job nonejob = Jobs.getNoneJob(); + if (nonejob != null) { + processesChat(nonejob.getDisplayMethod(), builder, -1, null, nonejob); + } + } - honorific = builder.toString().trim(); - if (!honorific.isEmpty()) - honorific = CMIChatColor.translate(Jobs.getGCManager().modifyChatPrefix + honorific + Jobs.getGCManager().modifyChatSuffix); + honorific = builder.toString().trim(); + if (!honorific.isEmpty()) + honorific = CMIChatColor.translate(Jobs.getGCManager().modifyChatPrefix + honorific + Jobs.getGCManager().modifyChatSuffix); } private static void processesChat(DisplayMethod method, StringBuilder builder, int level, Title title, Job job) { - String levelS = level < 0 ? "" : Integer.toString(level); - switch (method) { - case FULL: - processesTitle(builder, title, levelS); - processesJob(builder, job, levelS); - break; - case TITLE: - processesTitle(builder, title, levelS); - break; - case JOB: - processesJob(builder, job, levelS); - break; - case SHORT_FULL: - processesShortTitle(builder, title, levelS); - processesShortJob(builder, job, levelS); - break; - case SHORT_TITLE: - processesShortTitle(builder, title, levelS); - break; - case SHORT_JOB: - processesShortJob(builder, job, levelS); - break; - case SHORT_TITLE_JOB: - processesShortTitle(builder, title, levelS); - processesJob(builder, job, levelS); - break; - case TITLE_SHORT_JOB: - processesTitle(builder, title, levelS); - processesShortJob(builder, job, levelS); - break; - default: - break; - } + String levelS = level < 0 ? "" : Integer.toString(level); + switch (method) { + case FULL: + processesTitle(builder, title, levelS); + processesJob(builder, job, levelS); + break; + case TITLE: + processesTitle(builder, title, levelS); + break; + case JOB: + processesJob(builder, job, levelS); + break; + case SHORT_FULL: + processesShortTitle(builder, title, levelS); + processesShortJob(builder, job, levelS); + break; + case SHORT_TITLE: + processesShortTitle(builder, title, levelS); + break; + case SHORT_JOB: + processesShortJob(builder, job, levelS); + break; + case SHORT_TITLE_JOB: + processesShortTitle(builder, title, levelS); + processesJob(builder, job, levelS); + break; + case TITLE_SHORT_JOB: + processesTitle(builder, title, levelS); + processesShortJob(builder, job, levelS); + break; + default: + break; + } } private static void processesShortTitle(StringBuilder builder, Title title, String levelS) { - if (title == null) - return; - builder.append(title.getChatColor()); - builder.append(title.getShortName().replace("{level}", levelS)); - builder.append(CMIChatColor.WHITE); + if (title == null) + return; + builder.append(title.getChatColor()); + builder.append(title.getShortName().replace("{level}", levelS)); + builder.append(CMIChatColor.WHITE); } private static void processesTitle(StringBuilder builder, Title title, String levelS) { - if (title == null) - return; - builder.append(title.getChatColor()); - builder.append(title.getName().replace("{level}", levelS)); - builder.append(CMIChatColor.WHITE); + if (title == null) + return; + builder.append(title.getChatColor()); + builder.append(title.getName().replace("{level}", levelS)); + builder.append(CMIChatColor.WHITE); } private static void processesShortJob(StringBuilder builder, Job job, String levelS) { - if (job == null) - return; - builder.append(job.getChatColor()); - builder.append(job.getShortName().replace("{level}", levelS)); - builder.append(CMIChatColor.WHITE); + if (job == null) + return; + builder.append(job.getChatColor()); + builder.append(job.getShortName().replace("{level}", levelS)); + builder.append(CMIChatColor.WHITE); } private static void processesJob(StringBuilder builder, Job job, String levelS) { - if (job == null) - return; - builder.append(job.getChatColor()); - builder.append(job.getJobFullName().replace("{level}", levelS)); - builder.append(CMIChatColor.WHITE); + if (job == null) + return; + builder.append(job.getChatColor()); + builder.append(job.getJobFullName().replace("{level}", levelS)); + builder.append(CMIChatColor.WHITE); + } + + public void save() { + save(false); + } + + public void saveAsync() { + save(true); } /** * Performs player save into database */ - public void save() { + public void save(boolean async) { // synchronized (saveLock) { - if (!isSaved) { - JobsDAO dao = Jobs.getJobsDAO(); - dao.save(this); - dao.saveLog(this); - dao.savePoints(this); - dao.recordPlayersLimits(this); - dao.updateSeen(this); - setSaved(true); + if (!isSaved) { + JobsDAO dao = Jobs.getJobsDAO(); + if (!async) { + dao.save(this); + dao.saveLog(this); + dao.savePoints(this); + dao.recordPlayersLimits(this); + dao.updateSeen(this); + } else { + dao.saveAsync(this); + dao.saveLogAsync(this); + dao.savePointsAsync(this); + dao.recordPlayersLimitsAsync(this); + dao.updateSeenAsync(this); + } - Player player = getPlayer(); - if (player == null || !player.isOnline()) { - Jobs.getPlayerManager().addPlayerToCache(this); - Jobs.getPlayerManager().removePlayer(player); - } - } + setSaved(true); + + Player player = getPlayer(); + if (player == null || !player.isOnline()) { + Jobs.getPlayerManager().addPlayerToCache(this); + Jobs.getPlayerManager().removePlayer(player); + } + } // } } @@ -896,26 +914,26 @@ public class JobsPlayer { * Perform connect */ public void onConnect() { - isOnline = true; + isOnline = true; } /** * Perform disconnect for this player */ public void onDisconnect() { - clearBossMaps(); - isOnline = false; - blockOwnerShipInform = null; - Jobs.getPlayerManager().addPlayerToCache(this); + clearBossMaps(); + isOnline = false; + blockOwnerShipInform = null; + Jobs.getPlayerManager().addPlayerToCache(this); } public void clearBossMaps() { - for (BossBarInfo one : barMap) { - one.getBar().removeAll(); - one.cancel(); - } + for (BossBarInfo one : barMap) { + one.getBar().removeAll(); + one.cancel(); + } - barMap.clear(); + barMap.clear(); } /** @@ -923,44 +941,44 @@ public class JobsPlayer { * @return true if online, otherwise false */ public boolean isOnline() { - Player player = getPlayer(); - return player != null ? player.isOnline() : isOnline; + Player player = getPlayer(); + return player != null ? player.isOnline() : isOnline; } public boolean isSaved() { - return isSaved; + return isSaved; } public void setSaved(boolean isSaved) { - this.isSaved = isSaved; + this.isSaved = isSaved; } public long getSeen() { - return seen; + return seen; } public void setSeen(long seen) { - this.seen = seen; + this.seen = seen; } public Map getPermissionsCache() { - return permissionsCache; + return permissionsCache; } public void setPermissionsCache(Map permissionsCache) { - this.permissionsCache = permissionsCache; + this.permissionsCache = permissionsCache; } public void setPermissionsCache(String permission, Boolean state) { - permissionsCache.put(permission, state); + permissionsCache.put(permission, state); } public long getLastPermissionUpdate() { - return lastPermissionUpdate; + return lastPermissionUpdate; } public void setLastPermissionUpdate(Long lastPermissionUpdate) { - this.lastPermissionUpdate = lastPermissionUpdate; + this.lastPermissionUpdate = lastPermissionUpdate; } /** @@ -970,363 +988,363 @@ public class JobsPlayer { * @return true if yes */ public boolean canGetPaid(ActionInfo info) { - int numjobs = progression.size(); + int numjobs = progression.size(); - if (numjobs == 0) { - if (Jobs.getNoneJob() == null) - return false; - JobInfo jobinfo = Jobs.getNoneJob().getJobInfo(info, 1); - if (jobinfo == null) - return false; - double income = jobinfo.getIncome(1, numjobs, maxJobsEquation); - double points = jobinfo.getPoints(1, numjobs, maxJobsEquation); - if (income == 0D && points == 0D) - return false; - } + if (numjobs == 0) { + if (Jobs.getNoneJob() == null) + return false; + JobInfo jobinfo = Jobs.getNoneJob().getJobInfo(info, 1); + if (jobinfo == null) + return false; + double income = jobinfo.getIncome(1, numjobs, maxJobsEquation); + double points = jobinfo.getPoints(1, numjobs, maxJobsEquation); + if (income == 0D && points == 0D) + return false; + } - for (JobProgression prog : progression) { - int level = prog.getLevel(); - JobInfo jobinfo = prog.getJob().getJobInfo(info, level); - if (jobinfo == null) - continue; + for (JobProgression prog : progression) { + int level = prog.getLevel(); + JobInfo jobinfo = prog.getJob().getJobInfo(info, level); + if (jobinfo == null) + continue; - double income = jobinfo.getIncome(level, numjobs, maxJobsEquation); - double pointAmount = jobinfo.getPoints(level, numjobs, maxJobsEquation); - double expAmount = jobinfo.getExperience(level, numjobs, maxJobsEquation); - if (income != 0D || pointAmount != 0D || expAmount != 0D) - return true; - } + double income = jobinfo.getIncome(level, numjobs, maxJobsEquation); + double pointAmount = jobinfo.getPoints(level, numjobs, maxJobsEquation); + double expAmount = jobinfo.getExperience(level, numjobs, maxJobsEquation); + if (income != 0D || pointAmount != 0D || expAmount != 0D) + return true; + } - return false; + return false; } public boolean inDailyQuest(Job job, String questName) { - Map qpl = qProgression.get(job.getName()); - if (qpl == null) - return false; + Map qpl = qProgression.get(job.getName()); + if (qpl == null) + return false; - for (QuestProgression one : qpl.values()) { - Quest quest = one.getQuest(); + for (QuestProgression one : qpl.values()) { + Quest quest = one.getQuest(); - if (quest != null && quest.getConfigName().equalsIgnoreCase(questName)) - return true; - } + if (quest != null && quest.getConfigName().equalsIgnoreCase(questName)) + return true; + } - return false; + return false; } private List getQuestNameList(Job job, ActionType type) { - List ls = new ArrayList<>(); - if (!isInJob(job)) - return ls; + List ls = new ArrayList<>(); + if (!isInJob(job)) + return ls; - Map qpl = qProgression.get(job.getName()); - if (qpl == null) - return ls; + Map qpl = qProgression.get(job.getName()); + if (qpl == null) + return ls; - for (QuestProgression prog : qpl.values()) { - if (prog.isEnded()) - continue; + for (QuestProgression prog : qpl.values()) { + if (prog.isEnded()) + continue; - Quest quest = prog.getQuest(); - if (quest == null) - continue; + Quest quest = prog.getQuest(); + if (quest == null) + continue; - for (Map oneAction : quest.getObjectives().values()) { - for (QuestObjective oneObjective : oneAction.values()) { - if (type == null || type.name().equals(oneObjective.getAction().name())) { - ls.add(quest.getConfigName().toLowerCase()); - break; - } - } - } - } + for (Map oneAction : quest.getObjectives().values()) { + for (QuestObjective oneObjective : oneAction.values()) { + if (type == null || type.name().equals(oneObjective.getAction().name())) { + ls.add(quest.getConfigName().toLowerCase()); + break; + } + } + } + } - return ls; + return ls; } public void resetQuests(Job job) { - resetQuests(getQuestProgressions(job)); + resetQuests(getQuestProgressions(job)); } public void resetQuests(List quests) { - for (QuestProgression oneQ : quests) { - oneQ.reset(); - Quest quest = oneQ.getQuest(); - if (quest != null) { - Map map = qProgression.get(quest.getJob().getName()); + for (QuestProgression oneQ : quests) { + oneQ.reset(); + Quest quest = oneQ.getQuest(); + if (quest != null) { + Map map = qProgression.get(quest.getJob().getName()); - if (map != null) { - map.clear(); - } - } - } + if (map != null) { + map.clear(); + } + } + } } public void resetQuests() { - for (JobProgression prog : progression) { - resetQuests(prog.getJob()); - } + for (JobProgression prog : progression) { + resetQuests(prog.getJob()); + } } public void getNewQuests() { - qProgression.clear(); + qProgression.clear(); } public void getNewQuests(Job job) { - Map prog = qProgression.get(job.getName()); - if (prog != null) { - prog.clear(); - qProgression.put(job.getName(), prog); - } + Map prog = qProgression.get(job.getName()); + if (prog != null) { + prog.clear(); + qProgression.put(job.getName(), prog); + } } public void replaceQuest(Quest quest) { - Job job = quest.getJob(); - Map orProg = qProgression.get(job.getName()); + Job job = quest.getJob(); + Map orProg = qProgression.get(job.getName()); - Quest q = job.getNextQuest(getQuestNameList(job, null), getJobProgression(job).getLevel()); - if (q == null) { - for (JobProgression one : progression) { - if (one.getJob().isSame(job)) - continue; + Quest q = job.getNextQuest(getQuestNameList(job, null), getJobProgression(job).getLevel()); + if (q == null) { + for (JobProgression one : progression) { + if (one.getJob().isSame(job)) + continue; - q = one.getJob().getNextQuest(getQuestNameList(one.getJob(), null), getJobProgression(one.getJob()).getLevel()); - if (q != null) - break; - } - } + q = one.getJob().getNextQuest(getQuestNameList(one.getJob(), null), getJobProgression(one.getJob()).getLevel()); + if (q != null) + break; + } + } - if (q == null) - return; + if (q == null) + return; - Job qJob = q.getJob(); + Job qJob = q.getJob(); - Map prog = qProgression.get(qJob.getName()); - if (prog == null) { - prog = new HashMap<>(); - qProgression.put(qJob.getName(), prog); - } + Map prog = qProgression.get(qJob.getName()); + if (prog == null) { + prog = new HashMap<>(); + qProgression.put(qJob.getName(), prog); + } - if (q.getConfigName().equals(quest.getConfigName())) - return; + if (q.getConfigName().equals(quest.getConfigName())) + return; - String confName = q.getConfigName().toLowerCase(); + String confName = q.getConfigName().toLowerCase(); - if (prog.containsKey(confName)) - return; + if (prog.containsKey(confName)) + return; - if (!qJob.isSame(job) && prog.size() >= qJob.getMaxDailyQuests()) - return; + if (!qJob.isSame(job) && prog.size() >= qJob.getMaxDailyQuests()) + return; - if (orProg != null) { - orProg.remove(quest.getConfigName().toLowerCase()); - } + if (orProg != null) { + orProg.remove(quest.getConfigName().toLowerCase()); + } - prog.put(confName, new QuestProgression(q)); - skippedQuests++; + prog.put(confName, new QuestProgression(q)); + skippedQuests++; } public List getQuestProgressions() { - List g = new ArrayList<>(); - for (JobProgression one : progression) { - g.addAll(getQuestProgressions(one.getJob())); - } - return g; + List g = new ArrayList<>(); + for (JobProgression one : progression) { + g.addAll(getQuestProgressions(one.getJob())); + } + return g; } public List getQuestProgressions(Job job) { - return getQuestProgressions(job, null); + return getQuestProgressions(job, null); } public List getQuestProgressions(Job job, ActionType type) { - if (!isInJob(job)) - return new ArrayList<>(); + if (!isInJob(job)) + return new ArrayList<>(); - Map g = new HashMap<>(); + Map g = new HashMap<>(); - Map qProg = qProgression.get(job.getName()); - if (qProg != null) - g = new HashMap<>(qProg); + Map qProg = qProgression.get(job.getName()); + if (qProg != null) + g = new HashMap<>(qProg); - for (Entry one : new HashMap<>(g).entrySet()) { - QuestProgression qp = one.getValue(); + for (Entry one : new HashMap<>(g).entrySet()) { + QuestProgression qp = one.getValue(); - if (qp.isEnded()) { - g.remove(one.getKey().toLowerCase()); - skippedQuests = 0; - } - } + if (qp.isEnded()) { + g.remove(one.getKey().toLowerCase()); + skippedQuests = 0; + } + } - if (g.size() < job.getMaxDailyQuests()) { - int i = 0; - while (i <= job.getQuests().size()) { - ++i; + if (g.size() < job.getMaxDailyQuests()) { + int i = 0; + while (i <= job.getQuests().size()) { + ++i; - Quest q = job.getNextQuest(new ArrayList<>(g.keySet()), getJobProgression(job).getLevel()); - if (q == null) - continue; + Quest q = job.getNextQuest(new ArrayList<>(g.keySet()), getJobProgression(job).getLevel()); + if (q == null) + continue; - QuestProgression qp = new QuestProgression(q); - Quest quest = qp.getQuest(); - if (quest != null) - g.put(quest.getConfigName().toLowerCase(), qp); + QuestProgression qp = new QuestProgression(q); + Quest quest = qp.getQuest(); + if (quest != null) + g.put(quest.getConfigName().toLowerCase(), qp); - if (g.size() >= job.getMaxDailyQuests()) - break; - } - } + if (g.size() >= job.getMaxDailyQuests()) + break; + } + } - if (g.size() > job.getMaxDailyQuests()) { - int i = g.size(); - while (i > 0) { - --i; + if (g.size() > job.getMaxDailyQuests()) { + int i = g.size(); + while (i > 0) { + --i; - g.remove(g.keySet().iterator().next()); + g.remove(g.keySet().iterator().next()); - if (g.size() <= job.getMaxDailyQuests()) - break; - } - } + if (g.size() <= job.getMaxDailyQuests()) + break; + } + } - qProgression.put(job.getName(), g); + qProgression.put(job.getName(), g); - Map tmp = new HashMap<>(); - for (QuestProgression oneJ : g.values()) { - Quest q = oneJ.getQuest(); - if (q == null) { - continue; - } + Map tmp = new HashMap<>(); + for (QuestProgression oneJ : g.values()) { + Quest q = oneJ.getQuest(); + if (q == null) { + continue; + } - if (type == null) { - tmp.put(q.getConfigName().toLowerCase(), oneJ); - continue; - } + if (type == null) { + tmp.put(q.getConfigName().toLowerCase(), oneJ); + continue; + } - Map old = q.getObjectives().get(type); - if (old != null) - for (QuestObjective one : old.values()) { - if (type.name().equals(one.getAction().name())) { - tmp.put(q.getConfigName().toLowerCase(), oneJ); - break; - } - } - } + Map old = q.getObjectives().get(type); + if (old != null) + for (QuestObjective one : old.values()) { + if (type.name().equals(one.getAction().name())) { + tmp.put(q.getConfigName().toLowerCase(), oneJ); + break; + } + } + } - return new ArrayList<>(tmp.values()); + return new ArrayList<>(tmp.values()); } public String getQuestProgressionString() { - String prog = ""; + String prog = ""; - for (QuestProgression one : getQuestProgressions()) { - Quest q = one.getQuest(); - if (q == null || q.getObjectives().isEmpty()) - continue; + for (QuestProgression one : getQuestProgressions()) { + Quest q = one.getQuest(); + if (q == null || q.getObjectives().isEmpty()) + continue; - if (!prog.isEmpty()) - prog += ";:;"; + if (!prog.isEmpty()) + prog += ";:;"; - prog += q.getJob().getName() + ":" + q.getConfigName() + ":" + one.getValidUntil() + ":"; + prog += q.getJob().getName() + ":" + q.getConfigName() + ":" + one.getValidUntil() + ":"; - for (Map oneA : q.getObjectives().values()) { - for (Entry oneO : oneA.entrySet()) { - prog += oneO.getValue().getAction().toString() + ";" + oneO.getKey() + ";" + one.getAmountDone(oneO.getValue()) + ":;:"; - } - } + for (Map oneA : q.getObjectives().values()) { + for (Entry oneO : oneA.entrySet()) { + prog += oneO.getValue().getAction().toString() + ";" + oneO.getKey() + ";" + one.getAmountDone(oneO.getValue()) + ":;:"; + } + } - if (prog.endsWith(":;:")) - prog = prog.substring(0, prog.length() - 3); - } + if (prog.endsWith(":;:")) + prog = prog.substring(0, prog.length() - 3); + } - return prog.isEmpty() ? null : prog.endsWith(";:") ? prog.substring(0, prog.length() - 2) : prog; + return prog.isEmpty() ? null : prog.endsWith(";:") ? prog.substring(0, prog.length() - 2) : prog; } public void setQuestProgressionFromString(String qprog) { - if (qprog == null || qprog.isEmpty()) - return; + if (qprog == null || qprog.isEmpty()) + return; - for (String one : qprog.split(";:;")) { - String jname = one.split(":", 2)[0]; - Job job = Jobs.getJob(jname); - if (job == null) - continue; + for (String one : qprog.split(";:;")) { + String jname = one.split(":", 2)[0]; + Job job = Jobs.getJob(jname); + if (job == null) + continue; - one = one.substring(jname.length() + 1); + one = one.substring(jname.length() + 1); - String qname = one.split(":", 2)[0]; - Quest quest = job.getQuest(qname); - if (quest == null) - continue; + String qname = one.split(":", 2)[0]; + Quest quest = job.getQuest(qname); + if (quest == null) + continue; - one = one.substring(qname.length() + 1); + one = one.substring(qname.length() + 1); - String longS = one.split(":", 2)[0]; - long validUntil = Long.parseLong(longS); - one = one.substring(longS.length() + 1); + String longS = one.split(":", 2)[0]; + long validUntil = Long.parseLong(longS); + one = one.substring(longS.length() + 1); - Map currentProgression = qProgression.get(job.getName()); + Map currentProgression = qProgression.get(job.getName()); - if (currentProgression == null) { - currentProgression = new HashMap<>(); - qProgression.put(job.getName(), currentProgression); - } + if (currentProgression == null) { + currentProgression = new HashMap<>(); + qProgression.put(job.getName(), currentProgression); + } - String questName = qname.toLowerCase(); - QuestProgression qp = currentProgression.get(questName); - if (qp == null) { - qp = new QuestProgression(quest); - qp.setValidUntil(validUntil); - currentProgression.put(questName, qp); - } + String questName = qname.toLowerCase(); + QuestProgression qp = currentProgression.get(questName); + if (qp == null) { + qp = new QuestProgression(quest); + qp.setValidUntil(validUntil); + currentProgression.put(questName, qp); + } - for (String oneA : one.split(":;:")) { - String prog = oneA.split(";", 2)[0]; - ActionType action = ActionType.getByName(prog); - if (action == null || oneA.length() < prog.length() + 1) - continue; + for (String oneA : one.split(":;:")) { + String prog = oneA.split(";", 2)[0]; + ActionType action = ActionType.getByName(prog); + if (action == null || oneA.length() < prog.length() + 1) + continue; - Map old = quest.getObjectives().get(action); - if (old == null) - continue; + Map old = quest.getObjectives().get(action); + if (old == null) + continue; - oneA = oneA.substring(prog.length() + 1); + oneA = oneA.substring(prog.length() + 1); - String target = oneA.split(";", 2)[0]; - QuestObjective obj = old.get(target); - if (obj == null) - continue; + String target = oneA.split(";", 2)[0]; + QuestObjective obj = old.get(target); + if (obj == null) + continue; - oneA = oneA.substring(target.length() + 1); + oneA = oneA.substring(target.length() + 1); - qp.setAmountDone(obj, Integer.parseInt(oneA.split(";", 2)[0])); - } + qp.setAmountDone(obj, Integer.parseInt(oneA.split(";", 2)[0])); + } - if (qp.isCompleted()) - qp.setGivenReward(true); - } + if (qp.isCompleted()) + qp.setGivenReward(true); + } } public int getDoneQuests() { - return doneQuests; + return doneQuests; } public void setDoneQuests(int doneQuests) { - this.doneQuests = doneQuests; + this.doneQuests = doneQuests; } private Integer questSignUpdateShed; public void addDoneQuest(final Job job) { - doneQuests++; - setSaved(false); + doneQuests++; + setSaved(false); - if (questSignUpdateShed == null) { - questSignUpdateShed = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> { - Jobs.getSignUtil().signUpdate(job, SignTopType.questtoplist); - questSignUpdateShed = null; - }, Jobs.getGCManager().getSavePeriod() * 60 * 20L); - } + if (questSignUpdateShed == null) { + questSignUpdateShed = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> { + Jobs.getSignUtil().signUpdate(job, SignTopType.questtoplist); + questSignUpdateShed = null; + }, Jobs.getGCManager().getSavePeriod() * 60 * 20L); + } } /** @@ -1335,7 +1353,7 @@ public class JobsPlayer { */ @Deprecated public int getFurnaceCount() { - return !plugin.getBlockOwnerShip(BlockTypes.FURNACE).isPresent() ? 0 : plugin.getBlockOwnerShip(BlockTypes.FURNACE).get().getTotal(getUniqueId()); + return !plugin.getBlockOwnerShip(BlockTypes.FURNACE).isPresent() ? 0 : plugin.getBlockOwnerShip(BlockTypes.FURNACE).get().getTotal(getUniqueId()); } /** @@ -1344,7 +1362,7 @@ public class JobsPlayer { */ @Deprecated public int getBrewingStandCount() { - return !plugin.getBlockOwnerShip(BlockTypes.BREWING_STAND).isPresent() ? 0 : plugin.getBlockOwnerShip(BlockTypes.BREWING_STAND).get().getTotal(getUniqueId()); + return !plugin.getBlockOwnerShip(BlockTypes.BREWING_STAND).isPresent() ? 0 : plugin.getBlockOwnerShip(BlockTypes.BREWING_STAND).get().getTotal(getUniqueId()); } /** @@ -1353,12 +1371,12 @@ public class JobsPlayer { */ @Deprecated public int getMaxBrewingStandsAllowed() { - Double maxV = Jobs.getPermissionManager().getMaxPermission(this, "jobs.maxbrewingstands"); + Double maxV = Jobs.getPermissionManager().getMaxPermission(this, "jobs.maxbrewingstands"); - if (maxV == 0) - maxV = (double) Jobs.getGCManager().getBrewingStandsMaxDefault(); + if (maxV == 0) + maxV = (double) Jobs.getGCManager().getBrewingStandsMaxDefault(); - return maxV.intValue(); + return maxV.intValue(); } /** @@ -1367,7 +1385,7 @@ public class JobsPlayer { */ @Deprecated public int getMaxFurnacesAllowed() { - return getMaxOwnerShipAllowed(BlockTypes.FURNACE); + return getMaxOwnerShipAllowed(BlockTypes.FURNACE); } /** @@ -1376,82 +1394,82 @@ public class JobsPlayer { * @return max allowed owner ship */ public int getMaxOwnerShipAllowed(BlockTypes type) { - double maxV = Jobs.getPermissionManager().getMaxPermission(this, "jobs.maxownership"); - if (maxV > 0D) { - return (int) maxV; - } + double maxV = Jobs.getPermissionManager().getMaxPermission(this, "jobs.maxownership"); + if (maxV > 0D) { + return (int) maxV; + } - if (type != BlockTypes.BREWING_STAND && - (maxV = Jobs.getPermissionManager().getMaxPermission(this, "jobs.maxfurnaceownership")) > 0D) { - return (int) maxV; - } + if (type != BlockTypes.BREWING_STAND && + (maxV = Jobs.getPermissionManager().getMaxPermission(this, "jobs.maxfurnaceownership")) > 0D) { + return (int) maxV; + } - String perm = "jobs.max" + (type == BlockTypes.FURNACE - ? "furnaces" : type == BlockTypes.BLAST_FURNACE ? "blastfurnaces" : type == BlockTypes.SMOKER ? "smokers" - : type == BlockTypes.BREWING_STAND ? "brewingstands" : ""); + String perm = "jobs.max" + (type == BlockTypes.FURNACE + ? "furnaces" : type == BlockTypes.BLAST_FURNACE ? "blastfurnaces" : type == BlockTypes.SMOKER ? "smokers" + : type == BlockTypes.BREWING_STAND ? "brewingstands" : ""); - maxV = Jobs.getPermissionManager().getMaxPermission(this, perm); + maxV = Jobs.getPermissionManager().getMaxPermission(this, perm); - if (maxV == 0D && type == BlockTypes.FURNACE) - maxV = Jobs.getGCManager().getFurnacesMaxDefault(); + if (maxV == 0D && type == BlockTypes.FURNACE) + maxV = Jobs.getGCManager().getFurnacesMaxDefault(); - if (maxV == 0D && type == BlockTypes.BLAST_FURNACE) - maxV = Jobs.getGCManager().BlastFurnacesMaxDefault; + if (maxV == 0D && type == BlockTypes.BLAST_FURNACE) + maxV = Jobs.getGCManager().BlastFurnacesMaxDefault; - if (maxV == 0D && type == BlockTypes.SMOKER) - maxV = Jobs.getGCManager().SmokersMaxDefault; + if (maxV == 0D && type == BlockTypes.SMOKER) + maxV = Jobs.getGCManager().SmokersMaxDefault; - if (maxV == 0D && type == BlockTypes.BREWING_STAND) - maxV = Jobs.getGCManager().getBrewingStandsMaxDefault(); + if (maxV == 0D && type == BlockTypes.BREWING_STAND) + maxV = Jobs.getGCManager().getBrewingStandsMaxDefault(); - return (int) maxV; + return (int) maxV; } public int getSkippedQuests() { - return skippedQuests; + return skippedQuests; } public void setSkippedQuests(int skippedQuests) { - this.skippedQuests = skippedQuests; + this.skippedQuests = skippedQuests; } public Map> getLeftTimes() { - return leftTimes; + return leftTimes; } public boolean isLeftTimeEnded(Job job) { - Map map = leftTimes.get(getUniqueId()); - Long time = map != null ? map.get(job) : null; - return time != null && time.longValue() < System.currentTimeMillis(); + Map map = leftTimes.get(getUniqueId()); + Long time = map != null ? map.get(job) : null; + return time != null && time.longValue() < System.currentTimeMillis(); } public void setLeftTime(Job job) { - UUID uuid = getUniqueId(); - leftTimes.remove(uuid); + UUID uuid = getUniqueId(); + leftTimes.remove(uuid); - int hour = Jobs.getGCManager().jobExpiryTime; - if (hour == 0) - return; + int hour = Jobs.getGCManager().jobExpiryTime; + if (hour == 0) + return; - Calendar cal = Calendar.getInstance(); - cal.setTime(new Date()); + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); - cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + hour); + cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + hour); - Map map = new HashMap<>(); - map.put(job, cal.getTimeInMillis()); - leftTimes.put(uuid, map); + Map map = new HashMap<>(); + map.put(job, cal.getTimeInMillis()); + leftTimes.put(uuid, map); } public boolean hasBlockOwnerShipInform(String location) { - if (blockOwnerShipInform == null) - return false; - return blockOwnerShipInform.contains(location); + if (blockOwnerShipInform == null) + return false; + return blockOwnerShipInform.contains(location); } public void addBlockOwnerShipInform(String location) { - if (blockOwnerShipInform == null) - blockOwnerShipInform = new HashSet(); - this.blockOwnerShipInform.add(location); + if (blockOwnerShipInform == null) + blockOwnerShipInform = new HashSet(); + this.blockOwnerShipInform.add(location); } } diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsConnection.java b/src/main/java/com/gamingmesh/jobs/dao/JobsConnection.java index 8e6c7005..1efd78da 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsConnection.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsConnection.java @@ -39,7 +39,7 @@ public class JobsConnection { conn.close(); } - public synchronized Statement createStatement() throws SQLException { + public synchronized Statement createStatement() throws SQLException { return conn.createStatement(); } diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 36911932..d313c6aa 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -13,6 +13,8 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -55,448 +57,448 @@ public abstract class JobsDAO { // Not in use currently public enum TablesFieldsType { - decimal, number, text, varchar, stringList, stringLongMap, stringIntMap, locationMap, state, location, longNumber; + decimal, number, text, varchar, stringList, stringLongMap, stringIntMap, locationMap, state, location, longNumber; } public enum worldsTableFields implements JobsTableInterface { - name("varchar(36)", true); + name("varchar(36)", true); - private String type; + private String type; - private boolean unique = false; + private boolean unique = false; - worldsTableFields(String type) { - this(type, false); - } + worldsTableFields(String type) { + this(type, false); + } - worldsTableFields(String type, boolean unique) { - this.type = type; - this.unique = unique; - } + worldsTableFields(String type, boolean unique) { + this.type = type; + this.unique = unique; + } - @Override - public String getCollumn() { - return name(); - } + @Override + public String getCollumn() { + return name(); + } - @Override - public String getType() { - return type; - } + @Override + public String getType() { + return type; + } - @Override - public boolean isUnique() { - return unique; - } + @Override + public boolean isUnique() { + return unique; + } } public enum jobsNameTableFields implements JobsTableInterface { - name("varchar(36)", true); + name("varchar(36)", true); - private String type; + private String type; - private boolean unique = false; + private boolean unique = false; - jobsNameTableFields(String type) { - this(type, false); - } + jobsNameTableFields(String type) { + this(type, false); + } - jobsNameTableFields(String type, boolean unique) { - this.type = type; - this.unique = unique; - } + jobsNameTableFields(String type, boolean unique) { + this.type = type; + this.unique = unique; + } - @Override - public String getCollumn() { - return name(); - } + @Override + public String getCollumn() { + return name(); + } - @Override - public String getType() { - return type; - } + @Override + public String getType() { + return type; + } - @Override - public boolean isUnique() { - return unique; - } + @Override + public boolean isUnique() { + return unique; + } } public enum UserTableFields implements JobsTableInterface { - player_uuid("varchar(36)"), - username("text"), - seen("bigint"), - donequests("int"), - quests("text"); + player_uuid("varchar(36)"), + username("text"), + seen("bigint"), + donequests("int"), + quests("text"); - private String type; + private String type; - UserTableFields(String type) { - this.type = type; - } + UserTableFields(String type) { + this.type = type; + } - @Override - public String getCollumn() { - return name(); - } + @Override + public String getCollumn() { + return name(); + } - @Override - public String getType() { - return type; - } + @Override + public String getType() { + return type; + } - @Override - public boolean isUnique() { - return false; - } + @Override + public boolean isUnique() { + return false; + } } public enum JobsTableFields implements JobsTableInterface { - userid("int"), - job("text"), - experience("double"), - level("int"), - jobid("int"); + userid("int"), + job("text"), + experience("double"), + level("int"), + jobid("int"); - private String type; + private String type; - JobsTableFields(String type) { - this.type = type; - } + JobsTableFields(String type) { + this.type = type; + } - @Override - public String getCollumn() { - return name(); - } + @Override + public String getCollumn() { + return name(); + } - @Override - public String getType() { - return type; - } + @Override + public String getType() { + return type; + } - @Override - public boolean isUnique() { - return false; - } + @Override + public boolean isUnique() { + return false; + } } public enum ArchiveTableFields implements JobsTableInterface { - userid("int"), - job("text"), - experience("int"), - level("int"), - left("bigint"), - jobid("int"); + userid("int"), + job("text"), + experience("int"), + level("int"), + left("bigint"), + jobid("int"); - private String type; + private String type; - ArchiveTableFields(String type) { - this.type = type; - } + ArchiveTableFields(String type) { + this.type = type; + } - @Override - public String getCollumn() { - return name(); - } + @Override + public String getCollumn() { + return name(); + } - @Override - public String getType() { - return type; - } + @Override + public String getType() { + return type; + } - @Override - public boolean isUnique() { - return false; - } + @Override + public boolean isUnique() { + return false; + } } public enum BlockTableFields implements JobsTableInterface { - world("varchar(36)"), - x("int"), - y("int"), - z("int"), - recorded("bigint"), - resets("bigint"), - worldid("int"); + world("varchar(36)"), + x("int"), + y("int"), + z("int"), + recorded("bigint"), + resets("bigint"), + worldid("int"); - private String type; + private String type; - BlockTableFields(String type) { - this.type = type; - } + BlockTableFields(String type) { + this.type = type; + } - @Override - public String getCollumn() { - return name(); - } + @Override + public String getCollumn() { + return name(); + } - @Override - public String getType() { - return type; - } + @Override + public String getType() { + return type; + } - @Override - public boolean isUnique() { - return false; - } + @Override + public boolean isUnique() { + return false; + } } public enum LimitTableFields implements JobsTableInterface { - userid("int"), - type("varchar(36)"), - collected("double"), - started("bigint"), - typeid("int"); + userid("int"), + type("varchar(36)"), + collected("double"), + started("bigint"), + typeid("int"); - private String ttype; + private String ttype; - LimitTableFields(String type) { - this.ttype = type; - } + LimitTableFields(String type) { + this.ttype = type; + } - @Override - public String getCollumn() { - return name(); - } + @Override + public String getCollumn() { + return name(); + } - @Override - public String getType() { - return ttype; - } + @Override + public String getType() { + return ttype; + } - @Override - public boolean isUnique() { - return false; - } + @Override + public boolean isUnique() { + return false; + } } public enum LogTableFields implements JobsTableInterface { - userid("int"), - time("bigint"), - action("varchar(20)"), - itemname("text"), - count("int"), - money("double"), - exp("double"), - points("double"); + userid("int"), + time("bigint"), + action("varchar(20)"), + itemname("text"), + count("int"), + money("double"), + exp("double"), + points("double"); - private String type; + private String type; - LogTableFields(String type) { - this.type = type; - } + LogTableFields(String type) { + this.type = type; + } - @Override - public String getCollumn() { - return name(); - } + @Override + public String getCollumn() { + return name(); + } - @Override - public String getType() { - return type; - } + @Override + public String getType() { + return type; + } - @Override - public boolean isUnique() { - return false; - } + @Override + public boolean isUnique() { + return false; + } } public enum PointsTableFields implements JobsTableInterface { - userid("int"), - totalpoints("double"), - currentpoints("double"); + userid("int"), + totalpoints("double"), + currentpoints("double"); - private String type; + private String type; - PointsTableFields(String type) { - this.type = type; - } + PointsTableFields(String type) { + this.type = type; + } - @Override - public String getCollumn() { - return name(); - } + @Override + public String getCollumn() { + return name(); + } - @Override - public String getType() { - return type; - } + @Override + public String getType() { + return type; + } - @Override - public boolean isUnique() { - return false; - } + @Override + public boolean isUnique() { + return false; + } } public enum ExploreDataTableFields implements JobsTableInterface { - worldname("varchar(64)"), - chunkX("int"), - chunkZ("int"), - playerNames("text"), - worldid("int"); + worldname("varchar(64)"), + chunkX("int"), + chunkZ("int"), + playerNames("text"), + worldid("int"); - private String type; + private String type; - ExploreDataTableFields(String type) { - this.type = type; + ExploreDataTableFields(String type) { + this.type = type; - } + } - @Override - public String getCollumn() { - return name(); - } + @Override + public String getCollumn() { + return name(); + } - @Override - public String getType() { - return type; - } + @Override + public String getType() { + return type; + } - @Override - public boolean isUnique() { - return false; - } + @Override + public boolean isUnique() { + return false; + } } public enum DBTables { - JobNameTable("jobNames", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", jobsNameTableFields.class), - WorldTable("worlds", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", worldsTableFields.class), - UsersTable("users", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", UserTableFields.class), - JobsTable("jobs", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", JobsTableFields.class), - ArchiveTable("archive", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", ArchiveTableFields.class), - BlocksTable("blocks", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", BlockTableFields.class), - LimitsTable("limits", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", LimitTableFields.class), - LogTable("log", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", LogTableFields.class), - ExploreDataTable("exploreData", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", ExploreDataTableFields.class), - PointsTable("points", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", - "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", PointsTableFields.class); + JobNameTable("jobNames", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", jobsNameTableFields.class), + WorldTable("worlds", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", worldsTableFields.class), + UsersTable("users", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", UserTableFields.class), + JobsTable("jobs", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", JobsTableFields.class), + ArchiveTable("archive", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", ArchiveTableFields.class), + BlocksTable("blocks", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", BlockTableFields.class), + LimitsTable("limits", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", LimitTableFields.class), + LogTable("log", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", LogTableFields.class), + ExploreDataTable("exploreData", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", ExploreDataTableFields.class), + PointsTable("points", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY[fields]);", + "CREATE TABLE IF NOT EXISTS `[tableName]` (`id` INTEGER PRIMARY KEY AUTOINCREMENT[fields]);", PointsTableFields.class); - private String mySQL; - private String sQlite; - private String tableName; - private JobsTableInterface[] c; + private String mySQL; + private String sQlite; + private String tableName; + private JobsTableInterface[] c; - DBTables(String tableName, String MySQL, String SQlite, Class cc) { - this.tableName = tableName; - this.mySQL = MySQL; - this.sQlite = SQlite; - this.c = (JobsTableInterface[]) cc.getEnumConstants(); - } + DBTables(String tableName, String MySQL, String SQlite, Class cc) { + this.tableName = tableName; + this.mySQL = MySQL; + this.sQlite = SQlite; + this.c = (JobsTableInterface[]) cc.getEnumConstants(); + } - private String getQR() { - switch (dbType) { - case MySQL: - return mySQL.replace("[tableName]", prefix + tableName); - case SqLite: - return sQlite.replace("[tableName]", tableName); - default: - break; - } - return ""; - } + private String getQR() { + switch (dbType) { + case MySQL: + return mySQL.replace("[tableName]", prefix + tableName); + case SqLite: + return sQlite.replace("[tableName]", tableName); + default: + break; + } + return ""; + } - public String getQuery() { - String rp = ""; - List uniques = new ArrayList<>(); - for (JobsTableInterface one : c) { - if (one.isUnique()) { - uniques.add(one); - } + public String getQuery() { + String rp = ""; + List uniques = new ArrayList<>(); + for (JobsTableInterface one : c) { + if (one.isUnique()) { + uniques.add(one); + } - rp += " , `" + one.getCollumn() + "` " + one.getType(); - } + rp += " , `" + one.getCollumn() + "` " + one.getType(); + } - String unique = ""; + String unique = ""; - for (JobsTableInterface one : uniques) { - if (!unique.isEmpty()) { - unique += " ,"; - } - unique += "`" + one.getCollumn() + "`"; - } + for (JobsTableInterface one : uniques) { + if (!unique.isEmpty()) { + unique += " ,"; + } + unique += "`" + one.getCollumn() + "`"; + } - if (!unique.isEmpty()) { - switch (dbType) { - case MySQL: - unique = " , UNIQUE KEY template_" + tableName + " (" + unique + ")"; - break; - case SqLite: - unique = " , UNIQUE (" + unique + ")"; - break; - default: - break; - } - } + if (!unique.isEmpty()) { + switch (dbType) { + case MySQL: + unique = " , UNIQUE KEY template_" + tableName + " (" + unique + ")"; + break; + case SqLite: + unique = " , UNIQUE (" + unique + ")"; + break; + default: + break; + } + } - return getQR().replace("[fields]", rp + unique); - } + return getQR().replace("[fields]", rp + unique); + } - public JobsTableInterface[] getInterface() { - return c; - } + public JobsTableInterface[] getInterface() { + return c; + } - public String getTableName() { - return prefix + tableName; - } + public String getTableName() { + return prefix + tableName; + } } protected JobsDAO(Jobs plugin, String driverName, String url, String username, String password, String pr) { - this.plugin = plugin; + this.plugin = plugin; - prefix = pr; + prefix = pr; - try { - Class.forName(driverName); - } catch (ClassNotFoundException c) { - c.printStackTrace(); - return; - } + try { + Class.forName(driverName); + } catch (ClassNotFoundException c) { + c.printStackTrace(); + return; + } - try { - pool = new JobsConnectionPool(driverName, url, username, password); - } catch (Exception e) { - e.printStackTrace(); - } + try { + pool = new JobsConnectionPool(driverName, url, username, password); + } catch (Exception e) { + e.printStackTrace(); + } } public final synchronized boolean setUp() { - if (getConnection() == null) { - CMIMessages.consoleMessage("&cFAILED to connect to database"); - return false; - } + if (getConnection() == null) { + CMIMessages.consoleMessage("&cFAILED to connect to database"); + return false; + } - CMIMessages.consoleMessage("&eConnected to database (&6" + dbType + "&e)"); + CMIMessages.consoleMessage("&eConnected to database (&6" + dbType + "&e)"); - vacuum(); + vacuum(); - try { - for (DBTables one : DBTables.values()) { - createDefaultTable(one); - } - checkDefaultCollumns(); - } finally { - } - return true; + try { + for (DBTables one : DBTables.values()) { + createDefaultTable(one); + } + checkDefaultCollumns(); + } finally { + } + return true; } protected abstract void checkUpdate() throws SQLException; @@ -516,55 +518,55 @@ public abstract class JobsDAO { public abstract boolean drop(String table); public boolean isConnected() { - if (pool == null) { - return false; - } + if (pool == null) { + return false; + } - try { - JobsConnection conn = pool.getConnection(); - return conn != null && !conn.isClosed(); - } catch (SQLException e) { - return false; - } + try { + JobsConnection conn = pool.getConnection(); + return conn != null && !conn.isClosed(); + } catch (SQLException e) { + return false; + } } private boolean createDefaultTable(DBTables table) { - if (isTable(table.getTableName())) - return true; - try { - createTable(table.getQuery()); - return true; - } catch (SQLException e) { - e.printStackTrace(); - } - return false; + if (isTable(table.getTableName())) + return true; + try { + createTable(table.getQuery()); + return true; + } catch (SQLException e) { + e.printStackTrace(); + } + return false; } private boolean checkDefaultCollumns() { - for (DBTables one : DBTables.values()) { - String tableName = one.getTableName(); + for (DBTables one : DBTables.values()) { + String tableName = one.getTableName(); - for (JobsTableInterface oneT : one.getInterface()) { - if (!isCollumn(tableName, oneT.getCollumn())) - addCollumn(tableName, oneT.getCollumn(), oneT.getType()); - } - } + for (JobsTableInterface oneT : one.getInterface()) { + if (!isCollumn(tableName, oneT.getCollumn())) + addCollumn(tableName, oneT.getCollumn(), oneT.getType()); + } + } - return true; + return true; } public void truncateAllTables() { - for (DBTables one : DBTables.values()) { - truncate(one.getTableName()); - } + for (DBTables one : DBTables.values()) { + truncate(one.getTableName()); + } } public DataBaseType getDbType() { - return dbType; + return dbType; } public void setDbType(DataBaseType dabType) { - dbType = dabType; + dbType = dabType; } /** @@ -572,11 +574,11 @@ public abstract class JobsDAO { * @return the prefix */ protected String getPrefix() { - return prefix; + return prefix; } public List getAllJobs(OfflinePlayer player) { - return getAllJobs(player.getName(), player.getUniqueId()); + return getAllJobs(player.getName(), player.getUniqueId()); } /** @@ -586,697 +588,697 @@ public abstract class JobsDAO { */ public List getAllJobs(String playerName, UUID uuid) { - PlayerInfo userData = null; - if (Jobs.getGCManager().MultiServerCompatability()) - userData = loadPlayerData(uuid); - else - userData = Jobs.getPlayerManager().getPlayerInfo(uuid); + PlayerInfo userData = null; + if (Jobs.getGCManager().MultiServerCompatability()) + userData = loadPlayerData(uuid); + else + userData = Jobs.getPlayerManager().getPlayerInfo(uuid); - List jobs = new ArrayList<>(); + List jobs = new ArrayList<>(); - if (userData == null) { - recordNewPlayer(playerName, uuid); - return jobs; - } + if (userData == null) { + recordNewPlayer(playerName, uuid); + return jobs; + } - JobsConnection conn = getConnection(); - if (conn == null) - return jobs; + JobsConnection conn = getConnection(); + if (conn == null) + return jobs; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.userid.getCollumn() + "` = ?;"); - prest.setInt(1, userData.getID()); - res = prest.executeQuery(); - while (res.next()) { - int jobId = res.getInt(JobsTableFields.jobid.getCollumn()); - if (jobId == 0) { - jobs.add(new JobsDAOData(res.getString(JobsTableFields.job.getCollumn()), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); - } else { - Job job = Jobs.getJob(jobId); - if (job != null) - jobs.add(new JobsDAOData(job.getName(), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); - } - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return jobs; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.userid.getCollumn() + "` = ?;"); + prest.setInt(1, userData.getID()); + res = prest.executeQuery(); + while (res.next()) { + int jobId = res.getInt(JobsTableFields.jobid.getCollumn()); + if (jobId == 0) { + jobs.add(new JobsDAOData(res.getString(JobsTableFields.job.getCollumn()), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); + } else { + Job job = Jobs.getJob(jobId); + if (job != null) + jobs.add(new JobsDAOData(job.getName(), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + return jobs; } public Map> getAllJobs() { - Map> map = new HashMap<>(); - JobsConnection conn = getConnection(); - if (conn == null) - return map; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + getJobsTableName() + "`;"); - res = prest.executeQuery(); - while (res.next()) { - int id = res.getInt(JobsTableFields.userid.getCollumn()); + Map> map = new HashMap<>(); + JobsConnection conn = getConnection(); + if (conn == null) + return map; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + getJobsTableName() + "`;"); + res = prest.executeQuery(); + while (res.next()) { + int id = res.getInt(JobsTableFields.userid.getCollumn()); - List ls = map.get(id); - if (ls == null) - ls = new ArrayList<>(); + List ls = map.get(id); + if (ls == null) + 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()))); - converted = false; - } else { - // This should be removed when we switch over to id only method - if (converted && res.getString(JobsTableFields.job.getCollumn()) == null || res.getString(JobsTableFields.job.getCollumn()).isEmpty()) - converted = false; + 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()))); + converted = false; + } else { + // This should be removed when we switch over to id only method + if (converted && res.getString(JobsTableFields.job.getCollumn()) == null || res.getString(JobsTableFields.job.getCollumn()).isEmpty()) + converted = false; - Job job = Jobs.getJob(jobId); - if (job != null) { - ls.add(new JobsDAOData(job.getName(), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); - } - } + Job job = Jobs.getJob(jobId); + if (job != null) { + ls.add(new JobsDAOData(job.getName(), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); + } + } - map.put(id, ls); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return map; + map.put(id, ls); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + return map; } public Map getAllPoints() { - Map map = new HashMap<>(); - JobsConnection conn = getConnection(); - if (conn == null) - return map; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.PointsTable.getTableName() + "`;"); - res = prest.executeQuery(); - while (res.next()) { - map.put(res.getInt(PointsTableFields.userid.getCollumn()), new PlayerPoints(res.getDouble(PointsTableFields.currentpoints.getCollumn()), res.getDouble(PointsTableFields.totalpoints - .getCollumn()))); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return map; + Map map = new HashMap<>(); + JobsConnection conn = getConnection(); + if (conn == null) + return map; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.PointsTable.getTableName() + "`;"); + res = prest.executeQuery(); + while (res.next()) { + map.put(res.getInt(PointsTableFields.userid.getCollumn()), new PlayerPoints(res.getDouble(PointsTableFields.currentpoints.getCollumn()), res.getDouble(PointsTableFields.totalpoints + .getCollumn()))); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + return map; } public PlayerPoints getPlayerPoints(JobsPlayer player) { - PlayerPoints points = new PlayerPoints(); - JobsConnection conn = getConnection(); - if (conn == null) - return points; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.PointsTable.getTableName() + "` WHERE `" + PointsTableFields.userid.getCollumn() + "` = ?;"); - prest.setInt(1, player.getUserId()); - res = prest.executeQuery(); - while (res.next()) { - points = new PlayerPoints(res.getDouble(PointsTableFields.currentpoints.getCollumn()), res.getDouble(PointsTableFields.totalpoints.getCollumn())); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return points; + PlayerPoints points = new PlayerPoints(); + JobsConnection conn = getConnection(); + if (conn == null) + return points; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.PointsTable.getTableName() + "` WHERE `" + PointsTableFields.userid.getCollumn() + "` = ?;"); + prest.setInt(1, player.getUserId()); + res = prest.executeQuery(); + while (res.next()) { + points = new PlayerPoints(res.getDouble(PointsTableFields.currentpoints.getCollumn()), res.getDouble(PointsTableFields.totalpoints.getCollumn())); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + return points; } public Map getAllArchivedJobs() { - Map map = new HashMap<>(); - JobsConnection conn = getConnection(); - if (conn == null) - return map; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.ArchiveTable.getTableName() + "`;"); - res = prest.executeQuery(); - while (res.next()) { + Map map = new HashMap<>(); + JobsConnection conn = getConnection(); + if (conn == null) + return map; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.ArchiveTable.getTableName() + "`;"); + res = prest.executeQuery(); + while (res.next()) { - int id = res.getInt(ArchiveTableFields.userid.getCollumn()); - String jobName = res.getString(ArchiveTableFields.job.getCollumn()); - Double exp = res.getDouble(ArchiveTableFields.experience.getCollumn()); - int lvl = res.getInt(ArchiveTableFields.level.getCollumn()); - Long left = res.getLong(ArchiveTableFields.left.getCollumn()); - int jobid = res.getInt(ArchiveTableFields.jobid.getCollumn()); + int id = res.getInt(ArchiveTableFields.userid.getCollumn()); + String jobName = res.getString(ArchiveTableFields.job.getCollumn()); + Double exp = res.getDouble(ArchiveTableFields.experience.getCollumn()); + int lvl = res.getInt(ArchiveTableFields.level.getCollumn()); + Long left = res.getLong(ArchiveTableFields.left.getCollumn()); + int jobid = res.getInt(ArchiveTableFields.jobid.getCollumn()); - Job job = null; - if (jobid != 0) { - job = Jobs.getJob(jobid); - } else { - job = Jobs.getJob(jobName); - converted = false; - } + Job job = null; + if (jobid != 0) { + job = Jobs.getJob(jobid); + } else { + job = Jobs.getJob(jobName); + converted = false; + } - if (job == null) - continue; + if (job == null) + continue; - ArchivedJobs m = map.get(id); - if (m == null) - m = new ArchivedJobs(); - JobProgression jp = new JobProgression(job, null, lvl, exp); - if (left != 0L) - jp.setLeftOn(left); - m.addArchivedJob(jp); - map.put(id, m); - } - } catch (Exception e) { - close(res); - close(prest); - } finally { - close(res); - close(prest); - } - return map; + ArchivedJobs m = map.get(id); + if (m == null) + m = new ArchivedJobs(); + JobProgression jp = new JobProgression(job, null, lvl, exp); + if (left != 0L) + jp.setLeftOn(left); + m.addArchivedJob(jp); + map.put(id, m); + } + } catch (Exception e) { + close(res); + close(prest); + } finally { + close(res); + close(prest); + } + return map; } public ArchivedJobs getArchivedJobs(JobsPlayer player) { - ArchivedJobs jobs = new ArchivedJobs(); - JobsConnection conn = getConnection(); - if (conn == null || player == null) - return jobs; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.ArchiveTable.getTableName() + "` WHERE `" + ArchiveTableFields.userid.getCollumn() + "` = ?;"); - prest.setInt(1, player.getUserId()); - res = prest.executeQuery(); - while (res.next()) { + ArchivedJobs jobs = new ArchivedJobs(); + JobsConnection conn = getConnection(); + if (conn == null || player == null) + return jobs; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.ArchiveTable.getTableName() + "` WHERE `" + ArchiveTableFields.userid.getCollumn() + "` = ?;"); + prest.setInt(1, player.getUserId()); + res = prest.executeQuery(); + while (res.next()) { - String jobName = res.getString(ArchiveTableFields.job.getCollumn()); - double exp = res.getDouble(ArchiveTableFields.experience.getCollumn()); - int lvl = res.getInt(ArchiveTableFields.level.getCollumn()); - Long left = res.getLong(ArchiveTableFields.left.getCollumn()); - int jobid = res.getInt(ArchiveTableFields.jobid.getCollumn()); + String jobName = res.getString(ArchiveTableFields.job.getCollumn()); + double exp = res.getDouble(ArchiveTableFields.experience.getCollumn()); + int lvl = res.getInt(ArchiveTableFields.level.getCollumn()); + Long left = res.getLong(ArchiveTableFields.left.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 = null; + if (jobid != 0) { + job = Jobs.getJob(jobid); + } else { + job = Jobs.getJob(jobName); + } - if (job == null) - continue; + if (job == null) + continue; - JobProgression jp = new JobProgression(job, player, lvl, exp); - if (left != 0L) - jp.setLeftOn(left); - jobs.addArchivedJob(jp); - } - } catch (Exception e) { - close(res); - close(prest); - } finally { - close(res); - close(prest); - } - return jobs; + JobProgression jp = new JobProgression(job, player, lvl, exp); + if (left != 0L) + jp.setLeftOn(left); + jobs.addArchivedJob(jp); + } + } catch (Exception e) { + close(res); + close(prest); + } finally { + close(res); + close(prest); + } + return jobs; } public Map> getAllLogs() { - Map> map = new HashMap<>(); - JobsConnection conn = getConnection(); - if (conn == null) - return map; - PreparedStatement prest = null; - ResultSet res = null; - try { - int time = TimeManage.timeInInt(); - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.LogTable.getTableName() + "` WHERE `" + LogTableFields.time.getCollumn() + "` = ? ;"); - prest.setInt(1, time); - res = prest.executeQuery(); - while (res.next()) { - int id = res.getInt(LogTableFields.userid.getCollumn()); - String action = res.getString(LogTableFields.action.getCollumn()); + Map> map = new HashMap<>(); + JobsConnection conn = getConnection(); + if (conn == null) + return map; + PreparedStatement prest = null; + ResultSet res = null; + try { + int time = TimeManage.timeInInt(); + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.LogTable.getTableName() + "` WHERE `" + LogTableFields.time.getCollumn() + "` = ? ;"); + prest.setInt(1, time); + res = prest.executeQuery(); + while (res.next()) { + int id = res.getInt(LogTableFields.userid.getCollumn()); + String action = res.getString(LogTableFields.action.getCollumn()); - Map m = map.getOrDefault(id, new HashMap<>()); - Log log = m.getOrDefault(action, new Log(action)); + Map m = map.getOrDefault(id, new HashMap<>()); + Log log = m.getOrDefault(action, new Log(action)); - Map amounts = new HashMap<>(); - amounts.put(CurrencyType.MONEY, res.getDouble(LogTableFields.money.getCollumn())); - amounts.put(CurrencyType.EXP, res.getDouble(LogTableFields.exp.getCollumn())); - amounts.put(CurrencyType.POINTS, res.getDouble(LogTableFields.points.getCollumn())); + Map amounts = new HashMap<>(); + amounts.put(CurrencyType.MONEY, res.getDouble(LogTableFields.money.getCollumn())); + amounts.put(CurrencyType.EXP, res.getDouble(LogTableFields.exp.getCollumn())); + amounts.put(CurrencyType.POINTS, res.getDouble(LogTableFields.points.getCollumn())); - log.add(res.getString(LogTableFields.itemname.getCollumn()), res.getInt(LogTableFields.count.getCollumn()), amounts); + log.add(res.getString(LogTableFields.itemname.getCollumn()), res.getInt(LogTableFields.count.getCollumn()), amounts); - m.put(action, log); - map.put(id, m); + m.put(action, log); + map.put(id, m); // Jobs.getLoging().loadToLog(player, res.getString("action"), res.getString("itemname"), res.getInt("count"), res.getDouble("money"), res.getDouble("exp")); - } - } catch (Exception e) { - close(res); - close(prest); - } finally { - close(res); - close(prest); - } - return map; + } + } catch (Exception e) { + close(res); + close(prest); + } finally { + close(res); + close(prest); + } + return map; } public void cleanUsers() { - if (!Jobs.getGCManager().DBCleaningUsersUse) - return; + if (!Jobs.getGCManager().DBCleaningUsersUse) + return; - JobsConnection conn = getConnection(); - if (conn == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - Calendar cal = Calendar.getInstance(); - cal.add(Calendar.DATE, -Jobs.getGCManager().DBCleaningUsersDays); - long mark = cal.getTimeInMillis(); + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.DATE, -Jobs.getGCManager().DBCleaningUsersDays); + long mark = cal.getTimeInMillis(); - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("DELETE FROM `" + DBTables.UsersTable.getTableName() + "` WHERE `" + UserTableFields.seen.getCollumn() + "` < ?;"); - prest.setLong(1, mark); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - } + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("DELETE FROM `" + DBTables.UsersTable.getTableName() + "` WHERE `" + UserTableFields.seen.getCollumn() + "` < ?;"); + prest.setLong(1, mark); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } } public void cleanJobs() { - if (!Jobs.getGCManager().DBCleaningJobsUse) - return; + if (!Jobs.getGCManager().DBCleaningJobsUse) + return; - JobsConnection conn = getConnection(); - if (conn == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("DELETE FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.level.getCollumn() + "` <= ?;"); - prest.setInt(1, Jobs.getGCManager().DBCleaningJobsLvl); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - } + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("DELETE FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.level.getCollumn() + "` <= ?;"); + prest.setInt(1, Jobs.getGCManager().DBCleaningJobsLvl); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } } public void recordNewPlayer(Player player) { - recordNewPlayer(player.getName(), player.getUniqueId()); + recordNewPlayer(player.getName(), player.getUniqueId()); } public void recordNewPlayer(String playerName, UUID uuid) { - JobsConnection conn = getConnection(); - if (conn == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - // Checking possible record in database to avoid duplicates - PlayerInfo info = loadPlayerData(uuid); - if (info != null) { - Jobs.getPlayerManager().addPlayerToMap(info); - return; - } + // Checking possible record in database to avoid duplicates + PlayerInfo info = loadPlayerData(uuid); + if (info != null) { + Jobs.getPlayerManager().addPlayerToMap(info); + return; + } - PreparedStatement prestt = null; - ResultSet res2 = null; - try { - prestt = conn.prepareStatement("INSERT INTO `" + DBTables.UsersTable.getTableName() + "` (`" + UserTableFields.player_uuid.getCollumn() - + "`, `" + UserTableFields.username.getCollumn() - + "`, `" + UserTableFields.seen.getCollumn() - + "`, `" + UserTableFields.donequests.getCollumn() - + "`) VALUES (?, ?, ?, ?);", - Statement.RETURN_GENERATED_KEYS); - prestt.setString(1, uuid.toString()); - prestt.setString(2, playerName); - prestt.setLong(3, System.currentTimeMillis()); - prestt.setInt(4, 0); - prestt.execute(); + PreparedStatement prestt = null; + ResultSet res2 = null; + try { + prestt = conn.prepareStatement("INSERT INTO `" + DBTables.UsersTable.getTableName() + "` (`" + UserTableFields.player_uuid.getCollumn() + + "`, `" + UserTableFields.username.getCollumn() + + "`, `" + UserTableFields.seen.getCollumn() + + "`, `" + UserTableFields.donequests.getCollumn() + + "`) VALUES (?, ?, ?, ?);", + Statement.RETURN_GENERATED_KEYS); + prestt.setString(1, uuid.toString()); + prestt.setString(2, playerName); + prestt.setLong(3, System.currentTimeMillis()); + prestt.setInt(4, 0); + prestt.execute(); - res2 = prestt.getGeneratedKeys(); + res2 = prestt.getGeneratedKeys(); - Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo(playerName, res2.next() ? res2.getInt(1) : 0, uuid, System.currentTimeMillis(), 0)); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prestt); - close(res2); - } + Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo(playerName, res2.next() ? res2.getInt(1) : 0, uuid, System.currentTimeMillis(), 0)); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prestt); + close(res2); + } } public void recordNewWorld(String worldName) { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prestt = null; - ResultSet res2 = null; - try { - prestt = conn.prepareStatement("INSERT INTO `" + DBTables.WorldTable.getTableName() + "` (`" + worldsTableFields.name.getCollumn() + "`) VALUES (?);", - Statement.RETURN_GENERATED_KEYS); - prestt.setString(1, worldName); - prestt.executeUpdate(); + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prestt = null; + ResultSet res2 = null; + try { + prestt = conn.prepareStatement("INSERT INTO `" + DBTables.WorldTable.getTableName() + "` (`" + worldsTableFields.name.getCollumn() + "`) VALUES (?);", + Statement.RETURN_GENERATED_KEYS); + prestt.setString(1, worldName); + prestt.executeUpdate(); - res2 = prestt.getGeneratedKeys(); - Util.addJobsWorld(new JobsWorld(worldName, res2.next() ? res2.getInt(1) : 0)); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prestt); - close(res2); - } + res2 = prestt.getGeneratedKeys(); + Util.addJobsWorld(new JobsWorld(worldName, res2.next() ? res2.getInt(1) : 0)); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prestt); + close(res2); + } } public void recordNewWorld(String worldName, int id) { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prestt = null; - ResultSet res2 = null; - try { - prestt = conn.prepareStatement("INSERT INTO `" + DBTables.WorldTable.getTableName() + "` (`id`,`" + worldsTableFields.name.getCollumn() + "`) VALUES (?,?);", - Statement.RETURN_GENERATED_KEYS); - prestt.setInt(1, id); - prestt.setString(2, worldName); - prestt.executeUpdate(); + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prestt = null; + ResultSet res2 = null; + try { + prestt = conn.prepareStatement("INSERT INTO `" + DBTables.WorldTable.getTableName() + "` (`id`,`" + worldsTableFields.name.getCollumn() + "`) VALUES (?,?);", + Statement.RETURN_GENERATED_KEYS); + prestt.setInt(1, id); + prestt.setString(2, worldName); + prestt.executeUpdate(); - res2 = prestt.getGeneratedKeys(); - Util.addJobsWorld(new JobsWorld(worldName, res2.next() ? res2.getInt(1) : 0)); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prestt); - close(res2); - } + res2 = prestt.getGeneratedKeys(); + Util.addJobsWorld(new JobsWorld(worldName, res2.next() ? res2.getInt(1) : 0)); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prestt); + close(res2); + } } public synchronized void loadAllJobsWorlds() { - JobsConnection conn = getConnection(); - if (conn == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.WorldTable.getTableName() + "`;"); - res = prest.executeQuery(); - while (res.next()) { - int id = res.getInt("id"); - String name = res.getString(worldsTableFields.name.getCollumn()); - Util.addJobsWorld(new JobsWorld(name, id)); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.WorldTable.getTableName() + "`;"); + res = prest.executeQuery(); + while (res.next()) { + int id = res.getInt("id"); + String name = res.getString(worldsTableFields.name.getCollumn()); + Util.addJobsWorld(new JobsWorld(name, id)); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } - for (World one : Bukkit.getWorlds()) { - if (Util.getJobsWorld(one.getName()) == null) - recordNewWorld(one.getName()); - } + for (World one : Bukkit.getWorlds()) { + if (Util.getJobsWorld(one.getName()) == null) + recordNewWorld(one.getName()); + } } private boolean converted = true; public void triggerTableIdUpdate() { - // Lets convert old fields - if (!converted) { - Bukkit.getServer().getScheduler().runTaskLater(plugin, () -> { - Jobs.consoleMsg("&6[Jobs] Converting to new database format"); - convertID(); - Jobs.consoleMsg("&6[Jobs] Converted to new database format"); - converted = true; - }, 60L); - } + // Lets convert old fields + if (!converted) { + Bukkit.getServer().getScheduler().runTaskLater(plugin, () -> { + Jobs.consoleMsg("&6[Jobs] Converting to new database format"); + convertID(); + Jobs.consoleMsg("&6[Jobs] Converted to new database format"); + converted = true; + }, 60L); + } } private void convertID() { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement exploreStatement = null; - try { - exploreStatement = conn.prepareStatement("UPDATE `" + DBTables.ExploreDataTable.getTableName() + "` SET `" + ExploreDataTableFields.worldid.getCollumn() + "` = ? WHERE `" - + ExploreDataTableFields.worldname.getCollumn() + "` = ?;"); - for (JobsWorld jobsWorld : Util.getJobsWorlds().values()) { - exploreStatement.setInt(1, jobsWorld.getId()); - exploreStatement.setString(2, jobsWorld.getName()); - exploreStatement.executeUpdate(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(exploreStatement); - } + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement exploreStatement = null; + try { + exploreStatement = conn.prepareStatement("UPDATE `" + DBTables.ExploreDataTable.getTableName() + "` SET `" + ExploreDataTableFields.worldid.getCollumn() + "` = ? WHERE `" + + ExploreDataTableFields.worldname.getCollumn() + "` = ?;"); + for (JobsWorld jobsWorld : Util.getJobsWorlds().values()) { + exploreStatement.setInt(1, jobsWorld.getId()); + exploreStatement.setString(2, jobsWorld.getName()); + exploreStatement.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(exploreStatement); + } - PreparedStatement exploreStatementBack = null; - try { - exploreStatementBack = conn.prepareStatement("UPDATE `" + DBTables.ExploreDataTable.getTableName() + "` SET `" + ExploreDataTableFields.worldname.getCollumn() + "` = ? WHERE `" - + ExploreDataTableFields.worldid.getCollumn() + "` = ?;"); - for (JobsWorld jobsWorld : Util.getJobsWorlds().values()) { - exploreStatementBack.setString(1, jobsWorld.getName()); - exploreStatementBack.setInt(2, jobsWorld.getId()); - exploreStatementBack.executeUpdate(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(exploreStatementBack); - } + PreparedStatement exploreStatementBack = null; + try { + exploreStatementBack = conn.prepareStatement("UPDATE `" + DBTables.ExploreDataTable.getTableName() + "` SET `" + ExploreDataTableFields.worldname.getCollumn() + "` = ? WHERE `" + + ExploreDataTableFields.worldid.getCollumn() + "` = ?;"); + for (JobsWorld jobsWorld : Util.getJobsWorlds().values()) { + exploreStatementBack.setString(1, jobsWorld.getName()); + exploreStatementBack.setInt(2, jobsWorld.getId()); + exploreStatementBack.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(exploreStatementBack); + } - PreparedStatement bpStatement = null; - try { - bpStatement = conn.prepareStatement("UPDATE `" + DBTables.BlocksTable.getTableName() + "` SET `" + BlockTableFields.worldid.getCollumn() + "` = ? WHERE `" + BlockTableFields.world - .getCollumn() + "` = ?;"); - for (JobsWorld jobsWorld : Util.getJobsWorlds().values()) { - bpStatement.setInt(1, jobsWorld.getId()); - bpStatement.setString(2, jobsWorld.getName()); - bpStatement.executeUpdate(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(bpStatement); - } + PreparedStatement bpStatement = null; + try { + bpStatement = conn.prepareStatement("UPDATE `" + DBTables.BlocksTable.getTableName() + "` SET `" + BlockTableFields.worldid.getCollumn() + "` = ? WHERE `" + BlockTableFields.world + .getCollumn() + "` = ?;"); + for (JobsWorld jobsWorld : Util.getJobsWorlds().values()) { + bpStatement.setInt(1, jobsWorld.getId()); + bpStatement.setString(2, jobsWorld.getName()); + bpStatement.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(bpStatement); + } - PreparedStatement bpStatementback = null; - try { - bpStatementback = conn.prepareStatement("UPDATE `" + DBTables.BlocksTable.getTableName() + "` SET `" + BlockTableFields.world.getCollumn() + "` = ? WHERE `" + BlockTableFields.worldid - .getCollumn() + "` = ?;"); - for (JobsWorld jobsWorld : Util.getJobsWorlds().values()) { - bpStatementback.setString(1, jobsWorld.getName()); - bpStatementback.setInt(2, jobsWorld.getId()); - bpStatementback.executeUpdate(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(bpStatementback); - } + PreparedStatement bpStatementback = null; + try { + bpStatementback = conn.prepareStatement("UPDATE `" + DBTables.BlocksTable.getTableName() + "` SET `" + BlockTableFields.world.getCollumn() + "` = ? WHERE `" + BlockTableFields.worldid + .getCollumn() + "` = ?;"); + for (JobsWorld jobsWorld : Util.getJobsWorlds().values()) { + bpStatementback.setString(1, jobsWorld.getName()); + bpStatementback.setInt(2, jobsWorld.getId()); + bpStatementback.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(bpStatementback); + } - PreparedStatement archiveStatement = null; - try { - archiveStatement = conn.prepareStatement("UPDATE `" + DBTables.ArchiveTable.getTableName() + "` SET `" + ArchiveTableFields.jobid.getCollumn() + "` = ? WHERE `" + ArchiveTableFields.job - .getCollumn() + "` = ?;"); - for (Job job : Jobs.getJobs()) { - archiveStatement.setInt(1, job.getId()); - archiveStatement.setString(2, job.getName()); - archiveStatement.executeUpdate(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(archiveStatement); - } - PreparedStatement archiveStatementBack = null; - try { - archiveStatementBack = conn.prepareStatement("UPDATE `" + DBTables.ArchiveTable.getTableName() + "` SET `" + ArchiveTableFields.job.getCollumn() + "` = ? WHERE `" + ArchiveTableFields.jobid - .getCollumn() + "` = ?;"); - for (Job job : Jobs.getJobs()) { - archiveStatementBack.setString(1, job.getName()); - archiveStatementBack.setInt(2, job.getId()); - archiveStatementBack.executeUpdate(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(archiveStatementBack); - } + PreparedStatement archiveStatement = null; + try { + archiveStatement = conn.prepareStatement("UPDATE `" + DBTables.ArchiveTable.getTableName() + "` SET `" + ArchiveTableFields.jobid.getCollumn() + "` = ? WHERE `" + ArchiveTableFields.job + .getCollumn() + "` = ?;"); + for (Job job : Jobs.getJobs()) { + archiveStatement.setInt(1, job.getId()); + archiveStatement.setString(2, job.getName()); + archiveStatement.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(archiveStatement); + } + PreparedStatement archiveStatementBack = null; + try { + archiveStatementBack = conn.prepareStatement("UPDATE `" + DBTables.ArchiveTable.getTableName() + "` SET `" + ArchiveTableFields.job.getCollumn() + "` = ? WHERE `" + ArchiveTableFields.jobid + .getCollumn() + "` = ?;"); + for (Job job : Jobs.getJobs()) { + archiveStatementBack.setString(1, job.getName()); + archiveStatementBack.setInt(2, job.getId()); + archiveStatementBack.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(archiveStatementBack); + } - PreparedStatement usersStatement = null; - try { - usersStatement = conn.prepareStatement("UPDATE `" + getJobsTableName() + "` SET `" + JobsTableFields.jobid.getCollumn() + "` = ? WHERE `" + JobsTableFields.job.getCollumn() + "` = ?;"); - for (Job job : Jobs.getJobs()) { - usersStatement.setInt(1, job.getId()); - usersStatement.setString(2, job.getName()); - usersStatement.executeUpdate(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(usersStatement); - } - PreparedStatement usersStatementBack = null; - try { - usersStatementBack = conn.prepareStatement("UPDATE `" + getJobsTableName() + "` SET `" + JobsTableFields.job.getCollumn() + "` = ? WHERE `" + JobsTableFields.jobid.getCollumn() + "` = ?;"); - for (Job job : Jobs.getJobs()) { - usersStatementBack.setString(1, job.getName()); - usersStatementBack.setInt(2, job.getId()); - usersStatementBack.executeUpdate(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(usersStatementBack); - } + PreparedStatement usersStatement = null; + try { + usersStatement = conn.prepareStatement("UPDATE `" + getJobsTableName() + "` SET `" + JobsTableFields.jobid.getCollumn() + "` = ? WHERE `" + JobsTableFields.job.getCollumn() + "` = ?;"); + for (Job job : Jobs.getJobs()) { + usersStatement.setInt(1, job.getId()); + usersStatement.setString(2, job.getName()); + usersStatement.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(usersStatement); + } + PreparedStatement usersStatementBack = null; + try { + usersStatementBack = conn.prepareStatement("UPDATE `" + getJobsTableName() + "` SET `" + JobsTableFields.job.getCollumn() + "` = ? WHERE `" + JobsTableFields.jobid.getCollumn() + "` = ?;"); + for (Job job : Jobs.getJobs()) { + usersStatementBack.setString(1, job.getName()); + usersStatementBack.setInt(2, job.getId()); + usersStatementBack.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(usersStatementBack); + } - PreparedStatement limitsStatement = null; - try { - limitsStatement = conn.prepareStatement("UPDATE `" + DBTables.LimitsTable.getTableName() + "` SET `" + LimitTableFields.typeid.getCollumn() + "` = ? WHERE `" + LimitTableFields.type - .getCollumn() + "` = ?;"); - for (CurrencyType type : CurrencyType.values()) { - limitsStatement.setInt(1, type.getId()); - limitsStatement.setString(2, type.getName()); - limitsStatement.executeUpdate(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(limitsStatement); - } + PreparedStatement limitsStatement = null; + try { + limitsStatement = conn.prepareStatement("UPDATE `" + DBTables.LimitsTable.getTableName() + "` SET `" + LimitTableFields.typeid.getCollumn() + "` = ? WHERE `" + LimitTableFields.type + .getCollumn() + "` = ?;"); + for (CurrencyType type : CurrencyType.values()) { + limitsStatement.setInt(1, type.getId()); + limitsStatement.setString(2, type.getName()); + limitsStatement.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(limitsStatement); + } - PreparedStatement limitsStatementBack = null; - try { - limitsStatementBack = conn.prepareStatement("UPDATE `" + DBTables.LimitsTable.getTableName() + "` SET `" + LimitTableFields.type.getCollumn() + "` = ? WHERE `" + LimitTableFields.typeid - .getCollumn() + "` = ?;"); - for (CurrencyType type : CurrencyType.values()) { - limitsStatementBack.setString(1, type.getName()); - limitsStatementBack.setInt(2, type.getId()); - limitsStatementBack.executeUpdate(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(limitsStatementBack); - } + PreparedStatement limitsStatementBack = null; + try { + limitsStatementBack = conn.prepareStatement("UPDATE `" + DBTables.LimitsTable.getTableName() + "` SET `" + LimitTableFields.type.getCollumn() + "` = ? WHERE `" + LimitTableFields.typeid + .getCollumn() + "` = ?;"); + for (CurrencyType type : CurrencyType.values()) { + limitsStatementBack.setString(1, type.getName()); + limitsStatementBack.setInt(2, type.getId()); + limitsStatementBack.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(limitsStatementBack); + } } public void recordNewJobName(Job job) { - JobsConnection conn = getConnection(); - if (conn == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - PreparedStatement prestt = null; - ResultSet res2 = null; - try { - conn.setAutoCommit(false); + PreparedStatement prestt = null; + ResultSet res2 = null; + try { + conn.setAutoCommit(false); - prestt = conn.prepareStatement("INSERT INTO `" + DBTables.JobNameTable.getTableName() + "` (`" + jobsNameTableFields.name.getCollumn() + "`) VALUES (?);", - Statement.RETURN_GENERATED_KEYS); - prestt.setString(1, job.getName()); - int rowAffected = prestt.executeUpdate(); + prestt = conn.prepareStatement("INSERT INTO `" + DBTables.JobNameTable.getTableName() + "` (`" + jobsNameTableFields.name.getCollumn() + "`) VALUES (?);", + Statement.RETURN_GENERATED_KEYS); + prestt.setString(1, job.getName()); + int rowAffected = prestt.executeUpdate(); - res2 = prestt.getGeneratedKeys(); + res2 = prestt.getGeneratedKeys(); - job.setId(res2.next() ? res2.getInt(1) : 0); + job.setId(res2.next() ? res2.getInt(1) : 0); - if (rowAffected != 1) { - conn.getConnection().rollback(); - } + if (rowAffected != 1) { + conn.getConnection().rollback(); + } - conn.commit(); - } catch (SQLException e) { - } finally { - close(prestt); - close(res2); - } + conn.commit(); + } catch (SQLException e) { + } finally { + close(prestt); + close(res2); + } } public void recordNewJobName(Job job, int id) { - JobsConnection conn = getConnection(); - if (conn == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - PreparedStatement prestt = null; - ResultSet res2 = null; - try { - conn.setAutoCommit(false); + PreparedStatement prestt = null; + ResultSet res2 = null; + try { + conn.setAutoCommit(false); - prestt = conn.prepareStatement("INSERT INTO `" + DBTables.JobNameTable.getTableName() + "` (`id`,`" + jobsNameTableFields.name.getCollumn() + "`) VALUES (?,?);", - Statement.RETURN_GENERATED_KEYS); - prestt.setInt(1, id); - prestt.setString(2, job.getName()); - int rowAffected = prestt.executeUpdate(); + prestt = conn.prepareStatement("INSERT INTO `" + DBTables.JobNameTable.getTableName() + "` (`id`,`" + jobsNameTableFields.name.getCollumn() + "`) VALUES (?,?);", + Statement.RETURN_GENERATED_KEYS); + prestt.setInt(1, id); + prestt.setString(2, job.getName()); + int rowAffected = prestt.executeUpdate(); - res2 = prestt.getGeneratedKeys(); + res2 = prestt.getGeneratedKeys(); - job.setId(res2.next() ? res2.getInt(1) : 0); + job.setId(res2.next() ? res2.getInt(1) : 0); - if (rowAffected != 1) { - conn.getConnection().rollback(); - } + if (rowAffected != 1) { + conn.getConnection().rollback(); + } - conn.commit(); - } catch (SQLException e) { - } finally { - close(prestt); - close(res2); - } + conn.commit(); + } catch (SQLException e) { + } finally { + close(prestt); + close(res2); + } } public synchronized void loadAllJobsNames() { - JobsConnection conn = getConnection(); - if (conn == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.JobNameTable.getTableName() + "`;"); - res = prest.executeQuery(); - while (res.next()) { - int id = res.getInt("id"); - String name = res.getString(worldsTableFields.name.getCollumn()); + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.JobNameTable.getTableName() + "`;"); + res = prest.executeQuery(); + while (res.next()) { + int id = res.getInt("id"); + String name = res.getString(worldsTableFields.name.getCollumn()); - Job job = Jobs.getJob(name); - if (job != null) { - if (job.getId() == 0) - job.setId(id); - else { - // Prioritizing id which matches actual job name and not full name which can be different - if (job.getName().equals(name)) { - job.setLegacyId(job.getId()); - job.setId(id); - } else - job.setLegacyId(id); - } - } - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } + Job job = Jobs.getJob(name); + if (job != null) { + if (job.getId() == 0) + job.setId(id); + else { + // Prioritizing id which matches actual job name and not full name which can be different + if (job.getName().equals(name)) { + job.setLegacyId(job.getId()); + job.setId(id); + } else + job.setLegacyId(id); + } + } + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } - for (Job one : Jobs.getJobs()) { - if (one.getId() == 0) - recordNewJobName(one); - } + for (Job one : Jobs.getJobs()) { + if (one.getId() == 0) + recordNewJobName(one); + } } /** @@ -1285,31 +1287,31 @@ public abstract class JobsDAO { * @return amount of player currently working. */ public synchronized int getTotalPlayerAmountByJobName(String JobName) { - JobsConnection conn = getConnection(); - if (conn == null) - return 0; + JobsConnection conn = getConnection(); + if (conn == null) + return 0; - int count = 0; - PreparedStatement prest = null; - ResultSet res = null; - try { - Job job = Jobs.getJob(JobName); - if (job != null && job.getId() != 0) { - prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.jobid + "` = ?;"); - prest.setInt(1, job.getId()); - res = prest.executeQuery(); - if (res.next()) { - count += res.getInt(1); - } - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } + int count = 0; + PreparedStatement prest = null; + ResultSet res = null; + try { + Job job = Jobs.getJob(JobName); + if (job != null && job.getId() != 0) { + prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.jobid + "` = ?;"); + prest.setInt(1, job.getId()); + res = prest.executeQuery(); + if (res.next()) { + count += res.getInt(1); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } - return count; + return count; } /** @@ -1317,11 +1319,11 @@ public abstract class JobsDAO { * @return total amount of player currently working. */ public synchronized int getTotalPlayers() { - int total = 0; - for (Job one : Jobs.getJobs()) { - total += one.getTotalPlayers(); - } - return total; + int total = 0; + for (Job one : Jobs.getJobs()) { + total += one.getTotalPlayers(); + } + return total; } /** @@ -1330,157 +1332,161 @@ public abstract class JobsDAO { * @return list of all of the names of the jobs the players are part of. */ public synchronized List getAllJobsOffline2(String userName) { - List jobs = new ArrayList<>(); + List jobs = new ArrayList<>(); - PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(userName); - if (info == null) - return jobs; + PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(userName); + if (info == null) + return jobs; - JobsConnection conn = getConnection(); - if (conn == null) - return jobs; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.userid.getCollumn() + "` = ?;"); - prest.setInt(1, info.getID()); - res = prest.executeQuery(); - while (res.next()) { - int jobId = res.getInt(JobsTableFields.jobid.getCollumn()); - if (jobId == 0) { - jobs.add(new JobsDAOData(res.getString(JobsTableFields.job.getCollumn()), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); - } else { - Job job = Jobs.getJob(jobId); - jobs.add(new JobsDAOData(job.getName(), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); - } - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return jobs; + JobsConnection conn = getConnection(); + if (conn == null) + return jobs; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.userid.getCollumn() + "` = ?;"); + prest.setInt(1, info.getID()); + res = prest.executeQuery(); + while (res.next()) { + int jobId = res.getInt(JobsTableFields.jobid.getCollumn()); + if (jobId == 0) { + jobs.add(new JobsDAOData(res.getString(JobsTableFields.job.getCollumn()), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); + } else { + Job job = Jobs.getJob(jobId); + jobs.add(new JobsDAOData(job.getName(), res.getInt(JobsTableFields.level.getCollumn()), res.getDouble(JobsTableFields.experience.getCollumn()))); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + return jobs; + } + + public void recordPlayersLimitsAsync(JobsPlayer player) { + this.service.execute(() -> recordPlayersLimits(player)); } public synchronized void recordPlayersLimits(JobsPlayer jPlayer) { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest2 = null; - try { - prest2 = conn.prepareStatement("DELETE FROM `" + DBTables.LimitsTable.getTableName() + "` WHERE `" + LimitTableFields.userid.getCollumn() + "` = ?;"); - prest2.setInt(1, jPlayer.getUserId()); - prest2.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest2); - } + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest2 = null; + try { + prest2 = conn.prepareStatement("DELETE FROM `" + DBTables.LimitsTable.getTableName() + "` WHERE `" + LimitTableFields.userid.getCollumn() + "` = ?;"); + prest2.setInt(1, jPlayer.getUserId()); + prest2.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest2); + } - PaymentData limit = jPlayer.getPaymentLimit(); - if (limit == null) - return; + PaymentData limit = jPlayer.getPaymentLimit(); + if (limit == null) + return; - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("INSERT INTO `" + DBTables.LimitsTable.getTableName() + "` (`" + - LimitTableFields.userid.getCollumn() + "`, `" + - LimitTableFields.typeid.getCollumn() + "`, `" + - LimitTableFields.collected.getCollumn() + "`, `" + - LimitTableFields.started.getCollumn() + "`, `" + - LimitTableFields.type.getCollumn() + "`) VALUES (?, ?, ?, ?, ?);"); - conn.setAutoCommit(false); - for (CurrencyType type : CurrencyType.values()) { - if (limit.getAmount(type) == 0D || limit.getLeftTime(type) < 0) - continue; + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("INSERT INTO `" + DBTables.LimitsTable.getTableName() + "` (`" + + LimitTableFields.userid.getCollumn() + "`, `" + + LimitTableFields.typeid.getCollumn() + "`, `" + + LimitTableFields.collected.getCollumn() + "`, `" + + LimitTableFields.started.getCollumn() + "`, `" + + LimitTableFields.type.getCollumn() + "`) VALUES (?, ?, ?, ?, ?);"); + conn.setAutoCommit(false); + for (CurrencyType type : CurrencyType.values()) { + if (limit.getAmount(type) == 0D || limit.getLeftTime(type) < 0) + continue; - prest.setInt(1, jPlayer.getUserId()); - prest.setInt(2, type.getId()); - prest.setDouble(3, limit.getAmount(type)); - prest.setLong(4, limit.getTime(type)); - prest.setString(5, type.toString()); - prest.addBatch(); - } - prest.executeBatch(); - conn.commit(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - close(prest); - try { - conn.setAutoCommit(true); - } catch (SQLException e) { - e.printStackTrace(); - } - } + prest.setInt(1, jPlayer.getUserId()); + prest.setInt(2, type.getId()); + prest.setDouble(3, limit.getAmount(type)); + prest.setLong(4, limit.getTime(type)); + prest.setString(5, type.toString()); + prest.addBatch(); + } + prest.executeBatch(); + conn.commit(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + close(prest); + try { + conn.setAutoCommit(true); + } catch (SQLException e) { + e.printStackTrace(); + } + } } public synchronized PaymentData getPlayersLimits(JobsPlayer jPlayer) { - PaymentData data = new PaymentData(); - JobsConnection conn = getConnection(); - if (conn == null) - return data; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.LimitsTable.getTableName() + "` WHERE `" + LimitTableFields.userid.getCollumn() + "` = ?;"); - prest.setInt(1, jPlayer.getUserId()); - res = prest.executeQuery(); - while (res.next()) { - String typeName = res.getString(LimitTableFields.type.getCollumn()); - int typeId = res.getInt(LimitTableFields.typeid.getCollumn()); + PaymentData data = new PaymentData(); + JobsConnection conn = getConnection(); + if (conn == null) + return data; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.LimitsTable.getTableName() + "` WHERE `" + LimitTableFields.userid.getCollumn() + "` = ?;"); + prest.setInt(1, jPlayer.getUserId()); + res = prest.executeQuery(); + while (res.next()) { + String typeName = res.getString(LimitTableFields.type.getCollumn()); + int typeId = res.getInt(LimitTableFields.typeid.getCollumn()); - CurrencyType type = typeId != 0 ? CurrencyType.get(typeId) : CurrencyType.getByName(typeName); - if (type != null) - data.addNewAmount(type, res.getDouble(LimitTableFields.collected.getCollumn()), res.getLong(LimitTableFields.started.getCollumn())); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return data; + CurrencyType type = typeId != 0 ? CurrencyType.get(typeId) : CurrencyType.getByName(typeName); + if (type != null) + data.addNewAmount(type, res.getDouble(LimitTableFields.collected.getCollumn()), res.getLong(LimitTableFields.started.getCollumn())); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + return data; } public synchronized Map loadPlayerLimits() { - Map map = new HashMap<>(); - JobsConnection conn = getConnection(); - if (conn == null) - return map; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.LimitsTable.getTableName() + "`;"); - res = prest.executeQuery(); - while (res.next()) { - int id = res.getInt(LimitTableFields.userid.getCollumn()); + Map map = new HashMap<>(); + JobsConnection conn = getConnection(); + if (conn == null) + return map; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.LimitsTable.getTableName() + "`;"); + res = prest.executeQuery(); + while (res.next()) { + int id = res.getInt(LimitTableFields.userid.getCollumn()); - PaymentData data = map.getOrDefault(id, new PaymentData()); + PaymentData data = map.getOrDefault(id, new PaymentData()); - String typeName = res.getString(LimitTableFields.type.getCollumn()); - int typeId = res.getInt(LimitTableFields.typeid.getCollumn()); + String typeName = res.getString(LimitTableFields.type.getCollumn()); + int typeId = res.getInt(LimitTableFields.typeid.getCollumn()); - CurrencyType type = null; - if (typeId != 0) - type = CurrencyType.get(typeId); - else - type = CurrencyType.getByName(typeName); + CurrencyType type = null; + if (typeId != 0) + type = CurrencyType.get(typeId); + else + type = CurrencyType.getByName(typeName); - if (type == null) - continue; + if (type == null) + continue; - data.addNewAmount(type, res.getDouble(LimitTableFields.collected.getCollumn()), res.getLong(LimitTableFields.started.getCollumn())); - map.put(id, data); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return map; + data.addNewAmount(type, res.getDouble(LimitTableFields.collected.getCollumn()), res.getLong(LimitTableFields.started.getCollumn())); + map.put(id, data); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + return map; } /** @@ -1489,24 +1495,24 @@ public abstract class JobsDAO { * @param job - job that the player wishes to join */ public synchronized void joinJob(JobsPlayer jPlayer, JobProgression job) { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("INSERT INTO `" + getJobsTableName() + "` (`" + JobsTableFields.userid.getCollumn() + "`, `" + JobsTableFields.jobid.getCollumn() - + "`, `" + JobsTableFields.level.getCollumn() + "`, `" + JobsTableFields.experience.getCollumn() + "`, `" + JobsTableFields.job.getCollumn() + "`) VALUES (?, ?, ?, ?, ?);"); - prest.setInt(1, jPlayer.getUserId()); - prest.setInt(2, job.getJob().getId()); - prest.setInt(3, job.getLevel()); - prest.setDouble(4, job.getExperience()); - prest.setString(5, job.getJob().getName()); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - } + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("INSERT INTO `" + getJobsTableName() + "` (`" + JobsTableFields.userid.getCollumn() + "`, `" + JobsTableFields.jobid.getCollumn() + + "`, `" + JobsTableFields.level.getCollumn() + "`, `" + JobsTableFields.experience.getCollumn() + "`, `" + JobsTableFields.job.getCollumn() + "`) VALUES (?, ?, ?, ?, ?);"); + prest.setInt(1, jPlayer.getUserId()); + prest.setInt(2, job.getJob().getId()); + prest.setInt(3, job.getLevel()); + prest.setDouble(4, job.getExperience()); + prest.setString(5, job.getJob().getName()); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } } /** @@ -1515,27 +1521,27 @@ public abstract class JobsDAO { * @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 { - double exp = prog.getExperience(); - if (exp < 0) - exp = 0; - prest = conn.prepareStatement("INSERT INTO `" + getJobsTableName() + "` (`" + JobsTableFields.userid.getCollumn() + "`, `" + JobsTableFields.jobid.getCollumn() - + "`, `" + JobsTableFields.level.getCollumn() + "`, `" + JobsTableFields.experience.getCollumn() + "`, `" + JobsTableFields.job.getCollumn() + "`) VALUES (?, ?, ?, ?, ?);"); - prest.setInt(1, jPlayer.getUserId()); - prest.setInt(2, prog.getJob().getId()); - prest.setInt(3, prog.getLevel()); - prest.setDouble(4, exp); - prest.setString(5, prog.getJob().getName()); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - } + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest = null; + try { + double exp = prog.getExperience(); + if (exp < 0) + exp = 0; + prest = conn.prepareStatement("INSERT INTO `" + getJobsTableName() + "` (`" + JobsTableFields.userid.getCollumn() + "`, `" + JobsTableFields.jobid.getCollumn() + + "`, `" + JobsTableFields.level.getCollumn() + "`, `" + JobsTableFields.experience.getCollumn() + "`, `" + JobsTableFields.job.getCollumn() + "`) VALUES (?, ?, ?, ?, ?);"); + prest.setInt(1, jPlayer.getUserId()); + prest.setInt(2, prog.getJob().getId()); + prest.setInt(3, prog.getLevel()); + prest.setDouble(4, exp); + prest.setString(5, prog.getJob().getName()); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } } /** @@ -1545,90 +1551,90 @@ public abstract class JobsDAO { * @throws SQLException */ public List convertDatabase() throws SQLException { - List list = new ArrayList<>(); - JobsConnection conn = getConnection(); - if (conn == null) - return list; + List list = new ArrayList<>(); + JobsConnection conn = getConnection(); + if (conn == null) + return list; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.ArchiveTable.getTableName() + "`"); - res = prest.executeQuery(); - while (res.next()) { - int id = res.getInt(ArchiveTableFields.userid.getCollumn()); - PlayerInfo pi = Jobs.getPlayerManager().getPlayerInfo(id); - if (pi == null) - continue; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.ArchiveTable.getTableName() + "`"); + res = prest.executeQuery(); + while (res.next()) { + int id = res.getInt(ArchiveTableFields.userid.getCollumn()); + PlayerInfo pi = Jobs.getPlayerManager().getPlayerInfo(id); + if (pi == null) + continue; - JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(pi.getUuid()); - if (jPlayer == null) - continue; + JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(pi.getUuid()); + if (jPlayer == null) + continue; - String jobName = res.getString(ArchiveTableFields.job.getCollumn()); - int jobid = res.getInt(ArchiveTableFields.jobid.getCollumn()); + String jobName = res.getString(ArchiveTableFields.job.getCollumn()); + int jobid = res.getInt(ArchiveTableFields.jobid.getCollumn()); - Job job = jobid != 0 ? Jobs.getJob(jobid) : Jobs.getJob(jobName); - if (job == null) - continue; + Job job = jobid != 0 ? Jobs.getJob(jobid) : Jobs.getJob(jobName); + if (job == null) + continue; - list.add(new Convert(res.getInt("id"), jPlayer.getUniqueId(), job.getId(), res.getInt(ArchiveTableFields.level.getCollumn()), res.getInt(ArchiveTableFields.experience.getCollumn()))); - } - } finally { - close(res); - close(prest); - } + list.add(new Convert(res.getInt("id"), jPlayer.getUniqueId(), job.getId(), res.getInt(ArchiveTableFields.level.getCollumn()), res.getInt(ArchiveTableFields.experience.getCollumn()))); + } + } finally { + close(res); + close(prest); + } - conn.closeConnection(); - return list; + conn.closeConnection(); + return list; } public void continueConvertions(List list) throws SQLException { - JobsConnection conns = getConnection(); - if (conns == null) - return; - PreparedStatement insert = null; - Statement statement = null; - int i = list.size(); - try { - statement = conns.createStatement(); - if (Jobs.getDBManager().getDbType() == DataBaseType.MySQL) { - statement.executeUpdate("TRUNCATE TABLE `" + DBTables.ArchiveTable.getTableName() + "`"); - } else { - statement.executeUpdate("DELETE from `" + DBTables.ArchiveTable.getTableName() + "`"); - } + JobsConnection conns = getConnection(); + if (conns == null) + return; + PreparedStatement insert = null; + Statement statement = null; + int i = list.size(); + try { + statement = conns.createStatement(); + if (Jobs.getDBManager().getDbType() == DataBaseType.MySQL) { + statement.executeUpdate("TRUNCATE TABLE `" + DBTables.ArchiveTable.getTableName() + "`"); + } else { + statement.executeUpdate("DELETE from `" + DBTables.ArchiveTable.getTableName() + "`"); + } - insert = conns.prepareStatement("INSERT INTO `" + DBTables.ArchiveTable.getTableName() + "` (`" + ArchiveTableFields.userid.getCollumn() - + "`, `" + ArchiveTableFields.jobid.getCollumn() + "`, `" + ArchiveTableFields.level.getCollumn() + "`, `" + ArchiveTableFields.experience.getCollumn() + "`, `" - + ArchiveTableFields.job.getCollumn() + "`) VALUES (?, ?, ?, ?, ?);"); - conns.setAutoCommit(false); - while (i > 0) { - i--; + insert = conns.prepareStatement("INSERT INTO `" + DBTables.ArchiveTable.getTableName() + "` (`" + ArchiveTableFields.userid.getCollumn() + + "`, `" + ArchiveTableFields.jobid.getCollumn() + "`, `" + ArchiveTableFields.level.getCollumn() + "`, `" + ArchiveTableFields.experience.getCollumn() + "`, `" + + ArchiveTableFields.job.getCollumn() + "`) VALUES (?, ?, ?, ?, ?);"); + conns.setAutoCommit(false); + while (i > 0) { + i--; - Convert convertData = list.get(i); + Convert convertData = list.get(i); - JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(convertData.getUserUUID()); - Job job = Jobs.getJob(convertData.getJobId()); + JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(convertData.getUserUUID()); + Job job = Jobs.getJob(convertData.getJobId()); - insert.setInt(1, jPlayer != null ? jPlayer.getUserId() : -1); - insert.setInt(2, convertData.getJobId()); - insert.setInt(3, convertData.getLevel()); - insert.setInt(4, convertData.getExp()); - insert.setString(5, job != null ? job.getName() : ""); - insert.addBatch(); - } - insert.executeBatch(); - conns.commit(); - conns.setAutoCommit(true); - } finally { - close(statement); - close(insert); - try { - conns.setAutoCommit(true); - } catch (SQLException e) { - e.printStackTrace(); - } - } + insert.setInt(1, jPlayer != null ? jPlayer.getUserId() : -1); + insert.setInt(2, convertData.getJobId()); + insert.setInt(3, convertData.getLevel()); + insert.setInt(4, convertData.getExp()); + insert.setString(5, job != null ? job.getName() : ""); + insert.addBatch(); + } + insert.executeBatch(); + conns.commit(); + conns.setAutoCommit(true); + } finally { + close(statement); + close(insert); + try { + conns.setAutoCommit(true); + } catch (SQLException e) { + e.printStackTrace(); + } + } } /** @@ -1637,52 +1643,52 @@ public abstract class JobsDAO { * @param job - job that the player wishes to quit */ public synchronized boolean quitJob(JobsPlayer jPlayer, Job job) { - JobsConnection conn = getConnection(); - if (conn == null) - return false; - PreparedStatement prest = null; - boolean done = true; - try { - prest = conn.prepareStatement("DELETE FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.userid.getCollumn() + "` = ? AND `" + JobsTableFields.jobid.getCollumn() - + "` = ?;"); - prest.setInt(1, jPlayer.getUserId()); - prest.setInt(2, job.getId()); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - done = false; - } finally { - close(prest); - } - return done; + JobsConnection conn = getConnection(); + if (conn == null) + return false; + PreparedStatement prest = null; + boolean done = true; + try { + prest = conn.prepareStatement("DELETE FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.userid.getCollumn() + "` = ? AND `" + JobsTableFields.jobid.getCollumn() + + "` = ?;"); + prest.setInt(1, jPlayer.getUserId()); + prest.setInt(2, job.getId()); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + done = false; + } finally { + close(prest); + } + return done; } /** * Remove duplicated job by specific criteria */ public synchronized boolean removeSpecificJob(int userId, String jobName, String legacyName, int level, double exp) { - JobsConnection conn = getConnection(); - if (conn == null) - return false; - PreparedStatement prest = null; - boolean done = true; - try { - prest = conn.prepareStatement("DELETE FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.userid.getCollumn() + "` = ? AND (`" + JobsTableFields.job.getCollumn() - + "` = ? OR `" + JobsTableFields.job.getCollumn() - + "` = ?) AND `" + JobsTableFields.level.getCollumn() + "` = ? AND `" + JobsTableFields.experience.getCollumn() + "` = ?;"); - prest.setInt(1, userId); - prest.setString(2, jobName); - prest.setString(3, legacyName); - prest.setInt(4, level); - prest.setDouble(5, exp); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - done = false; - } finally { - close(prest); - } - return done; + JobsConnection conn = getConnection(); + if (conn == null) + return false; + PreparedStatement prest = null; + boolean done = true; + try { + prest = conn.prepareStatement("DELETE FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.userid.getCollumn() + "` = ? AND (`" + JobsTableFields.job.getCollumn() + + "` = ? OR `" + JobsTableFields.job.getCollumn() + + "` = ?) AND `" + JobsTableFields.level.getCollumn() + "` = ? AND `" + JobsTableFields.experience.getCollumn() + "` = ?;"); + prest.setInt(1, userId); + prest.setString(2, jobName); + prest.setString(3, legacyName); + prest.setInt(4, level); + prest.setDouble(5, exp); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + done = false; + } finally { + close(prest); + } + return done; } /** @@ -1691,40 +1697,40 @@ public abstract class JobsDAO { * @param job - job that the player wishes to quit */ public void recordToArchive(JobsPlayer jPlayer, Job job) { - JobProgression jp = jPlayer.getJobProgression(job); - if (jp == null) - return; - jp.setLeftOn(System.currentTimeMillis()); - jPlayer.getArchivedJobs().addArchivedJob(jp); - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest = null; - try { - Double exp = jp.getExperience(); - prest = conn.prepareStatement("INSERT INTO `" + DBTables.ArchiveTable.getTableName() + "` (`" + ArchiveTableFields.userid.getCollumn() - + "`, `" + ArchiveTableFields.jobid.getCollumn() - + "`, `" + ArchiveTableFields.level.getCollumn() - + "`, `" + ArchiveTableFields.experience.getCollumn() - + "`, `" + ArchiveTableFields.left.getCollumn() - + "`, `" + ArchiveTableFields.job.getCollumn() - + "`) VALUES (?, ?, ?, ?, ?, ?);"); - prest.setInt(1, jPlayer.getUserId()); - prest.setInt(2, job.getId()); - prest.setInt(3, jp.getLevel()); - prest.setInt(4, exp.intValue()); - prest.setLong(5, System.currentTimeMillis()); - prest.setString(6, job.getName()); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - } + JobProgression jp = jPlayer.getJobProgression(job); + if (jp == null) + return; + jp.setLeftOn(System.currentTimeMillis()); + jPlayer.getArchivedJobs().addArchivedJob(jp); + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest = null; + try { + Double exp = jp.getExperience(); + prest = conn.prepareStatement("INSERT INTO `" + DBTables.ArchiveTable.getTableName() + "` (`" + ArchiveTableFields.userid.getCollumn() + + "`, `" + ArchiveTableFields.jobid.getCollumn() + + "`, `" + ArchiveTableFields.level.getCollumn() + + "`, `" + ArchiveTableFields.experience.getCollumn() + + "`, `" + ArchiveTableFields.left.getCollumn() + + "`, `" + ArchiveTableFields.job.getCollumn() + + "`) VALUES (?, ?, ?, ?, ?, ?);"); + prest.setInt(1, jPlayer.getUserId()); + prest.setInt(2, job.getId()); + prest.setInt(3, jp.getLevel()); + prest.setInt(4, exp.intValue()); + prest.setLong(5, System.currentTimeMillis()); + prest.setString(6, job.getName()); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } } public List getGlobalTopList() { - return getGlobalTopList(0); + return getGlobalTopList(0); } /** @@ -1733,44 +1739,44 @@ public abstract class JobsDAO { * @return info - information about jobs */ public List getGlobalTopList(int start) { - JobsConnection conn = getConnection(); - List names = new ArrayList<>(); - if (conn == null) - return names; + JobsConnection conn = getConnection(); + List names = new ArrayList<>(); + if (conn == null) + return names; - if (start < 0) { - start = 0; - } + if (start < 0) { + start = 0; + } - int jobsTopAmount = Jobs.getGCManager().JobsTopAmount * 2; + int jobsTopAmount = Jobs.getGCManager().JobsTopAmount * 2; - PreparedStatement prest = null; - ResultSet res = null; - try { + PreparedStatement prest = null; + ResultSet res = null; + try { - prest = conn.prepareStatement("SELECT " + JobsTableFields.userid.getCollumn() - + ", COUNT(*) AS amount, sum(" + JobsTableFields.level.getCollumn() + ") AS totallvl, sum(" + JobsTableFields.experience.getCollumn() + ") AS totalexp FROM `" + getJobsTableName() - + "` GROUP BY userid ORDER BY totallvl DESC, totalexp DESC LIMIT " + start + "," + jobsTopAmount + ";"); - res = prest.executeQuery(); + prest = conn.prepareStatement("SELECT " + JobsTableFields.userid.getCollumn() + + ", COUNT(*) AS amount, sum(" + JobsTableFields.level.getCollumn() + ") AS totallvl, sum(" + JobsTableFields.experience.getCollumn() + ") AS totalexp FROM `" + getJobsTableName() + + "` GROUP BY userid ORDER BY totallvl DESC, totalexp DESC LIMIT " + start + "," + jobsTopAmount + ";"); + res = prest.executeQuery(); - while (res.next()) { - PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(res.getInt(JobsTableFields.userid.getCollumn())); - if (info == null) - continue; + while (res.next()) { + PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(res.getInt(JobsTableFields.userid.getCollumn())); + if (info == null) + continue; - names.add(new TopList(info, res.getInt("totallvl"), res.getInt("totalexp"))); + names.add(new TopList(info, res.getInt("totallvl"), res.getInt("totalexp"))); - if (names.size() >= jobsTopAmount) - break; - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } + if (names.size() >= jobsTopAmount) + break; + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } - return names; + return names; } /** @@ -1780,141 +1786,141 @@ public abstract class JobsDAO { * @return info - information about jobs */ public List getQuestTopList(int start) { - JobsConnection conn = getConnection(); - List names = new ArrayList<>(); - if (conn == null) - return names; + JobsConnection conn = getConnection(); + List names = new ArrayList<>(); + if (conn == null) + return names; - if (start < 0) { - start = 0; - } + if (start < 0) { + start = 0; + } - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT `id`, `" + UserTableFields.player_uuid.getCollumn() + "`, `" + UserTableFields.donequests.getCollumn() + "` FROM `" + DBTables.UsersTable.getTableName() - + "` ORDER BY `" + UserTableFields.donequests.getCollumn() + "` DESC, LOWER(" + UserTableFields.seen.getCollumn() + ") DESC LIMIT " + start + ", 30;"); + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT `id`, `" + UserTableFields.player_uuid.getCollumn() + "`, `" + UserTableFields.donequests.getCollumn() + "` FROM `" + DBTables.UsersTable.getTableName() + + "` ORDER BY `" + UserTableFields.donequests.getCollumn() + "` DESC, LOWER(" + UserTableFields.seen.getCollumn() + ") DESC LIMIT " + start + ", 30;"); - res = prest.executeQuery(); + res = prest.executeQuery(); - while (res.next()) { - PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(res.getInt("id")); - if (info == null) - continue; + while (res.next()) { + PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(res.getInt("id")); + if (info == null) + continue; - names.add(new TopList(info, res.getInt(UserTableFields.donequests.getCollumn()), 0)); + names.add(new TopList(info, res.getInt(UserTableFields.donequests.getCollumn()), 0)); - if (names.size() >= Jobs.getGCManager().JobsTopAmount) - break; - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return names; + if (names.size() >= Jobs.getGCManager().JobsTopAmount) + break; + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + return names; } public PlayerInfo loadPlayerData(UUID uuid) { - JobsConnection conn = getConnection(); - if (conn == null) - return null; - PlayerInfo pInfo = null; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.UsersTable.getTableName() + "` WHERE `" + UserTableFields.player_uuid.getCollumn() + "` = ?;"); - prest.setString(1, uuid.toString()); - res = prest.executeQuery(); - while (res.next()) { - pInfo = new PlayerInfo( - res.getString(UserTableFields.username.getCollumn()), - res.getInt("id"), uuid, - res.getLong(UserTableFields.seen.getCollumn()), - res.getInt(UserTableFields.donequests.getCollumn()), - res.getString(UserTableFields.quests.getCollumn())); - Jobs.getPlayerManager().addPlayerToMap(pInfo); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return pInfo; + JobsConnection conn = getConnection(); + if (conn == null) + return null; + PlayerInfo pInfo = null; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.UsersTable.getTableName() + "` WHERE `" + UserTableFields.player_uuid.getCollumn() + "` = ?;"); + prest.setString(1, uuid.toString()); + res = prest.executeQuery(); + while (res.next()) { + pInfo = new PlayerInfo( + res.getString(UserTableFields.username.getCollumn()), + res.getInt("id"), uuid, + res.getLong(UserTableFields.seen.getCollumn()), + res.getInt(UserTableFields.donequests.getCollumn()), + res.getString(UserTableFields.quests.getCollumn())); + Jobs.getPlayerManager().addPlayerToMap(pInfo); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + return pInfo; } public void loadPlayerData() { - Jobs.getPlayerManager().clearMaps(); + Jobs.getPlayerManager().clearMaps(); - JobsConnection conn = getConnection(); - if (conn == null) - return; + 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(); - List uuids = new ArrayList<>(); - while (res.next()) { - String uuid = res.getString(UserTableFields.player_uuid.getCollumn()); - if (uuid == null || uuid.isEmpty()) { - uuids.add(uuid); - continue; - } + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.UsersTable.getTableName() + "`;"); + res = prest.executeQuery(); + List uuids = new ArrayList<>(); + while (res.next()) { + String uuid = res.getString(UserTableFields.player_uuid.getCollumn()); + if (uuid == null || uuid.isEmpty()) { + uuids.add(uuid); + continue; + } - long seen = res.getLong(UserTableFields.seen.getCollumn()); + long seen = res.getLong(UserTableFields.seen.getCollumn()); - try { - Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo( - res.getString(UserTableFields.username.getCollumn()), - res.getInt("id"), - UUID.fromString(uuid), - seen, - res.getInt(UserTableFields.donequests.getCollumn()), - res.getString(UserTableFields.quests.getCollumn()))); - } catch (IllegalArgumentException e) { - } - } + try { + Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo( + res.getString(UserTableFields.username.getCollumn()), + res.getInt("id"), + UUID.fromString(uuid), + seen, + res.getInt(UserTableFields.donequests.getCollumn()), + res.getString(UserTableFields.quests.getCollumn()))); + } catch (IllegalArgumentException e) { + } + } - for (String u : uuids) { - PreparedStatement ps = conn.prepareStatement("DELETE FROM `" + DBTables.UsersTable.getTableName() - + "` WHERE `" + UserTableFields.player_uuid.getCollumn() + "` = ?;"); - ps.setString(1, u); - ps.execute(); - close(ps); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } + for (String u : uuids) { + PreparedStatement ps = conn.prepareStatement("DELETE FROM `" + DBTables.UsersTable.getTableName() + + "` WHERE `" + UserTableFields.player_uuid.getCollumn() + "` = ?;"); + ps.setString(1, u); + ps.execute(); + close(ps); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } } public JobsPlayer loadFromDao(JobsPlayer jPlayer) { - List list = getAllJobs(jPlayer.getName(), jPlayer.getUniqueId()); - jPlayer.progression.clear(); - for (JobsDAOData jobdata : list) { - if (!plugin.isEnabled()) - return null; + List list = getAllJobs(jPlayer.getName(), jPlayer.getUniqueId()); + jPlayer.progression.clear(); + for (JobsDAOData jobdata : list) { + if (!plugin.isEnabled()) + return null; - // add the job - Job job = Jobs.getJob(jobdata.getJobName()); - if (job != null) - jPlayer.progression.add(new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience())); - } - jPlayer.reloadMaxExperience(); - jPlayer.reloadLimits(); - jPlayer.setUserId(Jobs.getPlayerManager().getPlayerId(jPlayer.getUniqueId())); - return jPlayer; + // add the job + Job job = Jobs.getJob(jobdata.getJobName()); + if (job != null) + jPlayer.progression.add(new JobProgression(job, jPlayer, jobdata.getLevel(), jobdata.getExperience())); + } + jPlayer.reloadMaxExperience(); + jPlayer.reloadLimits(); + jPlayer.setUserId(Jobs.getPlayerManager().getPlayerId(jPlayer.getUniqueId())); + return jPlayer; } public JobsPlayer loadFromDao(OfflinePlayer player) { - JobsPlayer jPlayer = new JobsPlayer(player); - return loadFromDao(jPlayer); + JobsPlayer jPlayer = new JobsPlayer(player); + return loadFromDao(jPlayer); } /** @@ -1923,22 +1929,28 @@ public abstract class JobsDAO { * @param job - job that the player wishes to quit */ public synchronized void deleteArchive(JobsPlayer jPlayer, Job job) { - jPlayer.getArchivedJobs().removeArchivedJob(job); - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("DELETE FROM `" + DBTables.ArchiveTable.getTableName() + "` WHERE `" + ArchiveTableFields.userid.getCollumn() + "` = ? AND `" + ArchiveTableFields.jobid - .getCollumn() + "` = ?;"); - prest.setInt(1, jPlayer.getUserId()); - prest.setInt(2, job.getId()); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - } + jPlayer.getArchivedJobs().removeArchivedJob(job); + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("DELETE FROM `" + DBTables.ArchiveTable.getTableName() + "` WHERE `" + ArchiveTableFields.userid.getCollumn() + "` = ? AND `" + ArchiveTableFields.jobid + .getCollumn() + "` = ?;"); + prest.setInt(1, jPlayer.getUserId()); + prest.setInt(2, job.getId()); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } + } + + private final ExecutorService service = Executors.newCachedThreadPool(); + + public void saveAsync(JobsPlayer player) { + this.service.execute(() -> save(player)); } /** @@ -1946,202 +1958,214 @@ public abstract class JobsDAO { * @param jobInfo - the information getting saved */ public void save(JobsPlayer player) { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("UPDATE `" + getJobsTableName() + "` SET `" + JobsTableFields.level.getCollumn() + "` = ?, `" + JobsTableFields.experience.getCollumn() - + "` = ? WHERE `" + JobsTableFields.userid.getCollumn() + "` = ? AND `" + JobsTableFields.jobid.getCollumn() + "` = ? " - + "OR `" + JobsTableFields.userid.getCollumn() + "` = ? AND `" + JobsTableFields.jobid.getCollumn() + "` = ?;"); - for (JobProgression progression : player.getJobProgression()) { - prest.setInt(1, progression.getLevel()); - prest.setDouble(2, progression.getExperience()); - prest.setInt(3, player.getUserId()); - prest.setInt(4, progression.getJob().getId()); - prest.setInt(5, player.getUserId()); - prest.setInt(6, progression.getJob().getLegacyId()); - prest.execute(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - } + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("UPDATE `" + getJobsTableName() + "` SET `" + JobsTableFields.level.getCollumn() + "` = ?, `" + JobsTableFields.experience.getCollumn() + + "` = ? WHERE `" + JobsTableFields.userid.getCollumn() + "` = ? AND `" + JobsTableFields.jobid.getCollumn() + "` = ? " + + "OR `" + JobsTableFields.userid.getCollumn() + "` = ? AND `" + JobsTableFields.jobid.getCollumn() + "` = ?;"); + for (JobProgression progression : player.getJobProgression()) { + prest.setInt(1, progression.getLevel()); + prest.setDouble(2, progression.getExperience()); + prest.setInt(3, player.getUserId()); + prest.setInt(4, progression.getJob().getId()); + prest.setInt(5, player.getUserId()); + prest.setInt(6, progression.getJob().getLegacyId()); + prest.execute(); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } + } + + public void updateSeenAsync(JobsPlayer player) { + this.service.execute(() -> updateSeen(player)); } public void updateSeen(JobsPlayer player) { - if (player.getUserId() == -1) { - insertPlayer(player); - return; - } + if (player.getUserId() == -1) { + insertPlayer(player); + return; + } - JobsConnection conn = getConnection(); - if (conn == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("UPDATE `" + DBTables.UsersTable.getTableName() + "` SET `" + UserTableFields.seen.getCollumn() - + "` = ?, `" + UserTableFields.username.getCollumn() - + "` = ?, `" + UserTableFields.donequests.getCollumn() - + "` = ?, `" + UserTableFields.quests.getCollumn() - + "` = ? WHERE `id` = ?;"); - prest.setLong(1, System.currentTimeMillis()); - prest.setString(2, player.getName()); - prest.setInt(3, player.getDoneQuests()); - prest.setString(4, player.getQuestProgressionString()); - prest.setInt(5, player.getUserId()); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - } + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("UPDATE `" + DBTables.UsersTable.getTableName() + "` SET `" + UserTableFields.seen.getCollumn() + + "` = ?, `" + UserTableFields.username.getCollumn() + + "` = ?, `" + UserTableFields.donequests.getCollumn() + + "` = ?, `" + UserTableFields.quests.getCollumn() + + "` = ? WHERE `id` = ?;"); + prest.setLong(1, System.currentTimeMillis()); + prest.setString(2, player.getName()); + prest.setInt(3, player.getDoneQuests()); + prest.setString(4, player.getQuestProgressionString()); + prest.setInt(5, player.getUserId()); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } } public void resetDoneQuests() { - JobsConnection conn = getConnection(); - if (conn == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("UPDATE `" + DBTables.UsersTable.getTableName() + "` SET `" + UserTableFields.donequests.getCollumn() + "` = ?;"); - prest.setInt(1, 0); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - } + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("UPDATE `" + DBTables.UsersTable.getTableName() + "` SET `" + UserTableFields.donequests.getCollumn() + "` = ?;"); + prest.setInt(1, 0); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } } private void insertPlayer(JobsPlayer player) { - JobsConnection conn = getConnection(); - if (conn == null) - return; - String uuid = player.getUniqueId().toString(); - String name = player.getName(); - PreparedStatement prestt = null; - try { - prestt = conn.prepareStatement("INSERT INTO `" + DBTables.UsersTable.getTableName() + "` (`" + UserTableFields.player_uuid.getCollumn() - + "`, `" + UserTableFields.username.getCollumn() - + "`, `" + UserTableFields.seen.getCollumn() - + "`, `" + UserTableFields.donequests.getCollumn() - + "`) VALUES (?, ?, ?, ?);"); - prestt.setString(1, uuid); - prestt.setString(2, name); - prestt.setLong(3, player.getSeen()); - prestt.setInt(4, 0); - prestt.executeUpdate(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prestt); - } - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT `id`, `" + UserTableFields.donequests.getCollumn() - + "` FROM `" + DBTables.UsersTable.getTableName() + "` WHERE `" + UserTableFields.player_uuid.getCollumn() + "` = ?;"); - prest.setString(1, uuid); - res = prest.executeQuery(); - if (res.next()) { - int id = res.getInt("id"); - player.setUserId(id); - Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo( - name, - id, - player.getUniqueId(), - player.getSeen(), - res.getInt(UserTableFields.donequests.getCollumn()))); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } + JobsConnection conn = getConnection(); + if (conn == null) + return; + String uuid = player.getUniqueId().toString(); + String name = player.getName(); + PreparedStatement prestt = null; + try { + prestt = conn.prepareStatement("INSERT INTO `" + DBTables.UsersTable.getTableName() + "` (`" + UserTableFields.player_uuid.getCollumn() + + "`, `" + UserTableFields.username.getCollumn() + + "`, `" + UserTableFields.seen.getCollumn() + + "`, `" + UserTableFields.donequests.getCollumn() + + "`) VALUES (?, ?, ?, ?);"); + prestt.setString(1, uuid); + prestt.setString(2, name); + prestt.setLong(3, player.getSeen()); + prestt.setInt(4, 0); + prestt.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prestt); + } + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT `id`, `" + UserTableFields.donequests.getCollumn() + + "` FROM `" + DBTables.UsersTable.getTableName() + "` WHERE `" + UserTableFields.player_uuid.getCollumn() + "` = ?;"); + prest.setString(1, uuid); + res = prest.executeQuery(); + if (res.next()) { + int id = res.getInt("id"); + player.setUserId(id); + Jobs.getPlayerManager().addPlayerToMap(new PlayerInfo( + name, + id, + player.getUniqueId(), + player.getSeen(), + res.getInt(UserTableFields.donequests.getCollumn()))); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + } + + public void savePointsAsync(JobsPlayer player) { + this.service.execute(() -> savePoints(player)); } public void savePoints(JobsPlayer jPlayer) { - JobsConnection conn = getConnection(); - if (conn == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - PlayerPoints pointInfo = jPlayer.getPointsData(); + PlayerPoints pointInfo = jPlayer.getPointsData(); - if (pointInfo.getDbId() == 0) { - // This needs to exist, removing existing entry by user id unless we have actual line id - PreparedStatement prest2 = null; - try { - prest2 = conn.prepareStatement("DELETE FROM `" + DBTables.PointsTable.getTableName() + "` WHERE `" + PointsTableFields.userid.getCollumn() + "` = ?;"); - prest2.setInt(1, jPlayer.getUserId()); - prest2.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest2); - } - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("INSERT INTO `" + DBTables.PointsTable.getTableName() + "` (`" + PointsTableFields.totalpoints.getCollumn() + "`, `" + PointsTableFields.currentpoints - .getCollumn() - + "`, `" + PointsTableFields.userid.getCollumn() + "`) VALUES (?, ?, ?);"); - prest.setDouble(1, pointInfo.getTotalPoints()); - prest.setDouble(2, pointInfo.getCurrentPoints()); - prest.setInt(3, jPlayer.getUserId()); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - } - } else { + if (pointInfo.getDbId() == 0) { + // This needs to exist, removing existing entry by user id unless we have actual line id + PreparedStatement prest2 = null; + try { + prest2 = conn.prepareStatement("DELETE FROM `" + DBTables.PointsTable.getTableName() + "` WHERE `" + PointsTableFields.userid.getCollumn() + "` = ?;"); + prest2.setInt(1, jPlayer.getUserId()); + prest2.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest2); + } + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("INSERT INTO `" + DBTables.PointsTable.getTableName() + "` (`" + PointsTableFields.totalpoints.getCollumn() + "`, `" + PointsTableFields.currentpoints + .getCollumn() + + "`, `" + PointsTableFields.userid.getCollumn() + "`) VALUES (?, ?, ?);"); + prest.setDouble(1, pointInfo.getTotalPoints()); + prest.setDouble(2, pointInfo.getCurrentPoints()); + prest.setInt(3, jPlayer.getUserId()); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } + } else { - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("UPDATE `" + DBTables.PointsTable.getTableName() + "` SET `" + PointsTableFields.totalpoints.getCollumn() - + "` = ?, `" + PointsTableFields.currentpoints.getCollumn() - + "` = ? WHERE `id` = ?;"); - prest.setDouble(1, pointInfo.getTotalPoints()); - prest.setDouble(2, pointInfo.getCurrentPoints()); - prest.setInt(3, pointInfo.getDbId()); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - } + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("UPDATE `" + DBTables.PointsTable.getTableName() + "` SET `" + PointsTableFields.totalpoints.getCollumn() + + "` = ?, `" + PointsTableFields.currentpoints.getCollumn() + + "` = ? WHERE `id` = ?;"); + prest.setDouble(1, pointInfo.getTotalPoints()); + prest.setDouble(2, pointInfo.getCurrentPoints()); + prest.setInt(3, pointInfo.getDbId()); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } - } + } } public void loadPoints(JobsPlayer player) { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT `" + PointsTableFields.totalpoints.getCollumn() + "`, `" + PointsTableFields.currentpoints.getCollumn() + "` FROM `" + DBTables.PointsTable.getTableName() - + "` WHERE `" + PointsTableFields.userid.getCollumn() + "` = ?;"); - prest.setInt(1, player.getUserId()); - res = prest.executeQuery(); + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT `" + PointsTableFields.totalpoints.getCollumn() + "`, `" + PointsTableFields.currentpoints.getCollumn() + "` FROM `" + DBTables.PointsTable.getTableName() + + "` WHERE `" + PointsTableFields.userid.getCollumn() + "` = ?;"); + prest.setInt(1, player.getUserId()); + res = prest.executeQuery(); - if (res.next()) { - player.getPointsData().setDbId(res.getInt("id")); - player.getPointsData().setPoints(res.getDouble(PointsTableFields.currentpoints.getCollumn())); - player.getPointsData().setTotalPoints(res.getDouble(PointsTableFields.totalpoints.getCollumn())); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } + if (res.next()) { + player.getPointsData().setDbId(res.getInt("id")); + player.getPointsData().setPoints(res.getDouble(PointsTableFields.currentpoints.getCollumn())); + player.getPointsData().setTotalPoints(res.getDouble(PointsTableFields.totalpoints.getCollumn())); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + } + + public void saveLogAsync(JobsPlayer player) { + this.service.execute(() -> saveLog(player)); } /** @@ -2149,83 +2173,83 @@ public abstract class JobsDAO { * @param jobInfo - the information getting saved */ public void saveLog(JobsPlayer player) { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest1 = null; - PreparedStatement prest2 = null; - try { + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest1 = null; + PreparedStatement prest2 = null; + try { - conn.setAutoCommit(false); + conn.setAutoCommit(false); - prest1 = conn.prepareStatement("UPDATE `" + DBTables.LogTable.getTableName() - + "` SET `" + LogTableFields.count.getCollumn() + "` = ?, `" + LogTableFields.money.getCollumn() + "` = ?, `" + LogTableFields.exp.getCollumn() - + "` = ?, `" + LogTableFields.points.getCollumn() + "` = ? WHERE `" + LogTableFields.userid.getCollumn() + "` = ? AND `" + LogTableFields.time.getCollumn() - + "` = ? AND `" + LogTableFields.action.getCollumn() + "` = ? AND `" + LogTableFields.itemname.getCollumn() + "` = ?;"); + prest1 = conn.prepareStatement("UPDATE `" + DBTables.LogTable.getTableName() + + "` SET `" + LogTableFields.count.getCollumn() + "` = ?, `" + LogTableFields.money.getCollumn() + "` = ?, `" + LogTableFields.exp.getCollumn() + + "` = ?, `" + LogTableFields.points.getCollumn() + "` = ? WHERE `" + LogTableFields.userid.getCollumn() + "` = ? AND `" + LogTableFields.time.getCollumn() + + "` = ? AND `" + LogTableFields.action.getCollumn() + "` = ? AND `" + LogTableFields.itemname.getCollumn() + "` = ?;"); - boolean added = false; - for (Log log : player.getLog().values()) { - for (Entry one : log.getAmountList().entrySet()) { - if (one.getValue().isNewEntry()) - continue; + boolean added = false; + for (Log log : player.getLog().values()) { + for (Entry one : log.getAmountList().entrySet()) { + if (one.getValue().isNewEntry()) + continue; - prest1.setInt(1, one.getValue().getCount()); - prest1.setDouble(2, one.getValue().get(CurrencyType.MONEY)); - prest1.setDouble(3, one.getValue().get(CurrencyType.EXP)); - prest1.setDouble(4, one.getValue().get(CurrencyType.POINTS)); + prest1.setInt(1, one.getValue().getCount()); + prest1.setDouble(2, one.getValue().get(CurrencyType.MONEY)); + prest1.setDouble(3, one.getValue().get(CurrencyType.EXP)); + prest1.setDouble(4, one.getValue().get(CurrencyType.POINTS)); - prest1.setInt(5, player.getUserId()); - prest1.setInt(6, log.getDate()); - prest1.setString(7, log.getActionType()); - prest1.setString(8, one.getKey()); + prest1.setInt(5, player.getUserId()); + prest1.setInt(6, log.getDate()); + prest1.setString(7, log.getActionType()); + prest1.setString(8, one.getKey()); // prest1.addBatch(); - added = true; - } - } - if (added) { - prest1.execute(); - conn.commit(); - } - added = false; - prest2 = conn.prepareStatement("INSERT INTO `" + DBTables.LogTable.getTableName() - + "` (`" + LogTableFields.userid.getCollumn() + "`, `" + LogTableFields.time.getCollumn() + "`, `" + LogTableFields.action.getCollumn() - + "`, `" + LogTableFields.itemname.getCollumn() + "`, `" + LogTableFields.count.getCollumn() + "`, `" + LogTableFields.money.getCollumn() - + "`, `" + LogTableFields.exp.getCollumn() + "`, `" + LogTableFields.points.getCollumn() + "`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"); - for (Log log : player.getLog().values()) { - for (Entry one : log.getAmountList().entrySet()) { - if (!one.getValue().isNewEntry()) - continue; - one.getValue().setNewEntry(false); + added = true; + } + } + if (added) { + prest1.execute(); + conn.commit(); + } + added = false; + prest2 = conn.prepareStatement("INSERT INTO `" + DBTables.LogTable.getTableName() + + "` (`" + LogTableFields.userid.getCollumn() + "`, `" + LogTableFields.time.getCollumn() + "`, `" + LogTableFields.action.getCollumn() + + "`, `" + LogTableFields.itemname.getCollumn() + "`, `" + LogTableFields.count.getCollumn() + "`, `" + LogTableFields.money.getCollumn() + + "`, `" + LogTableFields.exp.getCollumn() + "`, `" + LogTableFields.points.getCollumn() + "`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"); + for (Log log : player.getLog().values()) { + for (Entry one : log.getAmountList().entrySet()) { + if (!one.getValue().isNewEntry()) + continue; + one.getValue().setNewEntry(false); - prest2.setInt(1, player.getUserId()); - prest2.setInt(2, log.getDate()); - prest2.setString(3, log.getActionType()); - prest2.setString(4, one.getKey()); - prest2.setInt(5, one.getValue().getCount()); - prest2.setDouble(6, one.getValue().get(CurrencyType.MONEY)); - prest2.setDouble(7, one.getValue().get(CurrencyType.EXP)); - prest2.setDouble(8, one.getValue().get(CurrencyType.POINTS)); + prest2.setInt(1, player.getUserId()); + prest2.setInt(2, log.getDate()); + prest2.setString(3, log.getActionType()); + prest2.setString(4, one.getKey()); + prest2.setInt(5, one.getValue().getCount()); + prest2.setDouble(6, one.getValue().get(CurrencyType.MONEY)); + prest2.setDouble(7, one.getValue().get(CurrencyType.EXP)); + prest2.setDouble(8, one.getValue().get(CurrencyType.POINTS)); // prest2.addBatch(); - added = true; - } - } - if (added) { - prest2.execute(); - conn.commit(); - } - } catch (SQLException e) { - e.printStackTrace(); - close(prest1); - close(prest2); - } finally { - close(prest1); - close(prest2); - try { - conn.setAutoCommit(true); - } catch (SQLException e) { - e.printStackTrace(); - } - } + added = true; + } + } + if (added) { + prest2.execute(); + conn.commit(); + } + } catch (SQLException e) { + e.printStackTrace(); + close(prest1); + close(prest2); + } finally { + close(prest1); + close(prest2); + try { + conn.setAutoCommit(true); + } catch (SQLException e) { + e.printStackTrace(); + } + } } /** @@ -2233,37 +2257,37 @@ public abstract class JobsDAO { * @param jobInfo - the information getting saved */ public void loadLog(JobsPlayer player) { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest = null; - ResultSet res = null; - try { - int time = TimeManage.timeInInt(); - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.LogTable.getTableName() - + "` WHERE `" + LogTableFields.userid.getCollumn() + "` = ? AND `" + LogTableFields.time.getCollumn() + "` = ? ;"); - prest.setInt(1, player.getUserId()); - prest.setInt(2, time); - res = prest.executeQuery(); - while (res.next()) { + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest = null; + ResultSet res = null; + try { + int time = TimeManage.timeInInt(); + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.LogTable.getTableName() + + "` WHERE `" + LogTableFields.userid.getCollumn() + "` = ? AND `" + LogTableFields.time.getCollumn() + "` = ? ;"); + prest.setInt(1, player.getUserId()); + prest.setInt(2, time); + res = prest.executeQuery(); + while (res.next()) { - Map amounts = new HashMap<>(); - amounts.put(CurrencyType.MONEY, res.getDouble(LogTableFields.money.getCollumn())); - amounts.put(CurrencyType.EXP, res.getDouble(LogTableFields.exp.getCollumn())); - amounts.put(CurrencyType.POINTS, res.getDouble(LogTableFields.points.getCollumn())); + Map amounts = new HashMap<>(); + amounts.put(CurrencyType.MONEY, res.getDouble(LogTableFields.money.getCollumn())); + amounts.put(CurrencyType.EXP, res.getDouble(LogTableFields.exp.getCollumn())); + amounts.put(CurrencyType.POINTS, res.getDouble(LogTableFields.points.getCollumn())); - Jobs.getLoging().loadToLog(player, res.getString(LogTableFields.action.getCollumn()), res.getString(LogTableFields.itemname.getCollumn()), res.getInt(LogTableFields.count.getCollumn()), - amounts); - } - } catch (Exception e) { - close(res); - close(prest); - drop(DBTables.LogTable.getTableName()); - createDefaultTable(DBTables.LogTable); - } finally { - close(res); - close(prest); - } + Jobs.getLoging().loadToLog(player, res.getString(LogTableFields.action.getCollumn()), res.getString(LogTableFields.itemname.getCollumn()), res.getInt(LogTableFields.count.getCollumn()), + amounts); + } + } catch (Exception e) { + close(res); + close(prest); + drop(DBTables.LogTable.getTableName()); + createDefaultTable(DBTables.LogTable); + } finally { + close(res); + close(prest); + } } /** @@ -2271,104 +2295,104 @@ public abstract class JobsDAO { * @param jobBlockProtection - the information getting saved */ public void saveBlockProtection(String world, java.util.concurrent.ConcurrentMap concurrentHashMap) { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement insert = null; - PreparedStatement update = null; - PreparedStatement delete = null; - try { - conn.setAutoCommit(false); - JobsWorld jobsWorld = Util.getJobsWorld(world); - if (jobsWorld == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement insert = null; + PreparedStatement update = null; + PreparedStatement delete = null; + try { + conn.setAutoCommit(false); + JobsWorld jobsWorld = Util.getJobsWorld(world); + if (jobsWorld == null) + return; - int worldId = jobsWorld.getId(); - if (worldId == 0) - return; + int worldId = jobsWorld.getId(); + if (worldId == 0) + return; - insert = conn.prepareStatement("INSERT INTO `" + DBTables.BlocksTable.getTableName() + "` (`" + BlockTableFields.worldid.getCollumn() - + "`, `" + BlockTableFields.x.getCollumn() - + "`, `" + BlockTableFields.y.getCollumn() - + "`, `" + BlockTableFields.z.getCollumn() - + "`, `" + BlockTableFields.recorded.getCollumn() - + "`, `" + BlockTableFields.resets.getCollumn() - + "`, `" + BlockTableFields.world.getCollumn() - + "`) VALUES (?, ?, ?, ?, ?, ?, ?);"); - update = conn.prepareStatement("UPDATE `" + DBTables.BlocksTable.getTableName() + "` SET `" + BlockTableFields.recorded.getCollumn() - + "` = ?, `" + BlockTableFields.resets.getCollumn() - + "` = ? WHERE `id` = ?;"); - delete = conn.prepareStatement("DELETE from `" + DBTables.BlocksTable.getTableName() + "` WHERE `id` = ?;"); + insert = conn.prepareStatement("INSERT INTO `" + DBTables.BlocksTable.getTableName() + "` (`" + BlockTableFields.worldid.getCollumn() + + "`, `" + BlockTableFields.x.getCollumn() + + "`, `" + BlockTableFields.y.getCollumn() + + "`, `" + BlockTableFields.z.getCollumn() + + "`, `" + BlockTableFields.recorded.getCollumn() + + "`, `" + BlockTableFields.resets.getCollumn() + + "`, `" + BlockTableFields.world.getCollumn() + + "`) VALUES (?, ?, ?, ?, ?, ?, ?);"); + update = conn.prepareStatement("UPDATE `" + DBTables.BlocksTable.getTableName() + "` SET `" + BlockTableFields.recorded.getCollumn() + + "` = ?, `" + BlockTableFields.resets.getCollumn() + + "` = ? WHERE `id` = ?;"); + delete = conn.prepareStatement("DELETE from `" + DBTables.BlocksTable.getTableName() + "` WHERE `id` = ?;"); - Long current = System.currentTimeMillis(); - Long mark = System.currentTimeMillis() - (Jobs.getGCManager().BlockProtectionDays * 24L * 60L * 60L * 1000L); + Long current = System.currentTimeMillis(); + Long mark = System.currentTimeMillis() - (Jobs.getGCManager().BlockProtectionDays * 24L * 60L * 60L * 1000L); - for (Entry block : concurrentHashMap.entrySet()) { - if (block.getValue() == null) - continue; - switch (block.getValue().getAction()) { - case DELETE: - delete.setInt(1, block.getValue().getId()); - delete.addBatch(); + for (Entry block : concurrentHashMap.entrySet()) { + if (block.getValue() == null) + continue; + switch (block.getValue().getAction()) { + case DELETE: + delete.setInt(1, block.getValue().getId()); + delete.addBatch(); - break; - case INSERT: - if (block.getValue().getTime() < current && block.getValue().getTime() != -1) - continue; + break; + case INSERT: + if (block.getValue().getTime() < current && block.getValue().getTime() != -1) + continue; - insert.setInt(1, worldId); - insert.setInt(2, block.getValue().getX()); - insert.setInt(3, block.getValue().getY()); - insert.setInt(4, block.getValue().getZ()); - insert.setLong(5, block.getValue().getRecorded()); - insert.setLong(6, block.getValue().getTime()); - insert.setString(7, world); - insert.addBatch(); - block.getValue().setAction(DBAction.NONE); + insert.setInt(1, worldId); + insert.setInt(2, block.getValue().getX()); + insert.setInt(3, block.getValue().getY()); + insert.setInt(4, block.getValue().getZ()); + insert.setLong(5, block.getValue().getRecorded()); + insert.setLong(6, block.getValue().getTime()); + insert.setString(7, world); + insert.addBatch(); + block.getValue().setAction(DBAction.NONE); - break; - case UPDATE: - if (block.getValue().getTime() < current && block.getValue().getTime() != -1) - continue; - update.setLong(1, block.getValue().getRecorded()); - update.setLong(2, block.getValue().getTime()); - update.setInt(3, block.getValue().getId()); - update.addBatch(); - block.getValue().setAction(DBAction.NONE); + break; + case UPDATE: + if (block.getValue().getTime() < current && block.getValue().getTime() != -1) + continue; + update.setLong(1, block.getValue().getRecorded()); + update.setLong(2, block.getValue().getTime()); + update.setInt(3, block.getValue().getId()); + update.addBatch(); + block.getValue().setAction(DBAction.NONE); - break; - case NONE: - if (block.getValue().getTime() < current && block.getValue().getTime() != -1) - continue; - if (block.getValue().getTime() == -1 && block.getValue().getRecorded() > mark) - continue; + break; + case NONE: + if (block.getValue().getTime() < current && block.getValue().getTime() != -1) + continue; + if (block.getValue().getTime() == -1 && block.getValue().getRecorded() > mark) + continue; - delete.setInt(1, block.getValue().getId()); - delete.addBatch(); + delete.setInt(1, block.getValue().getId()); + delete.addBatch(); - break; - default: - continue; - } - } + break; + default: + continue; + } + } - insert.executeBatch(); - update.executeBatch(); - delete.executeBatch(); - conn.commit(); + insert.executeBatch(); + update.executeBatch(); + delete.executeBatch(); + conn.commit(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(insert); - close(update); - close(delete); - try { - conn.setAutoCommit(true); - } catch (SQLException e) { - e.printStackTrace(); - } - } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(insert); + close(update); + close(delete); + try { + conn.setAutoCommit(true); + } catch (SQLException e) { + e.printStackTrace(); + } + } } /** @@ -2376,192 +2400,192 @@ public abstract class JobsDAO { * @param jobBlockProtection - the information getting saved */ public void loadBlockProtection() { - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest = null; - PreparedStatement prestDel = null; - ResultSet res = null; + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest = null; + PreparedStatement prestDel = null; + ResultSet res = null; - long timer = System.currentTimeMillis(); + long timer = System.currentTimeMillis(); - try { - long mark = System.currentTimeMillis() - (Jobs.getGCManager().BlockProtectionDays * 24L * 60L * 60L * 1000L); - prestDel = conn.prepareStatement("DELETE FROM `" + DBTables.BlocksTable.getTableName() + "` WHERE `" + BlockTableFields.recorded.getCollumn() + "` < ? OR `" + - BlockTableFields.resets.getCollumn() + "` < ? AND `" + BlockTableFields.resets.getCollumn() + "` > 0;"); - prestDel.setLong(1, mark); - prestDel.setLong(2, System.currentTimeMillis()); - prestDel.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prestDel); - } + try { + long mark = System.currentTimeMillis() - (Jobs.getGCManager().BlockProtectionDays * 24L * 60L * 60L * 1000L); + prestDel = conn.prepareStatement("DELETE FROM `" + DBTables.BlocksTable.getTableName() + "` WHERE `" + BlockTableFields.recorded.getCollumn() + "` < ? OR `" + + BlockTableFields.resets.getCollumn() + "` < ? AND `" + BlockTableFields.resets.getCollumn() + "` > 0;"); + prestDel.setLong(1, mark); + prestDel.setLong(2, System.currentTimeMillis()); + prestDel.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prestDel); + } - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.BlocksTable.getTableName() + "`;"); - res = prest.executeQuery(); - int i = 0; - int ii = 0; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.BlocksTable.getTableName() + "`;"); + res = prest.executeQuery(); + int i = 0; + int ii = 0; - while (res.next()) { + while (res.next()) { - String name = res.getString(BlockTableFields.world.getCollumn()); - int worldId = res.getInt(BlockTableFields.worldid.getCollumn()); + String name = res.getString(BlockTableFields.world.getCollumn()); + int worldId = res.getInt(BlockTableFields.worldid.getCollumn()); - World world = null; - if (worldId != 0) { - JobsWorld jobsWorld = Util.getJobsWorld(worldId); - if (jobsWorld != null) - world = jobsWorld.getWorld(); - } else { - world = Bukkit.getWorld(name); - } - if (world == null) - continue; + World world = null; + if (worldId != 0) { + JobsWorld jobsWorld = Util.getJobsWorld(worldId); + if (jobsWorld != null) + world = jobsWorld.getWorld(); + } else { + world = Bukkit.getWorld(name); + } + if (world == null) + continue; - int id = res.getInt("id"); - int x = res.getInt(BlockTableFields.x.getCollumn()); - int y = res.getInt(BlockTableFields.y.getCollumn()); - int z = res.getInt(BlockTableFields.z.getCollumn()); - long resets = res.getLong(BlockTableFields.resets.getCollumn()); - Location loc = new Location(world, x, y, z); + int id = res.getInt("id"); + int x = res.getInt(BlockTableFields.x.getCollumn()); + int y = res.getInt(BlockTableFields.y.getCollumn()); + int z = res.getInt(BlockTableFields.z.getCollumn()); + long resets = res.getLong(BlockTableFields.resets.getCollumn()); + Location loc = new Location(world, x, y, z); - BlockProtection bp = Jobs.getBpManager().addP(loc, resets, true, false); - bp.setId(id); - bp.setRecorded(res.getLong(BlockTableFields.recorded.getCollumn())); - bp.setAction(DBAction.NONE); - i++; + BlockProtection bp = Jobs.getBpManager().addP(loc, resets, true, false); + bp.setId(id); + bp.setRecorded(res.getLong(BlockTableFields.recorded.getCollumn())); + bp.setAction(DBAction.NONE); + i++; - if (ii++ >= 100000) { - Jobs.consoleMsg("&6[Jobs] Loading (" + i + ") BP"); - ii = 0; - } - } - if (i > 0) { - Jobs.consoleMsg("&e[Jobs] Loaded " + i + " block protection entries. " + (System.currentTimeMillis() - timer) + "ms"); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } + if (ii++ >= 100000) { + Jobs.consoleMsg("&6[Jobs] Loading (" + i + ") BP"); + ii = 0; + } + } + if (i > 0) { + Jobs.consoleMsg("&e[Jobs] Loaded " + i + " block protection entries. " + (System.currentTimeMillis() - timer) + "ms"); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } } /** * Save player-explore information */ public void saveExplore() { - insertExplore(); - updateExplore(); + insertExplore(); + updateExplore(); } public void insertExplore() { - if (!Jobs.getExploreManager().isExploreEnabled()) - return; + if (!Jobs.getExploreManager().isExploreEnabled()) + return; - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest2 = null; - try { + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest2 = null; + try { - prest2 = conn.prepareStatement("INSERT INTO `" + DBTables.ExploreDataTable.getTableName() + "` (`" + ExploreDataTableFields.worldid.getCollumn() - + "`, `" + ExploreDataTableFields.chunkX.getCollumn() - + "`, `" + ExploreDataTableFields.chunkZ.getCollumn() - + "`, `" + ExploreDataTableFields.playerNames.getCollumn() - + "`, `" + ExploreDataTableFields.worldname.getCollumn() - + "`) VALUES (?, ?, ?, ?, ?);"); - conn.setAutoCommit(false); - int i = 0; + prest2 = conn.prepareStatement("INSERT INTO `" + DBTables.ExploreDataTable.getTableName() + "` (`" + ExploreDataTableFields.worldid.getCollumn() + + "`, `" + ExploreDataTableFields.chunkX.getCollumn() + + "`, `" + ExploreDataTableFields.chunkZ.getCollumn() + + "`, `" + ExploreDataTableFields.playerNames.getCollumn() + + "`, `" + ExploreDataTableFields.worldname.getCollumn() + + "`) VALUES (?, ?, ?, ?, ?);"); + conn.setAutoCommit(false); + int i = 0; - Map> temp = new HashMap<>(Jobs.getExploreManager().getWorlds()); + Map> temp = new HashMap<>(Jobs.getExploreManager().getWorlds()); - for (Entry> worlds : temp.entrySet()) { - for (Entry region : worlds.getValue().entrySet()) { - JobsWorld jobsWorld = Util.getJobsWorld(worlds.getKey()); + for (Entry> worlds : temp.entrySet()) { + for (Entry region : worlds.getValue().entrySet()) { + JobsWorld jobsWorld = Util.getJobsWorld(worlds.getKey()); - 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(); - i++; - } - } - } - prest2.executeBatch(); - conn.commit(); - conn.setAutoCommit(true); + 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(); + i++; + } + } + } + prest2.executeBatch(); + conn.commit(); + conn.setAutoCommit(true); - if (i > 0) - Jobs.consoleMsg("&e[Jobs] Saved " + i + " new explorer entries."); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest2); - try { - conn.setAutoCommit(true); - } catch (SQLException e) { - e.printStackTrace(); - } - } + if (i > 0) + Jobs.consoleMsg("&e[Jobs] Saved " + i + " new explorer entries."); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest2); + try { + conn.setAutoCommit(true); + } catch (SQLException e) { + e.printStackTrace(); + } + } } public void updateExplore() { - if (!Jobs.getExploreManager().isExploreEnabled()) - return; + if (!Jobs.getExploreManager().isExploreEnabled()) + return; - JobsConnection conn = getConnection(); - if (conn == null) - return; - PreparedStatement prest = null; - try { - conn.setAutoCommit(false); - prest = conn.prepareStatement("UPDATE `" + DBTables.ExploreDataTable.getTableName() + "` SET `" + ExploreDataTableFields.playerNames.getCollumn() + "` = ? WHERE `id` = ?;"); + JobsConnection conn = getConnection(); + if (conn == null) + return; + PreparedStatement prest = null; + try { + conn.setAutoCommit(false); + prest = conn.prepareStatement("UPDATE `" + DBTables.ExploreDataTable.getTableName() + "` SET `" + ExploreDataTableFields.playerNames.getCollumn() + "` = ? WHERE `id` = ?;"); - int i = 0; + int i = 0; - Map> temp = new HashMap<>(Jobs.getExploreManager().getWorlds()); + Map> temp = new HashMap<>(Jobs.getExploreManager().getWorlds()); - for (Entry> worlds : temp.entrySet()) { - for (Entry region : worlds.getValue().entrySet()) { - for (ExploreChunk oneChunk : region.getValue().getChunks().values()) { - if (oneChunk.getDbId() == -1 || !oneChunk.isUpdated()) - continue; + for (Entry> worlds : temp.entrySet()) { + for (Entry region : worlds.getValue().entrySet()) { + for (ExploreChunk oneChunk : region.getValue().getChunks().values()) { + if (oneChunk.getDbId() == -1 || !oneChunk.isUpdated()) + continue; - prest.setString(1, oneChunk.serializeNames()); - prest.setInt(2, oneChunk.getDbId()); - prest.addBatch(); - i++; - } - } - } - prest.executeBatch(); - conn.commit(); - conn.setAutoCommit(true); + prest.setString(1, oneChunk.serializeNames()); + prest.setInt(2, oneChunk.getDbId()); + prest.addBatch(); + i++; + } + } + } + prest.executeBatch(); + conn.commit(); + conn.setAutoCommit(true); - if (i > 0) - Jobs.consoleMsg("&e[Jobs] Updated " + i + " explorer entries."); + if (i > 0) + Jobs.consoleMsg("&e[Jobs] Updated " + i + " explorer entries."); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); - try { - conn.setAutoCommit(true); - } catch (SQLException e) { - e.printStackTrace(); - } - } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + try { + conn.setAutoCommit(true); + } catch (SQLException e) { + e.printStackTrace(); + } + } } /** @@ -2569,49 +2593,49 @@ public abstract class JobsDAO { * @param jobexplore - the information getting saved */ public void loadExplore() { - if (!Jobs.getExploreManager().isExploreEnabled()) - return; + if (!Jobs.getExploreManager().isExploreEnabled()) + return; - JobsConnection conn = getConnection(); - if (conn == null) - return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT * FROM `" + DBTables.ExploreDataTable.getTableName() + "`;"); - res = prest.executeQuery(); - Set missingWorlds = new HashSet<>(); + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + DBTables.ExploreDataTable.getTableName() + "`;"); + res = prest.executeQuery(); + Set missingWorlds = new HashSet<>(); - while (res.next()) { - int worldId = res.getInt(ExploreDataTableFields.worldid.getCollumn()); - JobsWorld jworld = Util.getJobsWorld(worldId); - if (jworld == null || jworld.getWorld() == null) { - missingWorlds.add(worldId); - } else { - Jobs.getExploreManager().load(res); - } - } + while (res.next()) { + int worldId = res.getInt(ExploreDataTableFields.worldid.getCollumn()); + JobsWorld jworld = Util.getJobsWorld(worldId); + if (jworld == null || jworld.getWorld() == null) { + missingWorlds.add(worldId); + } else { + Jobs.getExploreManager().load(res); + } + } - for (Integer one : missingWorlds) { - PreparedStatement prest2 = null; - try { - prest2 = conn.prepareStatement("DELETE FROM `" + DBTables.ExploreDataTable.getTableName() + "` WHERE `" + ExploreDataTableFields.worldid.getCollumn() + "` = ?;"); - prest2.setInt(1, one); - prest2.execute(); - } catch (Throwable e) { - e.printStackTrace(); - } finally { - close(prest2); - } - } + for (Integer one : missingWorlds) { + PreparedStatement prest2 = null; + try { + prest2 = conn.prepareStatement("DELETE FROM `" + DBTables.ExploreDataTable.getTableName() + "` WHERE `" + ExploreDataTableFields.worldid.getCollumn() + "` = ?;"); + prest2.setInt(1, one); + prest2.execute(); + } catch (Throwable e) { + e.printStackTrace(); + } finally { + close(prest2); + } + } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } } @@ -2620,31 +2644,31 @@ public abstract class JobsDAO { * @param worldName - the world getting removed */ public boolean deleteExploredWorld(String worldName) { - if (!Jobs.getExploreManager().isExploreEnabled()) - return false; + if (!Jobs.getExploreManager().isExploreEnabled()) + return false; - JobsConnection conn = getConnection(); - if (conn == null) - return false; + JobsConnection conn = getConnection(); + if (conn == null) + return false; - JobsWorld target = Util.getJobsWorld(worldName); - if (null == target) { - return false; - } + JobsWorld target = Util.getJobsWorld(worldName); + if (null == target) { + return false; + } - boolean res = true; - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("DELETE FROM `" + DBTables.ExploreDataTable.getTableName() + "` WHERE `" + ExploreDataTableFields.worldid.getCollumn() + "` = ?;"); - prest.setInt(1, target.getId()); - prest.execute(); - } catch (Throwable e) { - e.printStackTrace(); - res = false; - } finally { - close(prest); - } - return res; + boolean res = true; + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("DELETE FROM `" + DBTables.ExploreDataTable.getTableName() + "` WHERE `" + ExploreDataTableFields.worldid.getCollumn() + "` = ?;"); + prest.setInt(1, target.getId()); + prest.execute(); + } catch (Throwable e) { + e.printStackTrace(); + res = false; + } finally { + close(prest); + } + return res; } /** @@ -2653,30 +2677,30 @@ public abstract class JobsDAO { * @return */ public List getLognameList(int fromtime, int untiltime) { - JobsConnection conn = getConnection(); - List nameList = new ArrayList<>(); - if (conn == null) - return nameList; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT `" + LogTableFields.userid.getCollumn() + "` FROM `" + DBTables.LogTable.getTableName() + "` WHERE `" + LogTableFields.time.getCollumn() + "` >= ? AND `" - + LogTableFields.time.getCollumn() + "` <= ? ;"); - prest.setInt(1, fromtime); - prest.setInt(2, untiltime); - res = prest.executeQuery(); - while (res.next()) { - int id = res.getInt(LogTableFields.userid.getCollumn()); - if (!nameList.contains(id)) - nameList.add(id); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return nameList; + JobsConnection conn = getConnection(); + List nameList = new ArrayList<>(); + if (conn == null) + return nameList; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT `" + LogTableFields.userid.getCollumn() + "` FROM `" + DBTables.LogTable.getTableName() + "` WHERE `" + LogTableFields.time.getCollumn() + "` >= ? AND `" + + LogTableFields.time.getCollumn() + "` <= ? ;"); + prest.setInt(1, fromtime); + prest.setInt(2, untiltime); + res = prest.executeQuery(); + while (res.next()) { + int id = res.getInt(LogTableFields.userid.getCollumn()); + if (!nameList.contains(id)) + nameList.add(id); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + return nameList; } /** @@ -2685,7 +2709,7 @@ public abstract class JobsDAO { * @return */ public List toplist(String jobsname) { - return toplist(jobsname, 0); + return toplist(jobsname, 0); } /** @@ -2694,42 +2718,42 @@ public abstract class JobsDAO { * @return */ public List toplist(String jobsname, int limit) { - List jobs = new ArrayList<>(); - JobsConnection conn = getConnection(); - if (conn == null) - return jobs; + List jobs = new ArrayList<>(); + JobsConnection conn = getConnection(); + if (conn == null) + return jobs; - Job job = Jobs.getJob(jobsname); - if (job == null) - return jobs; + Job job = Jobs.getJob(jobsname); + if (job == null) + return jobs; - PreparedStatement prest = null; - ResultSet res = null; + PreparedStatement prest = null; + ResultSet res = null; - if (limit < 0) - limit = 0; + if (limit < 0) + limit = 0; - try { - prest = conn.prepareStatement("SELECT `" + JobsTableFields.userid.getCollumn() + "`, `" + JobsTableFields.level.getCollumn() + "`, `" + JobsTableFields.experience.getCollumn() + "` FROM `" - + getJobsTableName() + "` WHERE `" + JobsTableFields.jobid.getCollumn() + "` LIKE ? OR `" + JobsTableFields.jobid.getCollumn() + "` LIKE ? ORDER BY `" + JobsTableFields.level.getCollumn() - + "` DESC, `" + JobsTableFields.experience.getCollumn() + "` DESC LIMIT " + limit + ", 50;"); - prest.setInt(1, job.getId()); - prest.setInt(2, job.getLegacyId()); - res = prest.executeQuery(); + try { + prest = conn.prepareStatement("SELECT `" + JobsTableFields.userid.getCollumn() + "`, `" + JobsTableFields.level.getCollumn() + "`, `" + JobsTableFields.experience.getCollumn() + "` FROM `" + + getJobsTableName() + "` WHERE `" + JobsTableFields.jobid.getCollumn() + "` LIKE ? OR `" + JobsTableFields.jobid.getCollumn() + "` LIKE ? ORDER BY `" + JobsTableFields.level.getCollumn() + + "` DESC, `" + JobsTableFields.experience.getCollumn() + "` DESC LIMIT " + limit + ", 50;"); + prest.setInt(1, job.getId()); + prest.setInt(2, job.getLegacyId()); + res = prest.executeQuery(); - while (res.next()) { - PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(res.getInt(JobsTableFields.userid.getCollumn())); - if (info != null) - jobs.add(new TopList(info, res.getInt(JobsTableFields.level.getCollumn()), res.getInt(JobsTableFields.experience.getCollumn()))); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } + while (res.next()) { + PlayerInfo info = Jobs.getPlayerManager().getPlayerInfo(res.getInt(JobsTableFields.userid.getCollumn())); + if (info != null) + jobs.add(new TopList(info, res.getInt(JobsTableFields.level.getCollumn()), res.getInt(JobsTableFields.experience.getCollumn()))); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } - return jobs; + return jobs; } /** @@ -2738,27 +2762,27 @@ public abstract class JobsDAO { * @return the number of players that have a particular job */ public synchronized int getSlotsTaken(Job job) { - int slot = 0; - JobsConnection conn = getConnection(); - if (conn == null) - return slot; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.jobid.getCollumn() + "` = ? OR `" + JobsTableFields.jobid.getCollumn() + "` = ?;"); - prest.setInt(1, job.getId()); - prest.setInt(2, job.getLegacyId()); - res = prest.executeQuery(); - if (res.next()) { - slot = res.getInt(1); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return slot; + int slot = 0; + JobsConnection conn = getConnection(); + if (conn == null) + return slot; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT COUNT(*) FROM `" + getJobsTableName() + "` WHERE `" + JobsTableFields.jobid.getCollumn() + "` = ? OR `" + JobsTableFields.jobid.getCollumn() + "` = ?;"); + prest.setInt(1, job.getId()); + prest.setInt(2, job.getLegacyId()); + res = prest.executeQuery(); + if (res.next()) { + slot = res.getInt(1); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(res); + close(prest); + } + return slot; } /** @@ -2767,78 +2791,78 @@ public abstract class JobsDAO { * @throws SQLException */ public void executeSQL(String sql) throws SQLException { - JobsConnection conn = getConnection(); - if (conn == null) { - return; - } + JobsConnection conn = getConnection(); + if (conn == null) { + return; + } - try (Statement stmt = conn.createStatement()) { - stmt.execute(sql); - } + try (Statement stmt = conn.createStatement()) { + stmt.execute(sql); + } } protected JobsConnection getConnection() { - try { - return isConnected() ? pool.getConnection() : null; - } catch (SQLException e) { - Jobs.getPluginLogger().severe("Unable to connect to the database: " + e.getMessage()); - return null; - } + try { + return isConnected() ? pool.getConnection() : null; + } catch (SQLException e) { + Jobs.getPluginLogger().severe("Unable to connect to the database: " + e.getMessage()); + return null; + } } public synchronized void vacuum() { - if (dbType != DataBaseType.SqLite) - return; - JobsConnection conn = getConnection(); - if (conn == null) - return; + if (dbType != DataBaseType.SqLite) + return; + JobsConnection conn = getConnection(); + if (conn == null) + return; - PreparedStatement prest = null; - try { - prest = conn.prepareStatement("VACUUM;"); - prest.execute(); - } catch (Throwable e) { - } finally { - close(prest); - } + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("VACUUM;"); + prest.execute(); + } catch (Throwable e) { + } finally { + close(prest); + } } /** * Close all active database handles */ public void closeConnections() { - pool.closeConnection(); + pool.closeConnection(); } protected static void close(ResultSet res) { - if (res != null) - try { - res.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + if (res != null) + try { + res.close(); + } catch (SQLException e) { + e.printStackTrace(); + } } protected static void close(Statement stmt) { - if (stmt != null) - try { - stmt.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + if (stmt != null) + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } } protected static void close(PreparedStatement stmt) { - if (stmt != null) - try { - stmt.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + if (stmt != null) + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } } public String getJobsTableName() { - return DBTables.JobsTable.getTableName(); + return DBTables.JobsTable.getTableName(); } }