PetDataTable.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.HashMap;
  17. import java.util.Map;
  18. import org.w3c.dom.Document;
  19. import org.w3c.dom.NamedNodeMap;
  20. import org.w3c.dom.Node;
  21. import com.l2jserver.gameserver.engines.DocumentParser;
  22. import com.l2jserver.gameserver.model.L2PetData;
  23. import com.l2jserver.gameserver.model.L2PetLevelData;
  24. import com.l2jserver.gameserver.model.StatsSet;
  25. /**
  26. * This class parse and hold all pet parameters.
  27. * TODO: Unhardcode where is possible boolean methods and load and use all pet parameters.
  28. * @author Zoey76 (rework)
  29. */
  30. public final class PetDataTable extends DocumentParser
  31. {
  32. private static final Map<Integer, L2PetData> _pets = new HashMap<>();
  33. protected PetDataTable()
  34. {
  35. _pets.clear();
  36. parseDatapackFile("data/stats/npc/PetData.xml");
  37. _log.info(getClass().getSimpleName() + ": Loaded " + _pets.size() + " Pets.");
  38. }
  39. @Override
  40. protected void parseDocument(Document doc)
  41. {
  42. NamedNodeMap attrs;
  43. Node n = doc.getFirstChild();
  44. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  45. {
  46. if (d.getNodeName().equals("pet"))
  47. {
  48. int npcId = parseInt(d.getAttributes(), "id");
  49. // index ignored for now
  50. L2PetData data = new L2PetData();
  51. for (Node p = d.getFirstChild(); p != null; p = p.getNextSibling())
  52. {
  53. if (p.getNodeName().equals("set"))
  54. {
  55. attrs = p.getAttributes();
  56. String type = attrs.getNamedItem("name").getNodeValue();
  57. if ("food".equals(type))
  58. {
  59. for (String foodId : attrs.getNamedItem("val").getNodeValue().split(";"))
  60. {
  61. data.addFood(Integer.valueOf(foodId));
  62. }
  63. }
  64. else if ("load".equals(type))
  65. {
  66. data.setLoad(parseInt(attrs, "val"));
  67. }
  68. else if ("hungry_limit".equals(type))
  69. {
  70. data.setHungryLimit(parseInt(attrs, "val"));
  71. }
  72. // sync_level and evolve ignored
  73. }
  74. else if (p.getNodeName().equals("skills"))
  75. {
  76. for (Node s = p.getFirstChild(); s != null; s = s.getNextSibling())
  77. {
  78. if (s.getNodeName().equals("skill"))
  79. {
  80. attrs = s.getAttributes();
  81. data.addNewSkill(parseInt(attrs, "skillId"), parseInt(attrs, "skillLvl"), parseInt(attrs, "minLvl"));
  82. }
  83. }
  84. }
  85. else if (p.getNodeName().equals("stats"))
  86. {
  87. for (Node s = p.getFirstChild(); s != null; s = s.getNextSibling())
  88. {
  89. if (s.getNodeName().equals("stat"))
  90. {
  91. final int level = Integer.parseInt(s.getAttributes().getNamedItem("level").getNodeValue());
  92. final StatsSet set = new StatsSet();
  93. for (Node bean = s.getFirstChild(); bean != null; bean = bean.getNextSibling())
  94. {
  95. if (bean.getNodeName().equals("set"))
  96. {
  97. attrs = bean.getAttributes();
  98. set.set(attrs.getNamedItem("name").getNodeValue(), attrs.getNamedItem("val").getNodeValue());
  99. }
  100. }
  101. data.addNewStat(level, new L2PetLevelData(set));
  102. }
  103. }
  104. }
  105. }
  106. _pets.put(npcId, data);
  107. }
  108. }
  109. }
  110. /**
  111. * @param petId the pet Id.
  112. * @param petLevel the pet level.
  113. * @return the pet's parameters for the given Id and level.
  114. */
  115. public L2PetLevelData getPetLevelData(int petId, int petLevel)
  116. {
  117. final L2PetData pd = getPetData(petId);
  118. if (pd != null)
  119. {
  120. return pd.getPetLevelData(petLevel);
  121. }
  122. return null;
  123. }
  124. /**
  125. * @param petId the pet Id.
  126. * @return
  127. */
  128. public L2PetData getPetData(int petId)
  129. {
  130. if (!_pets.containsKey(petId))
  131. {
  132. _log.info("Missing pet data for npcid: " + petId);
  133. }
  134. return _pets.get(petId);
  135. }
  136. /**
  137. * @param petId the pet Id.
  138. * @return
  139. */
  140. public int getPetMinLevel(int petId)
  141. {
  142. return _pets.get(petId).getMinLevel();
  143. }
  144. /**
  145. * @param npcId the NPC Id to verify.
  146. * @return {@code true} if the given Id is from a wolf, {@code false} otherwise.
  147. */
  148. public static boolean isWolf(int npcId)
  149. {
  150. return npcId == 12077;
  151. }
  152. /**
  153. * @param npcId the NPC Id to verify.
  154. * @return {@code true} if the given Id is from an evolved wolf, {@code false} otherwise.
  155. */
  156. public static boolean isEvolvedWolf(int npcId)
  157. {
  158. return (npcId == 16030) || (npcId == 16037) || (npcId == 16025) || (npcId == 16041) || (npcId == 16042);
  159. }
  160. /**
  161. * @param npcId the NPC Id to verify.
  162. * @return {@code true} if the given Id is from a Sin Eater, {@code false} otherwise.
  163. */
  164. public static boolean isSinEater(int npcId)
  165. {
  166. return npcId == 12564;
  167. }
  168. /**
  169. * @param npcId the NPC Id to verify.
  170. * @return {@code true} if the given Id is from a hatchling, {@code false} otherwise.
  171. */
  172. public static boolean isHatchling(int npcId)
  173. {
  174. return (npcId > 12310) && (npcId < 12314);
  175. }
  176. /**
  177. * @param npcId the NPC Id to verify.
  178. * @return {@code true} if the given Id is from a strider, {@code false} otherwise.
  179. */
  180. public static boolean isStrider(int npcId)
  181. {
  182. return ((npcId > 12525) && (npcId < 12529)) || ((npcId > 16037) && (npcId < 16041)) || (npcId == 16068);
  183. }
  184. /**
  185. * @param npcId the NPC Id to verify.
  186. * @return {@code true} if the given Id is from a wyvern, {@code false} otherwise.
  187. */
  188. public static boolean isWyvern(int npcId)
  189. {
  190. return npcId == 12621;
  191. }
  192. /**
  193. * @param npcId the NPC Id to verify.
  194. * @return {@code true} if the given Id is from a baby pet, {@code false} otherwise.
  195. */
  196. public static boolean isBaby(int npcId)
  197. {
  198. return (npcId > 12779) && (npcId < 12783);
  199. }
  200. /**
  201. * @param npcId the NPC Id to verify.
  202. * @return {@code true} if the given Id is from an improved baby pet, {@code false} otherwise.
  203. */
  204. public static boolean isImprovedBaby(int npcId)
  205. {
  206. return (npcId > 16033) && (npcId < 16037);
  207. }
  208. /**
  209. * @param itemId the item Id to verify.
  210. * @return {@code true} if the given Id is from a pet's food, {@code false} otherwise.
  211. */
  212. public static boolean isPetFood(int itemId)
  213. {
  214. switch (itemId)
  215. {
  216. case 2515:
  217. case 4038:
  218. case 5168:
  219. case 5169:
  220. case 6316:
  221. case 7582:
  222. case 9668:
  223. case 10425:
  224. return true;
  225. default:
  226. return false;
  227. }
  228. }
  229. /**
  230. * @param npcId the NPC Id to get its summoning item.
  231. * @return an array containing the list of summoning items for the given NPC Id.
  232. */
  233. public static int[] getPetItemsByNpc(int npcId)
  234. {
  235. switch (npcId)
  236. {
  237. case 12077:// Wolf
  238. return new int[]
  239. {
  240. 2375
  241. };
  242. case 16025:// Great Wolf
  243. return new int[]
  244. {
  245. 9882
  246. };
  247. case 16030:// Black Wolf
  248. return new int[]
  249. {
  250. 10163
  251. };
  252. case 16037:// White Great Wolf
  253. return new int[]
  254. {
  255. 10307
  256. };
  257. case 16041:// Fenrir
  258. return new int[]
  259. {
  260. 10426
  261. };
  262. case 16042:// White Fenrir
  263. return new int[]
  264. {
  265. 10611
  266. };
  267. case 12564:// Sin Eater
  268. return new int[]
  269. {
  270. 4425
  271. };
  272. case 12311:// hatchling of wind
  273. case 12312:// hatchling of star
  274. case 12313:// hatchling of twilight
  275. return new int[]
  276. {
  277. 3500,
  278. 3501,
  279. 3502
  280. };
  281. case 12526:// wind strider
  282. case 12527:// Star strider
  283. case 12528:// Twilight strider
  284. case 16038: // red strider of wind
  285. case 16039: // red strider of star
  286. case 16040: // red strider of dusk
  287. case 16068: // Guardian Strider
  288. return new int[]
  289. {
  290. 4422,
  291. 4423,
  292. 4424,
  293. 10308,
  294. 10309,
  295. 10310,
  296. 14819
  297. };
  298. case 12621:// Wyvern
  299. return new int[]
  300. {
  301. 8663
  302. };
  303. case 12780:// Baby Buffalo
  304. case 12782:// Baby Cougar
  305. case 12781:// Baby Kookaburra
  306. return new int[]
  307. {
  308. 6648,
  309. 6649,
  310. 6650
  311. };
  312. case 16034:// Improved Baby Buffalo
  313. case 16036:// Improved Baby Cougar
  314. case 16035:// Improved Baby Kookaburra
  315. return new int[]
  316. {
  317. 10311,
  318. 10312,
  319. 10313
  320. };
  321. // unknown item id.. should never happen
  322. default:
  323. return new int[]
  324. {
  325. 0
  326. };
  327. }
  328. }
  329. /**
  330. * @param npcId the NPC Id to verify.
  331. * @return {@code true} if the given Id is from a mountable pet, {@code false} otherwise.
  332. */
  333. public static boolean isMountable(int npcId)
  334. {
  335. return (npcId == 12526 // wind strider
  336. ) || (npcId == 12527 // star strider
  337. ) || (npcId == 12528 // twilight strider
  338. ) || (npcId == 12621 // wyvern
  339. ) || (npcId == 16037 // Great Snow Wolf
  340. ) || (npcId == 16041 // Fenrir Wolf
  341. ) || (npcId == 16042 // White Fenrir Wolf
  342. ) || (npcId == 16038 // Red Wind Strider
  343. ) || (npcId == 16039 // Red Star Strider
  344. ) || (npcId == 16040 // Red Twilight Strider
  345. ) || (npcId == 16068); // Guardian Strider
  346. }
  347. /**
  348. * @return this class unique instance.
  349. */
  350. public static PetDataTable getInstance()
  351. {
  352. return SingletonHolder._instance;
  353. }
  354. private static class SingletonHolder
  355. {
  356. protected static final PetDataTable _instance = new PetDataTable();
  357. }
  358. }