MacroList.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.LinkedHashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.StringTokenizer;
  30. import java.util.logging.Level;
  31. import java.util.logging.Logger;
  32. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  33. import com.l2jserver.gameserver.enums.MacroType;
  34. import com.l2jserver.gameserver.enums.ShortcutType;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  36. import com.l2jserver.gameserver.model.interfaces.IRestorable;
  37. import com.l2jserver.gameserver.network.serverpackets.SendMacroList;
  38. import com.l2jserver.util.StringUtil;
  39. public class MacroList implements IRestorable
  40. {
  41. private static final Logger _log = Logger.getLogger(MacroList.class.getName());
  42. private final L2PcInstance _owner;
  43. private int _revision;
  44. private int _macroId;
  45. private final Map<Integer, Macro> _macroses = Collections.synchronizedMap(new LinkedHashMap<Integer, Macro>());
  46. public MacroList(L2PcInstance owner)
  47. {
  48. _owner = owner;
  49. _revision = 1;
  50. _macroId = 1000;
  51. }
  52. public int getRevision()
  53. {
  54. return _revision;
  55. }
  56. public Map<Integer, Macro> getAllMacroses()
  57. {
  58. return _macroses;
  59. }
  60. public void registerMacro(Macro macro)
  61. {
  62. if (macro.getId() == 0)
  63. {
  64. macro.setId(_macroId++);
  65. while (_macroses.containsKey(macro.getId()))
  66. {
  67. macro.setId(_macroId++);
  68. }
  69. _macroses.put(macro.getId(), macro);
  70. registerMacroInDb(macro);
  71. }
  72. else
  73. {
  74. final Macro old = _macroses.put(macro.getId(), macro);
  75. if (old != null)
  76. {
  77. deleteMacroFromDb(old);
  78. }
  79. registerMacroInDb(macro);
  80. }
  81. sendUpdate();
  82. }
  83. public void deleteMacro(int id)
  84. {
  85. final Macro removed = _macroses.remove(id);
  86. if (removed != null)
  87. {
  88. deleteMacroFromDb(removed);
  89. }
  90. final Shortcut[] allShortCuts = _owner.getAllShortCuts();
  91. for (Shortcut sc : allShortCuts)
  92. {
  93. if ((sc.getId() == id) && (sc.getType() == ShortcutType.MACRO))
  94. {
  95. _owner.deleteShortCut(sc.getSlot(), sc.getPage());
  96. }
  97. }
  98. sendUpdate();
  99. }
  100. public void sendUpdate()
  101. {
  102. _revision++;
  103. final Collection<Macro> allMacros = _macroses.values();
  104. synchronized (_macroses)
  105. {
  106. if (allMacros.isEmpty())
  107. {
  108. _owner.sendPacket(new SendMacroList(_revision, 0, null));
  109. }
  110. else
  111. {
  112. for (Macro m : allMacros)
  113. {
  114. _owner.sendPacket(new SendMacroList(_revision, allMacros.size(), m));
  115. }
  116. }
  117. }
  118. }
  119. private void registerMacroInDb(Macro macro)
  120. {
  121. try (Connection con = ConnectionFactory.getInstance().getConnection();
  122. PreparedStatement ps = con.prepareStatement("INSERT INTO character_macroses (charId,id,icon,name,descr,acronym,commands) values(?,?,?,?,?,?,?)"))
  123. {
  124. ps.setInt(1, _owner.getObjectId());
  125. ps.setInt(2, macro.getId());
  126. ps.setInt(3, macro.getIcon());
  127. ps.setString(4, macro.getName());
  128. ps.setString(5, macro.getDescr());
  129. ps.setString(6, macro.getAcronym());
  130. final StringBuilder sb = new StringBuilder(300);
  131. for (MacroCmd cmd : macro.getCommands())
  132. {
  133. StringUtil.append(sb, String.valueOf(cmd.getType().ordinal()), ",", String.valueOf(cmd.getD1()), ",", String.valueOf(cmd.getD2()));
  134. if ((cmd.getCmd() != null) && (cmd.getCmd().length() > 0))
  135. {
  136. StringUtil.append(sb, ",", cmd.getCmd());
  137. }
  138. sb.append(';');
  139. }
  140. if (sb.length() > 255)
  141. {
  142. sb.setLength(255);
  143. }
  144. ps.setString(7, sb.toString());
  145. ps.execute();
  146. }
  147. catch (Exception e)
  148. {
  149. _log.log(Level.WARNING, "could not store macro:", e);
  150. }
  151. }
  152. private void deleteMacroFromDb(Macro macro)
  153. {
  154. try (Connection con = ConnectionFactory.getInstance().getConnection();
  155. PreparedStatement ps = con.prepareStatement("DELETE FROM character_macroses WHERE charId=? AND id=?"))
  156. {
  157. ps.setInt(1, _owner.getObjectId());
  158. ps.setInt(2, macro.getId());
  159. ps.execute();
  160. }
  161. catch (Exception e)
  162. {
  163. _log.log(Level.WARNING, "could not delete macro:", e);
  164. }
  165. }
  166. @Override
  167. public boolean restoreMe()
  168. {
  169. _macroses.clear();
  170. try (Connection con = ConnectionFactory.getInstance().getConnection();
  171. PreparedStatement ps = con.prepareStatement("SELECT charId, id, icon, name, descr, acronym, commands FROM character_macroses WHERE charId=?"))
  172. {
  173. ps.setInt(1, _owner.getObjectId());
  174. try (ResultSet rset = ps.executeQuery())
  175. {
  176. while (rset.next())
  177. {
  178. int id = rset.getInt("id");
  179. int icon = rset.getInt("icon");
  180. String name = rset.getString("name");
  181. String descr = rset.getString("descr");
  182. String acronym = rset.getString("acronym");
  183. List<MacroCmd> commands = new ArrayList<>();
  184. StringTokenizer st1 = new StringTokenizer(rset.getString("commands"), ";");
  185. while (st1.hasMoreTokens())
  186. {
  187. StringTokenizer st = new StringTokenizer(st1.nextToken(), ",");
  188. if (st.countTokens() < 3)
  189. {
  190. continue;
  191. }
  192. MacroType type = MacroType.values()[Integer.parseInt(st.nextToken())];
  193. int d1 = Integer.parseInt(st.nextToken());
  194. int d2 = Integer.parseInt(st.nextToken());
  195. String cmd = "";
  196. if (st.hasMoreTokens())
  197. {
  198. cmd = st.nextToken();
  199. }
  200. commands.add(new MacroCmd(commands.size(), type, d1, d2, cmd));
  201. }
  202. _macroses.put(id, new Macro(id, icon, name, descr, acronym, commands));
  203. }
  204. }
  205. }
  206. catch (Exception e)
  207. {
  208. _log.log(Level.WARNING, "could not store shortcuts:", e);
  209. return false;
  210. }
  211. return true;
  212. }
  213. }