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("aggroRange", NpcData.getInt("aggro"));
  95. npcDat.set("rhand", NpcData.getInt("rhand"));
  96. npcDat.set("lhand", NpcData.getInt("lhand"));
  97. npcDat.set("enchant", NpcData.getInt("enchant"));
  98. npcDat.set("baseWalkSpd", NpcData.getInt("walkspd"));
  99. npcDat.set("baseRunSpd", NpcData.getInt("runspd"));
  100. npcDat.set("targetable", NpcData.getBoolean("targetable"));
  101. npcDat.set("show_name", NpcData.getBoolean("show_name"));
  102. // constants, until we have stats in DB
  103. npcDat.safeSet("baseSTR", NpcData.getInt("str"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  104. npcDat.safeSet("baseCON", NpcData.getInt("con"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  105. npcDat.safeSet("baseDEX", NpcData.getInt("dex"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  106. npcDat.safeSet("baseINT", NpcData.getInt("int"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  107. npcDat.safeSet("baseWIT", NpcData.getInt("wit"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  108. npcDat.safeSet("baseMEN", NpcData.getInt("men"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: " + NpcData.getInt("idTemplate"));
  109. npcDat.set("baseHpMax", NpcData.getDouble("hp"));
  110. npcDat.set("baseCpMax", 0);
  111. npcDat.set("baseMpMax", NpcData.getDouble("mp"));
  112. npcDat.set("baseHpReg", NpcData.getFloat("hpreg") > 0 ? NpcData.getFloat("hpreg") : 1.5 + ((level - 1) / 10.0));
  113. npcDat.set("baseMpReg", NpcData.getFloat("mpreg") > 0 ? NpcData.getFloat("mpreg") : 0.9 + (0.3 * ((level - 1) / 10.0)));
  114. npcDat.set("basePAtk", NpcData.getInt("patk"));
  115. npcDat.set("basePDef", NpcData.getInt("pdef"));
  116. npcDat.set("baseMAtk", NpcData.getInt("matk"));
  117. npcDat.set("baseMDef", NpcData.getInt("mdef"));
  118. npcDat.set("dropHerbGroup", NpcData.getInt("dropHerbGroup"));
  119. // Default element resists
  120. npcDat.set("baseFireRes", 20);
  121. npcDat.set("baseWindRes", 20);
  122. npcDat.set("baseWaterRes", 20);
  123. npcDat.set("baseEarthRes", 20);
  124. npcDat.set("baseHolyRes", 20);
  125. npcDat.set("baseDarkRes", 20);
  126. _npcs.put(id, new L2NpcTemplate(npcDat));
  127. }
  128. /**
  129. * @param id of the NPC to reload.
  130. */
  131. public void reloadNpc(int id)
  132. {
  133. try
  134. {
  135. // save a copy of the old data
  136. L2NpcTemplate old = getTemplate(id);
  137. TIntObjectHashMap<L2Skill> skills = new TIntObjectHashMap<>();
  138. List<L2MinionData> minions = new FastList<>();
  139. Map<QuestEventType, Quest[]> quests = new FastMap<>();
  140. List<ClassId> classIds = new FastList<>();
  141. FastList<L2DropCategory> categories = new FastList<>();
  142. if (old != null)
  143. {
  144. skills.putAll(old.getSkills());
  145. categories.addAll(old.getDropData());
  146. classIds.addAll(old.getTeachInfo());
  147. minions.addAll(old.getMinionData());
  148. if (!old.getEventQuests().isEmpty())
  149. {
  150. quests.putAll(old.getEventQuests());
  151. }
  152. }
  153. loadNpcs(id);
  154. loadNpcsSkills(id);
  155. loadNpcsDrop(id);
  156. loadNpcsSkillLearn(id);
  157. loadMinions(id);
  158. loadNpcsAI(id);
  159. loadNpcsElement(id);
  160. // restore additional data from saved copy
  161. L2NpcTemplate created = getTemplate(id);
  162. if ((old != null) && (created != null))
  163. {
  164. if (!skills.isEmpty())
  165. {
  166. for (L2Skill skill : skills.values(new L2Skill[0]))
  167. {
  168. created.addSkill(skill);
  169. }
  170. }
  171. for (ClassId classId : classIds)
  172. {
  173. created.addTeachInfo(classId);
  174. }
  175. if (!minions.isEmpty())
  176. {
  177. for (L2MinionData minion : minions)
  178. {
  179. created.addRaidData(minion);
  180. }
  181. }
  182. if (!quests.isEmpty())
  183. {
  184. created.getEventQuests().putAll(quests);
  185. }
  186. }
  187. }
  188. catch (Exception e)
  189. {
  190. _log.log(Level.WARNING, "NPCTable: Could not reload data for NPC " + id + ": " + e.getMessage(), e);
  191. }
  192. }
  193. /**
  194. * Just wrapper.
  195. */
  196. public void reloadAllNpc()
  197. {
  198. restoreNpcData();
  199. }
  200. /**
  201. * @param npc
  202. */
  203. public void saveNpc(StatsSet npc)
  204. {
  205. Map<String, Object> set = npc.getSet();
  206. int length = 0;
  207. for (Object obj : set.keySet())
  208. {
  209. // 15 is just guessed npc name length
  210. length += ((String) obj).length() + 7 + 15;
  211. }
  212. final StringBuilder sbValues = new StringBuilder(length);
  213. for (Object obj : set.keySet())
  214. {
  215. final String name = (String) obj;
  216. if (!name.equalsIgnoreCase("npcId"))
  217. {
  218. if (sbValues.length() > 0)
  219. {
  220. sbValues.append(", ");
  221. }
  222. sbValues.append(name);
  223. sbValues.append(" = '");
  224. sbValues.append(set.get(name));
  225. sbValues.append('\'');
  226. }
  227. }
  228. Connection con = null;
  229. try
  230. {
  231. con = L2DatabaseFactory.getInstance().getConnection();
  232. int updated = 0;
  233. if (Config.CUSTOM_NPC_TABLE)
  234. {
  235. final StringBuilder sbQuery = new StringBuilder(sbValues.length() + 28);
  236. sbQuery.append("UPDATE custom_npc SET ");
  237. sbQuery.append(sbValues.toString());
  238. sbQuery.append(" WHERE id = ?");
  239. PreparedStatement statement = con.prepareStatement(sbQuery.toString());
  240. statement.setInt(1, npc.getInteger("npcId"));
  241. updated = statement.executeUpdate();
  242. statement.close();
  243. }
  244. if (updated == 0)
  245. {
  246. final StringBuilder sbQuery = new StringBuilder(sbValues.length() + 28);
  247. sbQuery.append("UPDATE npc SET ");
  248. sbQuery.append(sbValues.toString());
  249. sbQuery.append(" WHERE id = ?");
  250. PreparedStatement statement = con.prepareStatement(sbQuery.toString());
  251. statement.setInt(1, npc.getInteger("npcId"));
  252. statement.executeUpdate();
  253. statement.close();
  254. }
  255. }
  256. catch (Exception e)
  257. {
  258. _log.log(Level.WARNING, "NPCTable: Could not store new NPC data in database: " + e.getMessage(), e);
  259. }
  260. finally
  261. {
  262. L2DatabaseFactory.close(con);
  263. }
  264. }
  265. /**
  266. * @param id the template Id to get.
  267. * @return the template for the given id.
  268. */
  269. public L2NpcTemplate getTemplate(int id)
  270. {
  271. return _npcs.get(id);
  272. }
  273. /**
  274. * @param name of the template to get.
  275. * @return the template for the given name.
  276. */
  277. public L2NpcTemplate getTemplateByName(String name)
  278. {
  279. for (L2NpcTemplate npcTemplate : _npcs.values(new L2NpcTemplate[0]))
  280. {
  281. if (npcTemplate.getName().equalsIgnoreCase(name))
  282. {
  283. return npcTemplate;
  284. }
  285. }
  286. return null;
  287. }
  288. /**
  289. * @param lvls of all the templates to get.
  290. * @return the template list for the given level.
  291. */
  292. public List<L2NpcTemplate> getAllOfLevel(int ...lvls)
  293. {
  294. final List<L2NpcTemplate> list = new FastList<>();
  295. for (int lvl : lvls)
  296. {
  297. for (L2NpcTemplate t : _npcs.values(new L2NpcTemplate[0]))
  298. {
  299. if (t.getLevel() == lvl)
  300. {
  301. list.add(t);
  302. }
  303. }
  304. }
  305. return list;
  306. }
  307. /**
  308. * @param lvls of all the monster templates to get.
  309. * @return the template list for the given level.
  310. */
  311. public List<L2NpcTemplate> getAllMonstersOfLevel(int ...lvls)
  312. {
  313. final List<L2NpcTemplate> list = new FastList<>();
  314. for (int lvl : lvls)
  315. {
  316. for (L2NpcTemplate t : _npcs.values(new L2NpcTemplate[0]))
  317. {
  318. if ((t.getLevel() == lvl) && t.isType("L2Monster"))
  319. {
  320. list.add(t);
  321. }
  322. }
  323. }
  324. return list;
  325. }
  326. /**
  327. * @param letters of all the NPC templates which its name start with.
  328. * @return the template list for the given letter.
  329. */
  330. public List<L2NpcTemplate> getAllNpcStartingWith(String ...letters)
  331. {
  332. final List<L2NpcTemplate> list = new FastList<>();
  333. for (String letter : letters)
  334. {
  335. for (L2NpcTemplate t : _npcs.values(new L2NpcTemplate[0]))
  336. {
  337. if (t.getName().startsWith(letter) && t.isType("L2Npc"))
  338. {
  339. list.add(t);
  340. }
  341. }
  342. }
  343. return list;
  344. }
  345. /**
  346. * @param classTypes of all the templates to get.
  347. * @return the template list for the given class type.
  348. */
  349. public List<L2NpcTemplate> getAllNpcOfClassType(String ...classTypes)
  350. {
  351. final List<L2NpcTemplate> list = new FastList<>();
  352. for (String classType : classTypes)
  353. {
  354. for (L2NpcTemplate t : _npcs.values(new L2NpcTemplate[0]))
  355. {
  356. if (t.isType(classType))
  357. {
  358. list.add(t);
  359. }
  360. }
  361. }
  362. return list;
  363. }
  364. /**
  365. * Id equals to zero or lesser means all.
  366. * @param id of the NPC to load.
  367. */
  368. public void loadNpcs(int id)
  369. {
  370. Connection con = null;
  371. try
  372. {
  373. con = L2DatabaseFactory.getInstance().getConnection();
  374. PreparedStatement statement = null;
  375. if (id > 0)
  376. {
  377. statement = con.prepareStatement("SELECT * FROM npc WHERE id = ?");
  378. statement.setInt(1, id);
  379. }
  380. else
  381. {
  382. statement = con.prepareStatement("SELECT * FROM npc ORDER BY id");
  383. }
  384. ResultSet rset = statement.executeQuery();
  385. int cont = 0;
  386. int cCont = 0;
  387. while (rset.next())
  388. {
  389. fillNpcTable(rset);
  390. cont++;
  391. }
  392. rset.close();
  393. statement.close();
  394. if (Config.CUSTOM_NPC_TABLE)
  395. {
  396. if (id > 0)
  397. {
  398. statement = con.prepareStatement("SELECT * FROM custom_npc WHERE id = ?");
  399. statement.setInt(1, id);
  400. }
  401. else
  402. {
  403. statement = con.prepareStatement("SELECT * FROM custom_npc ORDER BY id");
  404. }
  405. rset = statement.executeQuery();
  406. while (rset.next())
  407. {
  408. fillNpcTable(rset);
  409. cCont++;
  410. }
  411. rset.close();
  412. statement.close();
  413. }
  414. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCont + ") NPC template(s).");
  415. }
  416. catch (Exception e)
  417. {
  418. _log.log(Level.SEVERE, "NPCTable: Error creating NPC table.", e);
  419. }
  420. finally
  421. {
  422. L2DatabaseFactory.close(con);
  423. }
  424. }
  425. /**
  426. * Id equals to zero or lesser means all.
  427. * @param id of the NPC to load it's skills.
  428. */
  429. public void loadNpcsSkills(int id)
  430. {
  431. Connection con = null;
  432. try
  433. {
  434. con = L2DatabaseFactory.getInstance().getConnection();
  435. PreparedStatement statement = null;
  436. if (id > 0)
  437. {
  438. statement = con.prepareStatement("SELECT * FROM npcskills WHERE npcid = ?");
  439. statement.setInt(1, id);
  440. }
  441. else
  442. {
  443. statement = con.prepareStatement("SELECT * FROM npcskills ORDER BY npcid");
  444. }
  445. ResultSet rset = statement.executeQuery();
  446. int cont = 0;
  447. int cCont = 0;
  448. L2NpcTemplate npcDat = null;
  449. L2Skill npcSkill = null;
  450. while (rset.next())
  451. {
  452. int mobId = rset.getInt("npcid");
  453. npcDat = _npcs.get(mobId);
  454. if (npcDat == null)
  455. {
  456. _log.warning("NPCTable: Skill data for undefined NPC. npcId: " + mobId);
  457. continue;
  458. }
  459. int skillId = rset.getInt("skillid");
  460. int level = rset.getInt("level");
  461. if (skillId == L2Skill.SKILL_NPC_RACE)
  462. {
  463. npcDat.setRace(level);
  464. continue;
  465. }
  466. npcSkill = SkillTable.getInstance().getInfo(skillId, level);
  467. if (npcSkill == null)
  468. {
  469. continue;
  470. }
  471. cont++;
  472. npcDat.addSkill(npcSkill);
  473. }
  474. rset.close();
  475. statement.close();
  476. if (Config.CUSTOM_NPC_SKILLS_TABLE)
  477. {
  478. if (id > 0)
  479. {
  480. statement = con.prepareStatement("SELECT * FROM custom_npcskills WHERE npcid = ?");
  481. statement.setInt(1, id);
  482. }
  483. else
  484. {
  485. statement = con.prepareStatement("SELECT * FROM custom_npcskills ORDER BY npcid");
  486. }
  487. rset = statement.executeQuery();
  488. while (rset.next())
  489. {
  490. int mobId = rset.getInt("npcid");
  491. npcDat = _npcs.get(mobId);
  492. if (npcDat == null)
  493. {
  494. _log.warning("Custom NPCTable: Skill data for undefined NPC. npcId: " + mobId);
  495. continue;
  496. }
  497. int skillId = rset.getInt("skillid");
  498. int level = rset.getInt("level");
  499. if ((npcDat.getRace() == null) && (skillId == L2Skill.SKILL_NPC_RACE))
  500. {
  501. npcDat.setRace(level);
  502. continue;
  503. }
  504. npcSkill = SkillTable.getInstance().getInfo(skillId, level);
  505. if (npcSkill == null)
  506. {
  507. continue;
  508. }
  509. cCont++;
  510. npcDat.addSkill(npcSkill);
  511. }
  512. rset.close();
  513. statement.close();
  514. }
  515. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCont + ") npc skills.");
  516. }
  517. catch (Exception e)
  518. {
  519. _log.log(Level.SEVERE, "NPCTable: Error reading NPC skills table.", e);
  520. }
  521. finally
  522. {
  523. L2DatabaseFactory.close(con);
  524. }
  525. }
  526. /**
  527. * Id equals to zero or lesser means all.
  528. * @param id of the NPC to load it's drops.
  529. */
  530. public void loadNpcsDrop(int id)
  531. {
  532. Connection con = null;
  533. try
  534. {
  535. con = L2DatabaseFactory.getInstance().getConnection();
  536. PreparedStatement statement = null;
  537. if (id > 0)
  538. {
  539. statement = con.prepareStatement("SELECT * FROM droplist WHERE mobId = ? ORDER BY mobId, chance DESC");
  540. statement.setInt(1, id);
  541. }
  542. else
  543. {
  544. statement = con.prepareStatement("SELECT * FROM droplist ORDER BY mobId, chance DESC");
  545. }
  546. ResultSet rset = statement.executeQuery();
  547. L2DropData dropDat = null;
  548. L2NpcTemplate npcDat = null;
  549. int cont = 0;
  550. int cCont = 0;
  551. while (rset.next())
  552. {
  553. int mobId = rset.getInt("mobId");
  554. npcDat = _npcs.get(mobId);
  555. if (npcDat == null)
  556. {
  557. _log.warning("NPCTable: Drop data for undefined NPC. npcId: " + mobId);
  558. continue;
  559. }
  560. dropDat = new L2DropData();
  561. dropDat.setItemId(rset.getInt("itemId"));
  562. dropDat.setMinDrop(rset.getInt("min"));
  563. dropDat.setMaxDrop(rset.getInt("max"));
  564. dropDat.setChance(rset.getInt("chance"));
  565. int category = rset.getInt("category");
  566. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  567. {
  568. _log.warning("Drop data for undefined item template! NpcId: " + mobId + " itemId: " + dropDat.getItemId());
  569. continue;
  570. }
  571. cont++;
  572. npcDat.addDropData(dropDat, category);
  573. }
  574. rset.close();
  575. statement.close();
  576. if (Config.CUSTOM_DROPLIST_TABLE)
  577. {
  578. if (id > 0)
  579. {
  580. statement = con.prepareStatement("SELECT * FROM custom_droplist WHERE mobId = ? ORDER BY mobId, chance DESC");
  581. statement.setInt(1, id);
  582. }
  583. else
  584. {
  585. statement = con.prepareStatement("SELECT * FROM custom_droplist ORDER BY mobId, chance DESC");
  586. }
  587. rset = statement.executeQuery();
  588. while (rset.next())
  589. {
  590. int mobId = rset.getInt("mobId");
  591. npcDat = _npcs.get(mobId);
  592. if (npcDat == null)
  593. {
  594. _log.warning("NPCTable: CUSTOM DROPLIST: Drop data for undefined NPC. npcId: " + mobId);
  595. continue;
  596. }
  597. dropDat = new L2DropData();
  598. dropDat.setItemId(rset.getInt("itemId"));
  599. dropDat.setMinDrop(rset.getInt("min"));
  600. dropDat.setMaxDrop(rset.getInt("max"));
  601. dropDat.setChance(rset.getInt("chance"));
  602. int category = rset.getInt("category");
  603. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  604. {
  605. _log.warning("Custom drop data for undefined item template! NpcId: " + mobId + " itemId: " + dropDat.getItemId());
  606. continue;
  607. }
  608. npcDat.addDropData(dropDat, category);
  609. cCont++;
  610. }
  611. rset.close();
  612. statement.close();
  613. }
  614. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCont + ") drops.");
  615. }
  616. catch (Exception e)
  617. {
  618. _log.log(Level.SEVERE, "NPCTable: Error reading NPC dropdata. ", e);
  619. }
  620. finally
  621. {
  622. L2DatabaseFactory.close(con);
  623. }
  624. }
  625. /**
  626. * Id equals to zero or lesser means all.
  627. * @param id of the NPC to load it's skill learn list.
  628. */
  629. private void loadNpcsSkillLearn(int id)
  630. {
  631. Connection con = null;
  632. try
  633. {
  634. con = L2DatabaseFactory.getInstance().getConnection();
  635. PreparedStatement statement = null;
  636. if (id > 0)
  637. {
  638. statement = con.prepareStatement("SELECT * FROM skill_learn WHERE npc_id = ?");
  639. statement.setInt(1, id);
  640. }
  641. else
  642. {
  643. statement = con.prepareStatement("SELECT * FROM skill_learn");
  644. }
  645. ResultSet rset = statement.executeQuery();
  646. int cont = 0;
  647. while (rset.next())
  648. {
  649. int npcId = rset.getInt("npc_id");
  650. int classId = rset.getInt("class_id");
  651. L2NpcTemplate npc = getTemplate(npcId);
  652. if (npc == null)
  653. {
  654. _log.warning("NPCTable: Error getting NPC template ID " + npcId + " while trying to load skill trainer data.");
  655. continue;
  656. }
  657. cont++;
  658. npc.addTeachInfo(ClassId.values()[classId]);
  659. }
  660. rset.close();
  661. statement.close();
  662. _log.info("NpcTable: Loaded " + cont + " Skill Learn.");
  663. }
  664. catch (Exception e)
  665. {
  666. _log.log(Level.SEVERE, "NPCTable: Error reading NPC trainer data.", e);
  667. }
  668. finally
  669. {
  670. L2DatabaseFactory.close(con);
  671. }
  672. }
  673. /**
  674. * Id equals to zero or lesser means all.
  675. * @param id of the NPC to load it's minions.
  676. */
  677. public void loadMinions(int id)
  678. {
  679. Connection con = null;
  680. try
  681. {
  682. con = L2DatabaseFactory.getInstance().getConnection();
  683. PreparedStatement statement = null;
  684. if (id > 0)
  685. {
  686. statement = con.prepareStatement("SELECT * FROM minions WHERE boss_id = ?");
  687. statement.setInt(1, id);
  688. }
  689. else
  690. {
  691. statement = con.prepareStatement("SELECT * FROM minions ORDER BY boss_id");
  692. }
  693. ResultSet rset = statement.executeQuery();
  694. L2MinionData minionDat = null;
  695. L2NpcTemplate npcDat = null;
  696. int cnt = 0;
  697. while (rset.next())
  698. {
  699. int raidId = rset.getInt("boss_id");
  700. npcDat = _npcs.get(raidId);
  701. if (npcDat == null)
  702. {
  703. _log.warning("Minion references undefined boss NPC. Boss NpcId: " + raidId);
  704. continue;
  705. }
  706. minionDat = new L2MinionData();
  707. minionDat.setMinionId(rset.getInt("minion_id"));
  708. minionDat.setAmountMin(rset.getInt("amount_min"));
  709. minionDat.setAmountMax(rset.getInt("amount_max"));
  710. npcDat.addRaidData(minionDat);
  711. cnt++;
  712. }
  713. rset.close();
  714. statement.close();
  715. _log.info("NpcTable: Loaded " + cnt + " Minions.");
  716. }
  717. catch (Exception e)
  718. {
  719. _log.log(Level.SEVERE, "NPCTable: Error loading minion data.", e);
  720. }
  721. finally
  722. {
  723. L2DatabaseFactory.close(con);
  724. }
  725. }
  726. /**
  727. * Id equals to zero or lesser means all.
  728. * @param id of the NPC to load it's AI data.
  729. */
  730. public void loadNpcsAI(int id)
  731. {
  732. Connection con = null;
  733. try
  734. {
  735. con = L2DatabaseFactory.getInstance().getConnection();
  736. PreparedStatement statement = null;
  737. if (id > 0)
  738. {
  739. statement = con.prepareStatement("SELECT * FROM npcaidata WHERE npcId = ?");
  740. statement.setInt(1, id);
  741. }
  742. else
  743. {
  744. statement = con.prepareStatement("SELECT * FROM npcaidata ORDER BY npcId");
  745. }
  746. ResultSet rset = statement.executeQuery();
  747. L2NpcAIData npcAIDat = null;
  748. L2NpcTemplate npcDat = null;
  749. int cont = 0;
  750. int cCont = 0;
  751. while (rset.next())
  752. {
  753. int npc_id = rset.getInt("npcId");
  754. npcDat = _npcs.get(npc_id);
  755. if (npcDat == null)
  756. {
  757. _log.severe("NPCTable: AI Data Error with id : " + npc_id);
  758. continue;
  759. }
  760. npcAIDat = new L2NpcAIData();
  761. npcAIDat.setPrimarySkillId(rset.getInt("primarySkillId"));
  762. npcAIDat.setMinSkillChance(rset.getInt("minSkillChance"));
  763. npcAIDat.setMaxSkillChance(rset.getInt("maxSkillChance"));
  764. npcAIDat.setCanMove(rset.getInt("canMove"));
  765. npcAIDat.setSoulShot(rset.getInt("soulshot"));
  766. npcAIDat.setSpiritShot(rset.getInt("spiritshot"));
  767. npcAIDat.setSoulShotChance(rset.getInt("ssChance"));
  768. npcAIDat.setSpiritShotChance(rset.getInt("spsChance"));
  769. npcAIDat.setIsChaos(rset.getInt("isChaos"));
  770. npcAIDat.setShortRangeSkill(rset.getInt("minRangeSkill"));
  771. npcAIDat.setShortRangeChance(rset.getInt("minRangeChance"));
  772. npcAIDat.setLongRangeSkill(rset.getInt("maxRangeSkill"));
  773. npcAIDat.setLongRangeChance(rset.getInt("maxRangeChance"));
  774. npcAIDat.setClan(rset.getString("clan"));
  775. npcAIDat.setClanRange(rset.getInt("clanRange"));
  776. npcAIDat.setEnemyClan(rset.getString("enemyClan"));
  777. npcAIDat.setEnemyRange(rset.getInt("enemyRange"));
  778. npcAIDat.setDodge(rset.getInt("dodge"));
  779. npcAIDat.setAi(rset.getString("aiType"));
  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. }