ShortCuts.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.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 com.l2jserver.L2DatabaseFactory;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.network.serverpackets.ExAutoSoulShot;
  25. import com.l2jserver.gameserver.network.serverpackets.ShortCutInit;
  26. import com.l2jserver.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. // verify shortcut
  62. if (shortcut.getType() == L2ShortCut.TYPE_ITEM)
  63. {
  64. L2ItemInstance item = _owner.getInventory().getItemByObjectId(shortcut.getId());
  65. if (item == null)
  66. return;
  67. if (item.isEtcItem())
  68. shortcut.setSharedReuseGroup(item.getEtcItem().getSharedReuseGroup());
  69. }
  70. L2ShortCut oldShortCut = _shortCuts.put(shortcut.getSlot() + 12 * shortcut.getPage(), shortcut);
  71. registerShortCutInDb(shortcut, oldShortCut);
  72. }
  73. private void registerShortCutInDb(L2ShortCut shortcut, L2ShortCut oldShortCut)
  74. {
  75. if (oldShortCut != null)
  76. deleteShortCutFromDb(oldShortCut);
  77. Connection con = null;
  78. try
  79. {
  80. con = L2DatabaseFactory.getInstance().getConnection();
  81. PreparedStatement statement = con.prepareStatement("REPLACE INTO character_shortcuts (charId,slot,page,type,shortcut_id,level,class_index) values(?,?,?,?,?,?,?)");
  82. statement.setInt(1, _owner.getObjectId());
  83. statement.setInt(2, shortcut.getSlot());
  84. statement.setInt(3, shortcut.getPage());
  85. statement.setInt(4, shortcut.getType());
  86. statement.setInt(5, shortcut.getId());
  87. statement.setInt(6, shortcut.getLevel());
  88. statement.setInt(7, _owner.getClassIndex());
  89. statement.execute();
  90. statement.close();
  91. }
  92. catch (Exception e)
  93. {
  94. _log.warning("Could not store character shortcut: " + e);
  95. }
  96. finally
  97. {
  98. try { con.close(); } catch (Exception e) {}
  99. }
  100. }
  101. /**
  102. * @param slot
  103. */
  104. public synchronized void deleteShortCut(int slot, int page)
  105. {
  106. L2ShortCut old = _shortCuts.remove(slot+page*12);
  107. if (old == null || _owner == null)
  108. return;
  109. deleteShortCutFromDb(old);
  110. if (old.getType() == L2ShortCut.TYPE_ITEM)
  111. {
  112. L2ItemInstance item = _owner.getInventory().getItemByObjectId(old.getId());
  113. if ((item != null) && (item.getItemType() == L2EtcItemType.SHOT))
  114. {
  115. _owner.removeAutoSoulShot(item.getItemId());
  116. _owner.sendPacket(new ExAutoSoulShot(item.getItemId(), 0));
  117. }
  118. }
  119. _owner.sendPacket(new ShortCutInit(_owner));
  120. for (int shotId : _owner.getAutoSoulShot().values())
  121. _owner.sendPacket(new ExAutoSoulShot(shotId, 1));
  122. }
  123. public synchronized void deleteShortCutByObjectId(int objectId)
  124. {
  125. L2ShortCut toRemove = null;
  126. for (L2ShortCut shortcut : _shortCuts.values())
  127. {
  128. if (shortcut.getType() == L2ShortCut.TYPE_ITEM && shortcut.getId() == objectId)
  129. {
  130. toRemove = shortcut;
  131. break;
  132. }
  133. }
  134. if (toRemove != null)
  135. deleteShortCut(toRemove.getSlot(), toRemove.getPage());
  136. }
  137. /**
  138. * @param shortcut
  139. */
  140. private void deleteShortCutFromDb(L2ShortCut shortcut)
  141. {
  142. Connection con = null;
  143. try
  144. {
  145. con = L2DatabaseFactory.getInstance().getConnection();
  146. PreparedStatement statement = con.prepareStatement("DELETE FROM character_shortcuts WHERE charId=? AND slot=? AND page=? AND class_index=?");
  147. statement.setInt(1, _owner.getObjectId());
  148. statement.setInt(2, shortcut.getSlot());
  149. statement.setInt(3, shortcut.getPage());
  150. statement.setInt(4, _owner.getClassIndex());
  151. statement.execute();
  152. statement.close();
  153. }
  154. catch (Exception e)
  155. {
  156. _log.warning("Could not delete character shortcut: " + e);
  157. }
  158. finally
  159. {
  160. try { con.close(); } catch (Exception e) {}
  161. }
  162. }
  163. public void restore()
  164. {
  165. _shortCuts.clear();
  166. Connection con = null;
  167. try
  168. {
  169. con = L2DatabaseFactory.getInstance().getConnection();
  170. PreparedStatement statement = con.prepareStatement("SELECT charId, slot, page, type, shortcut_id, level FROM character_shortcuts WHERE charId=? AND class_index=?");
  171. statement.setInt(1, _owner.getObjectId());
  172. statement.setInt(2, _owner.getClassIndex());
  173. ResultSet rset = statement.executeQuery();
  174. while (rset.next())
  175. {
  176. int slot = rset.getInt("slot");
  177. int page = rset.getInt("page");
  178. int type = rset.getInt("type");
  179. int id = rset.getInt("shortcut_id");
  180. int level = rset.getInt("level");
  181. L2ShortCut sc = new L2ShortCut(slot, page, type, id, level, 1);
  182. _shortCuts.put(slot+page*12, sc);
  183. }
  184. rset.close();
  185. statement.close();
  186. }
  187. catch (Exception e)
  188. {
  189. _log.warning("Could not restore character shortcuts: " + e);
  190. }
  191. finally
  192. {
  193. try { con.close(); } catch (Exception e) {}
  194. }
  195. // verify shortcuts
  196. for (L2ShortCut sc : getAllShortCuts())
  197. {
  198. if (sc.getType() == L2ShortCut.TYPE_ITEM)
  199. {
  200. L2ItemInstance item = _owner.getInventory().getItemByObjectId(sc.getId());
  201. if (item == null)
  202. deleteShortCut(sc.getSlot(), sc.getPage());
  203. else if (item.isEtcItem())
  204. sc.setSharedReuseGroup(item.getEtcItem().getSharedReuseGroup());
  205. }
  206. }
  207. }
  208. }