EnchantItemGroupsData.java 5.4 KB

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