HennaData.java 4.2 KB

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