HennaData.java 4.3 KB

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