L2UIKeysSettings.java 6.9 KB

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