2
0

NpcTable.java 32 KB

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