PetDataTable.java 9.7 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.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(getClass().getSimpleName() + ": 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. * Gets the pet items by npc.
  228. * @param npcId the NPC Id to get its summoning item.
  229. * @return an array containing the list of summoning items for the given NPC Id.
  230. */
  231. public static int[] getPetItemsByNpc(int npcId)
  232. {
  233. switch (npcId)
  234. {
  235. case 12077:// Wolf
  236. return new int[]
  237. {
  238. 2375
  239. };
  240. case 16025:// Great Wolf
  241. return new int[]
  242. {
  243. 9882
  244. };
  245. case 16030:// Black Wolf
  246. return new int[]
  247. {
  248. 10163
  249. };
  250. case 16037:// White Great Wolf
  251. return new int[]
  252. {
  253. 10307
  254. };
  255. case 16041:// Fenrir
  256. return new int[]
  257. {
  258. 10426
  259. };
  260. case 16042:// White Fenrir
  261. return new int[]
  262. {
  263. 10611
  264. };
  265. case 12564:// Sin Eater
  266. return new int[]
  267. {
  268. 4425
  269. };
  270. case 12311:// hatchling of wind
  271. case 12312:// hatchling of star
  272. case 12313:// hatchling of twilight
  273. return new int[]
  274. {
  275. 3500,
  276. 3501,
  277. 3502
  278. };
  279. case 12526:// wind strider
  280. case 12527:// Star strider
  281. case 12528:// Twilight strider
  282. case 16038: // red strider of wind
  283. case 16039: // red strider of star
  284. case 16040: // red strider of dusk
  285. case 16068: // Guardian Strider
  286. return new int[]
  287. {
  288. 4422,
  289. 4423,
  290. 4424,
  291. 10308,
  292. 10309,
  293. 10310,
  294. 14819
  295. };
  296. case 12621:// Wyvern
  297. return new int[]
  298. {
  299. 8663
  300. };
  301. case 12780:// Baby Buffalo
  302. case 12782:// Baby Cougar
  303. case 12781:// Baby Kookaburra
  304. return new int[]
  305. {
  306. 6648,
  307. 6649,
  308. 6650
  309. };
  310. case 16034:// Improved Baby Buffalo
  311. case 16036:// Improved Baby Cougar
  312. case 16035:// Improved Baby Kookaburra
  313. return new int[]
  314. {
  315. 10311,
  316. 10312,
  317. 10313
  318. };
  319. // unknown item id.. should never happen
  320. default:
  321. return new int[]
  322. {
  323. 0
  324. };
  325. }
  326. }
  327. /**
  328. * Checks if is mountable.
  329. * @param npcId the NPC Id to verify.
  330. * @return {@code true} if the given Id is from a mountable pet, {@code false} otherwise.
  331. */
  332. public static boolean isMountable(int npcId)
  333. {
  334. return (npcId == 12526 // wind strider
  335. ) || (npcId == 12527 // star strider
  336. ) || (npcId == 12528 // twilight strider
  337. ) || (npcId == 12621 // wyvern
  338. ) || (npcId == 16037 // Great Snow Wolf
  339. ) || (npcId == 16041 // Fenrir Wolf
  340. ) || (npcId == 16042 // White Fenrir Wolf
  341. ) || (npcId == 16038 // Red Wind Strider
  342. ) || (npcId == 16039 // Red Star Strider
  343. ) || (npcId == 16040 // Red Twilight Strider
  344. ) || (npcId == 16068); // Guardian Strider
  345. }
  346. /**
  347. * Gets the single instance of PetDataTable.
  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. }