HennaData.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.w3c.dom.NamedNodeMap;
  21. import org.w3c.dom.Node;
  22. import com.l2jserver.gameserver.engines.DocumentParser;
  23. import com.l2jserver.gameserver.model.StatsSet;
  24. import com.l2jserver.gameserver.model.base.ClassId;
  25. import com.l2jserver.gameserver.model.items.L2Henna;
  26. /**
  27. * This class holds the henna related information.<br>
  28. * Cost and required amount to add the henna to the player.<br>
  29. * Cost and retrieved amount for removing the henna from the player.<br>
  30. * Allowed classes to wear each henna.
  31. * @author Zoey76
  32. */
  33. public final class HennaData extends DocumentParser
  34. {
  35. private static final Map<Integer, L2Henna> _hennaList = new HashMap<>();
  36. /**
  37. * Instantiates a new henna data.
  38. */
  39. protected HennaData()
  40. {
  41. load();
  42. }
  43. @Override
  44. public void load()
  45. {
  46. _hennaList.clear();
  47. parseDatapackFile("data/stats/hennaList.xml");
  48. _log.info(getClass().getSimpleName() + ": Loaded " + _hennaList.size() + " Henna data.");
  49. }
  50. @Override
  51. protected void parseDocument()
  52. {
  53. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  54. {
  55. if ("list".equals(n.getNodeName()))
  56. {
  57. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  58. {
  59. if ("henna".equals(d.getNodeName()))
  60. {
  61. parseHenna(d);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. /**
  68. * Parses the henna.
  69. * @param d the d
  70. */
  71. private void parseHenna(Node d)
  72. {
  73. final StatsSet set = new StatsSet();
  74. final List<ClassId> wearClassIds = new ArrayList<>();
  75. NamedNodeMap attrs = d.getAttributes();
  76. Node attr;
  77. String name;
  78. for (int i = 0; i < attrs.getLength(); i++)
  79. {
  80. attr = attrs.item(i);
  81. set.set(attr.getNodeName(), attr.getNodeValue());
  82. }
  83. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  84. {
  85. name = c.getNodeName();
  86. attrs = c.getAttributes();
  87. switch (name)
  88. {
  89. case "stats":
  90. {
  91. for (int i = 0; i < attrs.getLength(); i++)
  92. {
  93. attr = attrs.item(i);
  94. set.set(attr.getNodeName(), attr.getNodeValue());
  95. }
  96. break;
  97. }
  98. case "wear":
  99. {
  100. attr = attrs.getNamedItem("count");
  101. set.set("wear_count", attr.getNodeValue());
  102. attr = attrs.getNamedItem("fee");
  103. set.set("wear_fee", attr.getNodeValue());
  104. break;
  105. }
  106. case "cancel":
  107. {
  108. attr = attrs.getNamedItem("count");
  109. set.set("cancel_count", attr.getNodeValue());
  110. attr = attrs.getNamedItem("fee");
  111. set.set("cancel_fee", attr.getNodeValue());
  112. break;
  113. }
  114. case "classId":
  115. {
  116. wearClassIds.add(ClassId.getClassId(Integer.parseInt(c.getTextContent())));
  117. break;
  118. }
  119. }
  120. }
  121. final L2Henna henna = new L2Henna(set);
  122. henna.setWearClassIds(wearClassIds);
  123. _hennaList.put(henna.getDyeId(), henna);
  124. }
  125. /**
  126. * Gets the henna.
  127. * @param id of the dye.
  128. * @return the dye with that id.
  129. */
  130. public L2Henna getHenna(int id)
  131. {
  132. return _hennaList.get(id);
  133. }
  134. /**
  135. * Gets the henna list.
  136. * @param classId the player's class Id.
  137. * @return the list with all the allowed dyes.
  138. */
  139. public List<L2Henna> getHennaList(ClassId classId)
  140. {
  141. final List<L2Henna> list = new ArrayList<>();
  142. for (L2Henna henna : _hennaList.values())
  143. {
  144. if (henna.isAllowedClass(classId))
  145. {
  146. list.add(henna);
  147. }
  148. }
  149. return list;
  150. }
  151. /**
  152. * Gets the single instance of HennaData.
  153. * @return single instance of HennaData
  154. */
  155. public static HennaData getInstance()
  156. {
  157. return SingletonHolder._instance;
  158. }
  159. private static class SingletonHolder
  160. {
  161. protected static final HennaData _instance = new HennaData();
  162. }
  163. }