EnchantItemOptionsData.java 3.7 KB

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