NpcTable.java 35 KB

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