Hello! I am attempting to create a class that extends an ItemStack so I can add a few more methods into it which allow me to retrieve and set specific stats. I am currently running into an issue where values are being added up to stats that they don't below to. All in all I am a bit confused as to why values from the defense stat is being added to the damage stat and so on. I was hoping someone could spot check this and see as to why this isn't working, I broke them out of the loops to do some debugging with error messages (removed them) but hopefully there is something blatantly obvious that I just missed. Pastie: http://pastie.org/10192908 or... Spoiler Code (Text): public class RPGItem extends ItemStack { public RPGItem(Material material) { this.setType(material); this.setAmount(1); } public RPGItem(ItemStack is){ this.setType(is.getType()); this.setAmount(is.getAmount()); this.setData(is.getData()); this.setDurability(is.getDurability()); if (is.hasItemMeta()){ this.setItemMeta(is.getItemMeta()); if (is.getItemMeta().hasLore()) this.setLore(is.getItemMeta().getLore()); } } public RPGItem(Material material, int amount) { this.setType(material); this.setAmount(amount); } public RPGItem(Material material, int amount, short durability) { this.setType(material); this.setAmount(amount); this.setDurability(durability); } public RPGItem setDisplayName(String displayName) { ItemMeta itemMeta = this.getItemMeta(); itemMeta.setDisplayName(displayName); this.setItemMeta(itemMeta); return this; } public RPGItem setLore(String... lore) { List<String> list = new ArrayList<>(); Collections.addAll(list, lore); ItemMeta itemMeta = this.getItemMeta(); itemMeta.setLore(list); this.setItemMeta(itemMeta); return this; } public RPGItem setLore(List<String> lore) { List<String> list = lore; ItemMeta itemMeta = this.getItemMeta(); itemMeta.setLore(list); this.setItemMeta(itemMeta); return this; } public RPGItem setLeatherArmor(Color c){ LeatherArmorMeta lam = (LeatherArmorMeta) this.getItemMeta(); lam.setColor(c); this.setItemMeta(lam); return this; } public RPGItem addGlow(){ Glow.addGlow(this); return this; } public RPGItem addEnchant(Enchantment enchantment, int level) { this.addUnsafeEnchantment(enchantment, level); return this; } public RPGItem addEnchants(Map<Enchantment, Integer> enchantments) { this.addUnsafeEnchantments(enchantments); return this; } public RPGItem setName(String name){ ItemMeta im = this.getItemMeta(); im.setDisplayName(name); this.setItemMeta(im); return this; } public RPGItem addLore(String... lore){ List<String> list = getLore(); Collections.addAll(list, lore); ItemMeta itemMeta = this.getItemMeta(); itemMeta.setLore(list); this.setItemMeta(itemMeta); return this; } public List<String> getLore(){ if (this.hasItemMeta()){ if (this.getItemMeta().hasLore()) return this.getItemMeta().getLore(); else{ List<String> das = new ArrayList<String>(); return das; } }else{ List<String> das = new ArrayList<String>(); return das; } } public ItemType setItemType(ItemType it){ addLore(ChatColor.GOLD + it.toString()); return it; } public int setMinLevel(int level){ addLore(" "); addLore(ChatColor.DARK_GREEN.toString() + "Level: " +ChatColor.GREEN + level); return level; } public int getMinLevel(){ for (String l : getLore()){ if (ChatColor.stripColor(l).startsWith("Level")){ String s = l.split(" ")[1]; return Integer.valueOf(ChatColor.stripColor(s)); } } return 1; } public int[] getFightStatValue(StatType st){ switch(st){ case DEFENSE: for (String l : getLore()){ if (ChatColor.stripColor(l).contains(StatType.DEFENSE.toString())){ String s = l.split(" ")[1]; String[] s2 = s.split("-"); int[] i = new int[2]; i[0] = Integer.parseInt(s2[0]); i[1] = Integer.parseInt(s2[1]); return i; } } case DAMAGE: for (String l : getLore()){ if (ChatColor.stripColor(l).contains(StatType.DAMAGE.toString())){ String s = l.split(" ")[1]; String[] s2 = s.split("-"); int[] i = new int[2]; i[0] = Integer.parseInt(s2[0]); i[1] = Integer.parseInt(s2[1]); return i; } } default: return new int[2]; } } public int getSingleStats(StatType st){ switch(st){ case AGILITY: for (String l : getLore()){ if (ChatColor.stripColor(l).contains(StatType.AGILITY.toString())){ String s = l.split(" ")[1]; return Integer.valueOf(s); } } case DPS: for (String l : getLore()){ if (ChatColor.stripColor(l).contains(StatType.DPS.toString())){ String s = l.split(" ")[1]; return Integer.valueOf(s.replace("%", "")); } } case HP: for (String l : getLore()){ if (ChatColor.stripColor(l).contains(StatType.HP.toString())){ String s = l.split(" ")[1]; return Integer.valueOf(s); } } case HP_REGEN: for (String l : getLore()){ if (ChatColor.stripColor(l).contains(StatType.HP_REGEN.toString())){ String s = l.split(" ")[1]; return Integer.valueOf(s); } } case STAM_REGEN: for (String l : getLore()){ if (ChatColor.stripColor(l).contains(StatType.STAM_REGEN.toString())){ String s = l.split(" ")[1]; return Integer.valueOf(s); } } case VITALITY: for (String l : getLore()){ if (ChatColor.stripColor(l).contains(StatType.VITALITY.toString())){ String s = l.split(" ")[1]; return Integer.valueOf(s.replace("%", "")); } } default: break; } return 0; } public RPGItem setStats(StatType st, int min, int max){ List<String> lore; int pos; boolean done; switch(st){ case AGILITY: lore = getLore(); done = false; pos = 0; for (int x = 0; x < lore.size(); x++){ if (lore.get(x).contains(StatType.AGILITY.toString())){ done = true; pos = x; } } if (done) lore.set(pos, StatType.AGILITY.toString()+": " + min); else lore.add(StatType.AGILITY.toString()+": " + min ); if (this.getItemMeta().hasLore()) this.getItemMeta().getLore().clear(); this.setLore(lore); return this; case DAMAGE: lore = getLore(); done = false; pos = 0; for (int x = 0; x < lore.size(); x++){ if (lore.get(x).contains(StatType.DAMAGE.toString())){ done = true; pos = x; } } if (done) lore.set(pos, StatType.DAMAGE.toString()+": " + min + "-" + max); else lore.add(StatType.DAMAGE.toString()+": " + min + "-" + max); if (this.getItemMeta().hasLore()) this.getItemMeta().getLore().clear(); this.setLore(lore); return this; case DEFENSE: lore = getLore(); done = false; pos = 0; for (int x = 0; x < lore.size(); x++){ if (lore.get(x).contains(StatType.DEFENSE.toString())){ done = true; pos = x; } } if (done) lore.set(pos, StatType.DEFENSE.toString()+": " + min + "-" + max); else lore.add(StatType.DEFENSE.toString()+": " + min + "-" + max); if (this.getItemMeta().hasLore()) this.getItemMeta().getLore().clear(); this.setLore(lore); return this; case DPS: lore = getLore(); done = false; pos = 0; for (int x = 0; x < lore.size(); x++){ if (lore.get(x).contains(StatType.DPS.toString())){ done = true; pos = x; } } if (done) lore.set(pos, StatType.DPS.toString()+": " + min + "%"); else lore.add(StatType.DPS.toString()+": " + min + "%"); if (this.getItemMeta().hasLore()) this.getItemMeta().getLore().clear(); this.setLore(lore); return this; case HP: lore = getLore(); done = false; pos = 0; for (int x = 0; x < lore.size(); x++){ if (lore.get(x).contains(StatType.HP.toString())){ done = true; pos = x; } } if (done) lore.set(pos, StatType.HP.toString()+": " + min); else lore.add(StatType.HP.toString()+": " + min ); if (this.getItemMeta().hasLore()) this.getItemMeta().getLore().clear(); this.setLore(lore); return this; case HP_REGEN: lore = getLore(); done = false; pos = 0; for (int x = 0; x < lore.size(); x++){ if (lore.get(x).contains(StatType.HP_REGEN.toString())){ done = true; pos = x; } } if (done) lore.set(pos, StatType.HP_REGEN.toString()+": " + min ); else lore.add(StatType.HP_REGEN.toString()+": " + min); if (this.getItemMeta().hasLore()) this.getItemMeta().getLore().clear(); this.setLore(lore); return this; case STAM_REGEN: lore = getLore(); done = false; pos = 0; for (int x = 0; x < lore.size(); x++){ if (lore.get(x).contains(StatType.STAM_REGEN.toString())){ done = true; pos = x; } } if (done) lore.set(pos, StatType.STAM_REGEN.toString()+": " + min); else lore.add(StatType.STAM_REGEN.toString()+": " + min); if (this.getItemMeta().hasLore()) this.getItemMeta().getLore().clear(); this.setLore(lore); return this; case VITALITY: lore = getLore(); done = false; pos = 0; for (int x = 0; x < lore.size(); x++){ if (lore.get(x).contains(StatType.VITALITY.toString())){ done = true; pos = x; } } if (done) lore.set(pos, StatType.VITALITY.toString()+": " + min + "%"); else lore.add(StatType.VITALITY.toString()+": " + min + "%"); if (this.getItemMeta().hasLore()) this.getItemMeta().getLore().clear(); this.setLore(lore); return this; } return this; } }
You will be better off with creating an utility class which handles this for you. (Why are you extending ItemStack, really?)
I just found it easier then having a separate utility method for it as I wanted to use the normal methods for an item stack. Either way it should still work correct? Even if it is in a Utility class vs a class that extends an itemstack.