2
0

NpcTable.java 32 KB

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