ShortCuts.java 7.5 KB

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