NpcTable.java 23 KB

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