L2UIKeysSettings.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.sql.SQLException;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import javolution.util.FastList;
  25. import javolution.util.FastMap;
  26. import com.l2jserver.L2DatabaseFactory;
  27. import com.l2jserver.gameserver.datatables.UITable;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.model.entity.ActionKey;
  30. /**
  31. *
  32. * @author mrTJO
  33. */
  34. public class L2UIKeysSettings
  35. {
  36. protected static final Logger _log = Logger.getLogger(L2UIKeysSettings.class.getName());
  37. private final L2PcInstance _player;
  38. Map<Integer, List<ActionKey>> _storedKeys;
  39. Map<Integer, List<Integer>> _storedCategories;
  40. boolean _saved = true;
  41. public L2UIKeysSettings(L2PcInstance player)
  42. {
  43. _player = player;
  44. loadFromDB();
  45. }
  46. public void storeAll(Map<Integer, List<Integer>> catMap, Map<Integer, List<ActionKey>> keyMap)
  47. {
  48. _saved = false;
  49. _storedCategories = catMap;
  50. _storedKeys = keyMap;
  51. }
  52. public void storeCategories(Map<Integer, List<Integer>> catMap)
  53. {
  54. _saved = false;
  55. _storedCategories = catMap;
  56. }
  57. public Map<Integer, List<Integer>> getCategories()
  58. {
  59. return _storedCategories;
  60. }
  61. public void storeKeys(Map<Integer, List<ActionKey>> keyMap)
  62. {
  63. _saved = false;
  64. _storedKeys = keyMap;
  65. }
  66. public Map<Integer, List<ActionKey>> getKeys()
  67. {
  68. return _storedKeys;
  69. }
  70. public void loadFromDB()
  71. {
  72. getCatsFromDB();
  73. getKeysFromDB();
  74. }
  75. /**
  76. * Save Categories and Mapped Keys into GameServer DataBase
  77. */
  78. public void saveInDB()
  79. {
  80. String query;
  81. int playerId = _player.getObjectId();
  82. if (_saved)
  83. return;
  84. query = "REPLACE INTO character_ui_categories (`charId`, `catId`, `order`, `cmdId`) VALUES ";
  85. for (int category : _storedCategories.keySet())
  86. {
  87. int order = 0;
  88. for (int key : _storedCategories.get(category))
  89. {
  90. query += "(" + playerId + ", " + category + ", " + (order++) + ", " + key + "),";
  91. }
  92. }
  93. query = query.substring(0, query.length() - 1) + "; ";
  94. Connection con = null;
  95. PreparedStatement statement;
  96. try
  97. {
  98. con = L2DatabaseFactory.getInstance().getConnection();
  99. statement = con.prepareStatement(query);
  100. statement.execute();
  101. statement.close();
  102. }
  103. catch (Exception e)
  104. {
  105. _log.log(Level.WARNING, "Exception: saveInDB(): " + e.getMessage(), e);
  106. }
  107. query = "REPLACE INTO character_ui_actions (`charId`, `cat`, `order`, `cmd`, `key`, `tgKey1`, `tgKey2`, `show`) VALUES";
  108. for (List<ActionKey> keyLst : _storedKeys.values())
  109. {
  110. int order = 0;
  111. for (ActionKey key : keyLst)
  112. {
  113. query += key.getSqlSaveString(playerId, order++) + ",";
  114. }
  115. }
  116. query = query.substring(0, query.length() - 1) + ";";
  117. try
  118. {
  119. if (con == null)
  120. con = L2DatabaseFactory.getInstance().getConnection();
  121. statement = con.prepareStatement(query);
  122. statement.execute();
  123. statement.close();
  124. }
  125. catch (Exception e)
  126. {
  127. _log.log(Level.WARNING, "Exception: saveInDB(): " + e.getMessage(), e);
  128. }
  129. finally
  130. {
  131. try
  132. {
  133. con.close();
  134. }
  135. catch (Exception e)
  136. {
  137. }
  138. }
  139. _saved = true;
  140. }
  141. public void getCatsFromDB()
  142. {
  143. if (_storedCategories != null)
  144. return;
  145. _storedCategories = new FastMap<Integer, List<Integer>>();
  146. Connection con = null;
  147. try
  148. {
  149. con = L2DatabaseFactory.getInstance().getConnection();
  150. PreparedStatement stmt = con.prepareStatement("SELECT * FROM character_ui_categories WHERE `charId` = ? ORDER BY `catId`, `order`");
  151. stmt.setInt(1, _player.getObjectId());
  152. ResultSet rs = stmt.executeQuery();
  153. while (rs.next())
  154. {
  155. int cat = rs.getInt("catId");
  156. int cmd = rs.getInt("cmdId");
  157. insertCategory(cat, cmd);
  158. }
  159. stmt.close();
  160. rs.close();
  161. }
  162. catch (Exception e)
  163. {
  164. _log.log(Level.WARNING, "Exception: getCatsFromDB(): " + e.getMessage(), e);
  165. }
  166. finally
  167. {
  168. try
  169. {
  170. con.close();
  171. }
  172. catch (SQLException e)
  173. {
  174. }
  175. }
  176. if (_storedCategories.size() < 1)
  177. _storedCategories = UITable.getInstance().getCategories();
  178. }
  179. public void getKeysFromDB()
  180. {
  181. if (_storedKeys != null)
  182. return;
  183. _storedKeys = new FastMap<Integer, List<ActionKey>>();
  184. Connection con = null;
  185. try
  186. {
  187. con = L2DatabaseFactory.getInstance().getConnection();
  188. PreparedStatement stmt = con.prepareStatement("SELECT * FROM character_ui_actions WHERE `charId` = ? ORDER BY `cat`, `order`");
  189. stmt.setInt(1, _player.getObjectId());
  190. ResultSet rs = stmt.executeQuery();
  191. while (rs.next())
  192. {
  193. int cat = rs.getInt("cat");
  194. int cmd = rs.getInt("cmd");
  195. int key = rs.getInt("key");
  196. int tgKey1 = rs.getInt("tgKey1");
  197. int tgKey2 = rs.getInt("tgKey2");
  198. int show = rs.getInt("show");
  199. insertKey(cat, cmd, key, tgKey1, tgKey2, show);
  200. }
  201. stmt.close();
  202. rs.close();
  203. }
  204. catch (Exception e)
  205. {
  206. _log.log(Level.WARNING, "Exception: getKeysFromDB(): " + e.getMessage(), e);
  207. }
  208. finally
  209. {
  210. try
  211. {
  212. con.close();
  213. }
  214. catch (SQLException e)
  215. {
  216. }
  217. }
  218. if (_storedKeys.size() < 1)
  219. _storedKeys = UITable.getInstance().getKeys();
  220. }
  221. public void insertCategory(int cat, int cmd)
  222. {
  223. if (_storedCategories.containsKey(cat))
  224. _storedCategories.get(cat).add(cmd);
  225. else
  226. {
  227. List<Integer> tmp = new FastList<Integer>();
  228. tmp.add(cmd);
  229. _storedCategories.put(cat, tmp);
  230. }
  231. }
  232. public void insertKey(int cat, int cmdId, int key, int tgKey1, int tgKey2, int show)
  233. {
  234. ActionKey tmk = new ActionKey(cat, cmdId, key, tgKey1, tgKey2, show);
  235. if (_storedKeys.containsKey(cat))
  236. _storedKeys.get(cat).add(tmk);
  237. else
  238. {
  239. List<ActionKey> tmp = new FastList<ActionKey>();
  240. tmp.add(tmk);
  241. _storedKeys.put(cat, tmp);
  242. }
  243. }
  244. public boolean isSaved()
  245. {
  246. return _saved;
  247. }
  248. }