NpcTable.java 32 KB

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