PetDataTable.java 6.8 KB

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