2
0

HennaData.java 4.5 KB

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