HennaData.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.NamedNodeMap;
  26. import org.w3c.dom.Node;
  27. import com.l2jserver.gameserver.model.StatsSet;
  28. import com.l2jserver.gameserver.model.base.ClassId;
  29. import com.l2jserver.gameserver.model.items.L2Henna;
  30. import com.l2jserver.util.data.xml.IXmlReader;
  31. /**
  32. * This class holds the henna related information.<br>
  33. * Cost and required amount to add the henna to the player.<br>
  34. * Cost and retrieved amount for removing the henna from the player.<br>
  35. * Allowed classes to wear each henna.
  36. * @author Zoey76
  37. */
  38. public final class HennaData implements IXmlReader
  39. {
  40. private final Map<Integer, L2Henna> _hennaList = new HashMap<>();
  41. /**
  42. * Instantiates a new henna data.
  43. */
  44. protected HennaData()
  45. {
  46. load();
  47. }
  48. @Override
  49. public void load()
  50. {
  51. _hennaList.clear();
  52. parseDatapackFile("data/stats/hennaList.xml");
  53. LOGGER.info("{}: Loaded {} Henna data.", getClass().getSimpleName(), _hennaList.size());
  54. }
  55. @Override
  56. public void parseDocument(Document doc)
  57. {
  58. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  59. {
  60. if ("list".equals(n.getNodeName()))
  61. {
  62. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  63. {
  64. if ("henna".equals(d.getNodeName()))
  65. {
  66. parseHenna(d);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * Parses the henna.
  74. * @param d the d
  75. */
  76. private void parseHenna(Node d)
  77. {
  78. final StatsSet set = new StatsSet();
  79. final List<ClassId> wearClassIds = new ArrayList<>();
  80. NamedNodeMap attrs = d.getAttributes();
  81. Node attr;
  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. final String 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. }