NpcTable.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. }
  340. catch (Exception e)
  341. {
  342. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not store new NPC data in database: " + e.getMessage(), e);
  343. }
  344. }
  345. /**
  346. * Append entry.
  347. * @param sb the string builder to append the attribute and value.
  348. * @param attribute the attribute to append.
  349. */
  350. private final void appendEntry(StringBuilder sb, String attribute)
  351. {
  352. if (sb.length() > 0)
  353. {
  354. sb.append(", ");
  355. }
  356. sb.append("`");
  357. sb.append(attribute);
  358. sb.append("` = ?");
  359. }
  360. /**
  361. * Perform update.
  362. * @param sb the string builder with the parameters
  363. * @param table the table to update.
  364. * @param key the key of the table.
  365. * @param values the values of keys.
  366. * @param npcId the Npc Id.
  367. * @param con the current database connection.
  368. * @return the count of updated NPCs.
  369. * @throws SQLException the SQL exception.
  370. */
  371. private final int performUpdate(StringBuilder sb, String table, String key, Collection<Object> values, int npcId, Connection con) throws SQLException
  372. {
  373. int updated = 0;
  374. if ((sb != null) && !sb.toString().isEmpty())
  375. {
  376. final StringBuilder sbQuery = new StringBuilder(sb.length() + 28);
  377. sbQuery.append("UPDATE ");
  378. sbQuery.append(table);
  379. sbQuery.append(" SET ");
  380. sbQuery.append(sb.toString());
  381. sbQuery.append(" WHERE ");
  382. sbQuery.append(key);
  383. sbQuery.append(" = ?");
  384. try (PreparedStatement ps = con.prepareStatement(sbQuery.toString()))
  385. {
  386. int i = 1;
  387. for (Object value : values)
  388. {
  389. ps.setObject(i, value);
  390. i++;
  391. }
  392. ps.setInt(i, npcId);
  393. updated = ps.executeUpdate();
  394. }
  395. }
  396. return updated;
  397. }
  398. /**
  399. * Gets the template.
  400. * @param id the template Id to get.
  401. * @return the template for the given id.
  402. */
  403. public L2NpcTemplate getTemplate(int id)
  404. {
  405. return _npcs.get(id);
  406. }
  407. /**
  408. * Gets the template by name.
  409. * @param name of the template to get.
  410. * @return the template for the given name.
  411. */
  412. public L2NpcTemplate getTemplateByName(String name)
  413. {
  414. for (L2NpcTemplate npcTemplate : _npcs.values())
  415. {
  416. if (npcTemplate.getName().equalsIgnoreCase(name))
  417. {
  418. return npcTemplate;
  419. }
  420. }
  421. return null;
  422. }
  423. /**
  424. * Gets the all of level.
  425. * @param lvls of all the templates to get.
  426. * @return the template list for the given level.
  427. */
  428. public List<L2NpcTemplate> getAllOfLevel(int... lvls)
  429. {
  430. final List<L2NpcTemplate> list = new ArrayList<>();
  431. for (int lvl : lvls)
  432. {
  433. for (L2NpcTemplate t : _npcs.values())
  434. {
  435. if (t.getLevel() == lvl)
  436. {
  437. list.add(t);
  438. }
  439. }
  440. }
  441. return list;
  442. }
  443. /**
  444. * Gets the all monsters of level.
  445. * @param lvls of all the monster templates to get.
  446. * @return the template list for the given level.
  447. */
  448. public List<L2NpcTemplate> getAllMonstersOfLevel(int... lvls)
  449. {
  450. final List<L2NpcTemplate> list = new ArrayList<>();
  451. for (int lvl : lvls)
  452. {
  453. for (L2NpcTemplate t : _npcs.values())
  454. {
  455. if ((t.getLevel() == lvl) && t.isType("L2Monster"))
  456. {
  457. list.add(t);
  458. }
  459. }
  460. }
  461. return list;
  462. }
  463. /**
  464. * Gets the all npc starting with.
  465. * @param letters of all the NPC templates which its name start with.
  466. * @return the template list for the given letter.
  467. */
  468. public List<L2NpcTemplate> getAllNpcStartingWith(String... letters)
  469. {
  470. final List<L2NpcTemplate> list = new ArrayList<>();
  471. for (String letter : letters)
  472. {
  473. for (L2NpcTemplate t : _npcs.values())
  474. {
  475. if (t.getName().startsWith(letter) && t.isType("L2Npc"))
  476. {
  477. list.add(t);
  478. }
  479. }
  480. }
  481. return list;
  482. }
  483. /**
  484. * Gets the all npc of class type.
  485. * @param classTypes of all the templates to get.
  486. * @return the template list for the given class type.
  487. */
  488. public List<L2NpcTemplate> getAllNpcOfClassType(String... classTypes)
  489. {
  490. final List<L2NpcTemplate> list = new ArrayList<>();
  491. for (String classType : classTypes)
  492. {
  493. for (L2NpcTemplate t : _npcs.values())
  494. {
  495. if (t.isType(classType))
  496. {
  497. list.add(t);
  498. }
  499. }
  500. }
  501. return list;
  502. }
  503. /**
  504. * Load npcs.
  505. * @param id the id
  506. */
  507. public void loadNpcs(int id)
  508. {
  509. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  510. {
  511. int count = loadNpcs(con, id, false);
  512. int ccount = 0;
  513. if (Config.CUSTOM_NPC_TABLE)
  514. {
  515. ccount = loadNpcs(con, id, true);
  516. }
  517. _log.info(getClass().getSimpleName() + ": Loaded " + count + " (Custom: " + ccount + ") NPC template(s).");
  518. }
  519. catch (Exception e)
  520. {
  521. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC AI Data: " + e.getMessage(), e);
  522. }
  523. }
  524. /**
  525. * Id equals to zero or lesser means all.
  526. * @param con the database connection
  527. * @param id of the NPC to load.
  528. * @param isCustom the is custom
  529. * @return the loaded NPC count
  530. */
  531. public int loadNpcs(Connection con, int id, boolean isCustom)
  532. {
  533. int count = 0;
  534. try
  535. {
  536. final String query = isCustom ? (((id > 0) ? CUSTOM_SELECT_NPC_BY_ID : CUSTOM_SELECT_NPC_ALL)) : ((id > 0) ? SELECT_NPC_BY_ID : SELECT_NPC_ALL);
  537. try (PreparedStatement ps = con.prepareStatement(query))
  538. {
  539. if (id > 0)
  540. {
  541. ps.setInt(1, id);
  542. }
  543. try (ResultSet rs = ps.executeQuery())
  544. {
  545. while (rs.next())
  546. {
  547. fillNpcTable(rs);
  548. count++;
  549. }
  550. }
  551. }
  552. }
  553. catch (Exception e)
  554. {
  555. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error creating NPC table.", e);
  556. }
  557. return count;
  558. }
  559. /**
  560. * Id equals to zero or lesser means all.
  561. * @param id of the NPC to load it's skills.
  562. */
  563. public void loadNpcsSkills(int id)
  564. {
  565. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  566. {
  567. int count = loadNpcsSkills(con, id, false);
  568. int ccount = 0;
  569. if (Config.CUSTOM_NPC_SKILLS_TABLE)
  570. {
  571. ccount = loadNpcsSkills(con, id, true);
  572. }
  573. _log.info(getClass().getSimpleName() + ": Loaded " + count + " (Custom: " + ccount + ") NPC skills.");
  574. }
  575. catch (Exception e)
  576. {
  577. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC AI Data: " + e.getMessage(), e);
  578. }
  579. }
  580. /**
  581. * Load npcs skills.
  582. * @param con the database connection
  583. * @param id the NPC Id
  584. * @param isCustom the is custom
  585. * @return the loaded NPC count
  586. */
  587. private int loadNpcsSkills(Connection con, int id, boolean isCustom)
  588. {
  589. int count = 0;
  590. final String query = isCustom ? (((id > 0) ? CUSTOM_SELECT_SKILLS_BY_ID : CUSTOM_SELECT_SKILLS_ALL)) : ((id > 0) ? SELECT_SKILLS_BY_ID : SELECT_SKILLS_ALL);
  591. try (PreparedStatement ps = con.prepareStatement(query))
  592. {
  593. if (id > 0)
  594. {
  595. ps.setInt(1, id);
  596. }
  597. try (ResultSet rs = ps.executeQuery())
  598. {
  599. L2NpcTemplate npcDat = null;
  600. L2Skill npcSkill = null;
  601. while (rs.next())
  602. {
  603. int mobId = rs.getInt("npcid");
  604. npcDat = _npcs.get(mobId);
  605. if (npcDat == null)
  606. {
  607. _log.warning(getClass().getSimpleName() + ": Skill data for undefined NPC. npcId: " + mobId);
  608. continue;
  609. }
  610. int skillId = rs.getInt("skillid");
  611. int level = rs.getInt("level");
  612. if (skillId == L2Skill.SKILL_NPC_RACE)
  613. {
  614. npcDat.setRace(level);
  615. continue;
  616. }
  617. npcSkill = SkillTable.getInstance().getInfo(skillId, level);
  618. if (npcSkill == null)
  619. {
  620. continue;
  621. }
  622. count++;
  623. npcDat.addSkill(npcSkill);
  624. }
  625. }
  626. }
  627. catch (Exception e)
  628. {
  629. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC skills table.", e);
  630. }
  631. return count;
  632. }
  633. /**
  634. * Id equals to zero or lesser means all.
  635. * @param id of the NPC to load it's drops.
  636. */
  637. public void loadNpcsDrop(int id)
  638. {
  639. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  640. {
  641. int count = loadNpcsDrop(con, id, false);
  642. int ccount = 0;
  643. if (Config.CUSTOM_DROPLIST_TABLE)
  644. {
  645. ccount = loadNpcsDrop(con, id, true);
  646. }
  647. _log.info(getClass().getSimpleName() + ": Loaded " + count + " (Custom: " + ccount + ") drops.");
  648. }
  649. catch (Exception e)
  650. {
  651. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC AI Data: " + e.getMessage(), e);
  652. }
  653. }
  654. /**
  655. * Load npcs drop.
  656. * @param con the con
  657. * @param id the id
  658. * @param isCustom the is custom
  659. * @return the int
  660. */
  661. public int loadNpcsDrop(Connection con, int id, boolean isCustom)
  662. {
  663. int count = 0;
  664. final String query = isCustom ? (((id > 0) ? CUSTOM_SELECT_DROPLIST_BY_ID : CUSTOM_SELECT_DROPLIST_ALL)) : ((id > 0) ? SELECT_DROPLIST_BY_ID : SELECT_DROPLIST_ALL);
  665. try (PreparedStatement ps = con.prepareStatement(query))
  666. {
  667. if (id > 0)
  668. {
  669. ps.setInt(1, id);
  670. }
  671. try (ResultSet rs = ps.executeQuery())
  672. {
  673. L2DropData dropDat = null;
  674. L2NpcTemplate npcDat = null;
  675. while (rs.next())
  676. {
  677. int mobId = rs.getInt("mobId");
  678. npcDat = _npcs.get(mobId);
  679. if (npcDat == null)
  680. {
  681. _log.warning(getClass().getSimpleName() + ": Drop data for undefined NPC. npcId: " + mobId);
  682. continue;
  683. }
  684. dropDat = new L2DropData();
  685. dropDat.setItemId(rs.getInt("itemId"));
  686. dropDat.setMinDrop(rs.getInt("min"));
  687. dropDat.setMaxDrop(rs.getInt("max"));
  688. dropDat.setChance(rs.getInt("chance"));
  689. int category = rs.getInt("category");
  690. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  691. {
  692. _log.warning(getClass().getSimpleName() + ": Drop data for undefined item template! NpcId: " + mobId + " itemId: " + dropDat.getItemId());
  693. continue;
  694. }
  695. count++;
  696. npcDat.addDropData(dropDat, category);
  697. }
  698. }
  699. }
  700. catch (Exception e)
  701. {
  702. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC dropdata. ", e);
  703. }
  704. return count;
  705. }
  706. /**
  707. * Id equals to zero or lesser means all.
  708. * @param id of the NPC to load it's skill learn list.
  709. */
  710. private void loadNpcsSkillLearn(int id)
  711. {
  712. final String query = (id > 0) ? SELECT_SKILL_LEARN_BY_ID : SELECT_SKILL_LEARN_ALL;
  713. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  714. PreparedStatement statement = con.prepareStatement(query))
  715. {
  716. if (id > 0)
  717. {
  718. statement.setInt(1, id);
  719. }
  720. int count = 0;
  721. try (ResultSet rs = statement.executeQuery())
  722. {
  723. int npcId;
  724. int classId;
  725. L2NpcTemplate npc;
  726. while (rs.next())
  727. {
  728. npcId = rs.getInt("npc_id");
  729. classId = rs.getInt("class_id");
  730. npc = getTemplate(npcId);
  731. if (npc == null)
  732. {
  733. _log.warning(getClass().getSimpleName() + ": Error getting NPC template ID " + npcId + " while trying to load skill trainer data.");
  734. continue;
  735. }
  736. count++;
  737. npc.addTeachInfo(ClassId.getClassId(classId));
  738. }
  739. }
  740. _log.info(getClass().getSimpleName() + ": Loaded " + count + " Skill Learn.");
  741. }
  742. catch (Exception e)
  743. {
  744. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC trainer data.", e);
  745. }
  746. }
  747. /**
  748. * Id equals to zero or lesser means all.
  749. * @param id of the NPC to load it's minions.
  750. */
  751. public void loadMinions(int id)
  752. {
  753. final String query = (id > 0) ? SELECT_MINION_BY_ID : SELECT_MINION_ALL;
  754. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  755. PreparedStatement statement = con.prepareStatement(query))
  756. {
  757. if (id > 0)
  758. {
  759. statement.setInt(1, id);
  760. }
  761. int count = 0;
  762. try (ResultSet rset = statement.executeQuery())
  763. {
  764. L2MinionData minionDat = null;
  765. L2NpcTemplate npcDat = null;
  766. int raidId;
  767. while (rset.next())
  768. {
  769. raidId = rset.getInt("boss_id");
  770. npcDat = _npcs.get(raidId);
  771. if (npcDat == null)
  772. {
  773. _log.warning(getClass().getSimpleName() + ": Minion references undefined boss NPC. Boss NpcId: " + raidId);
  774. continue;
  775. }
  776. minionDat = new L2MinionData();
  777. minionDat.setMinionId(rset.getInt("minion_id"));
  778. minionDat.setAmountMin(rset.getInt("amount_min"));
  779. minionDat.setAmountMax(rset.getInt("amount_max"));
  780. npcDat.addRaidData(minionDat);
  781. count++;
  782. }
  783. }
  784. _log.info(getClass().getSimpleName() + ": Loaded " + count + " Minions.");
  785. }
  786. catch (Exception e)
  787. {
  788. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error loading minion data.", e);
  789. }
  790. }
  791. /**
  792. * Id equals to zero or lesser means all.
  793. * @param id of the NPC to load it's AI data.
  794. */
  795. public void loadNpcsAI(int id)
  796. {
  797. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  798. {
  799. int count = loadNpcAi(con, id, false);
  800. int ccount = 0;
  801. if (Config.CUSTOM_NPC_TABLE)
  802. {
  803. ccount = loadNpcAi(con, id, true);
  804. }
  805. _log.info(getClass().getSimpleName() + ": Loaded " + count + " (Custom: " + ccount + ") AI Data.");
  806. }
  807. catch (Exception e)
  808. {
  809. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC AI Data: " + e.getMessage(), e);
  810. }
  811. }
  812. /**
  813. * Method that give the parameters will load one or all NPC AI from normal or custom tables.
  814. * @param con the database connection
  815. * @param id the NPC Id
  816. * @param isCustom if {@code true} the data will be loaded from the custom table
  817. * @return the count of NPC loaded
  818. */
  819. private int loadNpcAi(Connection con, int id, boolean isCustom)
  820. {
  821. int count = 0;
  822. 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);
  823. try (PreparedStatement ps = con.prepareStatement(query))
  824. {
  825. if (id > 0)
  826. {
  827. ps.setInt(1, id);
  828. }
  829. try (ResultSet rs = ps.executeQuery())
  830. {
  831. L2NpcAIData npcAIDat = null;
  832. L2NpcTemplate npcDat = null;
  833. int npcId;
  834. while (rs.next())
  835. {
  836. npcId = rs.getInt("npcId");
  837. npcDat = _npcs.get(npcId);
  838. if (npcDat == null)
  839. {
  840. _log.severe(getClass().getSimpleName() + ": AI Data Error with id : " + npcId);
  841. continue;
  842. }
  843. npcAIDat = new L2NpcAIData();
  844. npcAIDat.setPrimarySkillId(rs.getInt("primarySkillId"));
  845. npcAIDat.setMinSkillChance(rs.getInt("minSkillChance"));
  846. npcAIDat.setMaxSkillChance(rs.getInt("maxSkillChance"));
  847. npcAIDat.setAggro(rs.getInt("aggro"));
  848. npcAIDat.setCanMove(rs.getInt("canMove"));
  849. npcAIDat.setShowName(rs.getInt("showName") == 1);
  850. npcAIDat.setTargetable(rs.getInt("targetable") == 1);
  851. npcAIDat.setSoulShot(rs.getInt("soulshot"));
  852. npcAIDat.setSpiritShot(rs.getInt("spiritshot"));
  853. npcAIDat.setSoulShotChance(rs.getInt("ssChance"));
  854. npcAIDat.setSpiritShotChance(rs.getInt("spsChance"));
  855. npcAIDat.setIsChaos(rs.getInt("isChaos"));
  856. npcAIDat.setShortRangeSkill(rs.getInt("minRangeSkill"));
  857. npcAIDat.setShortRangeChance(rs.getInt("minRangeChance"));
  858. npcAIDat.setLongRangeSkill(rs.getInt("maxRangeSkill"));
  859. npcAIDat.setLongRangeChance(rs.getInt("maxRangeChance"));
  860. npcAIDat.setClan(rs.getString("clan"));
  861. npcAIDat.setClanRange(rs.getInt("clanRange"));
  862. npcAIDat.setEnemyClan(rs.getString("enemyClan"));
  863. npcAIDat.setEnemyRange(rs.getInt("enemyRange"));
  864. npcAIDat.setDodge(rs.getInt("dodge"));
  865. npcAIDat.setAi(rs.getString("aiType"));
  866. npcDat.setAIData(npcAIDat);
  867. count++;
  868. }
  869. }
  870. }
  871. catch (SQLException e)
  872. {
  873. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC AI Data: " + e.getMessage(), e);
  874. }
  875. return count;
  876. }
  877. /**
  878. * Id equals to zero or lesser means all.
  879. * @param id of the NPC to load it's element data.
  880. */
  881. public void loadNpcsElement(int id)
  882. {
  883. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  884. {
  885. int count = loadNpcsElement(con, id, false);
  886. int ccount = 0;
  887. if (Config.CUSTOM_NPC_TABLE)
  888. {
  889. ccount = loadNpcsElement(con, id, true);
  890. }
  891. _log.info(getClass().getSimpleName() + ": Loaded " + count + " (Custom: " + ccount + ") Elementals Data.");
  892. }
  893. catch (Exception e)
  894. {
  895. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC AI Data: " + e.getMessage(), e);
  896. }
  897. }
  898. /**
  899. * Load npcs element.
  900. * @param con the con
  901. * @param id the id
  902. * @param isCustom the is custom
  903. * @return the int
  904. */
  905. private int loadNpcsElement(Connection con, int id, boolean isCustom)
  906. {
  907. int count = 0;
  908. 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);
  909. try (PreparedStatement ps = con.prepareStatement(query))
  910. {
  911. if (id > 0)
  912. {
  913. ps.setInt(1, id);
  914. }
  915. try (ResultSet rset = ps.executeQuery())
  916. {
  917. L2NpcTemplate npcDat = null;
  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. count++;
  959. }
  960. }
  961. }
  962. catch (Exception e)
  963. {
  964. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading NPC Elementals Data: " + e.getMessage(), e);
  965. }
  966. return count;
  967. }
  968. /**
  969. * Gets the single instance of NpcTable.
  970. * @return single instance of NpcTable
  971. */
  972. public static NpcTable getInstance()
  973. {
  974. return SingletonHolder._instance;
  975. }
  976. private static class SingletonHolder
  977. {
  978. protected static final NpcTable _instance = new NpcTable();
  979. }
  980. }