PetDataTable.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.HashMap;
  21. import java.util.Map;
  22. import org.w3c.dom.Document;
  23. import org.w3c.dom.NamedNodeMap;
  24. import org.w3c.dom.Node;
  25. import com.l2jserver.gameserver.enums.MountType;
  26. import com.l2jserver.gameserver.model.L2PetData;
  27. import com.l2jserver.gameserver.model.L2PetLevelData;
  28. import com.l2jserver.gameserver.model.StatsSet;
  29. import com.l2jserver.util.data.xml.IXmlReader;
  30. /**
  31. * This class parse and hold all pet parameters.<br>
  32. * TODO: load and use all pet parameters.
  33. * @author Zoey76 (rework)
  34. */
  35. public final class PetDataTable implements IXmlReader
  36. {
  37. private final Map<Integer, L2PetData> _pets = new HashMap<>();
  38. /**
  39. * Instantiates a new pet data table.
  40. */
  41. protected PetDataTable()
  42. {
  43. load();
  44. }
  45. @Override
  46. public void load()
  47. {
  48. _pets.clear();
  49. parseDatapackDirectory("data/stats/pets", false);
  50. LOGGER.info("{}: Loaded {} Pets.", getClass().getSimpleName(), _pets.size());
  51. }
  52. @Override
  53. public void parseDocument(Document doc)
  54. {
  55. NamedNodeMap attrs;
  56. Node n = doc.getFirstChild();
  57. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  58. {
  59. if (d.getNodeName().equals("pet"))
  60. {
  61. int npcId = parseInteger(d.getAttributes(), "id");
  62. int itemId = parseInteger(d.getAttributes(), "itemId");
  63. // index ignored for now
  64. L2PetData data = new L2PetData(npcId, itemId);
  65. for (Node p = d.getFirstChild(); p != null; p = p.getNextSibling())
  66. {
  67. if (p.getNodeName().equals("set"))
  68. {
  69. attrs = p.getAttributes();
  70. String type = attrs.getNamedItem("name").getNodeValue();
  71. if ("food".equals(type))
  72. {
  73. for (String foodId : attrs.getNamedItem("val").getNodeValue().split(";"))
  74. {
  75. data.addFood(Integer.valueOf(foodId));
  76. }
  77. }
  78. else if ("load".equals(type))
  79. {
  80. data.setLoad(parseInteger(attrs, "val"));
  81. }
  82. else if ("hungry_limit".equals(type))
  83. {
  84. data.setHungryLimit(parseInteger(attrs, "val"));
  85. }
  86. else if ("sync_level".equals(type))
  87. {
  88. data.setSyncLevel(parseInteger(attrs, "val") == 1);
  89. }
  90. // evolve ignored
  91. }
  92. else if (p.getNodeName().equals("skills"))
  93. {
  94. for (Node s = p.getFirstChild(); s != null; s = s.getNextSibling())
  95. {
  96. if (s.getNodeName().equals("skill"))
  97. {
  98. attrs = s.getAttributes();
  99. data.addNewSkill(parseInteger(attrs, "skillId"), parseInteger(attrs, "skillLvl"), parseInteger(attrs, "minLvl"));
  100. }
  101. }
  102. }
  103. else if (p.getNodeName().equals("stats"))
  104. {
  105. for (Node s = p.getFirstChild(); s != null; s = s.getNextSibling())
  106. {
  107. if (s.getNodeName().equals("stat"))
  108. {
  109. final int level = Integer.parseInt(s.getAttributes().getNamedItem("level").getNodeValue());
  110. final StatsSet set = new StatsSet();
  111. for (Node bean = s.getFirstChild(); bean != null; bean = bean.getNextSibling())
  112. {
  113. if (bean.getNodeName().equals("set"))
  114. {
  115. attrs = bean.getAttributes();
  116. if (attrs.getNamedItem("name").getNodeValue().equals("speed_on_ride"))
  117. {
  118. set.set("walkSpeedOnRide", attrs.getNamedItem("walk").getNodeValue());
  119. set.set("runSpeedOnRide", attrs.getNamedItem("run").getNodeValue());
  120. set.set("slowSwimSpeedOnRide", attrs.getNamedItem("slowSwim").getNodeValue());
  121. set.set("fastSwimSpeedOnRide", attrs.getNamedItem("fastSwim").getNodeValue());
  122. if (attrs.getNamedItem("slowFly") != null)
  123. {
  124. set.set("slowFlySpeedOnRide", attrs.getNamedItem("slowFly").getNodeValue());
  125. }
  126. if (attrs.getNamedItem("fastFly") != null)
  127. {
  128. set.set("fastFlySpeedOnRide", attrs.getNamedItem("fastFly").getNodeValue());
  129. }
  130. }
  131. else
  132. {
  133. set.set(attrs.getNamedItem("name").getNodeValue(), attrs.getNamedItem("val").getNodeValue());
  134. }
  135. }
  136. }
  137. data.addNewStat(level, new L2PetLevelData(set));
  138. }
  139. }
  140. }
  141. }
  142. _pets.put(npcId, data);
  143. }
  144. }
  145. }
  146. /**
  147. * @param itemId
  148. * @return
  149. */
  150. public L2PetData getPetDataByItemId(int itemId)
  151. {
  152. for (L2PetData data : _pets.values())
  153. {
  154. if (data.getItemId() == itemId)
  155. {
  156. return data;
  157. }
  158. }
  159. return null;
  160. }
  161. /**
  162. * Gets the pet level data.
  163. * @param petId the pet Id.
  164. * @param petLevel the pet level.
  165. * @return the pet's parameters for the given Id and level.
  166. */
  167. public L2PetLevelData getPetLevelData(int petId, int petLevel)
  168. {
  169. final L2PetData pd = getPetData(petId);
  170. if (pd != null)
  171. {
  172. return pd.getPetLevelData(petLevel);
  173. }
  174. return null;
  175. }
  176. /**
  177. * Gets the pet data.
  178. * @param petId the pet Id.
  179. * @return the pet data
  180. */
  181. public L2PetData getPetData(int petId)
  182. {
  183. if (!_pets.containsKey(petId))
  184. {
  185. LOGGER.info("{}: Missing pet data for NPC ID {}!", getClass().getSimpleName(), petId);
  186. }
  187. return _pets.get(petId);
  188. }
  189. /**
  190. * Gets the pet min level.
  191. * @param petId the pet Id.
  192. * @return the pet min level
  193. */
  194. public int getPetMinLevel(int petId)
  195. {
  196. return _pets.get(petId).getMinLevel();
  197. }
  198. /**
  199. * Gets the pet items by npc.
  200. * @param npcId the NPC ID to get its summoning item
  201. * @return summoning item for the given NPC ID
  202. */
  203. public int getPetItemsByNpc(int npcId)
  204. {
  205. return _pets.get(npcId).getItemId();
  206. }
  207. /**
  208. * Checks if is mountable.
  209. * @param npcId the NPC Id to verify.
  210. * @return {@code true} if the given Id is from a mountable pet, {@code false} otherwise.
  211. */
  212. public static boolean isMountable(int npcId)
  213. {
  214. return MountType.findByNpcId(npcId) != MountType.NONE;
  215. }
  216. /**
  217. * Gets the single instance of PetDataTable.
  218. * @return this class unique instance.
  219. */
  220. public static PetDataTable getInstance()
  221. {
  222. return SingletonHolder._instance;
  223. }
  224. private static class SingletonHolder
  225. {
  226. protected static final PetDataTable _instance = new PetDataTable();
  227. }
  228. }