NpcTable.java 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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.map.hash.TIntObjectHashMap;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import javolution.util.FastList;
  25. import javolution.util.FastMap;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.model.Elementals;
  29. import com.l2jserver.gameserver.model.L2DropCategory;
  30. import com.l2jserver.gameserver.model.L2DropData;
  31. import com.l2jserver.gameserver.model.L2MinionData;
  32. import com.l2jserver.gameserver.model.L2NpcAIData;
  33. import com.l2jserver.gameserver.model.L2Skill;
  34. import com.l2jserver.gameserver.model.base.ClassId;
  35. import com.l2jserver.gameserver.model.quest.Quest;
  36. import com.l2jserver.gameserver.model.quest.Quest.QuestEventType;
  37. import com.l2jserver.gameserver.skills.BaseStats;
  38. import com.l2jserver.gameserver.templates.StatsSet;
  39. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  40. public class NpcTable
  41. {
  42. private static Logger _log = Logger.getLogger(NpcTable.class.getName());
  43. private final TIntObjectHashMap<L2NpcTemplate> _npcs;
  44. public static NpcTable getInstance()
  45. {
  46. return SingletonHolder._instance;
  47. }
  48. private NpcTable()
  49. {
  50. _npcs = new TIntObjectHashMap<L2NpcTemplate>();
  51. restoreNpcData();
  52. }
  53. private void restoreNpcData()
  54. {
  55. loadNpcs(0);
  56. loadNpcsSkills(0);
  57. loadNpcsDrop(0);
  58. loadNpcsSkillLearn(0);
  59. loadMinions(0);
  60. loadNpcsAI(0);
  61. loadNpcsElement(0);
  62. }
  63. private void fillNpcTable(ResultSet NpcData) throws Exception
  64. {
  65. StatsSet npcDat = new StatsSet();
  66. int id = NpcData.getInt("id");
  67. int idTemp = NpcData.getInt("idTemplate");
  68. assert idTemp < 1000000;
  69. npcDat.set("npcId", id);
  70. npcDat.set("idTemplate", idTemp);
  71. int level = NpcData.getInt("level");
  72. npcDat.set("level", level);
  73. npcDat.set("jClass", NpcData.getString("class"));
  74. npcDat.set("baseShldDef", 0);
  75. npcDat.set("baseShldRate", 0);
  76. npcDat.set("baseCritRate", NpcData.getInt("critical"));
  77. npcDat.set("name", NpcData.getString("name"));
  78. npcDat.set("serverSideName", NpcData.getBoolean("serverSideName"));
  79. //npcDat.set("name", "");
  80. npcDat.set("title", NpcData.getString("title"));
  81. npcDat.set("serverSideTitle", NpcData.getBoolean("serverSideTitle"));
  82. npcDat.set("collision_radius", NpcData.getDouble("collision_radius"));
  83. npcDat.set("collision_height", NpcData.getDouble("collision_height"));
  84. npcDat.set("sex", NpcData.getString("sex"));
  85. npcDat.set("type", NpcData.getString("type"));
  86. npcDat.set("baseAtkRange", NpcData.getInt("attackrange"));
  87. npcDat.set("rewardExp", NpcData.getInt("exp"));
  88. npcDat.set("rewardSp", NpcData.getInt("sp"));
  89. npcDat.set("basePAtkSpd", NpcData.getInt("atkspd"));
  90. npcDat.set("baseMAtkSpd", NpcData.getInt("matkspd"));
  91. npcDat.set("aggroRange", NpcData.getInt("aggro"));
  92. npcDat.set("rhand", NpcData.getInt("rhand"));
  93. npcDat.set("lhand", NpcData.getInt("lhand"));
  94. npcDat.set("enchant", NpcData.getInt("enchant"));
  95. npcDat.set("baseWalkSpd", NpcData.getInt("walkspd"));
  96. npcDat.set("baseRunSpd", NpcData.getInt("runspd"));
  97. npcDat.set("targetable", NpcData.getBoolean("targetable"));
  98. npcDat.set("show_name", NpcData.getBoolean("show_name"));
  99. // constants, until we have stats in DB
  100. npcDat.safeSet("baseSTR", NpcData.getInt("str"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: "+NpcData.getInt("idTemplate"));
  101. npcDat.safeSet("baseCON", NpcData.getInt("con"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: "+NpcData.getInt("idTemplate"));
  102. npcDat.safeSet("baseDEX", NpcData.getInt("dex"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: "+NpcData.getInt("idTemplate"));
  103. npcDat.safeSet("baseINT", NpcData.getInt("int"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: "+NpcData.getInt("idTemplate"));
  104. npcDat.safeSet("baseWIT", NpcData.getInt("wit"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: "+NpcData.getInt("idTemplate"));
  105. npcDat.safeSet("baseMEN", NpcData.getInt("men"), 0, BaseStats.MAX_STAT_VALUE, "Loading npc template id: "+NpcData.getInt("idTemplate"));
  106. npcDat.set("baseHpMax", NpcData.getDouble("hp"));
  107. npcDat.set("baseCpMax", 0);
  108. npcDat.set("baseMpMax", NpcData.getDouble("mp"));
  109. npcDat.set("baseHpReg", NpcData.getFloat("hpreg") > 0 ? NpcData.getFloat("hpreg") : 1.5 + ((level - 1) / 10.0));
  110. npcDat.set("baseMpReg", NpcData.getFloat("mpreg") > 0 ? NpcData.getFloat("mpreg") : 0.9 + 0.3 * ((level - 1) / 10.0));
  111. npcDat.set("basePAtk", NpcData.getInt("patk"));
  112. npcDat.set("basePDef", NpcData.getInt("pdef"));
  113. npcDat.set("baseMAtk", NpcData.getInt("matk"));
  114. npcDat.set("baseMDef", NpcData.getInt("mdef"));
  115. npcDat.set("dropHerbGroup", NpcData.getInt("dropHerbGroup"));
  116. // Default element resists
  117. npcDat.set("baseFireRes", 20);
  118. npcDat.set("baseWindRes", 20);
  119. npcDat.set("baseWaterRes", 20);
  120. npcDat.set("baseEarthRes", 20);
  121. npcDat.set("baseHolyRes", 20);
  122. npcDat.set("baseDarkRes", 20);
  123. _npcs.put(id, new L2NpcTemplate(npcDat));
  124. }
  125. public void reloadNpc(int id)
  126. {
  127. try
  128. {
  129. // save a copy of the old data
  130. L2NpcTemplate old = getTemplate(id);
  131. TIntObjectHashMap<L2Skill> skills = new TIntObjectHashMap<L2Skill>();
  132. List<L2MinionData> minions = new FastList<L2MinionData>();
  133. Map<QuestEventType, Quest[]> quests = new FastMap<QuestEventType, Quest[]>();
  134. ClassId[] classIds = null;
  135. FastList<L2DropCategory> categories = new FastList<L2DropCategory>();
  136. if (old != null)
  137. {
  138. if (old.getSkills() != null)
  139. skills.putAll(old.getSkills());
  140. if (old.getDropData() != null)
  141. categories.addAll(old.getDropData());
  142. if (old.getTeachInfo() != null)
  143. classIds = old.getTeachInfo().clone();
  144. if (old.getMinionData() != null)
  145. minions.addAll(old.getMinionData());
  146. if (!old.getEventQuests().isEmpty())
  147. quests.putAll(old.getEventQuests());
  148. }
  149. loadNpcs(id);
  150. loadNpcsSkills(id);
  151. loadNpcsDrop(id);
  152. loadNpcsSkillLearn(id);
  153. loadMinions(id);
  154. loadNpcsAI(id);
  155. loadNpcsElement(id);
  156. // restore additional data from saved copy
  157. L2NpcTemplate created = getTemplate(id);
  158. if (old != null && created != null)
  159. {
  160. if (!skills.isEmpty())
  161. {
  162. for (L2Skill skill : skills.values(new L2Skill[0]))
  163. created.addSkill(skill);
  164. }
  165. if (classIds != null)
  166. {
  167. for (ClassId classId : classIds)
  168. created.addTeachInfo(classId);
  169. }
  170. if (!minions.isEmpty())
  171. {
  172. for (L2MinionData minion : minions)
  173. created.addRaidData(minion);
  174. }
  175. if (!quests.isEmpty())
  176. {
  177. created.getEventQuests().putAll(quests);
  178. }
  179. }
  180. }
  181. catch (Exception e)
  182. {
  183. _log.log(Level.WARNING, "NPCTable: Could not reload data for NPC " + id + ": " + e.getMessage(), e);
  184. }
  185. }
  186. // just wrapper
  187. public void reloadAllNpc()
  188. {
  189. restoreNpcData();
  190. }
  191. public void saveNpc(StatsSet npc)
  192. {
  193. Map<String, Object> set = npc.getSet();
  194. int length = 0;
  195. for (Object obj : set.keySet())
  196. {
  197. // 15 is just guessed npc name length
  198. length += ((String) obj).length() + 7 + 15;
  199. }
  200. final StringBuilder sbValues = new StringBuilder(length);
  201. for (Object obj : set.keySet())
  202. {
  203. final String name = (String) obj;
  204. if (!name.equalsIgnoreCase("npcId"))
  205. {
  206. if (sbValues.length() > 0)
  207. {
  208. sbValues.append(", ");
  209. }
  210. sbValues.append(name);
  211. sbValues.append(" = '");
  212. sbValues.append(set.get(name));
  213. sbValues.append('\'');
  214. }
  215. }
  216. Connection con = null;
  217. try
  218. {
  219. con = L2DatabaseFactory.getInstance().getConnection();
  220. int updated = 0;
  221. if (Config.CUSTOM_NPC_TABLE)
  222. {
  223. final StringBuilder sbQuery = new StringBuilder(sbValues.length() + 28);
  224. sbQuery.append("UPDATE custom_npc SET ");
  225. sbQuery.append(sbValues.toString());
  226. sbQuery.append(" WHERE id = ?");
  227. PreparedStatement statement = con.prepareStatement(sbQuery.toString());
  228. statement.setInt(1, npc.getInteger("npcId"));
  229. updated = statement.executeUpdate();
  230. statement.close();
  231. }
  232. if (updated == 0)
  233. {
  234. final StringBuilder sbQuery = new StringBuilder(sbValues.length() + 28);
  235. sbQuery.append("UPDATE npc SET ");
  236. sbQuery.append(sbValues.toString());
  237. sbQuery.append(" WHERE id = ?");
  238. PreparedStatement statement = con.prepareStatement(sbQuery.toString());
  239. statement.setInt(1, npc.getInteger("npcId"));
  240. statement.executeUpdate();
  241. statement.close();
  242. }
  243. }
  244. catch (Exception e)
  245. {
  246. _log.log(Level.WARNING, "NPCTable: Could not store new NPC data in database: " + e.getMessage(), e);
  247. }
  248. finally
  249. {
  250. L2DatabaseFactory.close(con);
  251. }
  252. }
  253. public void replaceTemplate(L2NpcTemplate npc)
  254. {
  255. _npcs.put(npc.npcId, npc);
  256. }
  257. public L2NpcTemplate getTemplate(int id)
  258. {
  259. return _npcs.get(id);
  260. }
  261. public L2NpcTemplate getTemplateByName(String name)
  262. {
  263. for (L2NpcTemplate npcTemplate : _npcs.valueCollection())
  264. if (npcTemplate.name.equalsIgnoreCase(name))
  265. return npcTemplate;
  266. return null;
  267. }
  268. public L2NpcTemplate[] getAllOfLevel(int lvl)
  269. {
  270. List<L2NpcTemplate> list = new FastList<L2NpcTemplate>();
  271. for (L2NpcTemplate t : _npcs.valueCollection())
  272. if (t.level == lvl)
  273. list.add(t);
  274. return list.toArray(new L2NpcTemplate[list.size()]);
  275. }
  276. public L2NpcTemplate[] getAllMonstersOfLevel(int lvl)
  277. {
  278. List<L2NpcTemplate> list = new FastList<L2NpcTemplate>();
  279. for (L2NpcTemplate t : _npcs.valueCollection())
  280. if (t.level == lvl && "L2Monster".equals(t.type))
  281. list.add(t);
  282. return list.toArray(new L2NpcTemplate[list.size()]);
  283. }
  284. public L2NpcTemplate[] getAllNpcStartingWith(String letter)
  285. {
  286. List<L2NpcTemplate> list = new FastList<L2NpcTemplate>();
  287. for (L2NpcTemplate t : _npcs.valueCollection())
  288. if (t.name.startsWith(letter) && "L2Npc".equals((t).type))
  289. list.add(t);
  290. return list.toArray(new L2NpcTemplate[list.size()]);
  291. }
  292. /**
  293. * @param classType
  294. * @return
  295. */
  296. public L2NpcTemplate[] getAllNpcOfClassType(String classType)
  297. {
  298. List<L2NpcTemplate> list = new FastList<L2NpcTemplate>();
  299. for (L2NpcTemplate t : _npcs.valueCollection())
  300. if (classType.equals(t.type))
  301. list.add(t);
  302. return list.toArray(new L2NpcTemplate[list.size()]);
  303. }
  304. public void loadNpcs(int id)
  305. {
  306. Connection con = null;
  307. try
  308. {
  309. con = L2DatabaseFactory.getInstance().getConnection();
  310. PreparedStatement statement = null;
  311. if (id > 0)
  312. {
  313. statement = con.prepareStatement("SELECT * FROM npc WHERE id = ?");
  314. statement.setInt(1, id);
  315. }
  316. else
  317. {
  318. statement = con.prepareStatement("SELECT * FROM npc ORDER BY id");
  319. }
  320. ResultSet rset = statement.executeQuery();
  321. int cont = 0;
  322. int cCont = 0;
  323. while (rset.next())
  324. {
  325. fillNpcTable(rset);
  326. cont++;
  327. }
  328. rset.close();
  329. statement.close();
  330. if (Config.CUSTOM_NPC_TABLE)
  331. {
  332. if (id > 0)
  333. {
  334. statement = con.prepareStatement("SELECT * FROM custom_npc WHERE id = ?");
  335. statement.setInt(1, id);
  336. }
  337. else
  338. {
  339. statement = con.prepareStatement("SELECT * FROM custom_npc ORDER BY id");
  340. }
  341. rset = statement.executeQuery();
  342. while (rset.next())
  343. {
  344. fillNpcTable(rset);
  345. cCont++;
  346. }
  347. rset.close();
  348. statement.close();
  349. }
  350. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCont + ") NPC template(s).");
  351. }
  352. catch (Exception e)
  353. {
  354. _log.log(Level.SEVERE, "NPCTable: Error creating NPC table.", e);
  355. }
  356. finally
  357. {
  358. L2DatabaseFactory.close(con);
  359. }
  360. }
  361. public void loadNpcsSkills(int id)
  362. {
  363. Connection con = null;
  364. try
  365. {
  366. con = L2DatabaseFactory.getInstance().getConnection();
  367. PreparedStatement statement = null;
  368. if (id > 0)
  369. {
  370. statement = con.prepareStatement("SELECT * FROM npcskills WHERE npcid = ?");
  371. statement.setInt(1, id);
  372. }
  373. else
  374. {
  375. statement = con.prepareStatement("SELECT * FROM npcskills ORDER BY npcid");
  376. }
  377. ResultSet rset = statement.executeQuery();
  378. int cont = 0;
  379. int cCont = 0;
  380. L2NpcTemplate npcDat = null;
  381. L2Skill npcSkill = null;
  382. while (rset.next())
  383. {
  384. int mobId = rset.getInt("npcid");
  385. npcDat = _npcs.get(mobId);
  386. if (npcDat == null)
  387. {
  388. _log.warning("NPCTable: Skill data for undefined NPC. npcId: " + mobId);
  389. continue;
  390. }
  391. int skillId = rset.getInt("skillid");
  392. int level = rset.getInt("level");
  393. if (npcDat.race == null && skillId == 4416)
  394. {
  395. npcDat.setRace(level);
  396. continue;
  397. }
  398. npcSkill = SkillTable.getInstance().getInfo(skillId, level);
  399. if (npcSkill == null)
  400. continue;
  401. cont++;
  402. npcDat.addSkill(npcSkill);
  403. }
  404. rset.close();
  405. statement.close();
  406. if (Config.CUSTOM_NPC_SKILLS_TABLE)
  407. {
  408. if (id > 0)
  409. {
  410. statement = con.prepareStatement("SELECT * FROM custom_npcskills WHERE npcid = ?");
  411. statement.setInt(1, id);
  412. }
  413. else
  414. {
  415. statement = con.prepareStatement("SELECT * FROM custom_npcskills ORDER BY npcid");
  416. }
  417. rset = statement.executeQuery();
  418. while (rset.next())
  419. {
  420. int mobId = rset.getInt("npcid");
  421. npcDat = _npcs.get(mobId);
  422. if (npcDat == null)
  423. {
  424. _log.warning("Custom NPCTable: Skill data for undefined NPC. npcId: " + mobId);
  425. continue;
  426. }
  427. int skillId = rset.getInt("skillid");
  428. int level = rset.getInt("level");
  429. if (npcDat.race == null && skillId == 4416)
  430. {
  431. npcDat.setRace(level);
  432. continue;
  433. }
  434. npcSkill = SkillTable.getInstance().getInfo(skillId, level);
  435. if (npcSkill == null)
  436. continue;
  437. cCont++;
  438. npcDat.addSkill(npcSkill);
  439. }
  440. rset.close();
  441. statement.close();
  442. }
  443. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCont + ") npc skills.");
  444. }
  445. catch (Exception e)
  446. {
  447. _log.log(Level.SEVERE, "NPCTable: Error reading NPC skills table.", e);
  448. }
  449. finally
  450. {
  451. L2DatabaseFactory.close(con);
  452. }
  453. }
  454. public void loadNpcsDrop(int id)
  455. {
  456. Connection con = null;
  457. try
  458. {
  459. con = L2DatabaseFactory.getInstance().getConnection();
  460. PreparedStatement statement = null;
  461. if (id > 0)
  462. {
  463. statement = con.prepareStatement("SELECT * FROM droplist WHERE mobId = ? ORDER BY mobId, chance DESC");
  464. statement.setInt(1, id);
  465. }
  466. else
  467. {
  468. statement = con.prepareStatement("SELECT * FROM droplist ORDER BY mobId, chance DESC");
  469. }
  470. ResultSet rset = statement.executeQuery();
  471. L2DropData dropDat = null;
  472. L2NpcTemplate npcDat = null;
  473. int cont = 0;
  474. int cCont = 0;
  475. while (rset.next())
  476. {
  477. int mobId = rset.getInt("mobId");
  478. npcDat = _npcs.get(mobId);
  479. if (npcDat == null)
  480. {
  481. _log.warning("NPCTable: Drop data for undefined NPC. npcId: " + mobId);
  482. continue;
  483. }
  484. dropDat = new L2DropData();
  485. dropDat.setItemId(rset.getInt("itemId"));
  486. dropDat.setMinDrop(rset.getInt("min"));
  487. dropDat.setMaxDrop(rset.getInt("max"));
  488. dropDat.setChance(rset.getInt("chance"));
  489. int category = rset.getInt("category");
  490. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  491. {
  492. _log.warning("Drop data for undefined item template! NpcId: " + mobId+" itemId: "+dropDat.getItemId());
  493. continue;
  494. }
  495. cont++;
  496. npcDat.addDropData(dropDat, category);
  497. }
  498. rset.close();
  499. statement.close();
  500. if (Config.CUSTOM_DROPLIST_TABLE)
  501. {
  502. if (id > 0)
  503. {
  504. statement = con.prepareStatement("SELECT * FROM custom_droplist WHERE mobId = ? ORDER BY mobId, chance DESC");
  505. statement.setInt(1, id);
  506. }
  507. else
  508. {
  509. statement = con.prepareStatement("SELECT * FROM custom_droplist ORDER BY mobId, chance DESC");
  510. }
  511. rset = statement.executeQuery();
  512. while (rset.next())
  513. {
  514. int mobId = rset.getInt("mobId");
  515. npcDat = _npcs.get(mobId);
  516. if (npcDat == null)
  517. {
  518. _log.warning("NPCTable: CUSTOM DROPLIST: Drop data for undefined NPC. npcId: " + mobId);
  519. continue;
  520. }
  521. dropDat = new L2DropData();
  522. dropDat.setItemId(rset.getInt("itemId"));
  523. dropDat.setMinDrop(rset.getInt("min"));
  524. dropDat.setMaxDrop(rset.getInt("max"));
  525. dropDat.setChance(rset.getInt("chance"));
  526. int category = rset.getInt("category");
  527. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  528. {
  529. _log.warning("Custom drop data for undefined item template! NpcId: " + mobId+" itemId: "+dropDat.getItemId());
  530. continue;
  531. }
  532. npcDat.addDropData(dropDat, category);
  533. cCont++;
  534. }
  535. rset.close();
  536. statement.close();
  537. }
  538. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCont + ") drops.");
  539. }
  540. catch (Exception e)
  541. {
  542. _log.log(Level.SEVERE, "NPCTable: Error reading NPC dropdata. ", e);
  543. }
  544. finally
  545. {
  546. L2DatabaseFactory.close(con);
  547. }
  548. }
  549. private void loadNpcsSkillLearn(int id)
  550. {
  551. Connection con = null;
  552. try
  553. {
  554. con = L2DatabaseFactory.getInstance().getConnection();
  555. PreparedStatement statement = null;
  556. if (id > 0)
  557. {
  558. statement = con.prepareStatement("SELECT * FROM skill_learn WHERE npc_id = ?");
  559. statement.setInt(1, id);
  560. }
  561. else
  562. {
  563. statement = con.prepareStatement("SELECT * FROM skill_learn");
  564. }
  565. ResultSet rset = statement.executeQuery();
  566. int cont = 0;
  567. while (rset.next())
  568. {
  569. int npcId = rset.getInt("npc_id");
  570. int classId = rset.getInt("class_id");
  571. L2NpcTemplate npc = getTemplate(npcId);
  572. if (npc == null)
  573. {
  574. _log.warning("NPCTable: Error getting NPC template ID " + npcId + " while trying to load skill trainer data.");
  575. continue;
  576. }
  577. cont++;
  578. npc.addTeachInfo(ClassId.values()[classId]);
  579. }
  580. rset.close();
  581. statement.close();
  582. _log.info("NpcTable: Loaded " + cont + " Skill Learn.");
  583. }
  584. catch (Exception e)
  585. {
  586. _log.log(Level.SEVERE, "NPCTable: Error reading NPC trainer data.", e);
  587. }
  588. finally
  589. {
  590. L2DatabaseFactory.close(con);
  591. }
  592. }
  593. public void loadMinions(int id)
  594. {
  595. Connection con = null;
  596. try
  597. {
  598. con = L2DatabaseFactory.getInstance().getConnection();
  599. PreparedStatement statement = null;
  600. if (id > 0)
  601. {
  602. statement = con.prepareStatement("SELECT * FROM minions WHERE boss_id = ?");
  603. statement.setInt(1, id);
  604. }
  605. else
  606. {
  607. statement = con.prepareStatement("SELECT * FROM minions ORDER BY boss_id");
  608. }
  609. ResultSet rset = statement.executeQuery();
  610. L2MinionData minionDat = null;
  611. L2NpcTemplate npcDat = null;
  612. int cnt = 0;
  613. while (rset.next())
  614. {
  615. int raidId = rset.getInt("boss_id");
  616. npcDat = _npcs.get(raidId);
  617. if (npcDat == null)
  618. {
  619. _log.warning("Minion references undefined boss NPC. Boss NpcId: " + raidId);
  620. continue;
  621. }
  622. minionDat = new L2MinionData();
  623. minionDat.setMinionId(rset.getInt("minion_id"));
  624. minionDat.setAmountMin(rset.getInt("amount_min"));
  625. minionDat.setAmountMax(rset.getInt("amount_max"));
  626. npcDat.addRaidData(minionDat);
  627. cnt++;
  628. }
  629. rset.close();
  630. statement.close();
  631. _log.info("NpcTable: Loaded " + cnt + " Minions.");
  632. }
  633. catch (Exception e)
  634. {
  635. _log.log(Level.SEVERE, "NPCTable: Error loading minion data.", e);
  636. }
  637. finally
  638. {
  639. L2DatabaseFactory.close(con);
  640. }
  641. }
  642. public void loadNpcsAI(int id)
  643. {
  644. Connection con = null;
  645. try
  646. {
  647. con = L2DatabaseFactory.getInstance().getConnection();
  648. PreparedStatement statement = null;
  649. if (id > 0)
  650. {
  651. statement = con.prepareStatement("SELECT * FROM npcaidata WHERE npcId = ?");
  652. statement.setInt(1, id);
  653. }
  654. else
  655. {
  656. statement = con.prepareStatement("SELECT * FROM npcaidata ORDER BY npcId");
  657. }
  658. ResultSet rset = statement.executeQuery();
  659. L2NpcAIData npcAIDat = null;
  660. L2NpcTemplate npcDat = null;
  661. int cont = 0;
  662. int cCont = 0;
  663. while (rset.next())
  664. {
  665. int npc_id = rset.getInt("npcId");
  666. npcDat = _npcs.get(npc_id);
  667. if (npcDat == null)
  668. {
  669. _log.severe("NPCTable: AI Data Error with id : " + npc_id);
  670. continue;
  671. }
  672. npcAIDat = new L2NpcAIData();
  673. npcAIDat.setPrimarySkillId(rset.getInt("primarySkillId"));
  674. npcAIDat.setMinSkillChance(rset.getInt("minSkillChance"));
  675. npcAIDat.setMaxSkillChance(rset.getInt("maxSkillChance"));
  676. npcAIDat.setCanMove(rset.getInt("canMove"));
  677. npcAIDat.setSoulShot(rset.getInt("soulshot"));
  678. npcAIDat.setSpiritShot(rset.getInt("spiritshot"));
  679. npcAIDat.setSoulShotChance(rset.getInt("ssChance"));
  680. npcAIDat.setSpiritShotChance(rset.getInt("spsChance"));
  681. npcAIDat.setIsChaos(rset.getInt("isChaos"));
  682. npcAIDat.setShortRangeSkill(rset.getInt("minRangeSkill"));
  683. npcAIDat.setShortRangeChance(rset.getInt("minRangeChance"));
  684. npcAIDat.setLongRangeSkill(rset.getInt("maxRangeSkill"));
  685. npcAIDat.setLongRangeChance(rset.getInt("maxRangeChance"));
  686. npcAIDat.setClan(rset.getString("clan"));
  687. npcAIDat.setClanRange(rset.getInt("clanRange"));
  688. npcAIDat.setEnemyClan(rset.getString("enemyClan"));
  689. npcAIDat.setEnemyRange(rset.getInt("enemyRange"));
  690. npcAIDat.setDodge(rset.getInt("dodge"));
  691. npcAIDat.setAi(rset.getString("aiType"));
  692. npcDat.setAIData(npcAIDat);
  693. cont++;
  694. }
  695. rset.close();
  696. statement.close();
  697. if (Config.CUSTOM_NPC_TABLE)
  698. {
  699. if (id > 0)
  700. {
  701. statement = con.prepareStatement("SELECT * FROM custom_npcaidata WHERE npcId = ?");
  702. statement.setInt(1, id);
  703. }
  704. else
  705. {
  706. statement = con.prepareStatement("SELECT * FROM custom_npcaidata ORDER BY npcId");
  707. }
  708. rset = statement.executeQuery();
  709. while (rset.next())
  710. {
  711. int npc_id = rset.getInt("npcId");
  712. npcDat = _npcs.get(npc_id);
  713. if (npcDat == null)
  714. {
  715. _log.severe("NPCTable: Custom AI Data Error with id : " + npc_id);
  716. continue;
  717. }
  718. npcAIDat = new L2NpcAIData();
  719. npcAIDat.setPrimarySkillId(rset.getInt("primarySkillId"));
  720. npcAIDat.setMinSkillChance(rset.getInt("minSkillChance"));
  721. npcAIDat.setMaxSkillChance(rset.getInt("maxSkillChance"));
  722. npcAIDat.setCanMove(rset.getInt("canMove"));
  723. npcAIDat.setSoulShot(rset.getInt("soulshot"));
  724. npcAIDat.setSpiritShot(rset.getInt("spiritshot"));
  725. npcAIDat.setSoulShotChance(rset.getInt("ssChance"));
  726. npcAIDat.setSpiritShotChance(rset.getInt("spsChance"));
  727. npcAIDat.setIsChaos(rset.getInt("isChaos"));
  728. npcAIDat.setShortRangeSkill(rset.getInt("minRangeSkill"));
  729. npcAIDat.setShortRangeChance(rset.getInt("minRangeChance"));
  730. npcAIDat.setLongRangeSkill(rset.getInt("maxRangeSkill"));
  731. npcAIDat.setLongRangeChance(rset.getInt("maxRangeChance"));
  732. npcAIDat.setClan(rset.getString("clan"));
  733. npcAIDat.setClanRange(rset.getInt("clanRange"));
  734. npcAIDat.setEnemyClan(rset.getString("enemyClan"));
  735. npcAIDat.setEnemyRange(rset.getInt("enemyRange"));
  736. npcAIDat.setDodge(rset.getInt("dodge"));
  737. npcAIDat.setAi(rset.getString("aiType"));
  738. npcDat.setAIData(npcAIDat);
  739. cCont++;
  740. }
  741. rset.close();
  742. statement.close();
  743. }
  744. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCont + ") AI Data.");
  745. }
  746. catch (Exception e)
  747. {
  748. _log.log(Level.SEVERE, "NPCTable: Error reading NPC AI Data: " + e.getMessage(), e);
  749. }
  750. finally
  751. {
  752. L2DatabaseFactory.close(con);
  753. }
  754. }
  755. public void loadNpcsElement(int id)
  756. {
  757. Connection con = null;
  758. try
  759. {
  760. con = L2DatabaseFactory.getInstance().getConnection();
  761. PreparedStatement statement = null;
  762. if (id > 0)
  763. {
  764. statement = con.prepareStatement("SELECT * FROM npc_elementals WHERE npc_id = ?");
  765. statement.setInt(1, id);
  766. }
  767. else
  768. {
  769. statement = con.prepareStatement("SELECT * FROM npc_elementals ORDER BY npc_id");
  770. }
  771. ResultSet rset = statement.executeQuery();
  772. L2NpcTemplate npcDat = null;
  773. int cont = 0;
  774. int cCount = 0;
  775. while (rset.next())
  776. {
  777. int npc_id = rset.getInt("npc_id");
  778. npcDat = _npcs.get(npc_id);
  779. if (npcDat == null)
  780. {
  781. _log.severe("NPCElementals: Elementals Error with id : " + npc_id);
  782. continue;
  783. }
  784. switch(rset.getByte("elemAtkType"))
  785. {
  786. case Elementals.FIRE:
  787. npcDat.setBaseFire(rset.getInt("elemAtkValue"));
  788. break;
  789. case Elementals.WATER:
  790. npcDat.setBaseWater(rset.getInt("elemAtkValue"));
  791. break;
  792. case Elementals.EARTH:
  793. npcDat.setBaseEarth(rset.getInt("elemAtkValue"));
  794. break;
  795. case Elementals.WIND:
  796. npcDat.setBaseWind(rset.getInt("elemAtkValue"));
  797. break;
  798. case Elementals.HOLY:
  799. npcDat.setBaseHoly(rset.getInt("elemAtkValue"));
  800. break;
  801. case Elementals.DARK:
  802. npcDat.setBaseDark(rset.getInt("elemAtkValue"));
  803. break;
  804. default:
  805. _log.severe("NPCElementals: Elementals Error with id : " + npc_id + "; unknown elementType: " + rset.getByte("elemAtkType"));
  806. continue;
  807. }
  808. npcDat.setBaseFireRes(rset.getInt("fireDefValue"));
  809. npcDat.setBaseWaterRes(rset.getInt("waterDefValue"));
  810. npcDat.setBaseEarthRes(rset.getInt("earthDefValue"));
  811. npcDat.setBaseWindRes(rset.getInt("windDefValue"));
  812. npcDat.setBaseHolyRes(rset.getInt("holyDefValue"));
  813. npcDat.setBaseDarkRes(rset.getInt("darkDefValue"));
  814. cont++;
  815. }
  816. rset.close();
  817. statement.close();
  818. if (Config.CUSTOM_NPC_TABLE)
  819. {
  820. if (id > 0)
  821. {
  822. statement = con.prepareStatement("SELECT * FROM custom_npc_elementals WHERE npc_id = ?");
  823. statement.setInt(1, id);
  824. }
  825. else
  826. {
  827. statement = con.prepareStatement("SELECT * FROM custom_npc_elementals ORDER BY npc_id");
  828. }
  829. rset = statement.executeQuery();
  830. while (rset.next())
  831. {
  832. int npc_id = rset.getInt("npc_id");
  833. npcDat = _npcs.get(npc_id);
  834. if (npcDat == null)
  835. {
  836. _log.severe("NPCElementals: Custom Elementals Error with id : " + npc_id);
  837. continue;
  838. }
  839. switch(rset.getByte("elemAtkType"))
  840. {
  841. case Elementals.FIRE:
  842. npcDat.setBaseFire(rset.getInt("elemAtkValue"));
  843. break;
  844. case Elementals.WATER:
  845. npcDat.setBaseWater(rset.getInt("elemAtkValue"));
  846. break;
  847. case Elementals.EARTH:
  848. npcDat.setBaseEarth(rset.getInt("elemAtkValue"));
  849. break;
  850. case Elementals.WIND:
  851. npcDat.setBaseWind(rset.getInt("elemAtkValue"));
  852. break;
  853. case Elementals.HOLY:
  854. npcDat.setBaseHoly(rset.getInt("elemAtkValue"));
  855. break;
  856. case Elementals.DARK:
  857. npcDat.setBaseDark(rset.getInt("elemAtkValue"));
  858. break;
  859. default:
  860. _log.severe("NPCElementals: Custom Elementals Error with id : " + npc_id + "; unknown elementType: " + rset.getByte("elemAtkType"));
  861. continue;
  862. }
  863. npcDat.setBaseFireRes(rset.getInt("fireDefValue"));
  864. npcDat.setBaseWaterRes(rset.getInt("waterDefValue"));
  865. npcDat.setBaseEarthRes(rset.getInt("earthDefValue"));
  866. npcDat.setBaseWindRes(rset.getInt("windDefValue"));
  867. npcDat.setBaseHolyRes(rset.getInt("holyDefValue"));
  868. npcDat.setBaseDarkRes(rset.getInt("darkDefValue"));
  869. cont++;
  870. }
  871. rset.close();
  872. statement.close();
  873. }
  874. _log.info("NpcTable: Loaded " + cont + " (Custom: " + cCount + ") elementals Data.");
  875. }
  876. catch (Exception e)
  877. {
  878. _log.log(Level.SEVERE, "NPCTable: Error reading NPC Elementals Data: " + e.getMessage(), e);
  879. }
  880. finally
  881. {
  882. L2DatabaseFactory.close(con);
  883. }
  884. }
  885. @SuppressWarnings("synthetic-access")
  886. private static class SingletonHolder
  887. {
  888. protected static final NpcTable _instance = new NpcTable();
  889. }
  890. }