NpcTable.java 32 KB

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