CursedWeaponsManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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.actor.L2Attackable;
  35. import com.l2jserver.gameserver.model.actor.L2Character;
  36. import com.l2jserver.gameserver.model.actor.instance.L2DefenderInstance;
  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.L2GrandBossInstance;
  41. import com.l2jserver.gameserver.model.actor.instance.L2GuardInstance;
  42. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  43. import com.l2jserver.gameserver.model.actor.instance.L2RiftInvaderInstance;
  44. import com.l2jserver.gameserver.model.item.instance.L2ItemInstance;
  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.close();
  241. statement = con.prepareStatement("DELETE FROM items WHERE owner_id=? AND item_id=?");
  242. statement.setInt(1, playerId);
  243. statement.setInt(2, itemId);
  244. if (statement.executeUpdate() != 1)
  245. {
  246. _log.warning("Error while deleting cursed weapon " + itemId + " from userId " + playerId);
  247. }
  248. statement.close();
  249. // Delete the skill
  250. /*
  251. statement = con.prepareStatement("DELETE FROM character_skills WHERE charId=? AND skill_id=");
  252. statement.setInt(1, playerId);
  253. statement.setInt(2, cw.getSkillId());
  254. if (statement.executeUpdate() != 1)
  255. {
  256. _log.warning("Error while deleting cursed weapon "+itemId+" skill from userId "+playerId);
  257. }
  258. */
  259. // Restore the player's old karma and pk count
  260. statement = con.prepareStatement("UPDATE characters SET karma=?, pkkills=? WHERE charId=?");
  261. statement.setInt(1, cw.getPlayerKarma());
  262. statement.setInt(2, cw.getPlayerPkKills());
  263. statement.setInt(3, playerId);
  264. if (statement.executeUpdate() != 1)
  265. {
  266. _log.warning("Error while updating karma & pkkills for userId " + cw.getPlayerId());
  267. }
  268. // clean up the cursedweapons table.
  269. removeFromDb(itemId);
  270. }
  271. rset.close();
  272. statement.close();
  273. }
  274. catch (SQLException sqlE)
  275. {
  276. }
  277. }
  278. }
  279. catch (Exception e)
  280. {
  281. if (Config.DEBUG)
  282. _log.log(Level.WARNING, "Could not check CursedWeapons data: " + e.getMessage(), e);
  283. return;
  284. }
  285. finally
  286. {
  287. L2DatabaseFactory.close(con);
  288. }
  289. if (Config.DEBUG)
  290. _log.info("DONE");
  291. }
  292. // =========================================================
  293. // Properties - Public
  294. public synchronized void checkDrop(L2Attackable attackable, L2PcInstance player)
  295. {
  296. if (attackable instanceof L2DefenderInstance || attackable instanceof L2RiftInvaderInstance
  297. || attackable instanceof L2FestivalMonsterInstance || attackable instanceof L2GuardInstance
  298. || attackable instanceof L2GrandBossInstance || attackable instanceof L2FeedableBeastInstance
  299. || attackable instanceof L2FortCommanderInstance)
  300. return;
  301. for (CursedWeapon cw : _cursedWeapons.values())
  302. {
  303. if (cw.isActive())
  304. continue;
  305. if (cw.checkDrop(attackable, player))
  306. break;
  307. }
  308. }
  309. public void activate(L2PcInstance player, L2ItemInstance item)
  310. {
  311. CursedWeapon cw = _cursedWeapons.get(item.getItemId());
  312. if (player.isCursedWeaponEquipped()) // cannot own 2 cursed swords
  313. {
  314. CursedWeapon cw2 = _cursedWeapons.get(player.getCursedWeaponEquippedId());
  315. /* TODO: give the bonus level in a more appropriate manner.
  316. * The following code adds "_stageKills" levels. This will also show in the char status.
  317. * I do not have enough info to know if the bonus should be shown in the pk count, or if it
  318. * should be a full "_stageKills" bonus or just the remaining from the current count till the
  319. * of the current stage...
  320. * This code is a TEMP fix, so that the cursed weapon's bonus level can be observed with as
  321. * little change in the code as possible, until proper info arises.
  322. */
  323. cw2.setNbKills(cw2.getStageKills() - 1);
  324. cw2.increaseKills();
  325. // erase the newly obtained cursed weapon
  326. cw.setPlayer(player); // NECESSARY in order to find which inventory the weapon is in!
  327. cw.endOfLife(); // expire the weapon and clean up.
  328. }
  329. else
  330. cw.activate(player, item);
  331. }
  332. public void drop(int itemId, L2Character killer)
  333. {
  334. CursedWeapon cw = _cursedWeapons.get(itemId);
  335. cw.dropIt(killer);
  336. }
  337. public void increaseKills(int itemId)
  338. {
  339. CursedWeapon cw = _cursedWeapons.get(itemId);
  340. cw.increaseKills();
  341. }
  342. public int getLevel(int itemId)
  343. {
  344. CursedWeapon cw = _cursedWeapons.get(itemId);
  345. return cw.getLevel();
  346. }
  347. public static void announce(SystemMessage sm)
  348. {
  349. Broadcast.toAllOnlinePlayers(sm);
  350. }
  351. public void checkPlayer(L2PcInstance player)
  352. {
  353. if (player == null)
  354. return;
  355. for (CursedWeapon cw : _cursedWeapons.values())
  356. {
  357. if (cw.isActivated() && player.getObjectId() == cw.getPlayerId())
  358. {
  359. cw.setPlayer(player);
  360. cw.setItem(player.getInventory().getItemByItemId(cw.getItemId()));
  361. cw.giveSkill();
  362. player.setCursedWeaponEquippedId(cw.getItemId());
  363. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S2_MINUTE_OF_USAGE_TIME_ARE_LEFT_FOR_S1);
  364. sm.addString(cw.getName());
  365. //sm.addItemName(cw.getItemId());
  366. sm.addNumber((int) ((cw.getEndTime() - System.currentTimeMillis()) / 60000));
  367. player.sendPacket(sm);
  368. }
  369. }
  370. }
  371. public int checkOwnsWeaponId(int ownerId)
  372. {
  373. for (CursedWeapon cw : _cursedWeapons.values())
  374. if (cw.isActivated() && ownerId == cw.getPlayerId())
  375. return cw.getItemId();
  376. return -1;
  377. }
  378. public static void removeFromDb(int itemId)
  379. {
  380. Connection con = null;
  381. try
  382. {
  383. con = L2DatabaseFactory.getInstance().getConnection();
  384. // Delete datas
  385. PreparedStatement statement = con.prepareStatement("DELETE FROM cursed_weapons WHERE itemId = ?");
  386. statement.setInt(1, itemId);
  387. statement.executeUpdate();
  388. statement.close();
  389. }
  390. catch (SQLException e)
  391. {
  392. _log.log(Level.SEVERE, "CursedWeaponsManager: Failed to remove data: " + e.getMessage(), e);
  393. }
  394. finally
  395. {
  396. L2DatabaseFactory.close(con);
  397. }
  398. }
  399. public void saveData()
  400. {
  401. for (CursedWeapon cw : _cursedWeapons.values())
  402. {
  403. cw.saveData();
  404. }
  405. }
  406. // =========================================================
  407. public boolean isCursed(int itemId)
  408. {
  409. return _cursedWeapons.containsKey(itemId);
  410. }
  411. public Collection<CursedWeapon> getCursedWeapons()
  412. {
  413. return _cursedWeapons.values();
  414. }
  415. public Set<Integer> getCursedWeaponsIds()
  416. {
  417. return _cursedWeapons.keySet();
  418. }
  419. public CursedWeapon getCursedWeapon(int itemId)
  420. {
  421. return _cursedWeapons.get(itemId);
  422. }
  423. public void givePassive(int itemId)
  424. {
  425. try
  426. {
  427. _cursedWeapons.get(itemId).giveSkill();
  428. }
  429. catch (Exception e)
  430. {
  431. /***/
  432. }
  433. }
  434. @SuppressWarnings("synthetic-access")
  435. private static class SingletonHolder
  436. {
  437. protected static final CursedWeaponsManager _instance = new CursedWeaponsManager();
  438. }
  439. }