NpcTable.java 24 KB

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