From 741bd0ed97356a6a4a016537ef4396b6f2202e20 Mon Sep 17 00:00:00 2001 From: Zrips Date: Fri, 1 Sep 2017 10:32:56 +0300 Subject: [PATCH] Better way to control archived jobs Option to define rejoin job cooldown --- src/main/java/com/gamingmesh/jobs/Jobs.java | 7 +- .../com/gamingmesh/jobs/PlayerManager.java | 22 ++- .../jobs/commands/JobsCommands.java | 24 ++- .../jobs/commands/list/archive.java | 18 +- .../gamingmesh/jobs/commands/list/join.java | 10 ++ .../gamingmesh/jobs/config/ConfigManager.java | 10 +- .../jobs/config/LanguageManager.java | 3 +- .../jobs/container/ArchivedJobs.java | 38 ++++ .../com/gamingmesh/jobs/container/Job.java | 13 +- .../jobs/container/JobProgression.java | 28 +++ .../gamingmesh/jobs/container/JobsPlayer.java | 45 ++++- .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 162 +++++++++++------- .../com/gamingmesh/jobs/dao/JobsMySQL.java | 2 +- 13 files changed, 289 insertions(+), 93 deletions(-) create mode 100644 src/main/java/com/gamingmesh/jobs/container/ArchivedJobs.java diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index d128163b..4f0bf589 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -66,6 +66,7 @@ import com.gamingmesh.jobs.config.TitleManager; import com.gamingmesh.jobs.config.YmlMaker; import com.gamingmesh.jobs.container.ActionInfo; import com.gamingmesh.jobs.container.ActionType; +import com.gamingmesh.jobs.container.ArchivedJobs; import com.gamingmesh.jobs.container.BlockProtection; import com.gamingmesh.jobs.container.Boost; import com.gamingmesh.jobs.container.CurrencyType; @@ -542,18 +543,22 @@ public class Jobs extends JavaPlugin { HashMap> playersJobs = Jobs.getJobsDAO().getAllJobs(); HashMap playersPoints = Jobs.getJobsDAO().getAllPoints(); HashMap> playersLogs = Jobs.getJobsDAO().getAllLogs(); + HashMap playersArchives = Jobs.getJobsDAO().getAllArchivedJobs(); Iterator> it = temp.entrySet().iterator(); while (it.hasNext()) { Entry one = it.next(); try { int id = one.getValue().getID(); - JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayerOffline(one.getValue(), playersJobs.get(id), playersPoints.get(id), playersLogs.get(id)); + JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayerOffline(one.getValue(), playersJobs.get(id), playersPoints.get(id), playersLogs.get(id), playersArchives.get(id)); if (jPlayer == null) continue; Jobs.getPlayerManager().addPlayerToCache(jPlayer); } catch (Exception e) { + e.printStackTrace(); } } + + dao.getMap().clear(); Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Preloaded " + Jobs.getPlayerManager().getPlayersCache().size() + " players data in " + ((int) (((System.currentTimeMillis() - time) / 1000d) * 100) / 100D)); diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index 6e680f7c..4ef9d262 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -41,6 +41,7 @@ import org.bukkit.inventory.meta.ItemMeta; import com.gamingmesh.jobs.api.JobsJoinEvent; import com.gamingmesh.jobs.api.JobsLeaveEvent; import com.gamingmesh.jobs.api.JobsLevelUpEvent; +import com.gamingmesh.jobs.container.ArchivedJobs; import com.gamingmesh.jobs.container.Boost; import com.gamingmesh.jobs.container.BoostMultiplier; import com.gamingmesh.jobs.container.CurrencyType; @@ -64,6 +65,7 @@ public class PlayerManager { private ConcurrentHashMap playersUUIDCache = new ConcurrentHashMap(); private ConcurrentHashMap players = new ConcurrentHashMap(); private ConcurrentHashMap playersUUID = new ConcurrentHashMap(); + private PointsData PointsDatabase = new PointsData(); private final String mobSpawnerMetadata = "jobsMobSpawner"; @@ -281,10 +283,11 @@ public class PlayerManager { /** * Get the player job info for specific player + * @param archivedJobs * @param player - the player who's job you're getting * @return the player job info of the player */ - public JobsPlayer getJobsPlayerOffline(PlayerInfo info, List jobs, PlayerPoints points, HashMap logs) { + public JobsPlayer getJobsPlayerOffline(PlayerInfo info, List jobs, PlayerPoints points, HashMap logs, ArchivedJobs archivedJobs) { if (info == null) return null; @@ -296,7 +299,6 @@ public class PlayerManager { jPlayer.setPlayerUUID(info.getUuid()); jPlayer.setUserId(info.getID()); -// List list = Jobs.getJobsDAO().getAllJobs(info.getName(), info.getUuid()); if (jobs != null) for (JobsDAOData jobdata : jobs) { if (Jobs.getJob(jobdata.getJobName()) == null) @@ -319,6 +321,18 @@ public class PlayerManager { if (logs != null) jPlayer.setLog(logs); + if (archivedJobs != null) { + ArchivedJobs aj = new ArchivedJobs(); + for (JobProgression one : archivedJobs.getArchivedJobs()) { + JobProgression jp = new JobProgression(one.getJob(), jPlayer, one.getLevel(), one.getExperience()); + jp.reloadMaxExperience(); + if (one.getLeftOn() != null && one.getLeftOn() != 0L) + jp.setLeftOn(one.getLeftOn()); + aj.addArchivedJob(jp); + } + jPlayer.setArchivedJobs(aj); + } + return jPlayer; } @@ -342,7 +356,7 @@ public class PlayerManager { if (jobsjoinevent.isCancelled()) return; - Jobs.getJobsDAO().joinJob(jPlayer, job); + Jobs.getJobsDAO().joinJob(jPlayer, jPlayer.getJobProgression(job)); PerformCommands.PerformCommandsOnJoin(jPlayer, job); Jobs.takeSlot(job); Jobs.getSignUtil().SignUpdate(job.getName()); @@ -413,7 +427,7 @@ public class PlayerManager { if (!dao.quitJob(jPlayer, oldjob)) return false; oldjob.updateTotalPlayers(); - dao.joinJob(jPlayer, newjob); + dao.joinJob(jPlayer, jPlayer.getJobProgression(newjob)); newjob.updateTotalPlayers(); jPlayer.save(); // } diff --git a/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java b/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java index 3369f76a..d40ae7b0 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java +++ b/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java @@ -579,10 +579,10 @@ public class JobsCommands implements CommandExecutor { */ public String jobStatsMessage(JobProgression jobProg) { String message = Jobs.getLanguage().getMessage("command.stats.output", - "%joblevel%", Integer.valueOf(jobProg.getLevel()).toString(), + "%joblevel%", jobProg.getLevel(), "%jobname%", jobProg.getJob().getChatColor() + jobProg.getJob().getName() + ChatColor.WHITE, - "%jobxp%", Double.toString(Math.round(jobProg.getExperience() * 100.0) / 100.0), - "%jobmaxxp%", Integer.toString(jobProg.getMaxExperience())); + "%jobxp%",Math.round(jobProg.getExperience() * 100.0) / 100.0, + "%jobmaxxp%", jobProg.getMaxExperience()); return " " + jobProgressMessage(jobProg.getMaxExperience(), jobProg.getExperience()) + " " + message; } @@ -613,15 +613,13 @@ public class JobsCommands implements CommandExecutor { * @param jobInfo - jobinfo string line * @return the message */ - public String jobStatsMessage(String jobInfo) { - String[] splited = jobInfo.split(":"); - if (Jobs.getJob(splited[0]) == null) - return ""; - String message = Jobs.getLanguage().getMessage("command.archive.output", - "%joblevel%", Integer.valueOf(splited[1]).toString(), - "%getbackjoblevel%", Integer.valueOf(splited[2]).toString(), - "%jobname%", Jobs.getJob(splited[0]).getChatColor() + splited[0] + ChatColor.WHITE, - "%jobxp%", Double.toString(Math.round(Double.valueOf(splited[3]) * 100.0) / 100.0)); - return message; + public String jobStatsMessageArchive(JobsPlayer jPlayer, JobProgression jobProg) { + String message = Jobs.getLanguage().getMessage("command.archive.feedback", + "%joblevel%", jobProg.getLevel(), + "%leftjoblevel%", jPlayer.getLevelAfterRejoin(jobProg), + "%jobname%", jobProg.getJob().getChatColor() + jobProg.getJob().getName() + ChatColor.WHITE, + "%jobxp%", Math.round(jobProg.getExperience() * 100.0) / 100.0, + "%jobmaxxp%", jobProg.getMaxExperience()); + return " " + jobProgressMessage(jobProg.getMaxExperience(), jobProg.getExperience()) + " " + message; } } diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/archive.java b/src/main/java/com/gamingmesh/jobs/commands/list/archive.java index 61cc8f1c..d354a27b 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/archive.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/archive.java @@ -1,14 +1,18 @@ package com.gamingmesh.jobs.commands.list; -import java.util.List; +import java.util.Set; +import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.commands.JobCommand; +import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; +import com.gamingmesh.jobs.stuff.RawMessage; +import com.gamingmesh.jobs.stuff.TimeManage; public class archive implements Cmd { @@ -32,7 +36,7 @@ public class archive implements Cmd { return true; } - List AllJobs = Jobs.getJobsDAO().getJobsFromArchive(jPlayer); + Set AllJobs = jPlayer.getArchivedJobs().getArchivedJobs(); if (AllJobs.isEmpty()) { sender.sendMessage(Jobs.getLanguage().getMessage("command.archive.error.nojob")); @@ -40,8 +44,14 @@ public class archive implements Cmd { } sender.sendMessage(Jobs.getLanguage().getMessage("general.info.toplineseparator", "%playername%", jPlayer.getUserName())); - for (String jobInfo : AllJobs) { - sender.sendMessage(Jobs.getCommandManager().jobStatsMessage(jobInfo)); + for (JobProgression jobInfo : AllJobs) { + RawMessage rm = new RawMessage(); + if (jobInfo.canRejoin()) + rm.add(ChatColor.GREEN + "+" + Jobs.getCommandManager().jobStatsMessageArchive(jPlayer, jobInfo), "Click to rejoin this job", "jobs join " + jobInfo.getJob().getName()); + else + rm.add(ChatColor.RED + "-" + Jobs.getCommandManager().jobStatsMessageArchive(jPlayer, jobInfo), "You cant join, need to wait " + (TimeManage.to24hourShort(jobInfo.getLeftOn() + jobInfo + .getJob().getRejoinCd() - System.currentTimeMillis()))); + rm.show(sender); } sender.sendMessage(Jobs.getLanguage().getMessage("general.info.separator")); return true; diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/join.java b/src/main/java/com/gamingmesh/jobs/commands/list/join.java index ee45b588..944b1360 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/join.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/join.java @@ -8,8 +8,10 @@ import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.commands.JobCommand; import com.gamingmesh.jobs.container.Job; +import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.stuff.ChatColor; + public class join implements Cmd { @Override @@ -83,6 +85,14 @@ public class join implements Cmd { return true; } + JobProgression ajp = jPlayer.getArchivedJobProgression(job); + if (ajp != null) { + if (!ajp.canRejoin()) { + sender.sendMessage(Jobs.getLanguage().getMessage("command.join.error.rejoin", "[time]", ajp.getRejoinTimeMessage())); + return true; + } + } + Jobs.getPlayerManager().joinJob(jPlayer, job); String message = Jobs.getLanguage().getMessage("command.join.success"); diff --git a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java index 45583cff..8da6ac62 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java @@ -194,6 +194,12 @@ public class ConfigManager { maxSlots = null; } + Long rejoinCd = jobSection.getLong("rejoinCooldown", 0L); + if (rejoinCd < 0L) { + rejoinCd = 0L; + } + rejoinCd = rejoinCd * 1000L; + String jobShortName = jobSection.getString("shortname", null); if (jobShortName == null) { Jobs.getPluginLogger().warning("Job " + jobKey + " is missing the shortname property. Skipping job!"); @@ -472,12 +478,12 @@ public class ConfigManager { } Job job = new Job(jobName, jobShortName, description, color, maxExpEquation, displayMethod, maxLevel, vipmaxLevel, maxSlots, jobPermissions, jobCommand, - jobConditions, jobItems, jobLimitedItems, JobsCommandOnJoin, JobsCommandOnLeave, GUIitem, bossbar); + jobConditions, jobItems, jobLimitedItems, JobsCommandOnJoin, JobsCommandOnLeave, GUIitem, bossbar, rejoinCd); job.setMoneyEquation(incomeEquation); job.setXpEquation(expEquation); job.setPointsEquation(pointsEquation); - + for (ActionType actionType : ActionType.values()) { ConfigurationSection typeSection = jobSection.getConfigurationSection(actionType.getName()); ArrayList jobInfo = new ArrayList(); diff --git a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java index 39f28796..9a418cf2 100644 --- a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java @@ -250,7 +250,7 @@ public class LanguageManager { c.get("command.archive.help.args", "[playername]"); Jobs.getGCManager().commandArgs.put("archive", Arrays.asList("[playername]")); c.get("command.archive.error.nojob", "There is no jobs saved."); - c.get("command.archive.output", "lvl %joblevel% (%getbackjoblevel%) %jobname%"); + c.get("command.archive.feedback", " lvl%joblevel%(%leftjoblevel%) %jobname% : %jobxp%/%jobmaxxp% xp"); c.get("command.give.help.info", "Gives item by jobs name and item category name. Player name is optional"); c.get("command.give.help.args", "[playername] [jobname] [itemname]"); @@ -335,6 +335,7 @@ public class LanguageManager { c.get("command.join.error.alreadyin", "You are already in the job %jobname%."); c.get("command.join.error.fullslots", "You cannot join the job %jobname%, there are no slots available."); c.get("command.join.error.maxjobs", "You have already joined too many jobs."); + c.get("command.join.error.rejoin", "&cCan't rejoin this job. Wait [time]"); c.get("command.join.success", "You have joined the job %jobname%."); c.get("command.leave.help.info", "Leave the selected job."); diff --git a/src/main/java/com/gamingmesh/jobs/container/ArchivedJobs.java b/src/main/java/com/gamingmesh/jobs/container/ArchivedJobs.java new file mode 100644 index 00000000..d6cdb72d --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/container/ArchivedJobs.java @@ -0,0 +1,38 @@ +package com.gamingmesh.jobs.container; + +import java.util.HashSet; +import java.util.Set; + +public class ArchivedJobs { + + private Set jobs = new HashSet(); + + public Set getArchivedJobs() { + return jobs; + } + + public JobProgression getArchivedJobProgression(Job job) { + for (JobProgression one : jobs) { + if (one.getJob().isSame(job)) + return one; + } + return null; + } + + public void setArchivedJobs(Set jobs) { + this.jobs = jobs; + } + + public void addArchivedJob(JobProgression job) { + jobs.add(job); + } + + public void removeArchivedJob(Job job) { + for (JobProgression one : jobs) { + if (one.getJob().isSame(job)) { + jobs.remove(one); + break; + } + } + } +} diff --git a/src/main/java/com/gamingmesh/jobs/container/Job.java b/src/main/java/com/gamingmesh/jobs/container/Job.java index 3a87b4ae..f454f3c8 100644 --- a/src/main/java/com/gamingmesh/jobs/container/Job.java +++ b/src/main/java/com/gamingmesh/jobs/container/Job.java @@ -66,6 +66,8 @@ public class Job { private List CmdOnLeave = new ArrayList(); // Item for GUI private ItemStack GUIitem; + // Item for GUI + private Long rejoinCd = 0L; private int totalPlayers = -1; private Double bonus = null; @@ -96,7 +98,7 @@ public class Job { */ public Job(String jobName, String jobShortName, String description, ChatColor jobColour, Parser maxExpEquation, DisplayMethod displayMethod, int maxLevel, int vipmaxLevel, Integer maxSlots, List jobPermissions, List jobCommands, List jobConditions, List jobItems, - List jobLimitedItems, List CmdOnJoin, List CmdOnLeave, ItemStack GUIitem, String bossbar) { + List jobLimitedItems, List CmdOnJoin, List CmdOnLeave, ItemStack GUIitem, String bossbar, Long rejoinCD) { this.jobName = jobName; this.jobShortName = jobShortName; this.description = description; @@ -115,6 +117,7 @@ public class Job { this.CmdOnLeave = CmdOnLeave; this.GUIitem = GUIitem; this.bossbar = bossbar; + this.rejoinCd = rejoinCD; } public void addBoost(CurrencyType type, double Point) { @@ -378,4 +381,12 @@ public class Job { public void setPointsEquation(Parser pointsEquation) { this.pointsEquation = pointsEquation; } + + public Long getRejoinCd() { + return rejoinCd; + } + + public void setRejoinCd(Long rejoinCd) { + this.rejoinCd = rejoinCd; + } } diff --git a/src/main/java/com/gamingmesh/jobs/container/JobProgression.java b/src/main/java/com/gamingmesh/jobs/container/JobProgression.java index 52a1e3f7..b6148af3 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobProgression.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobProgression.java @@ -20,12 +20,15 @@ package com.gamingmesh.jobs.container; import java.util.HashMap; +import com.gamingmesh.jobs.stuff.TimeManage; + public class JobProgression { private Job job; private JobsPlayer jPlayer; private double experience; private int level; private transient int maxExperience = -1; + private Long leftOn = null; public JobProgression(Job job, JobsPlayer jPlayer, int level, double experience) { this.job = job; @@ -223,4 +226,29 @@ public class JobProgression { reloadMaxExperience(); return checkLevelUp(); } + + public Long getLeftOn() { + return leftOn; + } + + public JobProgression setLeftOn(Long leftOn) { + this.leftOn = leftOn; + return this; + } + + public boolean canRejoin() { + if (this.leftOn == null) + return true; + if (this.leftOn + this.getJob().getRejoinCd() < System.currentTimeMillis()) + return true; + return false; + } + + public String getRejoinTimeMessage() { + if (leftOn == null) + return ""; + String msg = (TimeManage.to24hourShort(getLeftOn() + getJob().getRejoinCd() - System.currentTimeMillis())); + return msg; + } + } diff --git a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java index fe2a5773..8287d756 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java @@ -23,6 +23,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; @@ -42,6 +43,7 @@ public class JobsPlayer { // progression of the player in each job public UUID playerUUID; public ArrayList progression = new ArrayList(); + private ArchivedJobs archivedJobs = new ArchivedJobs(); private PaymentData paymentLimits = null; @@ -80,6 +82,18 @@ public class JobsPlayer { this.player = Bukkit.getPlayer(userName); } + public ArchivedJobs getArchivedJobs() { + return archivedJobs; + } + + public JobProgression getArchivedJobProgression(Job job) { + return archivedJobs.getArchivedJobProgression(job); + } + + public void setArchivedJobs(ArchivedJobs archivedJob) { + this.archivedJobs = archivedJob; + } + public int getTotalLevels() { int i = 0; for (JobProgression job : progression) { @@ -370,10 +384,10 @@ public class JobsPlayer { if (!isInJob(job)) { int level = 1; int exp = 0; - if (Jobs.getJobsDAO().checkArchive(this, job).size() > 0) { - List info = Jobs.getJobsDAO().checkArchive(this, job); - level = info.get(0); - //exp = info.get(1); + + JobProgression archived = this.getArchivedJobProgression(job); + if (archived != null) { + level = getLevelAfterRejoin(archived); Jobs.getJobsDAO().deleteArchive(this, job); } @@ -388,6 +402,29 @@ public class JobsPlayer { // } } + public int getLevelAfterRejoin(JobProgression jp) { + if (jp == null) + return 1; + + int level = jp.getLevel(); + + level = (int) ((level - (level * (Jobs.getGCManager().levelLossPercentage / 100.0)))); + if (level < 1) + level = 1; + + Job job = jp.getJob(); + int maxLevel = 0; + if (havePermission("jobs." + job.getName() + ".vipmaxlevel") && job.getVipMaxLevel() != 0) + maxLevel = job.getVipMaxLevel(); + else + maxLevel = job.getMaxLevel(); + + if (Jobs.getGCManager().fixAtMaxLevel && jp.getLevel() == maxLevel) + level = jp.getLevel(); + + return level; + } + /** * Player leaves a job * @param job - the job left diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 903fb6d6..d759a2df 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -19,6 +19,7 @@ import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.container.ArchivedJobs; import com.gamingmesh.jobs.container.BlockProtection; import com.gamingmesh.jobs.container.Convert; import com.gamingmesh.jobs.container.CurrencyType; @@ -113,7 +114,8 @@ public abstract class JobsDAO { userid("int", TablesFieldsType.number), job("text", TablesFieldsType.text), experience("int", TablesFieldsType.number), - level("int", TablesFieldsType.number); + level("int", TablesFieldsType.number), + left("bigint", TablesFieldsType.longNumber); private String type; private TablesFieldsType fieldType; @@ -634,6 +636,47 @@ public abstract class JobsDAO { return map; } + public HashMap getAllArchivedJobs() { + HashMap map = new HashMap(); + JobsConnection conn = getConnection(); + if (conn == null) + return map; + PreparedStatement prest = null; + ResultSet res = null; + try { + prest = conn.prepareStatement("SELECT * FROM `" + prefix + "archive`;"); + res = prest.executeQuery(); + while (res.next()) { + + int id = res.getInt("userid"); + String jobName = res.getString("job"); + Double exp = res.getDouble("experience"); + int lvl = res.getInt("level"); + Long left = res.getLong("left"); + + Job job = Jobs.getJob(jobName); + 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; + } + public HashMap> getAllLogs() { HashMap> map = new HashMap>(); JobsConnection conn = getConnection(); @@ -941,24 +984,19 @@ public abstract class JobsDAO { * @param player - player that wishes to join the job * @param job - job that the player wishes to join */ - public synchronized void joinJob(JobsPlayer jPlayer, Job job) { + public synchronized void joinJob(JobsPlayer jPlayer, JobProgression job) { JobsConnection conn = getConnection(); if (conn == null) return; PreparedStatement prest = null; try { - int level = 1; - int exp = 0; - if (checkArchive(jPlayer, job).size() > 0) { - List info = checkArchive(jPlayer, job); - level = info.get(0); - deleteArchive(jPlayer, job); - } + int level = job.getLevel(); + Double exp = job.getExperience(); prest = conn.prepareStatement("INSERT INTO `" + prefix + "jobs` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);"); prest.setInt(1, jPlayer.getUserId()); - prest.setString(2, job.getName()); + prest.setString(2, job.getJob().getName()); prest.setInt(3, level); - prest.setInt(4, exp); + prest.setInt(4, exp.intValue()); prest.execute(); } catch (SQLException e) { e.printStackTrace(); @@ -1141,24 +1179,24 @@ 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 { - int level = 1; - int exp = 0; - for (JobProgression progression : jPlayer.getJobProgression()) { - if (progression.getJob().getName().equalsIgnoreCase(job.getName())) { - level = progression.getLevel(); - exp = (int) progression.getExperience(); - } - } - prest = conn.prepareStatement("INSERT INTO `" + prefix + "archive` (`userid`, `job`, `level`, `experience`) VALUES (?, ?, ?, ?);"); + int level = jp.getLevel(); + Double exp = jp.getExperience(); + prest = conn.prepareStatement("INSERT INTO `" + prefix + "archive` (`userid`, `job`, `level`, `experience`, `left`) VALUES (?, ?, ?, ?, ?);"); prest.setInt(1, jPlayer.getUserId()); prest.setString(2, job.getName()); prest.setInt(3, level); - prest.setInt(4, exp); + prest.setInt(4, exp.intValue()); + prest.setLong(5, System.currentTimeMillis()); prest.execute(); } catch (SQLException e) { e.printStackTrace(); @@ -1167,47 +1205,46 @@ public abstract class JobsDAO { } } - /** - * Check job in archive - * @param player - player that wishes to quit the job - * @param job - job that the player wishes to quit - */ - public synchronized List checkArchive(JobsPlayer jPlayer, Job job) { - JobsConnection conn = getConnection(); - List info = new ArrayList(); - if (conn == null) - return info; - PreparedStatement prest = null; - ResultSet res = null; - try { - prest = conn.prepareStatement("SELECT `level`, `experience` FROM `" + prefix + "archive` WHERE `userid` = ? AND `job` = ?;"); - prest.setInt(1, jPlayer.getUserId()); - prest.setString(2, job.getName()); - res = prest.executeQuery(); - if (res.next()) { - int level = (int) ((res.getInt(1) - (res.getInt(1) * (Jobs.getGCManager().levelLossPercentage / 100.0)))); - if (level < 1) - level = 1; - - int maxLevel = 0; - if (jPlayer.havePermission("jobs." + job.getName() + ".vipmaxlevel") && job.getVipMaxLevel() != 0) - maxLevel = job.getVipMaxLevel(); - else - maxLevel = job.getMaxLevel(); - - if (Jobs.getGCManager().fixAtMaxLevel && res.getInt(1) == maxLevel) - level = res.getInt(1); - info.add(level); - info.add(res.getInt(2)); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(res); - close(prest); - } - return info; - } +// /** +// * Check job in archive +// * @param player - player that wishes to quit the job +// * @param job - job that the player wishes to quit +// */ +// public synchronized JobProgression getOldJobProgresion(JobsPlayer jPlayer, Job job) { +// JobsConnection conn = getConnection(); +// JobProgression jp = null; +// if (conn == null) +// return jp; +// PreparedStatement prest = null; +// ResultSet res = null; +// try { +// prest = conn.prepareStatement("SELECT `level`, `experience`, `left` FROM `" + prefix + "archive` WHERE `userid` = ? AND `job` = ?;"); +// prest.setInt(1, jPlayer.getUserId()); +// prest.setString(2, job.getName()); +// res = prest.executeQuery(); +// if (res.next()) { +// int level = (int) ((res.getInt(1) - (res.getInt(1) * (Jobs.getGCManager().levelLossPercentage / 100.0)))); +// if (level < 1) +// level = 1; +// +// int maxLevel = 0; +// if (jPlayer.havePermission("jobs." + job.getName() + ".vipmaxlevel") && job.getVipMaxLevel() != 0) +// maxLevel = job.getVipMaxLevel(); +// else +// maxLevel = job.getMaxLevel(); +// +// if (Jobs.getGCManager().fixAtMaxLevel && res.getInt(1) == maxLevel) +// level = res.getInt(1); +// jp = new JobProgression(job, jPlayer, level, res.getInt("experience")); +// } +// } catch (SQLException e) { +// e.printStackTrace(); +// } finally { +// close(res); +// close(prest); +// } +// return jp; +// } public List getGlobalTopList() { return getGlobalTopList(0); @@ -1415,6 +1452,7 @@ 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; diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java b/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java index 61e84d55..4c2fc9e0 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java @@ -195,7 +195,7 @@ public class JobsMySQL extends JobsDAO { return true; } catch (SQLException e) { close(statement); - e.printStackTrace(); +// e.printStackTrace(); return false; } }