NpcTable.java 32 KB

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