From 2fd03a4384bf2204cc4ef4c04cda07e089f7457e Mon Sep 17 00:00:00 2001 From: Zrips Date: Thu, 16 Feb 2023 17:39:12 +0200 Subject: [PATCH] Actually working %jobsr_jtop_[jobName/number]_[1-15]% placeholder --- .../jobs/Placeholders/Placeholder.java | 47 +++++++-------- .../jobs/config/BlockProtectionManager.java | 6 ++ .../jobs/config/RestrictedBlockManager.java | 1 + .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 57 ++++++++++++++++++- 4 files changed, 83 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java index 1b023d53..f62227be 100644 --- a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java +++ b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java @@ -7,8 +7,6 @@ import java.util.List; import java.util.Locale; import java.util.Optional; import java.util.UUID; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -27,7 +25,6 @@ import com.gamingmesh.jobs.container.Title; import com.gamingmesh.jobs.container.TopList; import com.gamingmesh.jobs.container.blockOwnerShip.BlockOwnerShip; import com.gamingmesh.jobs.container.blockOwnerShip.BlockTypes; -import com.gamingmesh.jobs.stuff.TimeManage; import net.Zrips.CMILib.Colors.CMIChatColor; import net.Zrips.CMILib.Container.CMIList; @@ -38,7 +35,6 @@ public class Placeholder { private Jobs plugin; - private final AtomicInteger jobLevel = new AtomicInteger(); private final Pattern placeholderPatern = Pattern.compile("(%)([^\"^%]*)(%)"); public Placeholder(Jobs plugin) { @@ -75,7 +71,6 @@ public class Placeholder { user_jobs, user_boost_$1_$2("jname/number", "money/exp/points"), - user_jtoplvl_$1_$2("jname/number", "number"), user_isin_$1("jname/number"), user_canjoin_$1("jname/number"), user_jlevel_$1("jname/number"), @@ -92,6 +87,8 @@ public class Placeholder { user_archived_jobs_level_$1("jname/number"), user_archived_jobs_exp_$1("jname/number"), + jtop_$1_$2("jname/number", "[1-15]"), + maxjobs, total_workers, @@ -533,26 +530,6 @@ public class Placeholder { case user_boost_$1_$2: Boost boost = Jobs.getPlayerManager().getFinalBonus(user, job, true, true); return (vals.size() < 2 || j == null) ? "" : simplifyDouble(boost.getFinal(CurrencyType.getByName(vals.get(1)), false, true)); - case user_jtoplvl_$1_$2: - if (vals.size() < 2 || job == null) - return ""; - - try { - jobLevel.set(Integer.parseInt(vals.get(1))); - } catch (NumberFormatException e) { - return ""; - } - - return CompletableFuture.supplyAsync(() -> { - for (TopList l : Jobs.getJobsDAO().getGlobalTopList(jobLevel.get())) { - if (l.getPlayerInfo().getName().equals(user.getName())) { - JobProgression prog = l.getPlayerInfo().getJobsPlayer().getJobProgression(job); - return prog == null ? "" : prog.getLevelFormatted(); - } - } - - return ""; - }).join(); case user_isin_$1: return job == null ? "no" : convert(user.isInJob(job)); case user_job_$1: @@ -622,6 +599,26 @@ public class Placeholder { return ""; // Global placeholders by jobname switch (placeHolder) { + case jtop_$1_$2: + if (values.size() < 2) + return ""; + + int place = 0; + try { + place = Integer.parseInt(values.get(1)); + } catch (NumberFormatException e) { + return ""; + } + + if (place < 1) + return ""; + + List list = Jobs.getJobsDAO().getTopListByJob(jo, 15); + + if (list.size() < place) + return ""; + + return list.get(place - 1).getPlayerInfo().getDisplayName(); case name_$1: return jo.getName(); case shortname_$1: diff --git a/src/main/java/com/gamingmesh/jobs/config/BlockProtectionManager.java b/src/main/java/com/gamingmesh/jobs/config/BlockProtectionManager.java index 0fd7ed13..7a6e51d8 100644 --- a/src/main/java/com/gamingmesh/jobs/config/BlockProtectionManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/BlockProtectionManager.java @@ -40,6 +40,9 @@ public class BlockProtectionManager { } public void add(Block block, Integer cd) { + + if (cd == 0) + return; // Assuming that block is bottom part of flower we will add top part to the record too CMIMaterial cmat = CMIMaterial.get(block); @@ -77,6 +80,9 @@ public class BlockProtectionManager { public BlockProtection addP(Location loc, Long time, boolean paid, boolean cache) { + if (time == 0) + return null; + String v = loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ(); HashMap>> regions = map.getOrDefault(loc.getWorld(), new HashMap<>()); diff --git a/src/main/java/com/gamingmesh/jobs/config/RestrictedBlockManager.java b/src/main/java/com/gamingmesh/jobs/config/RestrictedBlockManager.java index bd2bd013..da7584d0 100644 --- a/src/main/java/com/gamingmesh/jobs/config/RestrictedBlockManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/RestrictedBlockManager.java @@ -35,6 +35,7 @@ public class RestrictedBlockManager { "Category name can be any you like to be easily recognized", "id can be actual block id (use /jobs blockinfo to get correct id) or use block name", "By setting time to -1 will keep block protected until global cleanup, mainly used for structure blocks like diamond", + "Set to 0 if you want to disable protection on specific blocks", "If you want to have default value for all blocks, enable GlobalBlockTimer in generalConfig file"); org.bukkit.configuration.ConfigurationSection section = cfg.getC().getConfigurationSection("blocksTimer"); diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index d313c6aa..209356e8 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -1733,6 +1733,55 @@ public abstract class JobsDAO { return getGlobalTopList(0); } + HashMap> TopListByJobCache = new HashMap>(); + HashMap TopListByJobUpdateCache = new HashMap(); + + /** + * Get player list by total job level + * @param start - starting entry + * @return info - information about jobs + */ + public List getTopListByJob(Job job, int amount) { + + if (System.currentTimeMillis() - TopListByJobUpdateCache.getOrDefault(job.getName(), 0L) < 30 * 1000L) { + return TopListByJobCache.get(job.getName()); + } + + TopListByJobUpdateCache.put(job.getName(), System.currentTimeMillis()); + + List jobs = new ArrayList<>(); + JobsConnection conn = getConnection(); + if (conn == null) + return jobs; + + PreparedStatement prest = null; + ResultSet res = null; + + 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 " + 0 + ", " + amount + ";"); + 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); + } + + TopListByJobCache.put(job.getName(), jobs); + + return jobs; + } + /** * Get player list by total job level * @param start - starting entry @@ -2452,9 +2501,11 @@ public abstract class JobsDAO { 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); + if (bp != null) { + bp.setId(id); + bp.setRecorded(res.getLong(BlockTableFields.recorded.getCollumn())); + bp.setAction(DBAction.NONE); + } i++; if (ii++ >= 100000) {