PetDataTable.java 9.6 KB

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