CursedWeaponsManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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.instancemanager;
  16. import java.io.File;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.util.Collection;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javax.xml.parsers.DocumentBuilderFactory;
  27. import javolution.util.FastMap;
  28. import net.sf.l2j.Config;
  29. import net.sf.l2j.L2DatabaseFactory;
  30. import net.sf.l2j.gameserver.model.CursedWeapon;
  31. import net.sf.l2j.gameserver.model.L2ItemInstance;
  32. import net.sf.l2j.gameserver.model.actor.L2Attackable;
  33. import net.sf.l2j.gameserver.model.actor.L2Character;
  34. import net.sf.l2j.gameserver.model.actor.instance.L2FeedableBeastInstance;
  35. import net.sf.l2j.gameserver.model.actor.instance.L2FestivalMonsterInstance;
  36. import net.sf.l2j.gameserver.model.actor.instance.L2FortCommanderInstance;
  37. import net.sf.l2j.gameserver.model.actor.instance.L2FortSiegeGuardInstance;
  38. import net.sf.l2j.gameserver.model.actor.instance.L2GrandBossInstance;
  39. import net.sf.l2j.gameserver.model.actor.instance.L2GuardInstance;
  40. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  41. import net.sf.l2j.gameserver.model.actor.instance.L2RiftInvaderInstance;
  42. import net.sf.l2j.gameserver.model.actor.instance.L2SiegeGuardInstance;
  43. import net.sf.l2j.gameserver.network.SystemMessageId;
  44. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  45. import net.sf.l2j.gameserver.util.Broadcast;
  46. import org.w3c.dom.Document;
  47. import org.w3c.dom.NamedNodeMap;
  48. import org.w3c.dom.Node;
  49. /**
  50. *
  51. * @author Micht
  52. */
  53. public class CursedWeaponsManager
  54. {
  55. private static final Logger _log = Logger.getLogger(CursedWeaponsManager.class.getName());
  56. public static final CursedWeaponsManager getInstance()
  57. {
  58. return SingletonHolder._instance;
  59. }
  60. // =========================================================
  61. // Data Field
  62. private Map<Integer, CursedWeapon> _cursedWeapons;
  63. // =========================================================
  64. // Constructor
  65. private CursedWeaponsManager()
  66. {
  67. init();
  68. }
  69. private void init()
  70. {
  71. _log.info("Initializing CursedWeaponsManager");
  72. _cursedWeapons = new FastMap<Integer, CursedWeapon>();
  73. if (!Config.ALLOW_CURSED_WEAPONS)
  74. return;
  75. load();
  76. restore();
  77. controlPlayers();
  78. _log.info("Loaded : " + _cursedWeapons.size() + " cursed weapon(s).");
  79. }
  80. // =========================================================
  81. // Method - Private
  82. public final void reload()
  83. {
  84. init();
  85. }
  86. private final void load()
  87. {
  88. if (Config.DEBUG)
  89. _log.info(" Parsing ... ");
  90. try
  91. {
  92. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  93. factory.setValidating(false);
  94. factory.setIgnoringComments(true);
  95. File file = new File(Config.DATAPACK_ROOT + "/data/cursedWeapons.xml");
  96. if (!file.exists())
  97. {
  98. if (Config.DEBUG)
  99. _log.info("NO FILE");
  100. return;
  101. }
  102. Document doc = factory.newDocumentBuilder().parse(file);
  103. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  104. {
  105. if ("list".equalsIgnoreCase(n.getNodeName()))
  106. {
  107. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  108. {
  109. if ("item".equalsIgnoreCase(d.getNodeName()))
  110. {
  111. NamedNodeMap attrs = d.getAttributes();
  112. int id = Integer.parseInt(attrs.getNamedItem("id").getNodeValue());
  113. int skillId = Integer.parseInt(attrs.getNamedItem("skillId").getNodeValue());
  114. String name = attrs.getNamedItem("name").getNodeValue();
  115. CursedWeapon cw = new CursedWeapon(id, skillId, name);
  116. int val;
  117. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  118. {
  119. if ("dropRate".equalsIgnoreCase(cd.getNodeName()))
  120. {
  121. attrs = cd.getAttributes();
  122. val = Integer.parseInt(attrs.getNamedItem("val").getNodeValue());
  123. cw.setDropRate(val);
  124. }
  125. else if ("duration".equalsIgnoreCase(cd.getNodeName()))
  126. {
  127. attrs = cd.getAttributes();
  128. val = Integer.parseInt(attrs.getNamedItem("val").getNodeValue());
  129. cw.setDuration(val);
  130. }
  131. else if ("durationLost".equalsIgnoreCase(cd.getNodeName()))
  132. {
  133. attrs = cd.getAttributes();
  134. val = Integer.parseInt(attrs.getNamedItem("val").getNodeValue());
  135. cw.setDurationLost(val);
  136. }
  137. else if ("disapearChance".equalsIgnoreCase(cd.getNodeName()))
  138. {
  139. attrs = cd.getAttributes();
  140. val = Integer.parseInt(attrs.getNamedItem("val").getNodeValue());
  141. cw.setDisapearChance(val);
  142. }
  143. else if ("stageKills".equalsIgnoreCase(cd.getNodeName()))
  144. {
  145. attrs = cd.getAttributes();
  146. val = Integer.parseInt(attrs.getNamedItem("val").getNodeValue());
  147. cw.setStageKills(val);
  148. }
  149. }
  150. // Store cursed weapon
  151. _cursedWeapons.put(id, cw);
  152. }
  153. }
  154. }
  155. }
  156. if (Config.DEBUG)
  157. _log.info("OK");
  158. }
  159. catch (Exception e)
  160. {
  161. _log.log(Level.SEVERE, "Error parsing cursed weapons file.", e);
  162. if (Config.DEBUG)
  163. _log.warning("ERROR");
  164. return;
  165. }
  166. }
  167. private final void restore()
  168. {
  169. if (Config.DEBUG)
  170. _log.info(" Restoring ... ");
  171. Connection con = null;
  172. try
  173. {
  174. // Retrieve the L2PcInstance from the characters table of the database
  175. con = L2DatabaseFactory.getInstance().getConnection();
  176. PreparedStatement statement = con.prepareStatement("SELECT itemId, charId, playerKarma, playerPkKills, nbKills, endTime FROM cursed_weapons");
  177. ResultSet rset = statement.executeQuery();
  178. while (rset.next())
  179. {
  180. int itemId = rset.getInt("itemId");
  181. int playerId = rset.getInt("charId");
  182. int playerKarma = rset.getInt("playerKarma");
  183. int playerPkKills = rset.getInt("playerPkKills");
  184. int nbKills = rset.getInt("nbKills");
  185. long endTime = rset.getLong("endTime");
  186. CursedWeapon cw = _cursedWeapons.get(itemId);
  187. cw.setPlayerId(playerId);
  188. cw.setPlayerKarma(playerKarma);
  189. cw.setPlayerPkKills(playerPkKills);
  190. cw.setNbKills(nbKills);
  191. cw.setEndTime(endTime);
  192. cw.reActivate();
  193. }
  194. rset.close();
  195. statement.close();
  196. if (Config.DEBUG)
  197. _log.info("OK");
  198. }
  199. catch (Exception e)
  200. {
  201. _log.warning("Could not restore CursedWeapons data: " + e);
  202. if (Config.DEBUG)
  203. _log.warning("ERROR");
  204. return;
  205. }
  206. finally
  207. {
  208. try
  209. {
  210. con.close();
  211. }
  212. catch (Exception e)
  213. {
  214. }
  215. }
  216. }
  217. private final void controlPlayers()
  218. {
  219. if (Config.DEBUG)
  220. _log.info(" Checking players ... ");
  221. Connection con = null;
  222. try
  223. {
  224. // Retrieve the L2PcInstance from the characters table of the database
  225. con = L2DatabaseFactory.getInstance().getConnection();
  226. PreparedStatement statement = null;
  227. ResultSet rset = null;
  228. // TODO: See comments below...
  229. // This entire for loop should NOT be necessary, since it is already handled by
  230. // CursedWeapon.endOfLife(). However, if we indeed *need* to duplicate it for safety,
  231. // then we'd better make sure that it FULLY cleans up inactive cursed weapons!
  232. // Undesired effects result otherwise, such as player with no zariche but with karma
  233. // or a lost-child entry in the cursedweapons table, without a corresponding one in items...
  234. for (CursedWeapon cw : _cursedWeapons.values())
  235. {
  236. if (cw.isActivated())
  237. continue;
  238. // Do an item check to be sure that the cursed weapon isn't hold by someone
  239. int itemId = cw.getItemId();
  240. try
  241. {
  242. statement = con.prepareStatement("SELECT owner_id FROM items WHERE item_id=?");
  243. statement.setInt(1, itemId);
  244. rset = statement.executeQuery();
  245. if (rset.next())
  246. {
  247. // A player has the cursed weapon in his inventory ...
  248. int playerId = rset.getInt("owner_id");
  249. _log.info("PROBLEM : Player " + playerId + " owns the cursed weapon " + itemId + " but he shouldn't.");
  250. // Delete the item
  251. statement = con.prepareStatement("DELETE FROM items WHERE owner_id=? AND item_id=?");
  252. statement.setInt(1, playerId);
  253. statement.setInt(2, itemId);
  254. if (statement.executeUpdate() != 1)
  255. {
  256. _log.warning("Error while deleting cursed weapon " + itemId + " from userId " + playerId);
  257. }
  258. statement.close();
  259. // Delete the skill
  260. /*
  261. statement = con.prepareStatement("DELETE FROM character_skills WHERE charId=? AND skill_id=");
  262. statement.setInt(1, playerId);
  263. statement.setInt(2, cw.getSkillId());
  264. if (statement.executeUpdate() != 1)
  265. {
  266. _log.warning("Error while deleting cursed weapon "+itemId+" skill from userId "+playerId);
  267. }
  268. */
  269. // Restore the player's old karma and pk count
  270. statement = con.prepareStatement("UPDATE characters SET karma=?, pkkills=? WHERE charId=?");
  271. statement.setInt(1, cw.getPlayerKarma());
  272. statement.setInt(2, cw.getPlayerPkKills());
  273. statement.setInt(3, playerId);
  274. if (statement.executeUpdate() != 1)
  275. {
  276. _log.warning("Error while updating karma & pkkills for userId " + cw.getPlayerId());
  277. }
  278. // clean up the cursedweapons table.
  279. removeFromDb(itemId);
  280. }
  281. rset.close();
  282. statement.close();
  283. }
  284. catch (SQLException sqlE)
  285. {
  286. }
  287. }
  288. }
  289. catch (Exception e)
  290. {
  291. if (Config.DEBUG)
  292. _log.warning("Could not check CursedWeapons data: " + e);
  293. return;
  294. }
  295. finally
  296. {
  297. try
  298. {
  299. con.close();
  300. }
  301. catch (Exception e)
  302. {
  303. }
  304. }
  305. if (Config.DEBUG)
  306. _log.info("DONE");
  307. }
  308. // =========================================================
  309. // Properties - Public
  310. public synchronized void checkDrop(L2Attackable attackable, L2PcInstance player)
  311. {
  312. if (attackable instanceof L2SiegeGuardInstance || attackable instanceof L2RiftInvaderInstance
  313. || attackable instanceof L2FestivalMonsterInstance || attackable instanceof L2GuardInstance
  314. || attackable instanceof L2GrandBossInstance || attackable instanceof L2FeedableBeastInstance
  315. || attackable instanceof L2FortSiegeGuardInstance || attackable instanceof L2FortCommanderInstance)
  316. return;
  317. for (CursedWeapon cw : _cursedWeapons.values())
  318. {
  319. if (cw.isActive())
  320. continue;
  321. if (cw.checkDrop(attackable, player))
  322. break;
  323. }
  324. }
  325. public void activate(L2PcInstance player, L2ItemInstance item)
  326. {
  327. CursedWeapon cw = _cursedWeapons.get(item.getItemId());
  328. if (player.isCursedWeaponEquipped()) // cannot own 2 cursed swords
  329. {
  330. CursedWeapon cw2 = _cursedWeapons.get(player.getCursedWeaponEquippedId());
  331. /* TODO: give the bonus level in a more appropriate manner.
  332. * The following code adds "_stageKills" levels. This will also show in the char status.
  333. * I do not have enough info to know if the bonus should be shown in the pk count, or if it
  334. * should be a full "_stageKills" bonus or just the remaining from the current count till the
  335. * of the current stage...
  336. * This code is a TEMP fix, so that the cursed weapon's bonus level can be observed with as
  337. * little change in the code as possible, until proper info arises.
  338. */
  339. cw2.setNbKills(cw2.getStageKills() - 1);
  340. cw2.increaseKills();
  341. // erase the newly obtained cursed weapon
  342. cw.setPlayer(player); // NECESSARY in order to find which inventory the weapon is in!
  343. cw.endOfLife(); // expire the weapon and clean up.
  344. }
  345. else
  346. cw.activate(player, item);
  347. }
  348. public void drop(int itemId, L2Character killer)
  349. {
  350. CursedWeapon cw = _cursedWeapons.get(itemId);
  351. cw.dropIt(killer);
  352. }
  353. public void increaseKills(int itemId)
  354. {
  355. CursedWeapon cw = _cursedWeapons.get(itemId);
  356. cw.increaseKills();
  357. }
  358. public int getLevel(int itemId)
  359. {
  360. CursedWeapon cw = _cursedWeapons.get(itemId);
  361. return cw.getLevel();
  362. }
  363. public static void announce(SystemMessage sm)
  364. {
  365. Broadcast.toAllOnlinePlayers(sm);
  366. }
  367. public void checkPlayer(L2PcInstance player)
  368. {
  369. if (player == null)
  370. return;
  371. for (CursedWeapon cw : _cursedWeapons.values())
  372. {
  373. if (cw.isActivated() && player.getObjectId() == cw.getPlayerId())
  374. {
  375. cw.setPlayer(player);
  376. cw.setItem(player.getInventory().getItemByItemId(cw.getItemId()));
  377. cw.giveSkill();
  378. player.setCursedWeaponEquippedId(cw.getItemId());
  379. SystemMessage sm = new SystemMessage(SystemMessageId.S2_MINUTE_OF_USAGE_TIME_ARE_LEFT_FOR_S1);
  380. sm.addString(cw.getName());
  381. //sm.addItemName(cw.getItemId());
  382. sm.addNumber((int) ((cw.getEndTime() - System.currentTimeMillis()) / 60000));
  383. player.sendPacket(sm);
  384. }
  385. }
  386. }
  387. public int checkOwnsWeaponId(int ownerId)
  388. {
  389. for (CursedWeapon cw : _cursedWeapons.values())
  390. if (cw.isActivated() && ownerId == cw.getPlayerId())
  391. return cw.getItemId();
  392. return -1;
  393. }
  394. public static void removeFromDb(int itemId)
  395. {
  396. Connection con = null;
  397. try
  398. {
  399. con = L2DatabaseFactory.getInstance().getConnection();
  400. // Delete datas
  401. PreparedStatement statement = con.prepareStatement("DELETE FROM cursed_weapons WHERE itemId = ?");
  402. statement.setInt(1, itemId);
  403. statement.executeUpdate();
  404. statement.close();
  405. con.close();
  406. }
  407. catch (SQLException e)
  408. {
  409. _log.severe("CursedWeaponsManager: Failed to remove data: " + e);
  410. }
  411. finally
  412. {
  413. try
  414. {
  415. con.close();
  416. }
  417. catch (Exception e)
  418. {
  419. }
  420. }
  421. }
  422. public void saveData()
  423. {
  424. for (CursedWeapon cw : _cursedWeapons.values())
  425. {
  426. cw.saveData();
  427. }
  428. }
  429. // =========================================================
  430. public boolean isCursed(int itemId)
  431. {
  432. return _cursedWeapons.containsKey(itemId);
  433. }
  434. public Collection<CursedWeapon> getCursedWeapons()
  435. {
  436. return _cursedWeapons.values();
  437. }
  438. public Set<Integer> getCursedWeaponsIds()
  439. {
  440. return _cursedWeapons.keySet();
  441. }
  442. public CursedWeapon getCursedWeapon(int itemId)
  443. {
  444. return _cursedWeapons.get(itemId);
  445. }
  446. public void givePassive(int itemId)
  447. {
  448. try
  449. {
  450. _cursedWeapons.get(itemId).giveSkill();
  451. }
  452. catch (Exception e)
  453. {
  454. /***/
  455. }
  456. }
  457. @SuppressWarnings("synthetic-access")
  458. private static class SingletonHolder
  459. {
  460. protected static final CursedWeaponsManager _instance = new CursedWeaponsManager();
  461. }
  462. }