ShortCuts.java 7.7 KB

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