ShortCuts.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.model;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.Map;
  20. import java.util.TreeMap;
  21. import java.util.logging.Logger;
  22. import net.sf.l2j.L2DatabaseFactory;
  23. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  24. import net.sf.l2j.gameserver.network.serverpackets.ExAutoSoulShot;
  25. import net.sf.l2j.gameserver.network.serverpackets.ShortCutInit;
  26. import net.sf.l2j.gameserver.templates.item.L2EtcItemType;
  27. /**
  28. * This class ...
  29. *
  30. * @version $Revision: 1.1.2.1.2.3 $ $Date: 2005/03/27 15:29:33 $
  31. */
  32. public class ShortCuts
  33. {
  34. private static Logger _log = Logger.getLogger(ShortCuts.class.getName());
  35. private L2PcInstance _owner;
  36. private Map<Integer, L2ShortCut> _shortCuts = new TreeMap<Integer, L2ShortCut>();
  37. public ShortCuts(L2PcInstance owner)
  38. {
  39. _owner = owner;
  40. }
  41. public L2ShortCut[] getAllShortCuts()
  42. {
  43. return _shortCuts.values().toArray(new L2ShortCut[_shortCuts.values().size()]);
  44. }
  45. public L2ShortCut getShortCut(int slot, int page)
  46. {
  47. L2ShortCut sc = _shortCuts.get(slot + page * 12);
  48. // verify shortcut
  49. if (sc != null && sc.getType() == L2ShortCut.TYPE_ITEM)
  50. {
  51. if (_owner.getInventory().getItemByObjectId(sc.getId()) == null)
  52. {
  53. deleteShortCut(sc.getSlot(), sc.getPage());
  54. sc = null;
  55. }
  56. }
  57. return sc;
  58. }
  59. public synchronized void registerShortCut(L2ShortCut shortcut)
  60. {
  61. L2ShortCut oldShortCut = _shortCuts.put(shortcut.getSlot() + 12 * shortcut.getPage(), shortcut);
  62. registerShortCutInDb(shortcut, oldShortCut);
  63. }
  64. private void registerShortCutInDb(L2ShortCut shortcut, L2ShortCut oldShortCut)
  65. {
  66. if (oldShortCut != null)
  67. deleteShortCutFromDb(oldShortCut);
  68. Connection con = null;
  69. try
  70. {
  71. con = L2DatabaseFactory.getInstance().getConnection();
  72. PreparedStatement statement = con.prepareStatement("REPLACE INTO character_shortcuts (charId,slot,page,type,shortcut_id,level,class_index) values(?,?,?,?,?,?,?)");
  73. statement.setInt(1, _owner.getObjectId());
  74. statement.setInt(2, shortcut.getSlot());
  75. statement.setInt(3, shortcut.getPage());
  76. statement.setInt(4, shortcut.getType());
  77. statement.setInt(5, shortcut.getId());
  78. statement.setInt(6, shortcut.getLevel());
  79. statement.setInt(7, _owner.getClassIndex());
  80. statement.execute();
  81. statement.close();
  82. }
  83. catch (Exception e)
  84. {
  85. _log.warning("Could not store character shortcut: " + e);
  86. }
  87. finally
  88. {
  89. try { con.close(); } catch (Exception e) {}
  90. }
  91. }
  92. /**
  93. * @param slot
  94. */
  95. public synchronized void deleteShortCut(int slot, int page)
  96. {
  97. L2ShortCut old = _shortCuts.remove(slot+page*12);
  98. if (old == null || _owner == null)
  99. return;
  100. deleteShortCutFromDb(old);
  101. if (old.getType() == L2ShortCut.TYPE_ITEM)
  102. {
  103. L2ItemInstance item = _owner.getInventory().getItemByObjectId(old.getId());
  104. if ((item != null) && (item.getItemType() == L2EtcItemType.SHOT))
  105. {
  106. _owner.removeAutoSoulShot(item.getItemId());
  107. _owner.sendPacket(new ExAutoSoulShot(item.getItemId(), 0));
  108. }
  109. }
  110. _owner.sendPacket(new ShortCutInit(_owner));
  111. for (int shotId : _owner.getAutoSoulShot().values())
  112. _owner.sendPacket(new ExAutoSoulShot(shotId, 1));
  113. }
  114. public synchronized void deleteShortCutByObjectId(int objectId)
  115. {
  116. L2ShortCut toRemove = null;
  117. for (L2ShortCut shortcut : _shortCuts.values())
  118. {
  119. if (shortcut.getType() == L2ShortCut.TYPE_ITEM && shortcut.getId() == objectId)
  120. {
  121. toRemove = shortcut;
  122. break;
  123. }
  124. }
  125. if (toRemove != null)
  126. deleteShortCut(toRemove.getSlot(), toRemove.getPage());
  127. }
  128. /**
  129. * @param shortcut
  130. */
  131. private void deleteShortCutFromDb(L2ShortCut shortcut)
  132. {
  133. Connection con = null;
  134. try
  135. {
  136. con = L2DatabaseFactory.getInstance().getConnection();
  137. PreparedStatement statement = con.prepareStatement("DELETE FROM character_shortcuts WHERE charId=? AND slot=? AND page=? AND class_index=?");
  138. statement.setInt(1, _owner.getObjectId());
  139. statement.setInt(2, shortcut.getSlot());
  140. statement.setInt(3, shortcut.getPage());
  141. statement.setInt(4, _owner.getClassIndex());
  142. statement.execute();
  143. statement.close();
  144. }
  145. catch (Exception e)
  146. {
  147. _log.warning("Could not delete character shortcut: " + e);
  148. }
  149. finally
  150. {
  151. try { con.close(); } catch (Exception e) {}
  152. }
  153. }
  154. public void restore()
  155. {
  156. _shortCuts.clear();
  157. Connection con = null;
  158. try
  159. {
  160. con = L2DatabaseFactory.getInstance().getConnection();
  161. PreparedStatement statement = con.prepareStatement("SELECT charId, slot, page, type, shortcut_id, level FROM character_shortcuts WHERE charId=? AND class_index=?");
  162. statement.setInt(1, _owner.getObjectId());
  163. statement.setInt(2, _owner.getClassIndex());
  164. ResultSet rset = statement.executeQuery();
  165. while (rset.next())
  166. {
  167. int slot = rset.getInt("slot");
  168. int page = rset.getInt("page");
  169. int type = rset.getInt("type");
  170. int id = rset.getInt("shortcut_id");
  171. int level = rset.getInt("level");
  172. L2ShortCut sc = new L2ShortCut(slot, page, type, id, level, 1);
  173. _shortCuts.put(slot+page*12, sc);
  174. }
  175. rset.close();
  176. statement.close();
  177. }
  178. catch (Exception e)
  179. {
  180. _log.warning("Could not restore character shortcuts: " + e);
  181. }
  182. finally
  183. {
  184. try { con.close(); } catch (Exception e) {}
  185. }
  186. // verify shortcuts
  187. for (L2ShortCut sc : getAllShortCuts())
  188. {
  189. if (sc.getType() == L2ShortCut.TYPE_ITEM)
  190. {
  191. if (_owner.getInventory().getItemByObjectId(sc.getId()) == null)
  192. deleteShortCut(sc.getSlot(), sc.getPage());
  193. }
  194. }
  195. }
  196. }