CursedWeaponsManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. return;
  162. }
  163. }
  164. private final void restore()
  165. {
  166. if (Config.DEBUG)
  167. _log.info(" Restoring ... ");
  168. Connection con = null;
  169. try
  170. {
  171. // Retrieve the L2PcInstance from the characters table of the database
  172. con = L2DatabaseFactory.getInstance().getConnection();
  173. PreparedStatement statement = con.prepareStatement("SELECT itemId, charId, playerKarma, playerPkKills, nbKills, endTime FROM cursed_weapons");
  174. ResultSet rset = statement.executeQuery();
  175. while (rset.next())
  176. {
  177. int itemId = rset.getInt("itemId");
  178. int playerId = rset.getInt("charId");
  179. int playerKarma = rset.getInt("playerKarma");
  180. int playerPkKills = rset.getInt("playerPkKills");
  181. int nbKills = rset.getInt("nbKills");
  182. long endTime = rset.getLong("endTime");
  183. CursedWeapon cw = _cursedWeapons.get(itemId);
  184. cw.setPlayerId(playerId);
  185. cw.setPlayerKarma(playerKarma);
  186. cw.setPlayerPkKills(playerPkKills);
  187. cw.setNbKills(nbKills);
  188. cw.setEndTime(endTime);
  189. cw.reActivate();
  190. }
  191. rset.close();
  192. statement.close();
  193. if (Config.DEBUG)
  194. _log.info("OK");
  195. }
  196. catch (Exception e)
  197. {
  198. _log.log(Level.WARNING, "Could not restore CursedWeapons data: " + e.getMessage(), e);
  199. return;
  200. }
  201. finally
  202. {
  203. L2DatabaseFactory.close(con);
  204. }
  205. }
  206. private final void controlPlayers()
  207. {
  208. if (Config.DEBUG)
  209. _log.info(" Checking players ... ");
  210. Connection con = null;
  211. try
  212. {
  213. // Retrieve the L2PcInstance from the characters table of the database
  214. con = L2DatabaseFactory.getInstance().getConnection();
  215. PreparedStatement statement = null;
  216. ResultSet rset = null;
  217. // TODO: See comments below...
  218. // This entire for loop should NOT be necessary, since it is already handled by
  219. // CursedWeapon.endOfLife(). However, if we indeed *need* to duplicate it for safety,
  220. // then we'd better make sure that it FULLY cleans up inactive cursed weapons!
  221. // Undesired effects result otherwise, such as player with no zariche but with karma
  222. // or a lost-child entry in the cursedweapons table, without a corresponding one in items...
  223. for (CursedWeapon cw : _cursedWeapons.values())
  224. {
  225. if (cw.isActivated())
  226. continue;
  227. // Do an item check to be sure that the cursed weapon isn't hold by someone
  228. int itemId = cw.getItemId();
  229. try
  230. {
  231. statement = con.prepareStatement("SELECT owner_id FROM items WHERE item_id=?");
  232. statement.setInt(1, itemId);
  233. rset = statement.executeQuery();
  234. if (rset.next())
  235. {
  236. // A player has the cursed weapon in his inventory ...
  237. int playerId = rset.getInt("owner_id");
  238. _log.info("PROBLEM : Player " + playerId + " owns the cursed weapon " + itemId + " but he shouldn't.");
  239. // Delete the item
  240. statement = con.prepareStatement("DELETE FROM items WHERE owner_id=? AND item_id=?");
  241. statement.setInt(1, playerId);
  242. statement.setInt(2, itemId);
  243. if (statement.executeUpdate() != 1)
  244. {
  245. _log.warning("Error while deleting cursed weapon " + itemId + " from userId " + playerId);
  246. }
  247. statement.close();
  248. // Delete the skill
  249. /*
  250. statement = con.prepareStatement("DELETE FROM character_skills WHERE charId=? AND skill_id=");
  251. statement.setInt(1, playerId);
  252. statement.setInt(2, cw.getSkillId());
  253. if (statement.executeUpdate() != 1)
  254. {
  255. _log.warning("Error while deleting cursed weapon "+itemId+" skill from userId "+playerId);
  256. }
  257. */
  258. // Restore the player's old karma and pk count
  259. statement = con.prepareStatement("UPDATE characters SET karma=?, pkkills=? WHERE charId=?");
  260. statement.setInt(1, cw.getPlayerKarma());
  261. statement.setInt(2, cw.getPlayerPkKills());
  262. statement.setInt(3, playerId);
  263. if (statement.executeUpdate() != 1)
  264. {
  265. _log.warning("Error while updating karma & pkkills for userId " + cw.getPlayerId());
  266. }
  267. // clean up the cursedweapons table.
  268. removeFromDb(itemId);
  269. }
  270. rset.close();
  271. statement.close();
  272. }
  273. catch (SQLException sqlE)
  274. {
  275. }
  276. }
  277. }
  278. catch (Exception e)
  279. {
  280. if (Config.DEBUG)
  281. _log.log(Level.WARNING, "Could not check CursedWeapons data: " + e.getMessage(), e);
  282. return;
  283. }
  284. finally
  285. {
  286. L2DatabaseFactory.close(con);
  287. }
  288. if (Config.DEBUG)
  289. _log.info("DONE");
  290. }
  291. // =========================================================
  292. // Properties - Public
  293. public synchronized void checkDrop(L2Attackable attackable, L2PcInstance player)
  294. {
  295. if (attackable instanceof L2DefenderInstance || attackable instanceof L2RiftInvaderInstance
  296. || attackable instanceof L2FestivalMonsterInstance || attackable instanceof L2GuardInstance
  297. || attackable instanceof L2GrandBossInstance || attackable instanceof L2FeedableBeastInstance
  298. || attackable instanceof L2FortCommanderInstance)
  299. return;
  300. for (CursedWeapon cw : _cursedWeapons.values())
  301. {
  302. if (cw.isActive())
  303. continue;
  304. if (cw.checkDrop(attackable, player))
  305. break;
  306. }
  307. }
  308. public void activate(L2PcInstance player, L2ItemInstance item)
  309. {
  310. CursedWeapon cw = _cursedWeapons.get(item.getItemId());
  311. if (player.isCursedWeaponEquipped()) // cannot own 2 cursed swords
  312. {
  313. CursedWeapon cw2 = _cursedWeapons.get(player.getCursedWeaponEquippedId());
  314. /* TODO: give the bonus level in a more appropriate manner.
  315. * The following code adds "_stageKills" levels. This will also show in the char status.
  316. * I do not have enough info to know if the bonus should be shown in the pk count, or if it
  317. * should be a full "_stageKills" bonus or just the remaining from the current count till the
  318. * of the current stage...
  319. * This code is a TEMP fix, so that the cursed weapon's bonus level can be observed with as
  320. * little change in the code as possible, until proper info arises.
  321. */
  322. cw2.setNbKills(cw2.getStageKills() - 1);
  323. cw2.increaseKills();
  324. // erase the newly obtained cursed weapon
  325. cw.setPlayer(player); // NECESSARY in order to find which inventory the weapon is in!
  326. cw.endOfLife(); // expire the weapon and clean up.
  327. }
  328. else
  329. cw.activate(player, item);
  330. }
  331. public void drop(int itemId, L2Character killer)
  332. {
  333. CursedWeapon cw = _cursedWeapons.get(itemId);
  334. cw.dropIt(killer);
  335. }
  336. public void increaseKills(int itemId)
  337. {
  338. CursedWeapon cw = _cursedWeapons.get(itemId);
  339. cw.increaseKills();
  340. }
  341. public int getLevel(int itemId)
  342. {
  343. CursedWeapon cw = _cursedWeapons.get(itemId);
  344. return cw.getLevel();
  345. }
  346. public static void announce(SystemMessage sm)
  347. {
  348. Broadcast.toAllOnlinePlayers(sm);
  349. }
  350. public void checkPlayer(L2PcInstance player)
  351. {
  352. if (player == null)
  353. return;
  354. for (CursedWeapon cw : _cursedWeapons.values())
  355. {
  356. if (cw.isActivated() && player.getObjectId() == cw.getPlayerId())
  357. {
  358. cw.setPlayer(player);
  359. cw.setItem(player.getInventory().getItemByItemId(cw.getItemId()));
  360. cw.giveSkill();
  361. player.setCursedWeaponEquippedId(cw.getItemId());
  362. SystemMessage sm = new SystemMessage(SystemMessageId.S2_MINUTE_OF_USAGE_TIME_ARE_LEFT_FOR_S1);
  363. sm.addString(cw.getName());
  364. //sm.addItemName(cw.getItemId());
  365. sm.addNumber((int) ((cw.getEndTime() - System.currentTimeMillis()) / 60000));
  366. player.sendPacket(sm);
  367. }
  368. }
  369. }
  370. public int checkOwnsWeaponId(int ownerId)
  371. {
  372. for (CursedWeapon cw : _cursedWeapons.values())
  373. if (cw.isActivated() && ownerId == cw.getPlayerId())
  374. return cw.getItemId();
  375. return -1;
  376. }
  377. public static void removeFromDb(int itemId)
  378. {
  379. Connection con = null;
  380. try
  381. {
  382. con = L2DatabaseFactory.getInstance().getConnection();
  383. // Delete datas
  384. PreparedStatement statement = con.prepareStatement("DELETE FROM cursed_weapons WHERE itemId = ?");
  385. statement.setInt(1, itemId);
  386. statement.executeUpdate();
  387. statement.close();
  388. }
  389. catch (SQLException e)
  390. {
  391. _log.log(Level.SEVERE, "CursedWeaponsManager: Failed to remove data: " + e.getMessage(), e);
  392. }
  393. finally
  394. {
  395. L2DatabaseFactory.close(con);
  396. }
  397. }
  398. public void saveData()
  399. {
  400. for (CursedWeapon cw : _cursedWeapons.values())
  401. {
  402. cw.saveData();
  403. }
  404. }
  405. // =========================================================
  406. public boolean isCursed(int itemId)
  407. {
  408. return _cursedWeapons.containsKey(itemId);
  409. }
  410. public Collection<CursedWeapon> getCursedWeapons()
  411. {
  412. return _cursedWeapons.values();
  413. }
  414. public Set<Integer> getCursedWeaponsIds()
  415. {
  416. return _cursedWeapons.keySet();
  417. }
  418. public CursedWeapon getCursedWeapon(int itemId)
  419. {
  420. return _cursedWeapons.get(itemId);
  421. }
  422. public void givePassive(int itemId)
  423. {
  424. try
  425. {
  426. _cursedWeapons.get(itemId).giveSkill();
  427. }
  428. catch (Exception e)
  429. {
  430. /***/
  431. }
  432. }
  433. @SuppressWarnings("synthetic-access")
  434. private static class SingletonHolder
  435. {
  436. protected static final CursedWeaponsManager _instance = new CursedWeaponsManager();
  437. }
  438. }