2
0

EnchantItemData.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 java.util.logging.Level;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.gameserver.model.StatsSet;
  27. import com.l2jserver.gameserver.model.items.enchant.EnchantScroll;
  28. import com.l2jserver.gameserver.model.items.enchant.EnchantSupportItem;
  29. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  30. import com.l2jserver.util.data.xml.IXmlReader;
  31. /**
  32. * Loads item enchant data.
  33. * @author UnAfraid
  34. */
  35. public class EnchantItemData implements IXmlReader
  36. {
  37. private final Map<Integer, EnchantScroll> _scrolls = new HashMap<>();
  38. private final Map<Integer, EnchantSupportItem> _supports = new HashMap<>();
  39. /**
  40. * Instantiates a new enchant item data.
  41. */
  42. public EnchantItemData()
  43. {
  44. load();
  45. }
  46. @Override
  47. public synchronized void load()
  48. {
  49. _scrolls.clear();
  50. _supports.clear();
  51. parseDatapackFile("data/enchantItemData.xml");
  52. LOGGER.info(getClass().getSimpleName() + ": Loaded " + _scrolls.size() + " Enchant Scrolls.");
  53. LOGGER.info(getClass().getSimpleName() + ": Loaded " + _supports.size() + " Support Items.");
  54. }
  55. @Override
  56. public void parseDocument(Document doc)
  57. {
  58. StatsSet set;
  59. Node att;
  60. NamedNodeMap attrs;
  61. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  62. {
  63. if ("list".equalsIgnoreCase(n.getNodeName()))
  64. {
  65. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  66. {
  67. if ("enchant".equalsIgnoreCase(d.getNodeName()))
  68. {
  69. attrs = d.getAttributes();
  70. set = new StatsSet();
  71. for (int i = 0; i < attrs.getLength(); i++)
  72. {
  73. att = attrs.item(i);
  74. set.set(att.getNodeName(), att.getNodeValue());
  75. }
  76. try
  77. {
  78. final EnchantScroll item = new EnchantScroll(set);
  79. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  80. {
  81. if ("item".equalsIgnoreCase(cd.getNodeName()))
  82. {
  83. item.addItem(parseInteger(cd.getAttributes(), "id"));
  84. }
  85. }
  86. _scrolls.put(item.getId(), item);
  87. }
  88. catch (NullPointerException e)
  89. {
  90. LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Unexistent enchant scroll: " + set.getString("id") + " defined in enchant data!");
  91. }
  92. catch (IllegalAccessError e)
  93. {
  94. LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Wrong enchant scroll item type: " + set.getString("id") + " defined in enchant data!");
  95. }
  96. }
  97. else if ("support".equalsIgnoreCase(d.getNodeName()))
  98. {
  99. attrs = d.getAttributes();
  100. set = new StatsSet();
  101. for (int i = 0; i < attrs.getLength(); i++)
  102. {
  103. att = attrs.item(i);
  104. set.set(att.getNodeName(), att.getNodeValue());
  105. }
  106. try
  107. {
  108. final EnchantSupportItem item = new EnchantSupportItem(set);
  109. _supports.put(item.getId(), item);
  110. }
  111. catch (NullPointerException e)
  112. {
  113. LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Unexistent enchant support item: " + set.getString("id") + " defined in enchant data!");
  114. }
  115. catch (IllegalAccessError e)
  116. {
  117. LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Wrong enchant support item type: " + set.getString("id") + " defined in enchant data!");
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. /**
  125. * Gets the enchant scroll.
  126. * @param scroll the scroll
  127. * @return enchant template for scroll
  128. */
  129. public final EnchantScroll getEnchantScroll(L2ItemInstance scroll)
  130. {
  131. return _scrolls.get(scroll.getId());
  132. }
  133. /**
  134. * Gets the support item.
  135. * @param item the item
  136. * @return enchant template for support item
  137. */
  138. public final EnchantSupportItem getSupportItem(L2ItemInstance item)
  139. {
  140. return _supports.get(item.getId());
  141. }
  142. /**
  143. * Gets the single instance of EnchantItemData.
  144. * @return single instance of EnchantItemData
  145. */
  146. public static final EnchantItemData getInstance()
  147. {
  148. return SingletonHolder._instance;
  149. }
  150. private static class SingletonHolder
  151. {
  152. protected static final EnchantItemData _instance = new EnchantItemData();
  153. }
  154. }