diff --git a/src/main/java/org/mcsg/survivalgames/util/ItemReader.java b/src/main/java/org/mcsg/survivalgames/util/ItemReader.java index 694b9b2..07a504f 100644 --- a/src/main/java/org/mcsg/survivalgames/util/ItemReader.java +++ b/src/main/java/org/mcsg/survivalgames/util/ItemReader.java @@ -1,7 +1,9 @@ package org.mcsg.survivalgames.util; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; @@ -19,52 +21,141 @@ public class ItemReader { private static void loadIds(){ encids = new HashMap(); - + for(Enchantment e:Enchantment.values()){ - encids.put(e.toString().toLowerCase().replace("_", ""), e); + encids.put(e.getName().toLowerCase().replace("_", ""), e); + encids.put(e.getName().toLowerCase(), e); + encids.put(e.getName(), e); } - - + encids.put("sharpness", Enchantment.DAMAGE_ALL); encids.put("dmg", Enchantment.DAMAGE_ALL); encids.put("fire", Enchantment.FIRE_ASPECT); - + /* eventually friendly names for all + encids.put("", Enchantment.PROTECTION_ENVIRONMENTAL); + encids.put("", Enchantment.PROTECTION_FIRE); + encids.put("", Enchantment.PROTECTION_FALL); + encids.put("", Enchantment.PROTECTION_EXPLOSIONS); + encids.put("", Enchantment.PROTECTION_PROJECTILE); + encids.put("", Enchantment.OXYGEN); + encids.put("", Enchantment.WATER_WORKER); + encids.put("", Enchantment.THORNS); + encids.put("", Enchantment.DAMAGE_ALL); + encids.put("", Enchantment.DAMAGE_UNDEAD); + encids.put("", Enchantment.DAMAGE_ARTHROPODS); + encids.put("", Enchantment.KNOCKBACK); + encids.put("", Enchantment.FIRE_ASPECT); + encids.put("", Enchantment.LOOT_BONUS_MOBS); + encids.put("", Enchantment.DIG_SPEED); + encids.put("", Enchantment.SILK_TOUCH); + encids.put("", Enchantment.DURABILITY); + encids.put("", Enchantment.LOOT_BONUS_BLOCKS); + encids.put("", Enchantment.ARROW_DAMAGE); + encids.put("", Enchantment.ARROW_KNOCKBACK); + encids.put("", Enchantment.ARROW_FIRE); + encids.put("", Enchantment.ARROW_INFINITE); + encids.put("", Enchantment.LUCK); + encids.put("", Enchantment.LURE); + */ } - - public static ItemStack read(String str){ if(encids == null){ loadIds(); } - String split[] = str.split(","); + //- 95:3, 3, enchants [enchants ... []], name, lore, lore, lore... + String split[] = str.split(",",4); SurvivalGames.debug("ItemReader: reading : "+Arrays.toString(split)); - for(int a = 0; a < split.length; a++){ - split[a] = split[a].toLowerCase().trim(); - } - if(split.length < 1){ - return null; - }else if(split.length == 1){ - return new ItemStack(Integer.parseInt(split[0])); - }else if(split.length == 2){ - return new ItemStack(Integer.parseInt(split[0]), Integer.parseInt(split[1])); - }else if(split.length == 3){ - return new ItemStack(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Short.parseShort(split[2])); - }else{ - ItemStack i = new ItemStack(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Short.parseShort(split[2])); - String encs[] = split[3].split(" "); - for(String enc: encs){ - System.out.println(enc); - String e[] = enc.split(":"); - i.addUnsafeEnchantment(encids.get(e[0]), Integer.parseInt(e[1])); - } - if(split.length == 5){ - ItemMeta im = i.getItemMeta(); - im.setDisplayName(MessageUtil.replaceColors(split[4])); - i.setItemMeta(im); - } - return i; + + if(split.length < 1) return null; + + int id, qty = 1; + short dmg; + + try{ + if (split[0].contains(":")) + { + id = Integer.parseInt(split[0].split(":",2)[0].trim()); + dmg = Short.parseShort(split[0].split(":",2)[1].trim()); + } + else + { + id = Integer.parseInt(split[0].trim()); + dmg = 0; + } + } + catch(NumberFormatException ex) + { + System.out.println("could not read id and/or damage value from:"); + System.out.println(" "+str); + return null; + } + + if(split.length > 1) + { + try + { + qty = Integer.parseInt(split[1].trim()); + } + catch(NumberFormatException ex) + { /* not really a big deal */ } + } + + ItemStack ret = new ItemStack(id, qty, dmg); + if(ret == null){ + System.out.println("invalid item id:"+id+" data:"+dmg); + return null; + } + if(split.length > 2) + { + String encs[] = split[2].toLowerCase().trim().split(" "); + for(String enc: encs){ + //System.out.println(enc); + if(enc.contains(":")) + { + try{ + String e[] = enc.split(":",2); + if(encids.containsKey(e[0].trim())) + { + ret.addUnsafeEnchantment(encids.get(e[0].trim()), Integer.parseInt(e[1].trim())); + }else{ + System.out.println("unknown enchantment: \""+ e[0].trim() +"\""); + } + } catch (NumberFormatException ex) + {System.out.println("\"" + enc.trim() + "\" is not a properly formatted enchantment.");} + } + else //assume lvl 0 + { + if(encids.containsKey(enc.trim())) + { + ret.addUnsafeEnchantment(encids.get(enc.trim()), 0); + }else{ + if(!enc.trim().equals("")) //likely omitted + System.out.println("unknown enchantment: \""+ enc.trim() +"\""); + } + } + } + } + if(split.length > 3) + { + ArrayList lore = new ArrayList(); + String[] ss = split[3].split(","); + ItemMeta im = ret.getItemMeta(); + if(!ss[0].trim().equals("")) + im.setDisplayName(MessageUtil.replaceColors(ss[0].trim())); + if(ss.length > 1) + { + for(String line:ss) + { + lore.add(MessageUtil.replaceColors(line.trim())); + } + lore.remove(0); // the display name + im.setLore(lore); + } + ret.setItemMeta(im); } + System.out.println(ret.toString()); + return ret; } public static String getFriendlyItemName(Material m){ diff --git a/src/main/resources/chest.yml b/src/main/resources/chest.yml index 8715ec3..1f352a0 100644 --- a/src/main/resources/chest.yml +++ b/src/main/resources/chest.yml @@ -6,7 +6,7 @@ #to spawn in. a ratio of 2 means that it is twice as unlikley #for the next level to spawn in. ##FORMAT -# , +# [:],, [[:]], , , , #DONT TOUCH THIS version: 0