NpcTable.java 29 KB

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