1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +01:00

Improvement on limitedItems processing

This commit is contained in:
Zrips 2023-11-15 17:58:14 +02:00
parent bd13bd52d0
commit 6a539686dd
2 changed files with 127 additions and 90 deletions

View File

@ -471,7 +471,6 @@ public class ConfigManager {
cfg.addComment(pt + ".limitedItems", "Limit item use to jobs level"); 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", "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.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.addComment(pt + ".limitedItems.firstOne.level", "Level of this job player can start using this item");
@ -1007,7 +1006,8 @@ public class ConfigManager {
return result.toString(); return result.toString();
} }
boolean informed = false; boolean informedGUI = false;
boolean informedLimited = false;
private Job loadJobs(ConfigurationSection jobsSection) { private Job loadJobs(ConfigurationSection jobsSection) {
java.util.logging.Logger log = Jobs.getPluginLogger(); java.util.logging.Logger log = Jobs.getPluginLogger();
@ -1205,9 +1205,9 @@ public class ConfigManager {
if (material != CMIMaterial.NONE) if (material != CMIMaterial.NONE)
guiItem = material.newItemStack(); 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"); 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")) { } else if (guiSection.isInt("Id") && guiSection.isInt("Data")) {
guiItem = CMIMaterial.get(guiSection.getInt("Id"), guiSection.getInt("Data")).newItemStack(); guiItem = CMIMaterial.get(guiSection.getInt("Id"), guiSection.getInt("Data")).newItemStack();
@ -1387,51 +1387,72 @@ public class ConfigManager {
continue; 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<String> lore = itemSection.getStringList("lore");
for (int a = 0; a < lore.size(); a++) {
lore.set(a, CMIChatColor.translate(lore.get(a)));
}
Map<Enchantment, Integer> 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(); 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<String> lore = itemSection.getStringList("lore");
for (int a = 0; a < lore.size(); a++) {
lore.set(a, CMIChatColor.translate(lore.get(a)));
}
Map<Enchantment, Integer> 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); job.setLimitedItems(jobLimitedItems);

View File

@ -29,84 +29,100 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import net.Zrips.CMILib.Colors.CMIChatColor; import net.Zrips.CMILib.Colors.CMIChatColor;
import net.Zrips.CMILib.Container.CMINumber;
import net.Zrips.CMILib.Items.CMIItemStack;
import net.Zrips.CMILib.Items.CMIMaterial; import net.Zrips.CMILib.Items.CMIMaterial;
public class JobLimitedItems { public class JobLimitedItems {
private String node; private String node;
CMIMaterial mat; CMIMaterial mat;
private int amount;
private String name; private String name;
private ItemStack item;
private List<String> lore; private List<String> lore;
private Map<Enchantment, Integer> enchants; private Map<Enchantment, Integer> enchants;
private int level; private int level;
public JobLimitedItems(String node, CMIMaterial material, int amount, String name, List<String> lore, Map<Enchantment, Integer> enchants, int level) { public JobLimitedItems(String node, CMIMaterial material, int amount, String name, List<String> lore, Map<Enchantment, Integer> enchants, int level) {
this.node = node; this.node = node;
this.amount = amount;
this.name = name; CMIItemStack ct = material.newCMIItemStack(CMINumber.clamp(amount, 1, material.getMaterial().getMaxStackSize()));
this.lore = lore; ct.setDisplayName(name);
this.enchants = enchants; ct.setLore(lore);
this.level = level; for (Entry<Enchantment, Integer> one : enchants.entrySet()) {
this.mat = material; 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() { public String getNode() {
return node; return node;
} }
public ItemStack getItemStack(Player player) { public ItemStack getItemStack(Player player) {
ItemStack item = mat.newItemStack(); ItemStack item = this.item.clone();
item.setAmount(amount);
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
if (meta == null) { if (meta == null) {
return item; return item;
} }
if (name != null) if (lore != null && !lore.isEmpty()) {
meta.setDisplayName(CMIChatColor.translate(name)); List<String> translatedLore = new ArrayList<>();
for (String oneLore : lore) {
translatedLore.add(CMIChatColor.translate(oneLore.replace("[player]", player.getName())));
}
if (lore != null && !lore.isEmpty()) { meta.setLore(translatedLore);
List<String> translatedLore = new ArrayList<>(); }
for (String oneLore : lore) {
translatedLore.add(CMIChatColor.translate(oneLore.replace("[player]", player.getName())));
}
meta.setLore(translatedLore); item.setItemMeta(meta);
} return item;
if (enchants != null)
for (Entry<Enchantment, Integer> oneEnchant : enchants.entrySet()) {
meta.addEnchant(oneEnchant.getKey(), oneEnchant.getValue(), true);
}
item.setItemMeta(meta);
return item;
} }
@Deprecated @Deprecated
public int getId() { public int getId() {
return mat.getId(); return mat.getId();
} }
public CMIMaterial getType(){ public CMIMaterial getType() {
return mat; return mat;
} }
public String getName() { public String getName() {
return name; return name;
} }
public List<String> getLore() { public List<String> getLore() {
return lore; return lore;
} }
public Map<Enchantment, Integer> getEnchants() { public Map<Enchantment, Integer> getEnchants() {
return enchants; return enchants;
} }
public int getLevel() { public int getLevel() {
return level; return level;
} }
} }