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