MacroList.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.List;
  20. import java.util.Map;
  21. import java.util.StringTokenizer;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.model.L2Macro.L2MacroCmd;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.gameserver.network.serverpackets.SendMacroList;
  28. import com.l2jserver.util.StringUtil;
  29. import javolution.util.FastList;
  30. import javolution.util.FastMap;
  31. /**
  32. * This class ...
  33. *
  34. * @version $Revision: 1.1.2.1.2.2 $ $Date: 2005/03/02 15:38:41 $
  35. */
  36. public class MacroList
  37. {
  38. private static Logger _log = Logger.getLogger(MacroList.class.getName());
  39. private L2PcInstance _owner;
  40. private int _revision;
  41. private int _macroId;
  42. private Map<Integer, L2Macro> _macroses = new FastMap<Integer, L2Macro>();
  43. public MacroList(L2PcInstance owner)
  44. {
  45. _owner = owner;
  46. _revision = 1;
  47. _macroId = 1000;
  48. }
  49. public int getRevision() {
  50. return _revision;
  51. }
  52. public L2Macro[] getAllMacroses()
  53. {
  54. return _macroses.values().toArray(new L2Macro[_macroses.size()]);
  55. }
  56. public L2Macro getMacro(int id)
  57. {
  58. return _macroses.get(id-1);
  59. }
  60. public void registerMacro(L2Macro macro)
  61. {
  62. if (macro.id == 0) {
  63. macro.id = _macroId++;
  64. while (_macroses.get(macro.id) != null)
  65. macro.id = _macroId++;
  66. _macroses.put(macro.id, macro);
  67. registerMacroInDb(macro);
  68. } else {
  69. L2Macro old = _macroses.put(macro.id, macro);
  70. if (old != null)
  71. deleteMacroFromDb(old);
  72. registerMacroInDb(macro);
  73. }
  74. sendUpdate();
  75. }
  76. public void deleteMacro(int id)
  77. {
  78. L2Macro toRemove = _macroses.get(id);
  79. if(toRemove != null)
  80. {
  81. deleteMacroFromDb(toRemove);
  82. }
  83. _macroses.remove(id);
  84. L2ShortCut[] allShortCuts = _owner.getAllShortCuts();
  85. for (L2ShortCut sc : allShortCuts) {
  86. if (sc.getId() == id && sc.getType() == L2ShortCut.TYPE_MACRO)
  87. _owner.deleteShortCut(sc.getSlot(), sc.getPage());
  88. }
  89. sendUpdate();
  90. }
  91. public void sendUpdate() {
  92. _revision++;
  93. L2Macro[] all = getAllMacroses();
  94. if (all.length == 0) {
  95. _owner.sendPacket(new SendMacroList(_revision, all.length, null));
  96. } else {
  97. for (L2Macro m : all) {
  98. _owner.sendPacket(new SendMacroList(_revision, all.length, m));
  99. }
  100. }
  101. }
  102. private void registerMacroInDb(L2Macro macro)
  103. {
  104. Connection con = null;
  105. try
  106. {
  107. con = L2DatabaseFactory.getInstance().getConnection();
  108. PreparedStatement statement = con.prepareStatement("INSERT INTO character_macroses (charId,id,icon,name,descr,acronym,commands) values(?,?,?,?,?,?,?)");
  109. statement.setInt(1, _owner.getObjectId());
  110. statement.setInt(2, macro.id);
  111. statement.setInt(3, macro.icon);
  112. statement.setString(4, macro.name);
  113. statement.setString(5, macro.descr);
  114. statement.setString(6, macro.acronym);
  115. final StringBuilder sb = new StringBuilder(300);
  116. for (L2MacroCmd cmd : macro.commands) {
  117. StringUtil.append(sb,
  118. String.valueOf(cmd.type),
  119. ",",
  120. String.valueOf(cmd.d1),
  121. ",",
  122. String.valueOf(cmd.d2)
  123. );
  124. if (cmd.cmd != null && cmd.cmd.length() > 0) {
  125. StringUtil.append(sb, ",", cmd.cmd);
  126. }
  127. sb.append(';');
  128. }
  129. if (sb.length() > 255) {
  130. sb.setLength(255);
  131. }
  132. statement.setString(7, sb.toString());
  133. statement.execute();
  134. statement.close();
  135. }
  136. catch (Exception e)
  137. {
  138. _log.log(Level.WARNING, "could not store macro:", e);
  139. }
  140. finally
  141. {
  142. L2DatabaseFactory.close(con);
  143. }
  144. }
  145. /**
  146. * @param shortcut
  147. */
  148. private void deleteMacroFromDb(L2Macro macro)
  149. {
  150. Connection con = null;
  151. try
  152. {
  153. con = L2DatabaseFactory.getInstance().getConnection();
  154. PreparedStatement statement = con.prepareStatement("DELETE FROM character_macroses WHERE charId=? AND id=?");
  155. statement.setInt(1, _owner.getObjectId());
  156. statement.setInt(2, macro.id);
  157. statement.execute();
  158. statement.close();
  159. }
  160. catch (Exception e)
  161. {
  162. _log.log(Level.WARNING, "could not delete macro:", e);
  163. }
  164. finally
  165. {
  166. L2DatabaseFactory.close(con);
  167. }
  168. }
  169. public void restore()
  170. {
  171. _macroses.clear();
  172. Connection con = null;
  173. try
  174. {
  175. con = L2DatabaseFactory.getInstance().getConnection();
  176. PreparedStatement statement = con.prepareStatement("SELECT charId, id, icon, name, descr, acronym, commands FROM character_macroses WHERE charId=?");
  177. statement.setInt(1, _owner.getObjectId());
  178. ResultSet rset = statement.executeQuery();
  179. while (rset.next())
  180. {
  181. int id = rset.getInt("id");
  182. int icon = rset.getInt("icon");
  183. String name = rset.getString("name");
  184. String descr = rset.getString("descr");
  185. String acronym = rset.getString("acronym");
  186. List<L2MacroCmd> commands = FastList.newInstance();
  187. StringTokenizer st1 = new StringTokenizer(rset.getString("commands"),";");
  188. while (st1.hasMoreTokens()) {
  189. StringTokenizer st = new StringTokenizer(st1.nextToken(),",");
  190. if(st.countTokens() < 3)
  191. continue;
  192. int type = 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. cmd = st.nextToken();
  198. L2MacroCmd mcmd = new L2MacroCmd(commands.size(), type, d1, d2, cmd);
  199. commands.add(mcmd);
  200. }
  201. L2Macro m = new L2Macro(id, icon, name, descr, acronym, commands.toArray(new L2MacroCmd[commands.size()]));
  202. FastList.recycle((FastList<?>) commands);
  203. _macroses.put(m.id, m);
  204. }
  205. rset.close();
  206. statement.close();
  207. }
  208. catch (Exception e)
  209. {
  210. _log.log(Level.WARNING, "could not store shortcuts:", e);
  211. }
  212. finally
  213. {
  214. L2DatabaseFactory.close(con);
  215. }
  216. }
  217. }