NpcTable.java 29 KB

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