EnchantOptionsData.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.HashMap;
  17. import java.util.Map;
  18. import java.util.logging.Level;
  19. import org.w3c.dom.Node;
  20. import com.l2jserver.gameserver.engines.DocumentParser;
  21. import com.l2jserver.gameserver.model.EnchantOptions;
  22. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  23. import com.l2jserver.gameserver.util.Util;
  24. /**
  25. * @author UnAfraid
  26. */
  27. public class EnchantOptionsData extends DocumentParser
  28. {
  29. private final Map<Integer, Map<Integer, EnchantOptions>> _data = new HashMap<>();
  30. protected EnchantOptionsData()
  31. {
  32. load();
  33. }
  34. @Override
  35. public synchronized void load()
  36. {
  37. parseDatapackFile("data/enchantOptions.xml");
  38. }
  39. @Override
  40. protected void parseDocument()
  41. {
  42. Node att = null;
  43. int counter = 0;
  44. EnchantOptions op = null;
  45. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  46. {
  47. if ("list".equalsIgnoreCase(n.getNodeName()))
  48. {
  49. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  50. {
  51. if ("item".equalsIgnoreCase(d.getNodeName()))
  52. {
  53. int itemId = parseInt(d.getAttributes(), "id");
  54. if (!_data.containsKey(itemId))
  55. {
  56. _data.put(itemId, new HashMap<Integer, EnchantOptions>());
  57. }
  58. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  59. {
  60. if ("options".equalsIgnoreCase(cd.getNodeName()))
  61. {
  62. op = new EnchantOptions(parseInt(cd.getAttributes(), "level"));
  63. _data.get(itemId).put(op.getLevel(), op);
  64. for (byte i = 0; i < 3; i++)
  65. {
  66. att = cd.getAttributes().getNamedItem("option" + (i + 1));
  67. if ((att != null) && Util.isDigit(att.getNodeValue()))
  68. {
  69. op.setOption(i, parseInt(att));
  70. }
  71. }
  72. counter++;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _data.size() + " Items and " + counter + " Options.");
  80. }
  81. /**
  82. * @param itemId
  83. * @param enchantLevel
  84. * @return enchant effects information.
  85. */
  86. public EnchantOptions getOptions(int itemId, int enchantLevel)
  87. {
  88. if (!_data.containsKey(itemId) || !_data.get(itemId).containsKey(enchantLevel))
  89. {
  90. return null;
  91. }
  92. return _data.get(itemId).get(enchantLevel);
  93. }
  94. /**
  95. * @param item
  96. * @return enchant effects information.
  97. */
  98. public EnchantOptions getOptions(L2ItemInstance item)
  99. {
  100. return item != null ? getOptions(item.getItemId(), item.getEnchantLevel()) : null;
  101. }
  102. /**
  103. * Gets the single instance of EnchantOptionsData.
  104. * @return single instance of EnchantOptionsData
  105. */
  106. public static final EnchantOptionsData getInstance()
  107. {
  108. return SingletonHolder._instance;
  109. }
  110. private static class SingletonHolder
  111. {
  112. protected static final EnchantOptionsData _instance = new EnchantOptionsData();
  113. }
  114. }