NpcTable.java 36 KB

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