NpcTable.java 32 KB

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