NpcTable.java 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 gnu.trove.map.hash.TIntObjectHashMap;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Map.Entry;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.model.Elementals;
  30. import com.l2jserver.gameserver.model.L2DropData;
  31. import com.l2jserver.gameserver.model.L2MinionData;
  32. import com.l2jserver.gameserver.model.L2NpcAIData;
  33. import com.l2jserver.gameserver.model.StatsSet;
  34. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  35. import com.l2jserver.gameserver.model.base.ClassId;
  36. import com.l2jserver.gameserver.model.quest.Quest;
  37. import com.l2jserver.gameserver.model.quest.Quest.QuestEventType;
  38. import com.l2jserver.gameserver.model.skills.L2Skill;
  39. import com.l2jserver.gameserver.model.stats.BaseStats;
  40. public class NpcTable
  41. {
  42. private static final Logger _log = Logger.getLogger(NpcTable.class.getName());
  43. private final TIntObjectHashMap<L2NpcTemplate> _npcs = new TIntObjectHashMap<L2NpcTemplate>();
  44. private NpcTable()
  45. {
  46. _npcs.clear();
  47. restoreNpcData();
  48. }
  49. private void restoreNpcData()
  50. {
  51. loadNpcs(0);
  52. loadNpcsSkills(0);
  53. loadNpcsDrop(0);
  54. loadNpcsSkillLearn(0);
  55. loadMinions(0);
  56. loadNpcsAI(0);
  57. loadNpcsElement(0);
  58. }
  59. /**
  60. * @param NpcData
  61. * @throws Exception
  62. */
  63. private void fillNpcTable(ResultSet NpcData) throws Exception
  64. {
  65. StatsSet npcDat = new StatsSet();
  66. int id = NpcData.getInt("id");
  67. int idTemp = NpcData.getInt("idTemplate");
  68. assert idTemp < 1000000;
  69. npcDat.set("npcId", id);
  70. npcDat.set("idTemplate", idTemp);
  71. int level = NpcData.getInt("level");
  72. npcDat.set("level", level);
  73. npcDat.set("client_class", NpcData.getString("class"));
  74. npcDat.set("baseShldDef", 0);
  75. npcDat.set("baseShldRate", 0);
  76. npcDat.set("baseCritRate", NpcData.getInt("critical"));
  77. npcDat.set("name", NpcData.getString("name"));
  78. npcDat.set("serverSideName", NpcData.getBoolean("serverSideName"));
  79. npcDat.set("title", NpcData.getString("title"));
  80. npcDat.set("serverSideTitle", NpcData.getBoolean("serverSideTitle"));
  81. npcDat.set("collision_radius", NpcData.getDouble("collision_radius"));
  82. npcDat.set("collision_height", NpcData.getDouble("collision_height"));
  83. npcDat.set("sex", NpcData.getString("sex"));
  84. npcDat.set("type", NpcData.getString("type"));
  85. npcDat.set("baseAtkRange", NpcData.getInt("attackrange"));
  86. npcDat.set("rewardExp", NpcData.getInt("exp"));
  87. npcDat.set("rewardSp", NpcData.getInt("sp"));
  88. npcDat.set("basePAtkSpd", NpcData.getInt("atkspd"));
  89. npcDat.set("baseMAtkSpd", NpcData.getInt("matkspd"));
  90. npcDat.set("rhand", NpcData.getInt("rhand"));
  91. npcDat.set("lhand", NpcData.getInt("lhand"));
  92. npcDat.set("enchant", NpcData.getInt("enchant"));
  93. npcDat.set("baseWalkSpd", NpcData.getInt("walkspd"));
  94. npcDat.set("baseRunSpd", NpcData.getInt("runspd"));
  95. // constants, until we have stats in DB
  96. npcDat.safeSet("baseSTR", NpcData.getInt("str"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  97. npcDat.safeSet("baseCON", NpcData.getInt("con"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  98. npcDat.safeSet("baseDEX", NpcData.getInt("dex"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  99. npcDat.safeSet("baseINT", NpcData.getInt("int"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  100. npcDat.safeSet("baseWIT", NpcData.getInt("wit"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  101. npcDat.safeSet("baseMEN", NpcData.getInt("men"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  102. npcDat.set("baseHpMax", NpcData.getDouble("hp"));
  103. npcDat.set("baseCpMax", 0);
  104. npcDat.set("baseMpMax", NpcData.getDouble("mp"));
  105. npcDat.set("baseHpReg", NpcData.getFloat("hpreg") > 0 ? NpcData.getFloat("hpreg") : 1.5 + ((level - 1) / 10.0));
  106. npcDat.set("baseMpReg", NpcData.getFloat("mpreg") > 0 ? NpcData.getFloat("mpreg") : 0.9 + (0.3 * ((level - 1) / 10.0)));
  107. npcDat.set("basePAtk", NpcData.getInt("patk"));
  108. npcDat.set("basePDef", NpcData.getInt("pdef"));
  109. npcDat.set("baseMAtk", NpcData.getInt("matk"));
  110. npcDat.set("baseMDef", NpcData.getInt("mdef"));
  111. npcDat.set("dropHerbGroup", NpcData.getInt("dropHerbGroup"));
  112. // Default element resists
  113. npcDat.set("baseFireRes", 20);
  114. npcDat.set("baseWindRes", 20);
  115. npcDat.set("baseWaterRes", 20);
  116. npcDat.set("baseEarthRes", 20);
  117. npcDat.set("baseHolyRes", 20);
  118. npcDat.set("baseDarkRes", 20);
  119. _npcs.put(id, new L2NpcTemplate(npcDat));
  120. }
  121. /**
  122. * @param id of the NPC to reload.
  123. */
  124. public void reloadNpc(int id)
  125. {
  126. Map<QuestEventType, List<Quest>> quests = null;
  127. try
  128. {
  129. // save a copy of the old data
  130. L2NpcTemplate old = getTemplate(id);
  131. if (old != null)
  132. {
  133. quests = old.getEventQuests();
  134. }
  135. loadNpcs(id);
  136. loadNpcsSkills(id);
  137. loadNpcsDrop(id);
  138. loadNpcsSkillLearn(id);
  139. loadMinions(id);
  140. loadNpcsAI(id);
  141. loadNpcsElement(id);
  142. // restore additional data from saved copy
  143. L2NpcTemplate created = getTemplate(id);
  144. if ((old != null) && (created != null))
  145. {
  146. created.getEventQuests().putAll(quests);
  147. }
  148. }
  149. catch (Exception e)
  150. {
  151. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not reload data for NPC " + id + ": " + e.getMessage(), e);
  152. }
  153. }
  154. /**
  155. * Just wrapper.
  156. */
  157. public void reloadAllNpc()
  158. {
  159. restoreNpcData();
  160. }
  161. /**
  162. * @param npc
  163. */
  164. public void saveNpc(StatsSet npc)
  165. {
  166. final Map<String, Object> set = npc.getSet();
  167. int length = 0;
  168. for (Object obj : set.keySet())
  169. {
  170. // 15 is just guessed npc name length
  171. length += ((String) obj).length() + 7 + 15;
  172. }
  173. final StringBuilder npcSb = new StringBuilder(length);
  174. final StringBuilder npcAiSb = new StringBuilder(30);
  175. String attribute;
  176. String value;
  177. for (Entry<String, Object> entry : set.entrySet())
  178. {
  179. attribute = entry.getKey();
  180. value = String.valueOf(entry.getValue());
  181. switch (attribute)
  182. {
  183. case "npcId":
  184. break;
  185. case "aggro":
  186. case "showName":
  187. case "targetable":
  188. {
  189. appendEntry(npcAiSb, attribute, value);
  190. break;
  191. }
  192. default:
  193. {
  194. appendEntry(npcSb, attribute, value);
  195. }
  196. }
  197. }
  198. Connection con = null;
  199. try
  200. {
  201. con = L2DatabaseFactory.getInstance().getConnection();
  202. int updated = 0;
  203. final int npcId = npc.getInteger("npcId");
  204. if (Config.CUSTOM_NPC_TABLE)
  205. {
  206. updated = performUpdate(npcSb, "custom_npc", "id", npcId, con);
  207. performUpdate(npcAiSb, "custom_npcaidata", "npcId", npcId, con);
  208. }
  209. if (updated == 0)
  210. {
  211. performUpdate(npcSb, "npc", "id", npcId, con);
  212. performUpdate(npcAiSb, "npcaidata", "npcId", npcId, con);
  213. }
  214. }
  215. catch (Exception e)
  216. {
  217. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not store new NPC data in database: " + e.getMessage(), e);
  218. }
  219. finally
  220. {
  221. L2DatabaseFactory.close(con);
  222. }
  223. }
  224. /**
  225. * @param sb the string builder to append the attribute and value.
  226. * @param attribute the attribute to append.
  227. * @param value the value to append.
  228. */
  229. private final void appendEntry(StringBuilder sb, String attribute, String value)
  230. {
  231. if (sb.length() > 0)
  232. {
  233. sb.append(", ");
  234. }
  235. sb.append(attribute);
  236. sb.append(" = '");
  237. sb.append(value);
  238. sb.append("'");
  239. }
  240. /**
  241. * @param sb the string builder with the parameters
  242. * @param table the table to update.
  243. * @param key the key of the table.
  244. * @param npcId the Npc Id.
  245. * @param con the current database connection.
  246. * @return the count of updated NPCs.
  247. * @throws SQLException the SQL exception.
  248. */
  249. private final int performUpdate(StringBuilder sb, String table, String key, int npcId, Connection con) throws SQLException
  250. {
  251. int updated = 0;
  252. if ((sb != null) && !sb.toString().isEmpty())
  253. {
  254. final StringBuilder sbQuery = new StringBuilder(sb.length() + 28);
  255. sbQuery.append("UPDATE ");
  256. sbQuery.append(table);
  257. sbQuery.append(" SET ");
  258. sbQuery.append(sb.toString());
  259. sbQuery.append(" WHERE ");
  260. sbQuery.append(key);
  261. sbQuery.append(" = ?");
  262. final PreparedStatement statement = con.prepareStatement(sbQuery.toString());
  263. statement.setInt(1, npcId);
  264. updated = statement.executeUpdate();
  265. statement.close();
  266. }
  267. return updated;
  268. }
  269. /**
  270. * @param id the template Id to get.
  271. * @return the template for the given id.
  272. */
  273. public L2NpcTemplate getTemplate(int id)
  274. {
  275. return _npcs.get(id);
  276. }
  277. /**
  278. * @param name of the template to get.
  279. * @return the template for the given name.
  280. */
  281. public L2NpcTemplate getTemplateByName(String name)
  282. {
  283. for (L2NpcTemplate npcTemplate : _npcs.values(new L2NpcTemplate[0]))
  284. {
  285. if (npcTemplate.getName().equalsIgnoreCase(name))
  286. {
  287. return npcTemplate;
  288. }
  289. }
  290. return null;
  291. }
  292. /**
  293. * @param lvls of all the templates to get.
  294. * @return the template list for the given level.
  295. */
  296. public List<L2NpcTemplate> getAllOfLevel(int... lvls)
  297. {
  298. final List<L2NpcTemplate> list = new ArrayList<>();
  299. for (int lvl : lvls)
  300. {
  301. for (L2NpcTemplate t : _npcs.values(new L2NpcTemplate[0]))
  302. {
  303. if (t.getLevel() == lvl)
  304. {
  305. list.add(t);
  306. }
  307. }
  308. }
  309. return list;
  310. }
  311. /**
  312. * @param lvls of all the monster templates to get.
  313. * @return the template list for the given level.
  314. */
  315. public List<L2NpcTemplate> getAllMonstersOfLevel(int... lvls)
  316. {
  317. final List<L2NpcTemplate> list = new ArrayList<>();
  318. for (int lvl : lvls)
  319. {
  320. for (L2NpcTemplate t : _npcs.values(new L2NpcTemplate[0]))
  321. {
  322. if ((t.getLevel() == lvl) && t.isType("L2Monster"))
  323. {
  324. list.add(t);
  325. }
  326. }
  327. }
  328. return list;
  329. }
  330. /**
  331. * @param letters of all the NPC templates which its name start with.
  332. * @return the template list for the given letter.
  333. */
  334. public List<L2NpcTemplate> getAllNpcStartingWith(String... letters)
  335. {
  336. final List<L2NpcTemplate> list = new ArrayList<>();
  337. for (String letter : letters)
  338. {
  339. for (L2NpcTemplate t : _npcs.values(new L2NpcTemplate[0]))
  340. {
  341. if (t.getName().startsWith(letter) && t.isType("L2Npc"))
  342. {
  343. list.add(t);
  344. }
  345. }
  346. }
  347. return list;
  348. }
  349. /**
  350. * @param classTypes of all the templates to get.
  351. * @return the template list for the given class type.
  352. */
  353. public List<L2NpcTemplate> getAllNpcOfClassType(String... classTypes)
  354. {
  355. final List<L2NpcTemplate> list = new ArrayList<>();
  356. for (String classType : classTypes)
  357. {
  358. for (L2NpcTemplate t : _npcs.values(new L2NpcTemplate[0]))
  359. {
  360. if (t.isType(classType))
  361. {
  362. list.add(t);
  363. }
  364. }
  365. }
  366. return list;
  367. }
  368. /**
  369. * Id equals to zero or lesser means all.
  370. * @param id of the NPC to load.
  371. */
  372. public void loadNpcs(int id)
  373. {
  374. Connection con = null;
  375. try
  376. {
  377. con = L2DatabaseFactory.getInstance().getConnection();
  378. PreparedStatement statement;
  379. if (id > 0)
  380. {
  381. statement = con.prepareStatement("SELECT * FROM npc WHERE id = ?");
  382. statement.setInt(1, id);
  383. }
  384. else
  385. {
  386. statement = con.prepareStatement("SELECT * FROM npc ORDER BY id");
  387. }
  388. ResultSet rset = statement.executeQuery();
  389. int cont = 0;
  390. int cCont = 0;
  391. while (rset.next())
  392. {
  393. fillNpcTable(rset);
  394. cont++;
  395. }
  396. rset.close();
  397. statement.close();
  398. if (Config.CUSTOM_NPC_TABLE)
  399. {
  400. if (id > 0)
  401. {
  402. statement = con.prepareStatement("SELECT * FROM custom_npc WHERE id = ?");
  403. statement.setInt(1, id);
  404. }
  405. else
  406. {
  407. statement = con.prepareStatement("SELECT * FROM custom_npc ORDER BY id");
  408. }
  409. rset = statement.executeQuery();
  410. while (rset.next())
  411. {
  412. fillNpcTable(rset);
  413. cCont++;
  414. }
  415. rset.close();
  416. statement.close();
  417. }
  418. _log.info(getClass().getSimpleName() + ": Loaded " + cont + " (Custom: " + cCont + ") NPC template(s).");
  419. }
  420. catch (Exception e)
  421. {
  422. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error creating NPC table.", e);
  423. }
  424. finally
  425. {
  426. L2DatabaseFactory.close(con);
  427. }
  428. }
  429. /**
  430. * Id equals to zero or lesser means all.
  431. * @param id of the NPC to load it's skills.
  432. */
  433. public void loadNpcsSkills(int id)
  434. {
  435. Connection con = null;
  436. try
  437. {
  438. con = L2DatabaseFactory.getInstance().getConnection();
  439. PreparedStatement statement;
  440. if (id > 0)
  441. {
  442. statement = con.prepareStatement("SELECT * FROM npcskills WHERE npcid = ?");
  443. statement.setInt(1, id);
  444. }
  445. else
  446. {
  447. statement = con.prepareStatement("SELECT * FROM npcskills ORDER BY npcid");
  448. }
  449. ResultSet rset = statement.executeQuery();
  450. int cont = 0;
  451. int cCont = 0;
  452. L2NpcTemplate npcDat = null;
  453. L2Skill npcSkill = null;
  454. while (rset.next())
  455. {
  456. int mobId = rset.getInt("npcid");
  457. npcDat = _npcs.get(mobId);
  458. if (npcDat == null)
  459. {
  460. _log.warning(getClass().getSimpleName() + ": Skill data for undefined NPC. npcId: " + mobId);
  461. continue;
  462. }
  463. int skillId = rset.getInt("skillid");
  464. int level = rset.getInt("level");
  465. if (skillId == L2Skill.SKILL_NPC_RACE)
  466. {
  467. npcDat.setRace(level);
  468. continue;
  469. }
  470. npcSkill = SkillTable.getInstance().getInfo(skillId, level);
  471. if (npcSkill == null)
  472. {
  473. continue;
  474. }
  475. cont++;
  476. npcDat.addSkill(npcSkill);
  477. }
  478. rset.close();
  479. statement.close();
  480. if (Config.CUSTOM_NPC_SKILLS_TABLE)
  481. {
  482. if (id > 0)
  483. {
  484. statement = con.prepareStatement("SELECT * FROM custom_npcskills WHERE npcid = ?");
  485. statement.setInt(1, id);
  486. }
  487. else
  488. {
  489. statement = con.prepareStatement("SELECT * FROM custom_npcskills ORDER BY npcid");
  490. }
  491. rset = statement.executeQuery();
  492. while (rset.next())
  493. {
  494. int mobId = rset.getInt("npcid");
  495. npcDat = _npcs.get(mobId);
  496. if (npcDat == null)
  497. {
  498. _log.warning("Custom " + getClass().getSimpleName() + ": Skill data for undefined NPC. npcId: " + mobId);
  499. continue;
  500. }
  501. int skillId = rset.getInt("skillid");
  502. int level = rset.getInt("level");
  503. if ((npcDat.getRace() == null) && (skillId == L2Skill.SKILL_NPC_RACE))
  504. {
  505. npcDat.setRace(level);
  506. continue;
  507. }
  508. npcSkill = SkillTable.getInstance().getInfo(skillId, level);
  509. if (npcSkill == null)
  510. {
  511. continue;
  512. }
  513. cCont++;
  514. npcDat.addSkill(npcSkill);
  515. }
  516. rset.close();
  517. statement.close();
  518. }
  519. _log.info(getClass().getSimpleName() + ": Loaded " + cont + " (Custom: " + cCont + ") npc skills.");
  520. }
  521. catch (Exception e)
  522. {
  523. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC skills table.", e);
  524. }
  525. finally
  526. {
  527. L2DatabaseFactory.close(con);
  528. }
  529. }
  530. /**
  531. * Id equals to zero or lesser means all.
  532. * @param id of the NPC to load it's drops.
  533. */
  534. public void loadNpcsDrop(int id)
  535. {
  536. Connection con = null;
  537. try
  538. {
  539. con = L2DatabaseFactory.getInstance().getConnection();
  540. PreparedStatement statement = null;
  541. if (id > 0)
  542. {
  543. statement = con.prepareStatement("SELECT * FROM droplist WHERE mobId = ? ORDER BY mobId, chance DESC");
  544. statement.setInt(1, id);
  545. }
  546. else
  547. {
  548. statement = con.prepareStatement("SELECT * FROM droplist ORDER BY mobId, chance DESC");
  549. }
  550. ResultSet rset = statement.executeQuery();
  551. L2DropData dropDat = null;
  552. L2NpcTemplate npcDat = null;
  553. int cont = 0;
  554. int cCont = 0;
  555. while (rset.next())
  556. {
  557. int mobId = rset.getInt("mobId");
  558. npcDat = _npcs.get(mobId);
  559. if (npcDat == null)
  560. {
  561. _log.warning(getClass().getSimpleName() + ": Drop data for undefined NPC. npcId: " + mobId);
  562. continue;
  563. }
  564. dropDat = new L2DropData();
  565. dropDat.setItemId(rset.getInt("itemId"));
  566. dropDat.setMinDrop(rset.getInt("min"));
  567. dropDat.setMaxDrop(rset.getInt("max"));
  568. dropDat.setChance(rset.getInt("chance"));
  569. int category = rset.getInt("category");
  570. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  571. {
  572. _log.warning(getClass().getSimpleName() + ": Drop data for undefined item template! NpcId: " + mobId + " itemId: " + dropDat.getItemId());
  573. continue;
  574. }
  575. cont++;
  576. npcDat.addDropData(dropDat, category);
  577. }
  578. rset.close();
  579. statement.close();
  580. if (Config.CUSTOM_DROPLIST_TABLE)
  581. {
  582. if (id > 0)
  583. {
  584. statement = con.prepareStatement("SELECT * FROM custom_droplist WHERE mobId = ? ORDER BY mobId, chance DESC");
  585. statement.setInt(1, id);
  586. }
  587. else
  588. {
  589. statement = con.prepareStatement("SELECT * FROM custom_droplist ORDER BY mobId, chance DESC");
  590. }
  591. rset = statement.executeQuery();
  592. int mobId;
  593. int category;
  594. while (rset.next())
  595. {
  596. mobId = rset.getInt("mobId");
  597. npcDat = _npcs.get(mobId);
  598. if (npcDat == null)
  599. {
  600. _log.warning(getClass().getSimpleName() + ": CUSTOM DROPLIST: Drop data for undefined NPC. npcId: " + mobId);
  601. continue;
  602. }
  603. dropDat = new L2DropData();
  604. dropDat.setItemId(rset.getInt("itemId"));
  605. dropDat.setMinDrop(rset.getInt("min"));
  606. dropDat.setMaxDrop(rset.getInt("max"));
  607. dropDat.setChance(rset.getInt("chance"));
  608. category = rset.getInt("category");
  609. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  610. {
  611. _log.warning(getClass().getSimpleName() + ": Custom drop data for undefined item template! NpcId: " + mobId + " itemId: " + dropDat.getItemId());
  612. continue;
  613. }
  614. npcDat.addDropData(dropDat, category);
  615. cCont++;
  616. }
  617. rset.close();
  618. statement.close();
  619. }
  620. _log.info(getClass().getSimpleName() + ": Loaded " + cont + " (Custom: " + cCont + ") drops.");
  621. }
  622. catch (Exception e)
  623. {
  624. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC dropdata. ", e);
  625. }
  626. finally
  627. {
  628. L2DatabaseFactory.close(con);
  629. }
  630. }
  631. /**
  632. * Id equals to zero or lesser means all.
  633. * @param id of the NPC to load it's skill learn list.
  634. */
  635. private void loadNpcsSkillLearn(int id)
  636. {
  637. Connection con = null;
  638. try
  639. {
  640. con = L2DatabaseFactory.getInstance().getConnection();
  641. PreparedStatement statement;
  642. if (id > 0)
  643. {
  644. statement = con.prepareStatement("SELECT * FROM skill_learn WHERE npc_id = ?");
  645. statement.setInt(1, id);
  646. }
  647. else
  648. {
  649. statement = con.prepareStatement("SELECT * FROM skill_learn");
  650. }
  651. ResultSet rset = statement.executeQuery();
  652. int cont = 0;
  653. int npcId;
  654. int classId;
  655. L2NpcTemplate npc;
  656. while (rset.next())
  657. {
  658. npcId = rset.getInt("npc_id");
  659. classId = rset.getInt("class_id");
  660. npc = getTemplate(npcId);
  661. if (npc == null)
  662. {
  663. _log.warning(getClass().getSimpleName() + ": Error getting NPC template ID " + npcId + " while trying to load skill trainer data.");
  664. continue;
  665. }
  666. cont++;
  667. npc.addTeachInfo(ClassId.getClassId(classId));
  668. }
  669. rset.close();
  670. statement.close();
  671. _log.info(getClass().getSimpleName() + ": Loaded " + cont + " Skill Learn.");
  672. }
  673. catch (Exception e)
  674. {
  675. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC trainer data.", e);
  676. }
  677. finally
  678. {
  679. L2DatabaseFactory.close(con);
  680. }
  681. }
  682. /**
  683. * Id equals to zero or lesser means all.
  684. * @param id of the NPC to load it's minions.
  685. */
  686. public void loadMinions(int id)
  687. {
  688. Connection con = null;
  689. try
  690. {
  691. con = L2DatabaseFactory.getInstance().getConnection();
  692. PreparedStatement statement = null;
  693. if (id > 0)
  694. {
  695. statement = con.prepareStatement("SELECT * FROM minions WHERE boss_id = ?");
  696. statement.setInt(1, id);
  697. }
  698. else
  699. {
  700. statement = con.prepareStatement("SELECT * FROM minions ORDER BY boss_id");
  701. }
  702. ResultSet rset = statement.executeQuery();
  703. L2MinionData minionDat = null;
  704. L2NpcTemplate npcDat = null;
  705. int cnt = 0;
  706. int raidId;
  707. while (rset.next())
  708. {
  709. raidId = rset.getInt("boss_id");
  710. npcDat = _npcs.get(raidId);
  711. if (npcDat == null)
  712. {
  713. _log.warning(getClass().getSimpleName() + ": Minion references undefined boss NPC. Boss NpcId: " + raidId);
  714. continue;
  715. }
  716. minionDat = new L2MinionData();
  717. minionDat.setMinionId(rset.getInt("minion_id"));
  718. minionDat.setAmountMin(rset.getInt("amount_min"));
  719. minionDat.setAmountMax(rset.getInt("amount_max"));
  720. npcDat.addRaidData(minionDat);
  721. cnt++;
  722. }
  723. rset.close();
  724. statement.close();
  725. _log.info(getClass().getSimpleName() + ": Loaded " + cnt + " Minions.");
  726. }
  727. catch (Exception e)
  728. {
  729. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error loading minion data.", e);
  730. }
  731. finally
  732. {
  733. L2DatabaseFactory.close(con);
  734. }
  735. }
  736. /**
  737. * Id equals to zero or lesser means all.
  738. * @param id of the NPC to load it's AI data.
  739. */
  740. public void loadNpcsAI(int id)
  741. {
  742. Connection con = null;
  743. try
  744. {
  745. con = L2DatabaseFactory.getInstance().getConnection();
  746. PreparedStatement statement = null;
  747. if (id > 0)
  748. {
  749. statement = con.prepareStatement("SELECT * FROM npcaidata WHERE npcId = ?");
  750. statement.setInt(1, id);
  751. }
  752. else
  753. {
  754. statement = con.prepareStatement("SELECT * FROM npcaidata ORDER BY npcId");
  755. }
  756. ResultSet rset = statement.executeQuery();
  757. L2NpcAIData npcAIDat = null;
  758. L2NpcTemplate npcDat = null;
  759. int cont = 0;
  760. int cCont = 0;
  761. int npcId;
  762. while (rset.next())
  763. {
  764. npcId = rset.getInt("npcId");
  765. npcDat = _npcs.get(npcId);
  766. if (npcDat == null)
  767. {
  768. _log.severe(getClass().getSimpleName() + ": AI Data Error with id : " + npcId);
  769. continue;
  770. }
  771. npcAIDat = new L2NpcAIData();
  772. npcAIDat.setPrimarySkillId(rset.getInt("primarySkillId"));
  773. npcAIDat.setMinSkillChance(rset.getInt("minSkillChance"));
  774. npcAIDat.setMaxSkillChance(rset.getInt("maxSkillChance"));
  775. npcAIDat.setAggro(rset.getInt("aggro"));
  776. npcAIDat.setCanMove(rset.getInt("canMove"));
  777. npcAIDat.setShowName(rset.getBoolean("showName"));
  778. npcAIDat.setTargetable(rset.getBoolean("targetable"));
  779. npcAIDat.setSoulShot(rset.getInt("soulshot"));
  780. npcAIDat.setSpiritShot(rset.getInt("spiritshot"));
  781. npcAIDat.setSoulShotChance(rset.getInt("ssChance"));
  782. npcAIDat.setSpiritShotChance(rset.getInt("spsChance"));
  783. npcAIDat.setIsChaos(rset.getInt("isChaos"));
  784. npcAIDat.setShortRangeSkill(rset.getInt("minRangeSkill"));
  785. npcAIDat.setShortRangeChance(rset.getInt("minRangeChance"));
  786. npcAIDat.setLongRangeSkill(rset.getInt("maxRangeSkill"));
  787. npcAIDat.setLongRangeChance(rset.getInt("maxRangeChance"));
  788. npcAIDat.setClan(rset.getString("clan"));
  789. npcAIDat.setClanRange(rset.getInt("clanRange"));
  790. npcAIDat.setEnemyClan(rset.getString("enemyClan"));
  791. npcAIDat.setEnemyRange(rset.getInt("enemyRange"));
  792. npcAIDat.setDodge(rset.getInt("dodge"));
  793. npcAIDat.setAi(rset.getString("aiType"));
  794. npcDat.setAIData(npcAIDat);
  795. cont++;
  796. }
  797. rset.close();
  798. statement.close();
  799. if (Config.CUSTOM_NPC_TABLE)
  800. {
  801. if (id > 0)
  802. {
  803. statement = con.prepareStatement("SELECT * FROM custom_npcaidata WHERE npcId = ?");
  804. statement.setInt(1, id);
  805. }
  806. else
  807. {
  808. statement = con.prepareStatement("SELECT * FROM custom_npcaidata ORDER BY npcId");
  809. }
  810. rset = statement.executeQuery();
  811. while (rset.next())
  812. {
  813. npcId = rset.getInt("npcId");
  814. npcDat = _npcs.get(npcId);
  815. if (npcDat == null)
  816. {
  817. _log.severe(getClass().getSimpleName() + ": Custom AI Data Error with id : " + npcId);
  818. continue;
  819. }
  820. npcAIDat = new L2NpcAIData();
  821. npcAIDat.setPrimarySkillId(rset.getInt("primarySkillId"));
  822. npcAIDat.setMinSkillChance(rset.getInt("minSkillChance"));
  823. npcAIDat.setMaxSkillChance(rset.getInt("maxSkillChance"));
  824. npcAIDat.setAggro(rset.getInt("aggro"));
  825. npcAIDat.setCanMove(rset.getInt("canMove"));
  826. npcAIDat.setShowName(rset.getBoolean("showName"));
  827. npcAIDat.setTargetable(rset.getBoolean("targetable"));
  828. npcAIDat.setSoulShot(rset.getInt("soulshot"));
  829. npcAIDat.setSpiritShot(rset.getInt("spiritshot"));
  830. npcAIDat.setSoulShotChance(rset.getInt("ssChance"));
  831. npcAIDat.setSpiritShotChance(rset.getInt("spsChance"));
  832. npcAIDat.setIsChaos(rset.getInt("isChaos"));
  833. npcAIDat.setShortRangeSkill(rset.getInt("minRangeSkill"));
  834. npcAIDat.setShortRangeChance(rset.getInt("minRangeChance"));
  835. npcAIDat.setLongRangeSkill(rset.getInt("maxRangeSkill"));
  836. npcAIDat.setLongRangeChance(rset.getInt("maxRangeChance"));
  837. npcAIDat.setClan(rset.getString("clan"));
  838. npcAIDat.setClanRange(rset.getInt("clanRange"));
  839. npcAIDat.setEnemyClan(rset.getString("enemyClan"));
  840. npcAIDat.setEnemyRange(rset.getInt("enemyRange"));
  841. npcAIDat.setDodge(rset.getInt("dodge"));
  842. npcAIDat.setAi(rset.getString("aiType"));
  843. npcDat.setAIData(npcAIDat);
  844. cCont++;
  845. }
  846. rset.close();
  847. statement.close();
  848. }
  849. _log.info(getClass().getSimpleName() + ": Loaded " + cont + " (Custom: " + cCont + ") AI Data.");
  850. }
  851. catch (Exception e)
  852. {
  853. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC AI Data: " + e.getMessage(), e);
  854. }
  855. finally
  856. {
  857. L2DatabaseFactory.close(con);
  858. }
  859. }
  860. /**
  861. * Id equals to zero or lesser means all.
  862. * @param id of the NPC to load it's element data.
  863. */
  864. public void loadNpcsElement(int id)
  865. {
  866. Connection con = null;
  867. try
  868. {
  869. con = L2DatabaseFactory.getInstance().getConnection();
  870. PreparedStatement statement = null;
  871. if (id > 0)
  872. {
  873. statement = con.prepareStatement("SELECT * FROM npc_elementals WHERE npc_id = ?");
  874. statement.setInt(1, id);
  875. }
  876. else
  877. {
  878. statement = con.prepareStatement("SELECT * FROM npc_elementals ORDER BY npc_id");
  879. }
  880. ResultSet rset = statement.executeQuery();
  881. L2NpcTemplate npcDat = null;
  882. int cont = 0;
  883. int cCount = 0;
  884. int npcId;
  885. while (rset.next())
  886. {
  887. npcId = rset.getInt("npc_id");
  888. npcDat = _npcs.get(npcId);
  889. if (npcDat == null)
  890. {
  891. _log.severe("NPCElementals: Elementals Error with id : " + npcId);
  892. continue;
  893. }
  894. switch (rset.getByte("elemAtkType"))
  895. {
  896. case Elementals.FIRE:
  897. npcDat.setBaseFire(rset.getInt("elemAtkValue"));
  898. break;
  899. case Elementals.WATER:
  900. npcDat.setBaseWater(rset.getInt("elemAtkValue"));
  901. break;
  902. case Elementals.EARTH:
  903. npcDat.setBaseEarth(rset.getInt("elemAtkValue"));
  904. break;
  905. case Elementals.WIND:
  906. npcDat.setBaseWind(rset.getInt("elemAtkValue"));
  907. break;
  908. case Elementals.HOLY:
  909. npcDat.setBaseHoly(rset.getInt("elemAtkValue"));
  910. break;
  911. case Elementals.DARK:
  912. npcDat.setBaseDark(rset.getInt("elemAtkValue"));
  913. break;
  914. default:
  915. _log.severe("NPCElementals: Elementals Error with id : " + npcId + "; unknown elementType: " + rset.getByte("elemAtkType"));
  916. continue;
  917. }
  918. npcDat.setBaseFireRes(rset.getInt("fireDefValue"));
  919. npcDat.setBaseWaterRes(rset.getInt("waterDefValue"));
  920. npcDat.setBaseEarthRes(rset.getInt("earthDefValue"));
  921. npcDat.setBaseWindRes(rset.getInt("windDefValue"));
  922. npcDat.setBaseHolyRes(rset.getInt("holyDefValue"));
  923. npcDat.setBaseDarkRes(rset.getInt("darkDefValue"));
  924. cont++;
  925. }
  926. rset.close();
  927. statement.close();
  928. if (Config.CUSTOM_NPC_TABLE)
  929. {
  930. if (id > 0)
  931. {
  932. statement = con.prepareStatement("SELECT * FROM custom_npc_elementals WHERE npc_id = ?");
  933. statement.setInt(1, id);
  934. }
  935. else
  936. {
  937. statement = con.prepareStatement("SELECT * FROM custom_npc_elementals ORDER BY npc_id");
  938. }
  939. rset = statement.executeQuery();
  940. while (rset.next())
  941. {
  942. npcId = rset.getInt("npc_id");
  943. npcDat = _npcs.get(npcId);
  944. if (npcDat == null)
  945. {
  946. _log.severe("NPCElementals: Custom Elementals Error with id : " + npcId);
  947. continue;
  948. }
  949. switch (rset.getByte("elemAtkType"))
  950. {
  951. case Elementals.FIRE:
  952. npcDat.setBaseFire(rset.getInt("elemAtkValue"));
  953. break;
  954. case Elementals.WATER:
  955. npcDat.setBaseWater(rset.getInt("elemAtkValue"));
  956. break;
  957. case Elementals.EARTH:
  958. npcDat.setBaseEarth(rset.getInt("elemAtkValue"));
  959. break;
  960. case Elementals.WIND:
  961. npcDat.setBaseWind(rset.getInt("elemAtkValue"));
  962. break;
  963. case Elementals.HOLY:
  964. npcDat.setBaseHoly(rset.getInt("elemAtkValue"));
  965. break;
  966. case Elementals.DARK:
  967. npcDat.setBaseDark(rset.getInt("elemAtkValue"));
  968. break;
  969. default:
  970. _log.severe("NPCElementals: Custom Elementals Error with id : " + npcId + "; unknown elementType: " + rset.getByte("elemAtkType"));
  971. continue;
  972. }
  973. npcDat.setBaseFireRes(rset.getInt("fireDefValue"));
  974. npcDat.setBaseWaterRes(rset.getInt("waterDefValue"));
  975. npcDat.setBaseEarthRes(rset.getInt("earthDefValue"));
  976. npcDat.setBaseWindRes(rset.getInt("windDefValue"));
  977. npcDat.setBaseHolyRes(rset.getInt("holyDefValue"));
  978. npcDat.setBaseDarkRes(rset.getInt("darkDefValue"));
  979. cont++;
  980. }
  981. rset.close();
  982. statement.close();
  983. }
  984. _log.info(getClass().getSimpleName() + ": Loaded " + cont + " (Custom: " + cCount + ") elementals Data.");
  985. }
  986. catch (Exception e)
  987. {
  988. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC Elementals Data: " + e.getMessage(), e);
  989. }
  990. finally
  991. {
  992. L2DatabaseFactory.close(con);
  993. }
  994. }
  995. public static NpcTable getInstance()
  996. {
  997. return SingletonHolder._instance;
  998. }
  999. @SuppressWarnings("synthetic-access")
  1000. private static class SingletonHolder
  1001. {
  1002. protected static final NpcTable _instance = new NpcTable();
  1003. }
  1004. }