From 84681df41a7400c281830c72b7a4b5a7b07dde73 Mon Sep 17 00:00:00 2001 From: Zrips Date: Thu, 31 Jan 2019 16:47:52 +0200 Subject: [PATCH] Lets add option to limit boosts by levels --- .../com/gamingmesh/jobs/ItemBoostManager.java | 71 +++++++++++++++++-- .../com/gamingmesh/jobs/PlayerManager.java | 62 ++++++++-------- .../java/com/gamingmesh/jobs/Reflections.java | 42 +++++------ .../jobs/commands/list/edititembonus.java | 3 + .../gamingmesh/jobs/commands/list/give.java | 5 +- .../jobs/commands/list/itembonus.java | 2 +- .../jobs/config/LanguageManager.java | 2 +- .../com/gamingmesh/jobs/container/Job.java | 2 + .../gamingmesh/jobs/container/JobItems.java | 28 +++++++- .../gamingmesh/jobs/stuff/TabComplete.java | 27 ++++--- 10 files changed, 168 insertions(+), 76 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/ItemBoostManager.java b/src/main/java/com/gamingmesh/jobs/ItemBoostManager.java index 9627bbc6..5ddccc13 100644 --- a/src/main/java/com/gamingmesh/jobs/ItemBoostManager.java +++ b/src/main/java/com/gamingmesh/jobs/ItemBoostManager.java @@ -21,7 +21,7 @@ import com.gamingmesh.jobs.container.JobItems; public class ItemBoostManager { - static HashMap items = new HashMap(); + private static HashMap items = new HashMap(); public ItemBoostManager() { @@ -82,24 +82,59 @@ public class ItemBoostManager { Set keys = cfg.getC().getKeys(false); + cfg.addComment("exampleBoost", "Name which will be used to identify this particular item boost", + "This is EXAMPLE boost and will be ignored"); + cfg.addComment("exampleBoost.id", "Item Id which can be any material name as of 1.13 update", + "This is only used when performing command like /jobs give, but boost itself is not dependent on item type", + "You can use ingame command /jobs edititemboost to give particular boost to any item you are holding"); + cfg.get("exampleBoost.id", "Golden_shovel"); + cfg.addComment("exampleBoost.name", "(Optional) Item custom name", "Custom colors like &2 &5 can be used"); + cfg.get("exampleBoost.name", "&2Custom item name"); + cfg.addComment("exampleBoost.lore", "(Optional) Item custom lore", "Same as name, supports color codes"); + cfg.get("exampleBoost.lore", Arrays.asList("&2Some random", "&5Lore with some", "&7Colors")); + cfg.addComment("exampleBoost.enchants", "(Optional) Item custom enchants", + "All enchantment names can be found https://hub.spigotmc.org/javadocs/spigot/org/bukkit/enchantments/Enchantment.html"); + cfg.get("exampleBoost.enchants", Arrays.asList("FIRE_ASPECT=1", "DAMAGE_ALL=1")); + cfg.addComment("exampleBoost.moneyBoost", "[Required] Money boost: 1.1 is equals 10% more income when 0.9 is equals 10% less from base income"); + for (CurrencyType oneC : CurrencyType.values()) { + cfg.get("exampleBoost." + oneC.toString().toLowerCase() + "Boost", 1D); + } + cfg.addComment("exampleBoost.jobs", "[Required] Jobs which should receive this boost", + "Can be specific jobs or use 'all' to give this boost for every job"); + cfg.get("exampleBoost.jobs", Arrays.asList("Miner", "Woodcutter", "All")); + + cfg.addComment("exampleBoost.levelFrom", "(Optional) Defines level of job from which this boost should be applied", + "Keep in mind that if boost have multiple jobs, then level will be checked by job which is requesting boost value"); + cfg.get("exampleBoost.levelFrom", 0); + cfg.addComment("exampleBoost.levelUntil", "(Optional) Defines level of job until which this boost should be applied"); + cfg.get("exampleBoost.levelUntil", 50); + for (String one : keys) { if (!cfg.getC().isConfigurationSection(one)) continue; - CMIMaterial mat = CMIMaterial.get(cfg.get(one + ".id", "Stone")); - if (mat == null) { - Jobs.getPluginLogger().warning("Cant load " + one + " boosted item!"); + // Ignoring example job + if (one.equalsIgnoreCase("exampleBoost")) continue; + + CMIMaterial mat = null; + + if (cfg.getC().isString(one + ".id")) { + mat = CMIMaterial.get(cfg.get(one + ".id", "Stone")); } + String name = null; - if (cfg.getC().isString(one + ".name")) + + if (cfg.getC().isString(one + ".name")) { name = cfg.get(one + ".name", ""); + } List lore = new ArrayList<>(); - if (cfg.getC().getStringList(one + ".lore") != null && !cfg.getC().getStringList(one + ".lore").isEmpty()) + if (cfg.getC().getStringList(one + ".lore") != null && !cfg.getC().getStringList(one + ".lore").isEmpty()) { for (String eachLine : cfg.get(one + ".lore", Arrays.asList(""))) { lore.add(org.bukkit.ChatColor.translateAlternateColorCodes('&', eachLine)); } + } HashMap enchants = new HashMap<>(); if (cfg.getC().getStringList(one + ".enchants") != null && !cfg.getC().getStringList(one + ".enchants").isEmpty()) @@ -132,7 +167,10 @@ public class ItemBoostManager { Jobs.getPluginLogger().warning("Cant determine job by " + oneJ + " name for " + one + " boosted item!"); continue; } - jobs.add(job); + if (oneJ.equalsIgnoreCase("all")) { + jobs.addAll(Jobs.getJobs()); + } else if (job != null) + jobs.add(job); } if (jobs.isEmpty()) { @@ -140,7 +178,18 @@ public class ItemBoostManager { continue; } JobItems item = new JobItems(one.toLowerCase(), mat, 1, name, lore, enchants, b, jobs); + + if (cfg.getC().isInt(one + ".levelFrom")) { + item.setFromLevel(cfg.get(one + ".levelFrom", 0)); + } + if (cfg.getC().isInt(one + ".levelUntil")) { + item.setUntilLevel(cfg.get(one + ".levelUntil", 1000)); + } + + Jobs.consoleMsg(one.toLowerCase() + " " + (item == null) + ""); for (Job oneJ : jobs) { + if (oneJ == null) + continue; oneJ.getItemBonus().put(one.toLowerCase(), item); } @@ -172,4 +221,12 @@ public class ItemBoostManager { public static JobItems getItemByKey(String key) { return items.get(key.toLowerCase()); } + + public static HashMap getItems() { + return items; + } + + public static void setItems(HashMap items) { + ItemBoostManager.items = items; + } } diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index 2555883e..decf1e40 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -58,6 +58,7 @@ import com.gamingmesh.jobs.dao.JobsDAO; import com.gamingmesh.jobs.dao.JobsDAOData; import com.gamingmesh.jobs.economy.PaymentData; import com.gamingmesh.jobs.economy.PointsData; +import com.gamingmesh.jobs.stuff.Debug; import com.gamingmesh.jobs.stuff.PerformCommands; public class PlayerManager { @@ -758,7 +759,7 @@ public class PlayerManager { ItemStack iih = Jobs.getNms().getItemInMainHand(player); JobItems jitem = getJobsItemByNbt(iih); if (jitem != null && jitem.getJobs().contains(prog)) - data.add(jitem.getBoost()); + data.add(jitem.getBoost(this.getJobsPlayer(player).getJobProgression(prog))); for (ItemStack OneArmor : player.getInventory().getArmorContents()) { if (OneArmor == null || OneArmor.getType() == Material.AIR) @@ -768,7 +769,7 @@ public class PlayerManager { if (armorboost == null || !armorboost.getJobs().contains(prog)) return data; - data.add(armorboost.getBoost()); + data.add(armorboost.getBoost(this.getJobsPlayer(player).getJobProgression(prog))); } return data; @@ -777,40 +778,33 @@ public class PlayerManager { public boolean containsItemBoostByNBT(ItemStack item) { if (item == null) return false; - return Jobs.getReflections().hasNbtString(item, "JobsItemBoost") || Jobs.getReflections().hasNbt(item, "JobsItemBoost"); + return Jobs.getReflections().hasNbtString(item, "JobsItemBoost"); } -// public BoostMultiplier getItemBoostByNBT(Job prog, ItemStack item) { -// BoostMultiplier bonus = new BoostMultiplier(); -//// if (prog.getItemBonus().isEmpty()) -//// return bonus; -// if (item == null) -// return bonus; -// -// if (!Jobs.getReflections().hasNbtString(item, "JobsItemBoost")) -// return bonus; -// -//// Object itemName = Jobs.getReflections().getNbt(item, "JobsItemBoost", prog.getName()); -// Object itemName = Jobs.getReflections().getNbt(item, "JobsItemBoost"); -// -// if (itemName == null) -// return bonus; -// JobItems b = ItemBoostManager.getItemByKey((String) itemName); -// if (b == null) -// return bonus; -// -// return b.getBoost(); -// } - public JobItems getJobsItemByNbt(ItemStack item) { if (item == null) return null; -// Object itemName = Jobs.getReflections().getNbt(item, "JobsItemBoost", prog.getName()); Object itemName = Jobs.getReflections().getNbt(item, "JobsItemBoost"); - if (itemName == null) { - return null; + if (itemName == null || ((String) itemName).isEmpty()) { + + // Checking old boost items and converting to new format if needed + if (Jobs.getReflections().hasNbt(item, "JobsItemBoost")) { + for (Job one : Jobs.getJobs()) { + itemName = Jobs.getReflections().getNbt(item, "JobsItemBoost", one.getName()); + if (itemName != null) { + JobItems b = ItemBoostManager.getItemByKey((String) itemName); + if (b != null) { + ItemStack ic = Jobs.getReflections().setNbt(item, "JobsItemBoost", b.getNode()); + item.setItemMeta(ic.getItemMeta()); + } + break; + } + } + } + if (itemName == null) + return null; } JobItems b = ItemBoostManager.getItemByKey((String) itemName); if (b == null) @@ -819,12 +813,12 @@ public class PlayerManager { return b; } - public BoostMultiplier getJobsBoostByNbt(ItemStack item) { - JobItems b = getJobsItemByNbt(item); - if (b == null) - return null; - return b.getBoost(); - } +// public BoostMultiplier getJobsBoostByNbt(ItemStack item) { +// JobItems b = getJobsItemByNbt(item); +// if (b == null) +// return null; +// return b.getBoost(); +// } public enum BoostOf { McMMO, PetPay, NearSpawner, Permission, Global, Dynamic, Item, Area diff --git a/src/main/java/com/gamingmesh/jobs/Reflections.java b/src/main/java/com/gamingmesh/jobs/Reflections.java index 0ed2a0f5..c31bc5b3 100644 --- a/src/main/java/com/gamingmesh/jobs/Reflections.java +++ b/src/main/java/com/gamingmesh/jobs/Reflections.java @@ -215,27 +215,27 @@ public class Reflections { } } -// public Object getNbt(ItemStack item, String base, String path) { -// if (item == null) -// return null; -// try { -// Object nbt = getNbt(item); -// if (nbt == null) -// return null; -// -// Method compoundMeth = nbt.getClass().getMethod("getCompound", String.class); -// Object compoundRes = compoundMeth.invoke(nbt, base); -// -// if (compoundRes == null) -// return null; -// -// Method meth = compoundRes.getClass().getMethod("getString", String.class); -// Object res = meth.invoke(compoundRes, path); -// return res; -// } catch (Throwable e) { -// return null; -// } -// } + public Object getNbt(ItemStack item, String base, String path) { + if (item == null) + return null; + try { + Object nbt = getNbt(item); + if (nbt == null) + return null; + + Method compoundMeth = nbt.getClass().getMethod("getCompound", String.class); + Object compoundRes = compoundMeth.invoke(nbt, base); + + if (compoundRes == null) + return null; + + Method meth = compoundRes.getClass().getMethod("getString", String.class); + Object res = meth.invoke(compoundRes, path); + return res; + } catch (Throwable e) { + return null; + } + } // // public ItemStack setNbt(ItemStack item, String base, String path, String value) { // if (item == null) diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/edititembonus.java b/src/main/java/com/gamingmesh/jobs/commands/list/edititembonus.java index 91899070..ed1a7bc8 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/edititembonus.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/edititembonus.java @@ -103,6 +103,9 @@ public class edititembonus implements Cmd { JobItems item = ItemBoostManager.getItemByKey((String) key); + if (item == null) + return true; + BoostMultiplier boost = item.getBoost(); for (Job one : item.getJobs()) { diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/give.java b/src/main/java/com/gamingmesh/jobs/commands/list/give.java index 1e6602c6..89667adb 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/give.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/give.java @@ -63,8 +63,11 @@ public class give implements Cmd { sender.sendMessage(Jobs.getLanguage().getMessage("command.give.output.notonline", "%playername%", args[0])); return true; } + + if (name == null) + name = actions.items; - if (name == null || itemName == null) { + if (itemName == null) { Jobs.getCommandManager().sendUsage(sender, "give"); return true; } diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/itembonus.java b/src/main/java/com/gamingmesh/jobs/commands/list/itembonus.java index fdeee9ff..f8f8cd97 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/itembonus.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/itembonus.java @@ -59,7 +59,7 @@ public class itembonus implements Cmd { if (jitem == null) continue; for (Job one : jitem.getJobs()) { - BoostMultiplier boost = jitem.getBoost(); + BoostMultiplier boost = jitem.getBoost(jPlayer.getJobProgression(one)); boolean any = false; for (CurrencyType oneC : CurrencyType.values()) { if (boost.get(oneC) != 0D) diff --git a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java index 7c480822..12dc504d 100644 --- a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java @@ -209,7 +209,7 @@ public class LanguageManager { c.get("command.edititembonus.help.info", "Edit item boost bonus"); c.get("command.edititembonus.help.args", "list/add/remove [jobname] [itemBoostName]"); - Jobs.getGCManager().commandArgs.put("edititembonus", Arrays.asList("list%%add%%remove", "[jobname]", "[jobitemname]")); + Jobs.getGCManager().commandArgs.put("edititembonus", Arrays.asList("list%%add%%remove", "[boosteditems]")); c.get("command.bonus.help.info", "Show job bonuses"); c.get("command.bonus.help.args", "[jobname]"); diff --git a/src/main/java/com/gamingmesh/jobs/container/Job.java b/src/main/java/com/gamingmesh/jobs/container/Job.java index 3602c57d..f48930ba 100644 --- a/src/main/java/com/gamingmesh/jobs/container/Job.java +++ b/src/main/java/com/gamingmesh/jobs/container/Job.java @@ -369,6 +369,8 @@ public class Job { */ @Deprecated public HashMap getItemBonus() { + if (jobItems == null) + jobItems = new HashMap(); return jobItems; } diff --git a/src/main/java/com/gamingmesh/jobs/container/JobItems.java b/src/main/java/com/gamingmesh/jobs/container/JobItems.java index 785450b1..266f44f1 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobItems.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobItems.java @@ -38,9 +38,11 @@ public class JobItems { ItemStack item; private BoostMultiplier boostMultiplier = new BoostMultiplier(); private List jobs = new ArrayList(); + private int fromLevel = 0; + private int untilLevel = Integer.MAX_VALUE; public JobItems(String node, CMIMaterial mat, int amount, String name, List lore, HashMap enchants, BoostMultiplier boostMultiplier, List jobs) { - + mat = mat == null ? CMIMaterial.STONE : mat; try { item = mat.newItemStack(); item.setAmount(amount); @@ -103,6 +105,14 @@ public class JobItems { return boostMultiplier.clone(); } + public BoostMultiplier getBoost(JobProgression job) { + if (job == null || !this.jobs.contains(job.getJob())) + return new BoostMultiplier(); + if (job.getLevel() < getFromLevel() || job.getLevel() > getUntilLevel()) + return new BoostMultiplier(); + return boostMultiplier.clone(); + } + public List getJobs() { return jobs; } @@ -110,4 +120,20 @@ public class JobItems { public void setJobs(List jobs) { this.jobs = jobs; } + + public int getFromLevel() { + return fromLevel; + } + + public void setFromLevel(int fromLevel) { + this.fromLevel = fromLevel; + } + + public int getUntilLevel() { + return untilLevel; + } + + public void setUntilLevel(int untilLevel) { + this.untilLevel = untilLevel; + } } diff --git a/src/main/java/com/gamingmesh/jobs/stuff/TabComplete.java b/src/main/java/com/gamingmesh/jobs/stuff/TabComplete.java index 8a7395b7..1e9a8005 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/TabComplete.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/TabComplete.java @@ -76,17 +76,24 @@ public class TabComplete implements TabCompleter { } break; case "[jobitemname]": - Job oneJob = Jobs.getJob(args[i - 1]); - if (oneJob != null) - if (args[3].equals("items")) { - for (Entry item : ItemBoostManager.getItemsMapByJob(oneJob).entrySet()) { - temp.add(item.getValue().getNode()); - } - } else if (args[3].equals("limiteditems")) { - for (Entry limitedItem : oneJob.getLimitedItems().entrySet()) { - temp.add(limitedItem.getValue().getNode()); - } + if (args[3].equals("items")) { + for (Entry one : ItemBoostManager.getItems().entrySet()) { + temp.add(one.getValue().getNode()); } + } else if (args[3].equals("limiteditems")) { + Job oneJob = Jobs.getJob(args[i - 1]); + if (oneJob != null) + if (args[3].equals("limiteditems")) { + for (Entry limitedItem : oneJob.getLimitedItems().entrySet()) { + temp.add(limitedItem.getValue().getNode()); + } + } + } + break; + case "[boosteditems]": + for (Entry one : ItemBoostManager.getItems().entrySet()) { + temp.add(one.getValue().getNode()); + } break; case "[oldjob]": JobsPlayer onePlayerJob = Jobs.getPlayerManager().getJobsPlayer(args[i - 1]);