ShortCuts.java 7.7 KB

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