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