EnchantItemData.java 4.7 KB

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