EnchantItemData.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.data.xml.impl;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import org.w3c.dom.Document;
  23. import org.w3c.dom.NamedNodeMap;
  24. import org.w3c.dom.Node;
  25. import com.l2jserver.gameserver.model.StatsSet;
  26. import com.l2jserver.gameserver.model.items.enchant.EnchantScroll;
  27. import com.l2jserver.gameserver.model.items.enchant.EnchantSupportItem;
  28. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  29. import com.l2jserver.util.data.xml.IXmlReader;
  30. /**
  31. * Loads item enchant data.
  32. * @author UnAfraid
  33. */
  34. public class EnchantItemData implements IXmlReader
  35. {
  36. private final Map<Integer, EnchantScroll> _scrolls = new HashMap<>();
  37. private final Map<Integer, EnchantSupportItem> _supports = new HashMap<>();
  38. /**
  39. * Instantiates a new enchant item data.
  40. */
  41. public EnchantItemData()
  42. {
  43. load();
  44. }
  45. @Override
  46. public synchronized void load()
  47. {
  48. _scrolls.clear();
  49. _supports.clear();
  50. parseDatapackFile("data/enchantItemData.xml");
  51. LOGGER.info("{}: Loaded {} Enchant Scrolls.", getClass().getSimpleName(), _scrolls.size());
  52. LOGGER.info("{}: Loaded {} Support Items.", getClass().getSimpleName(), _supports.size());
  53. }
  54. @Override
  55. public void parseDocument(Document doc)
  56. {
  57. StatsSet set;
  58. Node att;
  59. NamedNodeMap attrs;
  60. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  61. {
  62. if ("list".equalsIgnoreCase(n.getNodeName()))
  63. {
  64. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  65. {
  66. if ("enchant".equalsIgnoreCase(d.getNodeName()))
  67. {
  68. attrs = d.getAttributes();
  69. set = new StatsSet();
  70. for (int i = 0; i < attrs.getLength(); i++)
  71. {
  72. att = attrs.item(i);
  73. set.set(att.getNodeName(), att.getNodeValue());
  74. }
  75. try
  76. {
  77. final EnchantScroll item = new EnchantScroll(set);
  78. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  79. {
  80. if ("item".equalsIgnoreCase(cd.getNodeName()))
  81. {
  82. item.addItem(parseInteger(cd.getAttributes(), "id"));
  83. }
  84. }
  85. _scrolls.put(item.getId(), item);
  86. }
  87. catch (NullPointerException e)
  88. {
  89. LOGGER.warn("{}: Unexistent enchant scroll: {} defined in enchant data!", getClass().getSimpleName(), set.getString("id"));
  90. }
  91. catch (IllegalAccessError e)
  92. {
  93. LOGGER.warn("{}: Wrong enchant scroll item type: {} defined in enchant data!", getClass().getSimpleName(), set.getString("id"));
  94. }
  95. }
  96. else if ("support".equalsIgnoreCase(d.getNodeName()))
  97. {
  98. attrs = d.getAttributes();
  99. set = new StatsSet();
  100. for (int i = 0; i < attrs.getLength(); i++)
  101. {
  102. att = attrs.item(i);
  103. set.set(att.getNodeName(), att.getNodeValue());
  104. }
  105. try
  106. {
  107. final EnchantSupportItem item = new EnchantSupportItem(set);
  108. _supports.put(item.getId(), item);
  109. }
  110. catch (NullPointerException e)
  111. {
  112. LOGGER.warn("{}: Unexistent enchant support item: {} defined in enchant data!", getClass().getSimpleName(), set.getString("id"));
  113. }
  114. catch (IllegalAccessError e)
  115. {
  116. LOGGER.warn("{}: Wrong enchant support item type: {} defined in enchant data!", getClass().getSimpleName(), set.getString("id"));
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. /**
  124. * Gets the enchant scroll.
  125. * @param scroll the scroll
  126. * @return enchant template for scroll
  127. */
  128. public final EnchantScroll getEnchantScroll(L2ItemInstance scroll)
  129. {
  130. return _scrolls.get(scroll.getId());
  131. }
  132. /**
  133. * Gets the support item.
  134. * @param item the item
  135. * @return enchant template for support item
  136. */
  137. public final EnchantSupportItem getSupportItem(L2ItemInstance item)
  138. {
  139. return _supports.get(item.getId());
  140. }
  141. /**
  142. * Gets the single instance of EnchantItemData.
  143. * @return single instance of EnchantItemData
  144. */
  145. public static final EnchantItemData getInstance()
  146. {
  147. return SingletonHolder._instance;
  148. }
  149. private static class SingletonHolder
  150. {
  151. protected static final EnchantItemData _instance = new EnchantItemData();
  152. }
  153. }