ShortCuts.java 7.8 KB

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