EnchantItemOptionsData.java 3.7 KB

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