NpcTable.java 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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, chance DESC";
  53. private static final String SELECT_DROPLIST_BY_ID = "SELECT * FROM droplist WHERE mobId = ? ORDER BY mobId, chance DESC";
  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, chance DESC";
  68. private static final String CUSTOM_SELECT_DROPLIST_BY_ID = "SELECT * FROM custom_droplist WHERE mobId = ? ORDER BY mobId, chance DESC";
  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. }
  601. try (ResultSet rs = ps.executeQuery())
  602. {
  603. L2NpcTemplate npcDat = null;
  604. L2Skill npcSkill = null;
  605. while (rs.next())
  606. {
  607. int mobId = rs.getInt("npcid");
  608. npcDat = _npcs.get(mobId);
  609. if (npcDat == null)
  610. {
  611. _log.warning(getClass().getSimpleName() + ": Skill data for undefined NPC. npcId: " + mobId);
  612. continue;
  613. }
  614. int skillId = rs.getInt("skillid");
  615. int level = rs.getInt("level");
  616. if (skillId == L2Skill.SKILL_NPC_RACE)
  617. {
  618. npcDat.setRace(level);
  619. continue;
  620. }
  621. npcSkill = SkillTable.getInstance().getInfo(skillId, level);
  622. if (npcSkill == null)
  623. {
  624. continue;
  625. }
  626. count++;
  627. npcDat.addSkill(npcSkill);
  628. }
  629. }
  630. }
  631. catch (Exception e)
  632. {
  633. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC skills table.", e);
  634. }
  635. return count;
  636. }
  637. /**
  638. * Id equals to zero or lesser means all.
  639. * @param id of the NPC to load it's drops.
  640. */
  641. public void loadNpcsDrop(int id)
  642. {
  643. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  644. {
  645. int count = loadNpcsDrop(con, id, false);
  646. int ccount = 0;
  647. if (Config.CUSTOM_DROPLIST_TABLE)
  648. {
  649. ccount = loadNpcsDrop(con, id, true);
  650. }
  651. _log.info(getClass().getSimpleName() + ": Loaded " + count + " (Custom: " + ccount + ") drops.");
  652. }
  653. catch (Exception e)
  654. {
  655. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC AI Data: " + e.getMessage(), e);
  656. }
  657. }
  658. /**
  659. * Load npcs drop.
  660. * @param con the con
  661. * @param id the id
  662. * @param isCustom the is custom
  663. * @return the int
  664. */
  665. public int loadNpcsDrop(Connection con, int id, boolean isCustom)
  666. {
  667. int count = 0;
  668. final String query = isCustom ? (((id > 0) ? CUSTOM_SELECT_DROPLIST_BY_ID : CUSTOM_SELECT_DROPLIST_ALL)) : ((id > 0) ? SELECT_DROPLIST_BY_ID : SELECT_DROPLIST_ALL);
  669. try (PreparedStatement ps = con.prepareStatement(query))
  670. {
  671. if (id > 0)
  672. {
  673. ps.setInt(1, id);
  674. }
  675. try (ResultSet rs = ps.executeQuery())
  676. {
  677. L2DropData dropDat = null;
  678. L2NpcTemplate npcDat = null;
  679. while (rs.next())
  680. {
  681. int mobId = rs.getInt("mobId");
  682. npcDat = _npcs.get(mobId);
  683. if (npcDat == null)
  684. {
  685. _log.warning(getClass().getSimpleName() + ": Drop data for undefined NPC. npcId: " + mobId);
  686. continue;
  687. }
  688. dropDat = new L2DropData();
  689. dropDat.setItemId(rs.getInt("itemId"));
  690. dropDat.setMinDrop(rs.getInt("min"));
  691. dropDat.setMaxDrop(rs.getInt("max"));
  692. dropDat.setChance(rs.getInt("chance"));
  693. int category = rs.getInt("category");
  694. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  695. {
  696. _log.warning(getClass().getSimpleName() + ": Drop data for undefined item template! NpcId: " + mobId + " itemId: " + dropDat.getItemId());
  697. continue;
  698. }
  699. count++;
  700. npcDat.addDropData(dropDat, category);
  701. }
  702. }
  703. }
  704. catch (Exception e)
  705. {
  706. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC dropdata. ", e);
  707. }
  708. return count;
  709. }
  710. /**
  711. * Id equals to zero or lesser means all.
  712. * @param id of the NPC to load it's skill learn list.
  713. */
  714. private void loadNpcsSkillLearn(int id)
  715. {
  716. final String query = (id > 0) ? SELECT_SKILL_LEARN_BY_ID : SELECT_SKILL_LEARN_ALL;
  717. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  718. PreparedStatement statement = con.prepareStatement(query))
  719. {
  720. if (id > 0)
  721. {
  722. statement.setInt(1, id);
  723. }
  724. int count = 0;
  725. try (ResultSet rs = statement.executeQuery())
  726. {
  727. int npcId;
  728. int classId;
  729. L2NpcTemplate npc;
  730. while (rs.next())
  731. {
  732. npcId = rs.getInt("npc_id");
  733. classId = rs.getInt("class_id");
  734. npc = getTemplate(npcId);
  735. if (npc == null)
  736. {
  737. _log.warning(getClass().getSimpleName() + ": Error getting NPC template ID " + npcId + " while trying to load skill trainer data.");
  738. continue;
  739. }
  740. count++;
  741. npc.addTeachInfo(ClassId.getClassId(classId));
  742. }
  743. }
  744. _log.info(getClass().getSimpleName() + ": Loaded " + count + " Skill Learn.");
  745. }
  746. catch (Exception e)
  747. {
  748. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC trainer data.", e);
  749. }
  750. }
  751. /**
  752. * Id equals to zero or lesser means all.
  753. * @param id of the NPC to load it's minions.
  754. */
  755. public void loadMinions(int id)
  756. {
  757. final String query = (id > 0) ? SELECT_MINION_BY_ID : SELECT_MINION_ALL;
  758. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  759. PreparedStatement statement = con.prepareStatement(query))
  760. {
  761. if (id > 0)
  762. {
  763. statement.setInt(1, id);
  764. }
  765. int count = 0;
  766. try (ResultSet rset = statement.executeQuery())
  767. {
  768. L2MinionData minionDat = null;
  769. L2NpcTemplate npcDat = null;
  770. int raidId;
  771. while (rset.next())
  772. {
  773. raidId = rset.getInt("boss_id");
  774. npcDat = _npcs.get(raidId);
  775. if (npcDat == null)
  776. {
  777. _log.warning(getClass().getSimpleName() + ": Minion references undefined boss NPC. Boss NpcId: " + raidId);
  778. continue;
  779. }
  780. minionDat = new L2MinionData();
  781. minionDat.setMinionId(rset.getInt("minion_id"));
  782. minionDat.setAmountMin(rset.getInt("amount_min"));
  783. minionDat.setAmountMax(rset.getInt("amount_max"));
  784. npcDat.addRaidData(minionDat);
  785. count++;
  786. }
  787. }
  788. _log.info(getClass().getSimpleName() + ": Loaded " + count + " Minions.");
  789. }
  790. catch (Exception e)
  791. {
  792. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error loading minion data.", e);
  793. }
  794. }
  795. /**
  796. * Id equals to zero or lesser means all.
  797. * @param id of the NPC to load it's AI data.
  798. */
  799. public void loadNpcsAI(int id)
  800. {
  801. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  802. {
  803. int count = loadNpcAi(con, id, false);
  804. int ccount = 0;
  805. if (Config.CUSTOM_NPC_TABLE)
  806. {
  807. ccount = loadNpcAi(con, id, true);
  808. }
  809. _log.info(getClass().getSimpleName() + ": Loaded " + count + " (Custom: " + ccount + ") AI Data.");
  810. }
  811. catch (Exception e)
  812. {
  813. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC AI Data: " + e.getMessage(), e);
  814. }
  815. }
  816. /**
  817. * Method that give the parameters will load one or all NPC AI from normal or custom tables.
  818. * @param con the database connection
  819. * @param id the NPC Id
  820. * @param isCustom if {@code true} the data will be loaded from the custom table
  821. * @return the count of NPC loaded
  822. */
  823. private int loadNpcAi(Connection con, int id, boolean isCustom)
  824. {
  825. int count = 0;
  826. 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);
  827. try (PreparedStatement ps = con.prepareStatement(query))
  828. {
  829. if (id > 0)
  830. {
  831. ps.setInt(1, id);
  832. }
  833. try (ResultSet rs = ps.executeQuery())
  834. {
  835. L2NpcAIData npcAIDat = null;
  836. L2NpcTemplate npcDat = null;
  837. int npcId;
  838. while (rs.next())
  839. {
  840. npcId = rs.getInt("npcId");
  841. npcDat = _npcs.get(npcId);
  842. if (npcDat == null)
  843. {
  844. _log.severe(getClass().getSimpleName() + ": AI Data Error with id : " + npcId);
  845. continue;
  846. }
  847. npcAIDat = new L2NpcAIData();
  848. npcAIDat.setPrimarySkillId(rs.getInt("primarySkillId"));
  849. npcAIDat.setMinSkillChance(rs.getInt("minSkillChance"));
  850. npcAIDat.setMaxSkillChance(rs.getInt("maxSkillChance"));
  851. npcAIDat.setAggro(rs.getInt("aggro"));
  852. npcAIDat.setCanMove(rs.getInt("canMove"));
  853. npcAIDat.setShowName(rs.getInt("showName") == 1);
  854. npcAIDat.setTargetable(rs.getInt("targetable") == 1);
  855. npcAIDat.setSoulShot(rs.getInt("soulshot"));
  856. npcAIDat.setSpiritShot(rs.getInt("spiritshot"));
  857. npcAIDat.setSoulShotChance(rs.getInt("ssChance"));
  858. npcAIDat.setSpiritShotChance(rs.getInt("spsChance"));
  859. npcAIDat.setIsChaos(rs.getInt("isChaos"));
  860. npcAIDat.setShortRangeSkill(rs.getInt("minRangeSkill"));
  861. npcAIDat.setShortRangeChance(rs.getInt("minRangeChance"));
  862. npcAIDat.setLongRangeSkill(rs.getInt("maxRangeSkill"));
  863. npcAIDat.setLongRangeChance(rs.getInt("maxRangeChance"));
  864. npcAIDat.setClan(rs.getString("clan"));
  865. npcAIDat.setClanRange(rs.getInt("clanRange"));
  866. npcAIDat.setEnemyClan(rs.getString("enemyClan"));
  867. npcAIDat.setEnemyRange(rs.getInt("enemyRange"));
  868. npcAIDat.setDodge(rs.getInt("dodge"));
  869. npcAIDat.setAi(rs.getString("aiType"));
  870. npcDat.setAIData(npcAIDat);
  871. count++;
  872. }
  873. }
  874. }
  875. catch (SQLException e)
  876. {
  877. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC AI Data: " + e.getMessage(), e);
  878. }
  879. return count;
  880. }
  881. /**
  882. * Id equals to zero or lesser means all.
  883. * @param id of the NPC to load it's element data.
  884. */
  885. public void loadNpcsElement(int id)
  886. {
  887. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  888. {
  889. int count = loadNpcsElement(con, id, false);
  890. int ccount = 0;
  891. if (Config.CUSTOM_NPC_TABLE)
  892. {
  893. ccount = loadNpcsElement(con, id, true);
  894. }
  895. _log.info(getClass().getSimpleName() + ": Loaded " + count + " (Custom: " + ccount + ") Elementals Data.");
  896. }
  897. catch (Exception e)
  898. {
  899. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC AI Data: " + e.getMessage(), e);
  900. }
  901. }
  902. /**
  903. * Load npcs element.
  904. * @param con the con
  905. * @param id the id
  906. * @param isCustom the is custom
  907. * @return the int
  908. */
  909. private int loadNpcsElement(Connection con, int id, boolean isCustom)
  910. {
  911. int count = 0;
  912. 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);
  913. try (PreparedStatement ps = con.prepareStatement(query))
  914. {
  915. if (id > 0)
  916. {
  917. ps.setInt(1, id);
  918. }
  919. try (ResultSet rset = ps.executeQuery())
  920. {
  921. L2NpcTemplate npcDat = null;
  922. int npcId;
  923. while (rset.next())
  924. {
  925. npcId = rset.getInt("npc_id");
  926. npcDat = _npcs.get(npcId);
  927. if (npcDat == null)
  928. {
  929. _log.severe("NPCElementals: Elementals Error with id : " + npcId);
  930. continue;
  931. }
  932. switch (rset.getByte("elemAtkType"))
  933. {
  934. case Elementals.FIRE:
  935. npcDat.setBaseFire(rset.getInt("elemAtkValue"));
  936. break;
  937. case Elementals.WATER:
  938. npcDat.setBaseWater(rset.getInt("elemAtkValue"));
  939. break;
  940. case Elementals.EARTH:
  941. npcDat.setBaseEarth(rset.getInt("elemAtkValue"));
  942. break;
  943. case Elementals.WIND:
  944. npcDat.setBaseWind(rset.getInt("elemAtkValue"));
  945. break;
  946. case Elementals.HOLY:
  947. npcDat.setBaseHoly(rset.getInt("elemAtkValue"));
  948. break;
  949. case Elementals.DARK:
  950. npcDat.setBaseDark(rset.getInt("elemAtkValue"));
  951. break;
  952. default:
  953. _log.severe("NPCElementals: Elementals Error with id : " + npcId + "; unknown elementType: " + rset.getByte("elemAtkType"));
  954. continue;
  955. }
  956. npcDat.setBaseFireRes(rset.getInt("fireDefValue"));
  957. npcDat.setBaseWaterRes(rset.getInt("waterDefValue"));
  958. npcDat.setBaseEarthRes(rset.getInt("earthDefValue"));
  959. npcDat.setBaseWindRes(rset.getInt("windDefValue"));
  960. npcDat.setBaseHolyRes(rset.getInt("holyDefValue"));
  961. npcDat.setBaseDarkRes(rset.getInt("darkDefValue"));
  962. count++;
  963. }
  964. }
  965. }
  966. catch (Exception e)
  967. {
  968. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC Elementals Data: " + e.getMessage(), e);
  969. }
  970. return count;
  971. }
  972. /**
  973. * Gets the single instance of NpcTable.
  974. * @return single instance of NpcTable
  975. */
  976. public static NpcTable getInstance()
  977. {
  978. return SingletonHolder._instance;
  979. }
  980. private static class SingletonHolder
  981. {
  982. protected static final NpcTable _instance = new NpcTable();
  983. }
  984. }