ShortCuts.java 7.7 KB

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