PetDataTable.java 10 KB

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