NpcTable.java 27 KB

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