NpcTable.java 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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.valueCollection())
  280. {
  281. if (npcTemplate.getName().equalsIgnoreCase(name))
  282. {
  283. return npcTemplate;
  284. }
  285. }
  286. return null;
  287. }
  288. /**
  289. * @param lvl of all the templates to get.
  290. * @return the template list for the given level.
  291. */
  292. public List<L2NpcTemplate> getAllOfLevel(int lvl)
  293. {
  294. final List<L2NpcTemplate> list = new FastList<>();
  295. for (L2NpcTemplate t : _npcs.valueCollection())
  296. {
  297. if (t.getLevel() == lvl)
  298. {
  299. list.add(t);
  300. }
  301. }
  302. return list;
  303. }
  304. /**
  305. * @param lvl of all the monster templates to get.
  306. * @return the template list for the given level.
  307. */
  308. public List<L2NpcTemplate> getAllMonstersOfLevel(int lvl)
  309. {
  310. final List<L2NpcTemplate> list = new FastList<>();
  311. for (L2NpcTemplate t : _npcs.valueCollection())
  312. {
  313. if ((t.getLevel() == lvl) && t.isType("L2Monster"))
  314. {
  315. list.add(t);
  316. }
  317. }
  318. return list;
  319. }
  320. /**
  321. * @param letter of all the NPC templates which its name start with.
  322. * @return the template list for the given letter.
  323. */
  324. public List<L2NpcTemplate> getAllNpcStartingWith(String letter)
  325. {
  326. final List<L2NpcTemplate> list = new FastList<>();
  327. for (L2NpcTemplate t : _npcs.valueCollection())
  328. {
  329. if (t.getName().startsWith(letter) && t.isType("L2Npc"))
  330. {
  331. list.add(t);
  332. }
  333. }
  334. return list;
  335. }
  336. /**
  337. * @param classType of all the templates to get.
  338. * @return the template list for the given class type.
  339. */
  340. public List<L2NpcTemplate> getAllNpcOfClassType(String classType)
  341. {
  342. final List<L2NpcTemplate> list = new FastList<>();
  343. for (L2NpcTemplate t : _npcs.valueCollection())
  344. {
  345. if (t.isType(classType))
  346. {
  347. list.add(t);
  348. }
  349. }
  350. return list;
  351. }
  352. /**
  353. * Id equals to zero or lesser means all.
  354. * @param id of the NPC to load.
  355. */
  356. public void loadNpcs(int id)
  357. {
  358. Connection con = null;
  359. try
  360. {
  361. con = L2DatabaseFactory.getInstance().getConnection();
  362. PreparedStatement statement = null;
  363. if (id > 0)
  364. {
  365. statement = con.prepareStatement("SELECT * FROM npc WHERE id = ?");
  366. statement.setInt(1, id);
  367. }
  368. else
  369. {
  370. statement = con.prepareStatement("SELECT * FROM npc ORDER BY id");
  371. }
  372. ResultSet rset = statement.executeQuery();
  373. int cont = 0;
  374. int cCont = 0;
  375. while (rset.next())
  376. {
  377. fillNpcTable(rset);
  378. cont++;
  379. }
  380. rset.close();
  381. statement.close();
  382. if (Config.CUSTOM_NPC_TABLE)
  383. {
  384. if (id > 0)
  385. {
  386. statement = con.prepareStatement("SELECT * FROM custom_npc WHERE id = ?");
  387. statement.setInt(1, id);
  388. }
  389. else
  390. {
  391. statement = con.prepareStatement("SELECT * FROM custom_npc ORDER BY id");
  392. }
  393. rset = statement.executeQuery();
  394. while (rset.next())
  395. {
  396. fillNpcTable(rset);
  397. cCont++;
  398. }
  399. rset.close();
  400. statement.close();
  401. }
  402. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCont + ") NPC template(s).");
  403. }
  404. catch (Exception e)
  405. {
  406. _log.log(Level.SEVERE, "NPCTable: Error creating NPC table.", e);
  407. }
  408. finally
  409. {
  410. L2DatabaseFactory.close(con);
  411. }
  412. }
  413. /**
  414. * Id equals to zero or lesser means all.
  415. * @param id of the NPC to load it's skills.
  416. */
  417. public void loadNpcsSkills(int id)
  418. {
  419. Connection con = null;
  420. try
  421. {
  422. con = L2DatabaseFactory.getInstance().getConnection();
  423. PreparedStatement statement = null;
  424. if (id > 0)
  425. {
  426. statement = con.prepareStatement("SELECT * FROM npcskills WHERE npcid = ?");
  427. statement.setInt(1, id);
  428. }
  429. else
  430. {
  431. statement = con.prepareStatement("SELECT * FROM npcskills ORDER BY npcid");
  432. }
  433. ResultSet rset = statement.executeQuery();
  434. int cont = 0;
  435. int cCont = 0;
  436. L2NpcTemplate npcDat = null;
  437. L2Skill npcSkill = null;
  438. while (rset.next())
  439. {
  440. int mobId = rset.getInt("npcid");
  441. npcDat = _npcs.get(mobId);
  442. if (npcDat == null)
  443. {
  444. _log.warning("NPCTable: Skill data for undefined NPC. npcId: " + mobId);
  445. continue;
  446. }
  447. int skillId = rset.getInt("skillid");
  448. int level = rset.getInt("level");
  449. if (skillId == L2Skill.SKILL_NPC_RACE)
  450. {
  451. npcDat.setRace(level);
  452. continue;
  453. }
  454. npcSkill = SkillTable.getInstance().getInfo(skillId, level);
  455. if (npcSkill == null)
  456. {
  457. continue;
  458. }
  459. cont++;
  460. npcDat.addSkill(npcSkill);
  461. }
  462. rset.close();
  463. statement.close();
  464. if (Config.CUSTOM_NPC_SKILLS_TABLE)
  465. {
  466. if (id > 0)
  467. {
  468. statement = con.prepareStatement("SELECT * FROM custom_npcskills WHERE npcid = ?");
  469. statement.setInt(1, id);
  470. }
  471. else
  472. {
  473. statement = con.prepareStatement("SELECT * FROM custom_npcskills ORDER BY npcid");
  474. }
  475. rset = statement.executeQuery();
  476. while (rset.next())
  477. {
  478. int mobId = rset.getInt("npcid");
  479. npcDat = _npcs.get(mobId);
  480. if (npcDat == null)
  481. {
  482. _log.warning("Custom NPCTable: Skill data for undefined NPC. npcId: " + mobId);
  483. continue;
  484. }
  485. int skillId = rset.getInt("skillid");
  486. int level = rset.getInt("level");
  487. if ((npcDat.getRace() == null) && (skillId == L2Skill.SKILL_NPC_RACE))
  488. {
  489. npcDat.setRace(level);
  490. continue;
  491. }
  492. npcSkill = SkillTable.getInstance().getInfo(skillId, level);
  493. if (npcSkill == null)
  494. {
  495. continue;
  496. }
  497. cCont++;
  498. npcDat.addSkill(npcSkill);
  499. }
  500. rset.close();
  501. statement.close();
  502. }
  503. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCont + ") npc skills.");
  504. }
  505. catch (Exception e)
  506. {
  507. _log.log(Level.SEVERE, "NPCTable: Error reading NPC skills table.", e);
  508. }
  509. finally
  510. {
  511. L2DatabaseFactory.close(con);
  512. }
  513. }
  514. /**
  515. * Id equals to zero or lesser means all.
  516. * @param id of the NPC to load it's drops.
  517. */
  518. public void loadNpcsDrop(int id)
  519. {
  520. Connection con = null;
  521. try
  522. {
  523. con = L2DatabaseFactory.getInstance().getConnection();
  524. PreparedStatement statement = null;
  525. if (id > 0)
  526. {
  527. statement = con.prepareStatement("SELECT * FROM droplist WHERE mobId = ? ORDER BY mobId, chance DESC");
  528. statement.setInt(1, id);
  529. }
  530. else
  531. {
  532. statement = con.prepareStatement("SELECT * FROM droplist ORDER BY mobId, chance DESC");
  533. }
  534. ResultSet rset = statement.executeQuery();
  535. L2DropData dropDat = null;
  536. L2NpcTemplate npcDat = null;
  537. int cont = 0;
  538. int cCont = 0;
  539. while (rset.next())
  540. {
  541. int mobId = rset.getInt("mobId");
  542. npcDat = _npcs.get(mobId);
  543. if (npcDat == null)
  544. {
  545. _log.warning("NPCTable: Drop data for undefined NPC. npcId: " + mobId);
  546. continue;
  547. }
  548. dropDat = new L2DropData();
  549. dropDat.setItemId(rset.getInt("itemId"));
  550. dropDat.setMinDrop(rset.getInt("min"));
  551. dropDat.setMaxDrop(rset.getInt("max"));
  552. dropDat.setChance(rset.getInt("chance"));
  553. int category = rset.getInt("category");
  554. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  555. {
  556. _log.warning("Drop data for undefined item template! NpcId: " + mobId + " itemId: " + dropDat.getItemId());
  557. continue;
  558. }
  559. cont++;
  560. npcDat.addDropData(dropDat, category);
  561. }
  562. rset.close();
  563. statement.close();
  564. if (Config.CUSTOM_DROPLIST_TABLE)
  565. {
  566. if (id > 0)
  567. {
  568. statement = con.prepareStatement("SELECT * FROM custom_droplist WHERE mobId = ? ORDER BY mobId, chance DESC");
  569. statement.setInt(1, id);
  570. }
  571. else
  572. {
  573. statement = con.prepareStatement("SELECT * FROM custom_droplist ORDER BY mobId, chance DESC");
  574. }
  575. rset = statement.executeQuery();
  576. while (rset.next())
  577. {
  578. int mobId = rset.getInt("mobId");
  579. npcDat = _npcs.get(mobId);
  580. if (npcDat == null)
  581. {
  582. _log.warning("NPCTable: CUSTOM DROPLIST: Drop data for undefined NPC. npcId: " + mobId);
  583. continue;
  584. }
  585. dropDat = new L2DropData();
  586. dropDat.setItemId(rset.getInt("itemId"));
  587. dropDat.setMinDrop(rset.getInt("min"));
  588. dropDat.setMaxDrop(rset.getInt("max"));
  589. dropDat.setChance(rset.getInt("chance"));
  590. int category = rset.getInt("category");
  591. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  592. {
  593. _log.warning("Custom drop data for undefined item template! NpcId: " + mobId + " itemId: " + dropDat.getItemId());
  594. continue;
  595. }
  596. npcDat.addDropData(dropDat, category);
  597. cCont++;
  598. }
  599. rset.close();
  600. statement.close();
  601. }
  602. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCont + ") drops.");
  603. }
  604. catch (Exception e)
  605. {
  606. _log.log(Level.SEVERE, "NPCTable: Error reading NPC dropdata. ", e);
  607. }
  608. finally
  609. {
  610. L2DatabaseFactory.close(con);
  611. }
  612. }
  613. /**
  614. * Id equals to zero or lesser means all.
  615. * @param id of the NPC to load it's skill learn list.
  616. */
  617. private void loadNpcsSkillLearn(int id)
  618. {
  619. Connection con = null;
  620. try
  621. {
  622. con = L2DatabaseFactory.getInstance().getConnection();
  623. PreparedStatement statement = null;
  624. if (id > 0)
  625. {
  626. statement = con.prepareStatement("SELECT * FROM skill_learn WHERE npc_id = ?");
  627. statement.setInt(1, id);
  628. }
  629. else
  630. {
  631. statement = con.prepareStatement("SELECT * FROM skill_learn");
  632. }
  633. ResultSet rset = statement.executeQuery();
  634. int cont = 0;
  635. while (rset.next())
  636. {
  637. int npcId = rset.getInt("npc_id");
  638. int classId = rset.getInt("class_id");
  639. L2NpcTemplate npc = getTemplate(npcId);
  640. if (npc == null)
  641. {
  642. _log.warning("NPCTable: Error getting NPC template ID " + npcId + " while trying to load skill trainer data.");
  643. continue;
  644. }
  645. cont++;
  646. npc.addTeachInfo(ClassId.values()[classId]);
  647. }
  648. rset.close();
  649. statement.close();
  650. _log.info("NpcTable: Loaded " + cont + " Skill Learn.");
  651. }
  652. catch (Exception e)
  653. {
  654. _log.log(Level.SEVERE, "NPCTable: Error reading NPC trainer data.", e);
  655. }
  656. finally
  657. {
  658. L2DatabaseFactory.close(con);
  659. }
  660. }
  661. /**
  662. * Id equals to zero or lesser means all.
  663. * @param id of the NPC to load it's minions.
  664. */
  665. public void loadMinions(int id)
  666. {
  667. Connection con = null;
  668. try
  669. {
  670. con = L2DatabaseFactory.getInstance().getConnection();
  671. PreparedStatement statement = null;
  672. if (id > 0)
  673. {
  674. statement = con.prepareStatement("SELECT * FROM minions WHERE boss_id = ?");
  675. statement.setInt(1, id);
  676. }
  677. else
  678. {
  679. statement = con.prepareStatement("SELECT * FROM minions ORDER BY boss_id");
  680. }
  681. ResultSet rset = statement.executeQuery();
  682. L2MinionData minionDat = null;
  683. L2NpcTemplate npcDat = null;
  684. int cnt = 0;
  685. while (rset.next())
  686. {
  687. int raidId = rset.getInt("boss_id");
  688. npcDat = _npcs.get(raidId);
  689. if (npcDat == null)
  690. {
  691. _log.warning("Minion references undefined boss NPC. Boss NpcId: " + raidId);
  692. continue;
  693. }
  694. minionDat = new L2MinionData();
  695. minionDat.setMinionId(rset.getInt("minion_id"));
  696. minionDat.setAmountMin(rset.getInt("amount_min"));
  697. minionDat.setAmountMax(rset.getInt("amount_max"));
  698. npcDat.addRaidData(minionDat);
  699. cnt++;
  700. }
  701. rset.close();
  702. statement.close();
  703. _log.info("NpcTable: Loaded " + cnt + " Minions.");
  704. }
  705. catch (Exception e)
  706. {
  707. _log.log(Level.SEVERE, "NPCTable: Error loading minion data.", e);
  708. }
  709. finally
  710. {
  711. L2DatabaseFactory.close(con);
  712. }
  713. }
  714. /**
  715. * Id equals to zero or lesser means all.
  716. * @param id of the NPC to load it's AI data.
  717. */
  718. public void loadNpcsAI(int id)
  719. {
  720. Connection con = null;
  721. try
  722. {
  723. con = L2DatabaseFactory.getInstance().getConnection();
  724. PreparedStatement statement = null;
  725. if (id > 0)
  726. {
  727. statement = con.prepareStatement("SELECT * FROM npcaidata WHERE npcId = ?");
  728. statement.setInt(1, id);
  729. }
  730. else
  731. {
  732. statement = con.prepareStatement("SELECT * FROM npcaidata ORDER BY npcId");
  733. }
  734. ResultSet rset = statement.executeQuery();
  735. L2NpcAIData npcAIDat = null;
  736. L2NpcTemplate npcDat = null;
  737. int cont = 0;
  738. int cCont = 0;
  739. while (rset.next())
  740. {
  741. int npc_id = rset.getInt("npcId");
  742. npcDat = _npcs.get(npc_id);
  743. if (npcDat == null)
  744. {
  745. _log.severe("NPCTable: AI Data Error with id : " + npc_id);
  746. continue;
  747. }
  748. npcAIDat = new L2NpcAIData();
  749. npcAIDat.setPrimarySkillId(rset.getInt("primarySkillId"));
  750. npcAIDat.setMinSkillChance(rset.getInt("minSkillChance"));
  751. npcAIDat.setMaxSkillChance(rset.getInt("maxSkillChance"));
  752. npcAIDat.setCanMove(rset.getInt("canMove"));
  753. npcAIDat.setSoulShot(rset.getInt("soulshot"));
  754. npcAIDat.setSpiritShot(rset.getInt("spiritshot"));
  755. npcAIDat.setSoulShotChance(rset.getInt("ssChance"));
  756. npcAIDat.setSpiritShotChance(rset.getInt("spsChance"));
  757. npcAIDat.setIsChaos(rset.getInt("isChaos"));
  758. npcAIDat.setShortRangeSkill(rset.getInt("minRangeSkill"));
  759. npcAIDat.setShortRangeChance(rset.getInt("minRangeChance"));
  760. npcAIDat.setLongRangeSkill(rset.getInt("maxRangeSkill"));
  761. npcAIDat.setLongRangeChance(rset.getInt("maxRangeChance"));
  762. npcAIDat.setClan(rset.getString("clan"));
  763. npcAIDat.setClanRange(rset.getInt("clanRange"));
  764. npcAIDat.setEnemyClan(rset.getString("enemyClan"));
  765. npcAIDat.setEnemyRange(rset.getInt("enemyRange"));
  766. npcAIDat.setDodge(rset.getInt("dodge"));
  767. npcAIDat.setAi(rset.getString("aiType"));
  768. npcDat.setAIData(npcAIDat);
  769. cont++;
  770. }
  771. rset.close();
  772. statement.close();
  773. if (Config.CUSTOM_NPC_TABLE)
  774. {
  775. if (id > 0)
  776. {
  777. statement = con.prepareStatement("SELECT * FROM custom_npcaidata WHERE npcId = ?");
  778. statement.setInt(1, id);
  779. }
  780. else
  781. {
  782. statement = con.prepareStatement("SELECT * FROM custom_npcaidata ORDER BY npcId");
  783. }
  784. rset = statement.executeQuery();
  785. while (rset.next())
  786. {
  787. int npc_id = rset.getInt("npcId");
  788. npcDat = _npcs.get(npc_id);
  789. if (npcDat == null)
  790. {
  791. _log.severe("NPCTable: Custom AI Data Error with id : " + npc_id);
  792. continue;
  793. }
  794. npcAIDat = new L2NpcAIData();
  795. npcAIDat.setPrimarySkillId(rset.getInt("primarySkillId"));
  796. npcAIDat.setMinSkillChance(rset.getInt("minSkillChance"));
  797. npcAIDat.setMaxSkillChance(rset.getInt("maxSkillChance"));
  798. npcAIDat.setCanMove(rset.getInt("canMove"));
  799. npcAIDat.setSoulShot(rset.getInt("soulshot"));
  800. npcAIDat.setSpiritShot(rset.getInt("spiritshot"));
  801. npcAIDat.setSoulShotChance(rset.getInt("ssChance"));
  802. npcAIDat.setSpiritShotChance(rset.getInt("spsChance"));
  803. npcAIDat.setIsChaos(rset.getInt("isChaos"));
  804. npcAIDat.setShortRangeSkill(rset.getInt("minRangeSkill"));
  805. npcAIDat.setShortRangeChance(rset.getInt("minRangeChance"));
  806. npcAIDat.setLongRangeSkill(rset.getInt("maxRangeSkill"));
  807. npcAIDat.setLongRangeChance(rset.getInt("maxRangeChance"));
  808. npcAIDat.setClan(rset.getString("clan"));
  809. npcAIDat.setClanRange(rset.getInt("clanRange"));
  810. npcAIDat.setEnemyClan(rset.getString("enemyClan"));
  811. npcAIDat.setEnemyRange(rset.getInt("enemyRange"));
  812. npcAIDat.setDodge(rset.getInt("dodge"));
  813. npcAIDat.setAi(rset.getString("aiType"));
  814. npcDat.setAIData(npcAIDat);
  815. cCont++;
  816. }
  817. rset.close();
  818. statement.close();
  819. }
  820. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCont + ") AI Data.");
  821. }
  822. catch (Exception e)
  823. {
  824. _log.log(Level.SEVERE, "NPCTable: Error reading NPC AI Data: " + e.getMessage(), e);
  825. }
  826. finally
  827. {
  828. L2DatabaseFactory.close(con);
  829. }
  830. }
  831. /**
  832. * Id equals to zero or lesser means all.
  833. * @param id of the NPC to load it's element data.
  834. */
  835. public void loadNpcsElement(int id)
  836. {
  837. Connection con = null;
  838. try
  839. {
  840. con = L2DatabaseFactory.getInstance().getConnection();
  841. PreparedStatement statement = null;
  842. if (id > 0)
  843. {
  844. statement = con.prepareStatement("SELECT * FROM npc_elementals WHERE npc_id = ?");
  845. statement.setInt(1, id);
  846. }
  847. else
  848. {
  849. statement = con.prepareStatement("SELECT * FROM npc_elementals ORDER BY npc_id");
  850. }
  851. ResultSet rset = statement.executeQuery();
  852. L2NpcTemplate npcDat = null;
  853. int cont = 0;
  854. int cCount = 0;
  855. while (rset.next())
  856. {
  857. int npc_id = rset.getInt("npc_id");
  858. npcDat = _npcs.get(npc_id);
  859. if (npcDat == null)
  860. {
  861. _log.severe("NPCElementals: Elementals Error with id : " + npc_id);
  862. continue;
  863. }
  864. switch (rset.getByte("elemAtkType"))
  865. {
  866. case Elementals.FIRE:
  867. npcDat.setBaseFire(rset.getInt("elemAtkValue"));
  868. break;
  869. case Elementals.WATER:
  870. npcDat.setBaseWater(rset.getInt("elemAtkValue"));
  871. break;
  872. case Elementals.EARTH:
  873. npcDat.setBaseEarth(rset.getInt("elemAtkValue"));
  874. break;
  875. case Elementals.WIND:
  876. npcDat.setBaseWind(rset.getInt("elemAtkValue"));
  877. break;
  878. case Elementals.HOLY:
  879. npcDat.setBaseHoly(rset.getInt("elemAtkValue"));
  880. break;
  881. case Elementals.DARK:
  882. npcDat.setBaseDark(rset.getInt("elemAtkValue"));
  883. break;
  884. default:
  885. _log.severe("NPCElementals: Elementals Error with id : " + npc_id + "; unknown elementType: " + rset.getByte("elemAtkType"));
  886. continue;
  887. }
  888. npcDat.setBaseFireRes(rset.getInt("fireDefValue"));
  889. npcDat.setBaseWaterRes(rset.getInt("waterDefValue"));
  890. npcDat.setBaseEarthRes(rset.getInt("earthDefValue"));
  891. npcDat.setBaseWindRes(rset.getInt("windDefValue"));
  892. npcDat.setBaseHolyRes(rset.getInt("holyDefValue"));
  893. npcDat.setBaseDarkRes(rset.getInt("darkDefValue"));
  894. cont++;
  895. }
  896. rset.close();
  897. statement.close();
  898. if (Config.CUSTOM_NPC_TABLE)
  899. {
  900. if (id > 0)
  901. {
  902. statement = con.prepareStatement("SELECT * FROM custom_npc_elementals WHERE npc_id = ?");
  903. statement.setInt(1, id);
  904. }
  905. else
  906. {
  907. statement = con.prepareStatement("SELECT * FROM custom_npc_elementals ORDER BY npc_id");
  908. }
  909. rset = statement.executeQuery();
  910. while (rset.next())
  911. {
  912. int npc_id = rset.getInt("npc_id");
  913. npcDat = _npcs.get(npc_id);
  914. if (npcDat == null)
  915. {
  916. _log.severe("NPCElementals: Custom Elementals Error with id : " + npc_id);
  917. continue;
  918. }
  919. switch (rset.getByte("elemAtkType"))
  920. {
  921. case Elementals.FIRE:
  922. npcDat.setBaseFire(rset.getInt("elemAtkValue"));
  923. break;
  924. case Elementals.WATER:
  925. npcDat.setBaseWater(rset.getInt("elemAtkValue"));
  926. break;
  927. case Elementals.EARTH:
  928. npcDat.setBaseEarth(rset.getInt("elemAtkValue"));
  929. break;
  930. case Elementals.WIND:
  931. npcDat.setBaseWind(rset.getInt("elemAtkValue"));
  932. break;
  933. case Elementals.HOLY:
  934. npcDat.setBaseHoly(rset.getInt("elemAtkValue"));
  935. break;
  936. case Elementals.DARK:
  937. npcDat.setBaseDark(rset.getInt("elemAtkValue"));
  938. break;
  939. default:
  940. _log.severe("NPCElementals: Custom Elementals Error with id : " + npc_id + "; unknown elementType: " + rset.getByte("elemAtkType"));
  941. continue;
  942. }
  943. npcDat.setBaseFireRes(rset.getInt("fireDefValue"));
  944. npcDat.setBaseWaterRes(rset.getInt("waterDefValue"));
  945. npcDat.setBaseEarthRes(rset.getInt("earthDefValue"));
  946. npcDat.setBaseWindRes(rset.getInt("windDefValue"));
  947. npcDat.setBaseHolyRes(rset.getInt("holyDefValue"));
  948. npcDat.setBaseDarkRes(rset.getInt("darkDefValue"));
  949. cont++;
  950. }
  951. rset.close();
  952. statement.close();
  953. }
  954. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCount + ") elementals Data.");
  955. }
  956. catch (Exception e)
  957. {
  958. _log.log(Level.SEVERE, "NPCTable: Error reading NPC Elementals Data: " + e.getMessage(), e);
  959. }
  960. finally
  961. {
  962. L2DatabaseFactory.close(con);
  963. }
  964. }
  965. @SuppressWarnings("synthetic-access")
  966. private static class SingletonHolder
  967. {
  968. protected static final NpcTable _instance = new NpcTable();
  969. }
  970. }