EnchantItemTable.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.datatables;
  16. import gnu.trove.map.hash.TIntObjectHashMap;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.NamedNodeMap;
  26. import org.w3c.dom.Node;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.gameserver.model.EnchantItem;
  29. import com.l2jserver.gameserver.model.EnchantScroll;
  30. import com.l2jserver.gameserver.model.items.L2Item;
  31. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  32. /**
  33. * @author UnAfraid
  34. *
  35. */
  36. public class EnchantItemTable
  37. {
  38. private static final Logger _log = Logger.getLogger(EnchantItemTable.class.getName());
  39. public final TIntObjectHashMap<EnchantScroll> _scrolls;
  40. public final TIntObjectHashMap<EnchantItem> _supports;
  41. public EnchantItemTable()
  42. {
  43. _scrolls = new TIntObjectHashMap<EnchantScroll>();
  44. _supports = new TIntObjectHashMap<EnchantItem>();
  45. load();
  46. }
  47. public void load()
  48. {
  49. try
  50. {
  51. _scrolls.clear();
  52. _supports.clear();
  53. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  54. factory.setValidating(false);
  55. factory.setIgnoringComments(true);
  56. File file = new File(Config.DATAPACK_ROOT + "/data/enchantData.xml");
  57. if (!file.exists())
  58. {
  59. _log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] File is missing " + Config.DATAPACK_ROOT + "/data/enchantData.xml !");
  60. return;
  61. }
  62. Document doc = factory.newDocumentBuilder().parse(file);
  63. Node first = doc.getFirstChild();
  64. if (first != null && "list".equalsIgnoreCase(first.getNodeName()))
  65. {
  66. for (Node n = first.getFirstChild(); n != null; n = n.getNextSibling())
  67. {
  68. if ("enchant".equalsIgnoreCase(n.getNodeName()))
  69. {
  70. int scrollId = 0;
  71. boolean isWeapon = true;
  72. boolean isBlessed = false;
  73. boolean isSafe = false;
  74. int type = L2Item.CRYSTAL_NONE;
  75. int maxEnchant = Config.MAX_ENCHANT_LEVEL;
  76. double chance = Config.ENCHANT_CHANCE;
  77. int[] items = null;
  78. NamedNodeMap attrs = n.getAttributes();
  79. Node att = attrs.getNamedItem("id");
  80. if (att == null)
  81. {
  82. _log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Enchant id, skipping");
  83. continue;
  84. }
  85. scrollId = Integer.parseInt(att.getNodeValue());
  86. att = attrs.getNamedItem("isWeapon");
  87. if (att != null)
  88. {
  89. isWeapon = Boolean.parseBoolean(att.getNodeValue());
  90. }
  91. att = attrs.getNamedItem("isBlessed");
  92. if (att != null)
  93. {
  94. isBlessed = Boolean.parseBoolean(att.getNodeValue());
  95. }
  96. att = attrs.getNamedItem("isSafe");
  97. if (att != null)
  98. {
  99. isSafe = Boolean.parseBoolean(att.getNodeValue());
  100. }
  101. att = attrs.getNamedItem("targetGrade");
  102. if (att != null)
  103. {
  104. type = ItemTable._crystalTypes.get(att.getNodeValue());
  105. }
  106. att = attrs.getNamedItem("maxEnchant");
  107. if (att != null)
  108. {
  109. maxEnchant = Integer.parseInt(att.getNodeValue());
  110. }
  111. att = attrs.getNamedItem("successRate");
  112. if (att != null)
  113. {
  114. chance = Double.parseDouble(att.getNodeValue());
  115. chance = Math.max(chance, 1.0); // Enchant bonus cannot be below 1.0
  116. }
  117. List<Integer> itemz = new ArrayList<Integer>();
  118. for (Node cd = n.getFirstChild(); cd != null; cd = cd.getNextSibling())
  119. {
  120. if ("item".equalsIgnoreCase(cd.getNodeName()))
  121. {
  122. att = cd.getAttributes().getNamedItem("id");
  123. if (att != null)
  124. itemz.add(Integer.parseInt(att.getNodeValue()));
  125. else
  126. {
  127. _log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Item id, skipping");
  128. continue;
  129. }
  130. }
  131. }
  132. if (itemz.size() > 0)
  133. {
  134. items = new int[itemz.size()];
  135. int i = 0;
  136. for (Integer id : itemz)
  137. {
  138. items[i++] = id;
  139. }
  140. Arrays.sort(items);
  141. }
  142. _scrolls.put(scrollId, new EnchantScroll(isWeapon, isBlessed, isSafe, type, maxEnchant, chance, items));
  143. }
  144. else if ("support".equalsIgnoreCase(n.getNodeName()))
  145. {
  146. int scrollId = 0;
  147. boolean isWeapon = true;
  148. int type = L2Item.CRYSTAL_NONE;
  149. int maxEnchant = 0;
  150. double chance = 1.0;
  151. int[] items = null;
  152. NamedNodeMap attrs = n.getAttributes();
  153. Node att = attrs.getNamedItem("id");
  154. if (att == null)
  155. {
  156. _log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Support id, skipping");
  157. continue;
  158. }
  159. scrollId = Integer.parseInt(att.getNodeValue());
  160. att = attrs.getNamedItem("isWeapon");
  161. if (att != null)
  162. {
  163. isWeapon = Boolean.parseBoolean(att.getNodeValue());
  164. }
  165. att = attrs.getNamedItem("targetGrade");
  166. if (att != null)
  167. {
  168. type = ItemTable._crystalTypes.get(att.getNodeValue());
  169. }
  170. att = attrs.getNamedItem("maxEnchant");
  171. if (att != null)
  172. {
  173. maxEnchant = Integer.parseInt(att.getNodeValue());
  174. }
  175. att = attrs.getNamedItem("successBonus");
  176. if (att != null)
  177. {
  178. chance = Double.parseDouble(att.getNodeValue());
  179. chance = Math.max(chance, 1.0); // Enchant bonus cannot be below 1.0
  180. }
  181. List<Integer> itemz = new ArrayList<Integer>();
  182. for (Node cd = n.getFirstChild(); cd != null; cd = cd.getNextSibling())
  183. {
  184. if ("item".equalsIgnoreCase(cd.getNodeName()))
  185. {
  186. att = cd.getAttributes().getNamedItem("id");
  187. if (att != null)
  188. itemz.add(Integer.parseInt(att.getNodeValue()));
  189. else
  190. {
  191. _log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Item id, skipping");
  192. continue;
  193. }
  194. }
  195. }
  196. if (itemz.size() > 0)
  197. {
  198. items = new int[itemz.size()];
  199. int i = 0;
  200. for (Integer id : itemz)
  201. {
  202. items[i++] = id;
  203. }
  204. Arrays.sort(items);
  205. }
  206. _supports.put(scrollId, new EnchantItem(isWeapon, type, maxEnchant, chance, items));
  207. }
  208. }
  209. }
  210. }
  211. catch (Exception e)
  212. {
  213. _log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Failed to parse xml: " + e.getMessage(), e);
  214. }
  215. _log.info(getClass().getSimpleName() + ": Loaded " + _scrolls.size() + " Enchant Scrolls");
  216. _log.info(getClass().getSimpleName() + ": Loaded " + _supports.size() + " Support Items");
  217. }
  218. /**
  219. * @param scroll
  220. * @return enchant template for scroll
  221. */
  222. public final EnchantScroll getEnchantScroll(L2ItemInstance scroll)
  223. {
  224. return _scrolls.get(scroll.getItemId());
  225. }
  226. /**
  227. * @param item
  228. * @return enchant template for support item
  229. */
  230. public final EnchantItem getSupportItem(L2ItemInstance item)
  231. {
  232. return _supports.get(item.getItemId());
  233. }
  234. public static final EnchantItemTable getInstance()
  235. {
  236. return SingletonHolder._instance;
  237. }
  238. @SuppressWarnings("synthetic-access")
  239. private static class SingletonHolder
  240. {
  241. protected static final EnchantItemTable _instance = new EnchantItemTable();
  242. }
  243. }