EnchantItemGroupsData.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.NamedNodeMap;
  24. import org.w3c.dom.Node;
  25. import com.l2jserver.gameserver.engines.DocumentParser;
  26. import com.l2jserver.gameserver.model.holders.RangeChanceHolder;
  27. import com.l2jserver.gameserver.model.items.L2Item;
  28. import com.l2jserver.gameserver.model.items.enchant.EnchantItemGroup;
  29. import com.l2jserver.gameserver.model.items.enchant.EnchantRateItem;
  30. import com.l2jserver.gameserver.model.items.enchant.EnchantScrollGroup;
  31. import com.l2jserver.gameserver.util.Util;
  32. /**
  33. * @author UnAfraid
  34. */
  35. public final class EnchantItemGroupsData extends DocumentParser
  36. {
  37. private final Map<String, EnchantItemGroup> _itemGroups = new HashMap<>();
  38. private final Map<Integer, EnchantScrollGroup> _scrollGroups = new HashMap<>();
  39. protected EnchantItemGroupsData()
  40. {
  41. load();
  42. }
  43. @Override
  44. public synchronized void load()
  45. {
  46. _itemGroups.clear();
  47. _scrollGroups.clear();
  48. parseDatapackFile("data/enchantItemGroups.xml");
  49. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _itemGroups.size() + " item group templates.");
  50. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _scrollGroups.size() + " scroll group templates.");
  51. }
  52. @Override
  53. protected void parseDocument()
  54. {
  55. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  56. {
  57. if ("list".equalsIgnoreCase(n.getNodeName()))
  58. {
  59. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  60. {
  61. if ("enchantRateGroup".equalsIgnoreCase(d.getNodeName()))
  62. {
  63. String name = parseString(d.getAttributes(), "name");
  64. final EnchantItemGroup group = new EnchantItemGroup(name);
  65. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  66. {
  67. if ("current".equalsIgnoreCase(cd.getNodeName()))
  68. {
  69. String range = parseString(cd.getAttributes(), "enchant");
  70. double chance = parseDouble(cd.getAttributes(), "chance");
  71. int min = -1;
  72. int max = 0;
  73. if (range.contains("-"))
  74. {
  75. String[] split = range.split("-");
  76. if ((split.length == 2) && Util.isDigit(split[0]) && Util.isDigit(split[1]))
  77. {
  78. min = Integer.parseInt(split[0]);
  79. max = Integer.parseInt(split[1]);
  80. }
  81. }
  82. else if (Util.isDigit(range))
  83. {
  84. min = Integer.parseInt(range);
  85. max = min;
  86. }
  87. if ((min > -1) && (max > 0))
  88. {
  89. group.addChance(new RangeChanceHolder(min, max, chance));
  90. }
  91. }
  92. }
  93. _itemGroups.put(name, group);
  94. }
  95. else if ("enchantScrollGroup".equals(d.getNodeName()))
  96. {
  97. int id = parseInt(d.getAttributes(), "id");
  98. final EnchantScrollGroup group = new EnchantScrollGroup(id);
  99. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  100. {
  101. if ("enchantRate".equalsIgnoreCase(cd.getNodeName()))
  102. {
  103. final EnchantRateItem rateGroup = new EnchantRateItem(parseString(cd.getAttributes(), "group"));
  104. for (Node z = cd.getFirstChild(); z != null; z = z.getNextSibling())
  105. {
  106. if ("item".equals(z.getNodeName()))
  107. {
  108. final NamedNodeMap attrs = z.getAttributes();
  109. if (attrs.getNamedItem("slot") != null)
  110. {
  111. rateGroup.addSlot(ItemTable._slots.get(parseString(attrs, "slot")));
  112. }
  113. if (attrs.getNamedItem("magicWeapon") != null)
  114. {
  115. rateGroup.setMagicWeapon(parseBoolean(attrs, "magicWeapon"));
  116. }
  117. if (attrs.getNamedItem("id") != null)
  118. {
  119. rateGroup.setItemId(parseInt(attrs, "id"));
  120. }
  121. }
  122. }
  123. group.addRateGroup(rateGroup);
  124. }
  125. }
  126. _scrollGroups.put(id, group);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. public EnchantItemGroup getItemGroup(L2Item item, int scrollGroup)
  133. {
  134. final EnchantScrollGroup group = _scrollGroups.get(scrollGroup);
  135. final EnchantRateItem rateGroup = group.getRateGroup(item);
  136. return rateGroup != null ? _itemGroups.get(rateGroup.getName()) : null;
  137. }
  138. public EnchantItemGroup getItemGroup(String name)
  139. {
  140. return _itemGroups.get(name);
  141. }
  142. public EnchantScrollGroup getScrollGroup(int id)
  143. {
  144. return _scrollGroups.get(id);
  145. }
  146. public static EnchantItemGroupsData getInstance()
  147. {
  148. return SingletonHolder._instance;
  149. }
  150. private static class SingletonHolder
  151. {
  152. protected static final EnchantItemGroupsData _instance = new EnchantItemGroupsData();
  153. }
  154. }