NpcTable.java 26 KB

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