ShortCuts.java 7.1 KB

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