HennaData.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. private HennaData()
  38. {
  39. _hennaList.clear();
  40. parseDatapackFile("data/stats/hennaList.xml");
  41. _log.info(getClass().getSimpleName() + ": Loaded " + _hennaList.size() + " Henna data.");
  42. }
  43. @Override
  44. protected void parseDocument(Document doc)
  45. {
  46. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  47. {
  48. if ("list".equals(n.getNodeName()))
  49. {
  50. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  51. {
  52. if ("henna".equals(d.getNodeName()))
  53. {
  54. parseHenna(d);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. private void parseHenna(Node d)
  61. {
  62. final StatsSet set = new StatsSet();
  63. final List<ClassId> wearClassIds = new ArrayList<>();
  64. NamedNodeMap attrs = d.getAttributes();
  65. Node attr;
  66. String name;
  67. for (int i = 0; i < attrs.getLength(); i++)
  68. {
  69. attr = attrs.item(i);
  70. set.set(attr.getNodeName(), attr.getNodeValue());
  71. }
  72. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  73. {
  74. name = c.getNodeName();
  75. attrs = c.getAttributes();
  76. switch (name)
  77. {
  78. case "stats":
  79. {
  80. for (int i = 0; i < attrs.getLength(); i++)
  81. {
  82. attr = attrs.item(i);
  83. set.set(attr.getNodeName(), attr.getNodeValue());
  84. }
  85. break;
  86. }
  87. case "wear":
  88. {
  89. attr = attrs.getNamedItem("count");
  90. set.set("wear_count", attr.getNodeValue());
  91. attr = attrs.getNamedItem("fee");
  92. set.set("wear_fee", attr.getNodeValue());
  93. break;
  94. }
  95. case "cancel":
  96. {
  97. attr = attrs.getNamedItem("count");
  98. set.set("cancel_count", attr.getNodeValue());
  99. attr = attrs.getNamedItem("fee");
  100. set.set("cancel_fee", attr.getNodeValue());
  101. break;
  102. }
  103. case "classId":
  104. {
  105. wearClassIds.add(ClassId.getClassId(Integer.parseInt(c.getTextContent())));
  106. break;
  107. }
  108. }
  109. }
  110. final L2Henna henna = new L2Henna(set);
  111. henna.setWearClassIds(wearClassIds);
  112. _hennaList.put(henna.getDyeId(), henna);
  113. }
  114. /**
  115. * @param id of the dye.
  116. * @return the dye with that id.
  117. */
  118. public L2Henna getHenna(int id)
  119. {
  120. return _hennaList.get(id);
  121. }
  122. /**
  123. * @param classId the player's class Id.
  124. * @return the list with all the allowed dyes.
  125. */
  126. public List<L2Henna> getHennaList(ClassId classId)
  127. {
  128. final List<L2Henna> list = new ArrayList<>();
  129. for (L2Henna henna : _hennaList.values())
  130. {
  131. if (henna.isAllowedClass(classId))
  132. {
  133. list.add(henna);
  134. }
  135. }
  136. return list;
  137. }
  138. public static HennaData getInstance()
  139. {
  140. return SingletonHolder._instance;
  141. }
  142. @SuppressWarnings("synthetic-access")
  143. private static class SingletonHolder
  144. {
  145. protected static final HennaData _instance = new HennaData();
  146. }
  147. }