NpcTable.java 27 KB

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