NpcTable.java 21 KB

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