NpcTable.java 29 KB

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