From 6a539686dd5e1a13d29e5498d8f7b309648990a6 Mon Sep 17 00:00:00 2001 From: Zrips Date: Wed, 15 Nov 2023 17:58:14 +0200 Subject: [PATCH] Improvement on limitedItems processing --- .../gamingmesh/jobs/config/ConfigManager.java | 123 ++++++++++-------- .../jobs/container/JobLimitedItems.java | 94 +++++++------ 2 files changed, 127 insertions(+), 90 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java index e8dee5d6..bc0a253b 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java @@ -470,12 +470,11 @@ public class ConfigManager { cfg.addComment(pt + ".limitedItems", "Limit item use to jobs level"); cfg.addComment(pt + ".limitedItems.firstOne", "Just name, don't have any impact"); - - + cfg.addComment(pt + ".limitedItems.firstOne.ItemStack", "Tool/Weapon data. More information on usage www.zrips.net/cmi/commands/icwol/"); - cfg.get(pt + ".limitedItems.firstOne.ItemStack", "DIAMOND_PICKAXE;n{&8Miner_Pickaxe};l{&eBobs_pick\\n&710%_bonus_XP};DAMAGE_ALL:1,FIRE_ASPECT:1"); + cfg.get(pt + ".limitedItems.firstOne.ItemStack", "DIAMOND_PICKAXE;n{&8Miner_Pickaxe};l{&eBobs_pick\\n&710%_bonus_XP};DAMAGE_ALL:1,FIRE_ASPECT:1"); cfg.addComment(pt + ".limitedItems.firstOne.level", "Level of this job player can start using this item"); - cfg.get(pt + ".limitedItems.firstOne.level", 5); + cfg.get(pt + ".limitedItems.firstOne.level", 5); cfg.save(); } @@ -1007,7 +1006,8 @@ public class ConfigManager { return result.toString(); } - boolean informed = false; + boolean informedGUI = false; + boolean informedLimited = false; private Job loadJobs(ConfigurationSection jobsSection) { java.util.logging.Logger log = Jobs.getPluginLogger(); @@ -1169,7 +1169,7 @@ public class ConfigManager { CMIItemStack item = CMIItemStack.deserialize(guiSection.getString("ItemStack"), ahead); if (!ahead.isAsyncHead() && item != null && item.getCMIType().isNone()) gItem.setGuiItem(item.getItemStack()); - + } else if (guiSection.isString("Item")) { String item = guiSection.getString("Item"); @@ -1205,9 +1205,9 @@ public class ConfigManager { if (material != CMIMaterial.NONE) guiItem = material.newItemStack(); - if (!informed) { + if (!informedGUI) { CMIMessages.consoleMessage("&5Update " + jobConfigName + " jobs gui item section to use `ItemStack` instead of `Item` sections format. More information inside _EXAMPLE job file"); - informed = true; + informedGUI = true; } } else if (guiSection.isInt("Id") && guiSection.isInt("Data")) { guiItem = CMIMaterial.get(guiSection.getInt("Id"), guiSection.getInt("Data")).newItemStack(); @@ -1387,51 +1387,72 @@ public class ConfigManager { continue; } - CMIMaterial mat = CMIMaterial.NONE; - - if (itemSection.isInt("id")) { - mat = CMIMaterial.get(itemSection.getInt("id")); - } else { - mat = CMIMaterial.get(itemSection.getString("id")); - } - - if (mat == CMIMaterial.NONE) { - log.warning("Job " + jobConfigName + " has incorrect limitedItems material id!"); - continue; - } - - List lore = itemSection.getStringList("lore"); - - for (int a = 0; a < lore.size(); a++) { - lore.set(a, CMIChatColor.translate(lore.get(a))); - } - - Map enchants = new HashMap<>(); - for (String eachLine : itemSection.getStringList("enchants")) { - String[] split = eachLine.split("=", 2); - if (split.length == 0) - continue; - - Enchantment ench = CMIEnchantment.getEnchantment(split[0]); - if (ench == null) - continue; - - int level = -1; - - if (split.length > 1) { - try { - level = Integer.parseInt(split[1]); - } catch (NumberFormatException e) { - } - } - - if (level != -1) - enchants.put(ench, level); - } - String node = itemKey.toLowerCase(); - jobLimitedItems.put(node, new JobLimitedItems(node, mat, 1, itemSection.getString("name"), lore, enchants, itemSection.getInt("level"))); + if (itemSection.contains("id")) { + + CMIMaterial mat = CMIMaterial.NONE; + + if (itemSection.isInt("id")) { + mat = CMIMaterial.get(itemSection.getInt("id")); + } else { + mat = CMIMaterial.get(itemSection.getString("id")); + } + + if (mat == CMIMaterial.NONE) { + log.warning("Job " + jobConfigName + " has incorrect limitedItems material id!"); + continue; + } + + List lore = itemSection.getStringList("lore"); + + for (int a = 0; a < lore.size(); a++) { + lore.set(a, CMIChatColor.translate(lore.get(a))); + } + + Map enchants = new HashMap<>(); + for (String eachLine : itemSection.getStringList("enchants")) { + String[] split = eachLine.split("=", 2); + if (split.length == 0) + continue; + + Enchantment ench = CMIEnchantment.getEnchantment(split[0]); + if (ench == null) + continue; + + int level = -1; + + if (split.length > 1) { + try { + level = Integer.parseInt(split[1]); + } catch (NumberFormatException e) { + } + } + + if (level != -1) + enchants.put(ench, level); + } + + jobLimitedItems.put(node, new JobLimitedItems(node, mat, 1, itemSection.getString("name"), lore, enchants, itemSection.getInt("level"))); + + if (!informedLimited) { + CMIMessages.consoleMessage("&5Update " + jobConfigName + + " jobs limited items section to use `ItemStack` instead of `id` sections format. More information inside _EXAMPLE job file"); + informedLimited = true; + } + + } else if (itemSection.contains("ItemStack")) { + + CMIItemStack limitedItem = CMIItemStack.deserialize(itemSection.getString("ItemStack")); + + if (limitedItem == null || limitedItem.getCMIType().isNone()) { + log.warning("Job " + jobConfigName + " has incorrect limitedItems material id!"); + continue; + } + + jobLimitedItems.put(node, new JobLimitedItems(node, limitedItem.getItemStack(), itemSection.getInt("level"))); + + } } } job.setLimitedItems(jobLimitedItems); diff --git a/src/main/java/com/gamingmesh/jobs/container/JobLimitedItems.java b/src/main/java/com/gamingmesh/jobs/container/JobLimitedItems.java index 3e7e9a7f..3c009caa 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobLimitedItems.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobLimitedItems.java @@ -29,84 +29,100 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import net.Zrips.CMILib.Colors.CMIChatColor; +import net.Zrips.CMILib.Container.CMINumber; +import net.Zrips.CMILib.Items.CMIItemStack; import net.Zrips.CMILib.Items.CMIMaterial; public class JobLimitedItems { private String node; CMIMaterial mat; - private int amount; private String name; + private ItemStack item; private List lore; private Map enchants; private int level; public JobLimitedItems(String node, CMIMaterial material, int amount, String name, List lore, Map enchants, int level) { - this.node = node; - this.amount = amount; - this.name = name; - this.lore = lore; - this.enchants = enchants; - this.level = level; - this.mat = material; + this.node = node; + + CMIItemStack ct = material.newCMIItemStack(CMINumber.clamp(amount, 1, material.getMaterial().getMaxStackSize())); + ct.setDisplayName(name); + ct.setLore(lore); + for (Entry one : enchants.entrySet()) { + ct.addEnchant(one.getKey(), one.getValue()); + } + this.item = ct.getItemStack(); + + this.name = name; + this.lore = lore; + this.enchants = enchants; + this.level = level; + this.mat = material; + } + + public JobLimitedItems(String node, ItemStack item, int level) { + this.node = node; + this.item = item; + if (this.item.hasItemMeta()) { + ItemMeta meta = this.item.getItemMeta(); + if (meta.hasDisplayName()) + name = meta.getDisplayName(); + if (meta.hasLore()) + lore = meta.getLore(); + } + enchants = item.getEnchantments(); + + this.level = level; } public String getNode() { - return node; + return node; } public ItemStack getItemStack(Player player) { - ItemStack item = mat.newItemStack(); - item.setAmount(amount); + ItemStack item = this.item.clone(); - ItemMeta meta = item.getItemMeta(); - if (meta == null) { - return item; - } + ItemMeta meta = item.getItemMeta(); + if (meta == null) { + return item; + } - if (name != null) - meta.setDisplayName(CMIChatColor.translate(name)); + if (lore != null && !lore.isEmpty()) { + List translatedLore = new ArrayList<>(); + for (String oneLore : lore) { + translatedLore.add(CMIChatColor.translate(oneLore.replace("[player]", player.getName()))); + } - if (lore != null && !lore.isEmpty()) { - List translatedLore = new ArrayList<>(); - for (String oneLore : lore) { - translatedLore.add(CMIChatColor.translate(oneLore.replace("[player]", player.getName()))); - } + meta.setLore(translatedLore); + } - meta.setLore(translatedLore); - } - - if (enchants != null) - for (Entry oneEnchant : enchants.entrySet()) { - meta.addEnchant(oneEnchant.getKey(), oneEnchant.getValue(), true); - } - - item.setItemMeta(meta); - return item; + item.setItemMeta(meta); + return item; } @Deprecated public int getId() { - return mat.getId(); + return mat.getId(); } - public CMIMaterial getType(){ - return mat; + public CMIMaterial getType() { + return mat; } public String getName() { - return name; + return name; } public List getLore() { - return lore; + return lore; } public Map getEnchants() { - return enchants; + return enchants; } public int getLevel() { - return level; + return level; } }