ShortCuts.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.Map;
  24. import java.util.TreeMap;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.model.interfaces.IRestorable;
  30. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  31. import com.l2jserver.gameserver.model.items.type.L2EtcItemType;
  32. import com.l2jserver.gameserver.network.serverpackets.ExAutoSoulShot;
  33. import com.l2jserver.gameserver.network.serverpackets.ShortCutInit;
  34. import com.l2jserver.gameserver.network.serverpackets.ShortCutRegister;
  35. public class ShortCuts implements IRestorable
  36. {
  37. private static Logger _log = Logger.getLogger(ShortCuts.class.getName());
  38. private static final int MAX_SHORTCUTS_PER_BAR = 12;
  39. private final L2PcInstance _owner;
  40. private final Map<Integer, L2ShortCut> _shortCuts = new TreeMap<>();
  41. public ShortCuts(L2PcInstance owner)
  42. {
  43. _owner = owner;
  44. }
  45. public L2ShortCut[] getAllShortCuts()
  46. {
  47. return _shortCuts.values().toArray(new L2ShortCut[_shortCuts.values().size()]);
  48. }
  49. public L2ShortCut getShortCut(int slot, int page)
  50. {
  51. L2ShortCut sc = _shortCuts.get(slot + (page * MAX_SHORTCUTS_PER_BAR));
  52. // Verify shortcut
  53. if ((sc != null) && (sc.getType() == L2ShortCut.TYPE_ITEM))
  54. {
  55. if (_owner.getInventory().getItemByObjectId(sc.getId()) == null)
  56. {
  57. deleteShortCut(sc.getSlot(), sc.getPage());
  58. sc = null;
  59. }
  60. }
  61. return sc;
  62. }
  63. public synchronized void registerShortCut(L2ShortCut shortcut)
  64. {
  65. // Verify shortcut
  66. if (shortcut.getType() == L2ShortCut.TYPE_ITEM)
  67. {
  68. final L2ItemInstance item = _owner.getInventory().getItemByObjectId(shortcut.getId());
  69. if (item == null)
  70. {
  71. return;
  72. }
  73. shortcut.setSharedReuseGroup(item.getSharedReuseGroup());
  74. }
  75. final L2ShortCut oldShortCut = _shortCuts.put(shortcut.getSlot() + (shortcut.getPage() * MAX_SHORTCUTS_PER_BAR), shortcut);
  76. registerShortCutInDb(shortcut, oldShortCut);
  77. }
  78. private void registerShortCutInDb(L2ShortCut shortcut, L2ShortCut oldShortCut)
  79. {
  80. if (oldShortCut != null)
  81. {
  82. deleteShortCutFromDb(oldShortCut);
  83. }
  84. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  85. PreparedStatement statement = con.prepareStatement("REPLACE INTO character_shortcuts (charId,slot,page,type,shortcut_id,level,class_index) values(?,?,?,?,?,?,?)"))
  86. {
  87. statement.setInt(1, _owner.getObjectId());
  88. statement.setInt(2, shortcut.getSlot());
  89. statement.setInt(3, shortcut.getPage());
  90. statement.setInt(4, shortcut.getType());
  91. statement.setInt(5, shortcut.getId());
  92. statement.setInt(6, shortcut.getLevel());
  93. statement.setInt(7, _owner.getClassIndex());
  94. statement.execute();
  95. }
  96. catch (Exception e)
  97. {
  98. _log.log(Level.WARNING, "Could not store character shortcut: " + e.getMessage(), e);
  99. }
  100. }
  101. /**
  102. * @param slot
  103. * @param page
  104. */
  105. public synchronized void deleteShortCut(int slot, int page)
  106. {
  107. final L2ShortCut old = _shortCuts.remove(slot + (page * MAX_SHORTCUTS_PER_BAR));
  108. if ((old == null) || (_owner == null))
  109. {
  110. return;
  111. }
  112. deleteShortCutFromDb(old);
  113. if (old.getType() == L2ShortCut.TYPE_ITEM)
  114. {
  115. L2ItemInstance item = _owner.getInventory().getItemByObjectId(old.getId());
  116. if ((item != null) && (item.getItemType() == L2EtcItemType.SHOT))
  117. {
  118. if (_owner.removeAutoSoulShot(item.getId()))
  119. {
  120. _owner.sendPacket(new ExAutoSoulShot(item.getId(), 0));
  121. }
  122. }
  123. }
  124. _owner.sendPacket(new ShortCutInit(_owner));
  125. for (int shotId : _owner.getAutoSoulShot())
  126. {
  127. _owner.sendPacket(new ExAutoSoulShot(shotId, 1));
  128. }
  129. }
  130. public synchronized void deleteShortCutByObjectId(int objectId)
  131. {
  132. for (L2ShortCut shortcut : _shortCuts.values())
  133. {
  134. if ((shortcut.getType() == L2ShortCut.TYPE_ITEM) && (shortcut.getId() == objectId))
  135. {
  136. deleteShortCut(shortcut.getSlot(), shortcut.getPage());
  137. break;
  138. }
  139. }
  140. }
  141. /**
  142. * @param shortcut
  143. */
  144. private void deleteShortCutFromDb(L2ShortCut shortcut)
  145. {
  146. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  147. PreparedStatement statement = con.prepareStatement("DELETE FROM character_shortcuts WHERE charId=? AND slot=? AND page=? AND class_index=?"))
  148. {
  149. statement.setInt(1, _owner.getObjectId());
  150. statement.setInt(2, shortcut.getSlot());
  151. statement.setInt(3, shortcut.getPage());
  152. statement.setInt(4, _owner.getClassIndex());
  153. statement.execute();
  154. }
  155. catch (Exception e)
  156. {
  157. _log.log(Level.WARNING, "Could not delete character shortcut: " + e.getMessage(), e);
  158. }
  159. }
  160. @Override
  161. public boolean restoreMe()
  162. {
  163. _shortCuts.clear();
  164. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  165. PreparedStatement statement = con.prepareStatement("SELECT charId, slot, page, type, shortcut_id, level FROM character_shortcuts WHERE charId=? AND class_index=?"))
  166. {
  167. statement.setInt(1, _owner.getObjectId());
  168. statement.setInt(2, _owner.getClassIndex());
  169. try (ResultSet rset = statement.executeQuery())
  170. {
  171. while (rset.next())
  172. {
  173. int slot = rset.getInt("slot");
  174. int page = rset.getInt("page");
  175. int type = rset.getInt("type");
  176. int id = rset.getInt("shortcut_id");
  177. int level = rset.getInt("level");
  178. _shortCuts.put(slot + (page * MAX_SHORTCUTS_PER_BAR), new L2ShortCut(slot, page, type, id, level, 1));
  179. }
  180. }
  181. }
  182. catch (Exception e)
  183. {
  184. _log.log(Level.WARNING, "Could not restore character shortcuts: " + e.getMessage(), e);
  185. return false;
  186. }
  187. // Verify shortcuts
  188. for (L2ShortCut sc : getAllShortCuts())
  189. {
  190. if (sc.getType() == L2ShortCut.TYPE_ITEM)
  191. {
  192. L2ItemInstance item = _owner.getInventory().getItemByObjectId(sc.getId());
  193. if (item == null)
  194. {
  195. deleteShortCut(sc.getSlot(), sc.getPage());
  196. }
  197. else if (item.isEtcItem())
  198. {
  199. sc.setSharedReuseGroup(item.getEtcItem().getSharedReuseGroup());
  200. }
  201. }
  202. }
  203. return true;
  204. }
  205. /**
  206. * Updates the shortcut bars with the new skill.
  207. * @param skillId the skill Id to search and update.
  208. * @param skillLevel the skill level to update.
  209. */
  210. public synchronized void updateShortCuts(int skillId, int skillLevel)
  211. {
  212. // Update all the shortcuts for this skill
  213. for (L2ShortCut sc : _shortCuts.values())
  214. {
  215. if ((sc.getId() == skillId) && (sc.getType() == L2ShortCut.TYPE_SKILL))
  216. {
  217. L2ShortCut newsc = new L2ShortCut(sc.getSlot(), sc.getPage(), sc.getType(), sc.getId(), skillLevel, 1);
  218. _owner.sendPacket(new ShortCutRegister(newsc));
  219. _owner.registerShortCut(newsc);
  220. }
  221. }
  222. }
  223. }