EnchantItemData.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.w3c.dom.NamedNodeMap;
  21. import org.w3c.dom.Node;
  22. import com.l2jserver.gameserver.engines.DocumentParser;
  23. import com.l2jserver.gameserver.model.EnchantItem;
  24. import com.l2jserver.gameserver.model.EnchantScroll;
  25. import com.l2jserver.gameserver.model.StatsSet;
  26. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  27. /**
  28. * This class holds the Enchant Item information.
  29. * @author UnAfraid
  30. */
  31. public class EnchantItemData extends DocumentParser
  32. {
  33. public static final Map<Integer, EnchantScroll> _scrolls = new HashMap<>();
  34. public static final Map<Integer, EnchantItem> _supports = new HashMap<>();
  35. /**
  36. * Instantiates a new enchant item data.
  37. */
  38. public EnchantItemData()
  39. {
  40. load();
  41. }
  42. @Override
  43. public void load()
  44. {
  45. _scrolls.clear();
  46. _supports.clear();
  47. parseDatapackFile("data/enchantData.xml");
  48. _log.info(getClass().getSimpleName() + ": Loaded " + _scrolls.size() + " Enchant Scrolls.");
  49. _log.info(getClass().getSimpleName() + ": Loaded " + _supports.size() + " Support Items.");
  50. }
  51. @Override
  52. protected void parseDocument()
  53. {
  54. StatsSet set;
  55. Node att;
  56. Map<Integer, Double> enchantSteps;
  57. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  58. {
  59. if ("list".equalsIgnoreCase(n.getNodeName()))
  60. {
  61. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  62. {
  63. if ("enchant".equalsIgnoreCase(d.getNodeName()))
  64. {
  65. NamedNodeMap attrs = d.getAttributes();
  66. set = new StatsSet();
  67. enchantSteps = new HashMap<>();
  68. for (int i = 0; i < attrs.getLength(); i++)
  69. {
  70. att = attrs.item(i);
  71. set.set(att.getNodeName(), att.getNodeValue());
  72. }
  73. List<Integer> items = new ArrayList<>();
  74. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  75. {
  76. if ("item".equalsIgnoreCase(cd.getNodeName()))
  77. {
  78. items.add(parseInteger(cd.getAttributes(), "id"));
  79. }
  80. else if ("step".equalsIgnoreCase(cd.getNodeName()))
  81. {
  82. enchantSteps.put(parseInt(cd.getAttributes(), "level"), parseDouble(cd.getAttributes(), "successRate"));
  83. }
  84. }
  85. EnchantScroll item = new EnchantScroll(set, items, enchantSteps);
  86. _scrolls.put(item.getScrollId(), item);
  87. }
  88. else if ("support".equalsIgnoreCase(d.getNodeName()))
  89. {
  90. NamedNodeMap attrs = d.getAttributes();
  91. set = new StatsSet();
  92. for (int i = 0; i < attrs.getLength(); i++)
  93. {
  94. att = attrs.item(i);
  95. set.set(att.getNodeName(), att.getNodeValue());
  96. }
  97. List<Integer> items = new ArrayList<>();
  98. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  99. {
  100. if ("item".equalsIgnoreCase(cd.getNodeName()))
  101. {
  102. items.add(parseInteger(cd.getAttributes(), "id"));
  103. }
  104. }
  105. EnchantItem item = new EnchantItem(set, items);
  106. _supports.put(item.getScrollId(), item);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * Gets the enchant scroll.
  114. * @param scroll the scroll
  115. * @return enchant template for scroll
  116. */
  117. public final EnchantScroll getEnchantScroll(L2ItemInstance scroll)
  118. {
  119. return _scrolls.get(scroll.getItemId());
  120. }
  121. /**
  122. * Gets the support item.
  123. * @param item the item
  124. * @return enchant template for support item
  125. */
  126. public final EnchantItem getSupportItem(L2ItemInstance item)
  127. {
  128. return _supports.get(item.getItemId());
  129. }
  130. /**
  131. * Gets the single instance of EnchantItemData.
  132. * @return single instance of EnchantItemData
  133. */
  134. public static final EnchantItemData getInstance()
  135. {
  136. return SingletonHolder._instance;
  137. }
  138. private static class SingletonHolder
  139. {
  140. protected static final EnchantItemData _instance = new EnchantItemData();
  141. }
  142. }