NpcTable.java 30 KB

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