EnchantItemTable.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.item.L2Item;
  31. import com.l2jserver.gameserver.model.item.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 isCrystal = false;
  74. boolean isSafe = false;
  75. int type = L2Item.CRYSTAL_NONE;
  76. int maxEnchant = 0;
  77. double chance = 66.66;
  78. int[] items = null;
  79. NamedNodeMap attrs = n.getAttributes();
  80. Node att = attrs.getNamedItem("id");
  81. if (att == null)
  82. {
  83. _log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Enchant id, skipping");
  84. continue;
  85. }
  86. scrollId = Integer.parseInt(att.getNodeValue());
  87. att = attrs.getNamedItem("isWeapon");
  88. if (att != null)
  89. {
  90. isWeapon = Boolean.parseBoolean(att.getNodeValue());
  91. }
  92. att = attrs.getNamedItem("isBlessed");
  93. if (att != null)
  94. {
  95. isBlessed = Boolean.parseBoolean(att.getNodeValue());
  96. }
  97. att = attrs.getNamedItem("isCrystal");
  98. if (att != null)
  99. {
  100. isCrystal = Boolean.parseBoolean(att.getNodeValue());
  101. }
  102. att = attrs.getNamedItem("isSafe");
  103. if (att != null)
  104. {
  105. isSafe = Boolean.parseBoolean(att.getNodeValue());
  106. }
  107. att = attrs.getNamedItem("targetGrade");
  108. if (att != null)
  109. {
  110. type = ItemTable._crystalTypes.get(att.getNodeValue());
  111. }
  112. att = attrs.getNamedItem("maxEnchant");
  113. if (att != null)
  114. {
  115. maxEnchant = Integer.parseInt(att.getNodeValue());
  116. }
  117. att = attrs.getNamedItem("successRate");
  118. if (att != null)
  119. {
  120. chance = Double.parseDouble(att.getNodeValue());
  121. chance = Math.max(chance, 1.0); // Enchant bonus cannot be below 1.0
  122. }
  123. List<Integer> itemz = new ArrayList<Integer>();
  124. for (Node cd = n.getFirstChild(); cd != null; cd = cd.getNextSibling())
  125. {
  126. if ("item".equalsIgnoreCase(cd.getNodeName()))
  127. {
  128. att = cd.getAttributes().getNamedItem("id");
  129. if (itemz != null)
  130. itemz.add(Integer.parseInt(att.getNodeValue()));
  131. else
  132. {
  133. _log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Item id, skipping");
  134. continue;
  135. }
  136. }
  137. }
  138. if (itemz.size() > 0)
  139. {
  140. items = new int[itemz.size()];
  141. int i = 0;
  142. for (Integer id : itemz)
  143. {
  144. items[i++] = id;
  145. }
  146. Arrays.sort(items);
  147. }
  148. _scrolls.put(scrollId, new EnchantScroll(isWeapon, isBlessed, isCrystal, isSafe, type, maxEnchant, chance, items));
  149. }
  150. else if ("support".equalsIgnoreCase(n.getNodeName()))
  151. {
  152. int scrollId = 0;
  153. boolean isWeapon = true;
  154. int type = L2Item.CRYSTAL_NONE;
  155. int maxEnchant = 0;
  156. double chance = 1.0;
  157. int[] items = null;
  158. NamedNodeMap attrs = n.getAttributes();
  159. Node att = attrs.getNamedItem("id");
  160. if (att == null)
  161. {
  162. _log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Support id, skipping");
  163. continue;
  164. }
  165. scrollId = Integer.parseInt(att.getNodeValue());
  166. att = attrs.getNamedItem("isWeapon");
  167. if (att != null)
  168. {
  169. isWeapon = Boolean.parseBoolean(att.getNodeValue());
  170. }
  171. att = attrs.getNamedItem("targetGrade");
  172. if (att != null)
  173. {
  174. type = ItemTable._crystalTypes.get(att.getNodeValue());
  175. }
  176. att = attrs.getNamedItem("maxEnchant");
  177. if (att != null)
  178. {
  179. maxEnchant = Integer.parseInt(att.getNodeValue());
  180. }
  181. att = attrs.getNamedItem("successBonus");
  182. if (att != null)
  183. {
  184. chance = Double.parseDouble(att.getNodeValue());
  185. chance = Math.max(chance, 1.0); // Enchant bonus cannot be below 1.0
  186. }
  187. List<Integer> itemz = new ArrayList<Integer>();
  188. for (Node cd = n.getFirstChild(); cd != null; cd = cd.getNextSibling())
  189. {
  190. if ("item".equalsIgnoreCase(cd.getNodeName()))
  191. {
  192. att = cd.getAttributes().getNamedItem("id");
  193. if (itemz != null)
  194. itemz.add(Integer.parseInt(att.getNodeValue()));
  195. else
  196. {
  197. _log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Missing Item id, skipping");
  198. continue;
  199. }
  200. }
  201. }
  202. if (itemz.size() > 0)
  203. {
  204. items = new int[itemz.size()];
  205. int i = 0;
  206. for (Integer id : itemz)
  207. {
  208. items[i++] = id;
  209. }
  210. Arrays.sort(items);
  211. }
  212. _supports.put(scrollId, new EnchantItem(isWeapon, type, maxEnchant, chance, items));
  213. }
  214. }
  215. }
  216. }
  217. catch (Exception e)
  218. {
  219. _log.log(Level.WARNING, "[" + getClass().getSimpleName() + "] Failed to parse xml: " + e.getMessage(), e);
  220. }
  221. _log.info(getClass().getSimpleName() + ": Loaded " + _scrolls.size() + " Enchant Scrolls");
  222. _log.info(getClass().getSimpleName() + ": Loaded " + _supports.size() + " Support Items");
  223. }
  224. /**
  225. * @param scroll
  226. * @return enchant template for scroll
  227. */
  228. public final EnchantScroll getEnchantScroll(L2ItemInstance scroll)
  229. {
  230. return _scrolls.get(scroll.getItemId());
  231. }
  232. /**
  233. * @param item
  234. * @return enchant template for support item
  235. */
  236. public final EnchantItem getSupportItem(L2ItemInstance item)
  237. {
  238. return _supports.get(item.getItemId());
  239. }
  240. public static final EnchantItemTable getInstance()
  241. {
  242. return SingletonHolder._instance;
  243. }
  244. @SuppressWarnings("synthetic-access")
  245. private static class SingletonHolder
  246. {
  247. protected static final EnchantItemTable _instance = new EnchantItemTable();
  248. }
  249. }