L2UIKeysSettings.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. con = L2DatabaseFactory.getInstance().getConnection();
  98. statement = con.prepareStatement(query);
  99. statement.execute();
  100. statement.close();
  101. }
  102. catch (Exception e)
  103. {
  104. _log.warning("Exception: saveInDB(): " + e.getMessage());
  105. e.printStackTrace();
  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.warning("Exception: saveInDB(): " + e.getMessage());
  128. e.printStackTrace();
  129. }
  130. finally
  131. {
  132. try
  133. {
  134. con.close();
  135. }
  136. catch (Exception e)
  137. {
  138. _log.warning("Exception while closing connection: saveInDB(): " + e.getMessage());
  139. e.printStackTrace();
  140. }
  141. }
  142. _saved = true;
  143. }
  144. public void getCatsFromDB()
  145. {
  146. if (_storedCategories != null)
  147. return;
  148. _storedCategories = new FastMap<Integer, List<Integer>>();
  149. Connection con = null;
  150. try
  151. {
  152. con = L2DatabaseFactory.getInstance().getConnection();
  153. PreparedStatement stmt = con.prepareStatement("SELECT * FROM character_ui_categories WHERE `charId` = ? ORDER BY `catId`, `order`");
  154. stmt.setInt(1, _player.getObjectId());
  155. ResultSet rs = stmt.executeQuery();
  156. while (rs.next())
  157. {
  158. int cat = rs.getInt("catId");
  159. int cmd = rs.getInt("cmdId");
  160. insertCategory(cat, cmd);
  161. }
  162. stmt.close();
  163. rs.close();
  164. }
  165. catch (Exception e)
  166. {
  167. _log.warning("Exception: getCatsFromDB(): " + e.getMessage());
  168. e.printStackTrace();
  169. }
  170. finally
  171. {
  172. try
  173. {
  174. con.close();
  175. }
  176. catch (SQLException e)
  177. {
  178. // TODO Auto-generated catch block
  179. e.printStackTrace();
  180. }
  181. }
  182. if (_storedCategories.size() < 1)
  183. _storedCategories = UITable.getInstance().getCategories();
  184. }
  185. public void getKeysFromDB()
  186. {
  187. if (_storedKeys != null)
  188. return;
  189. _storedKeys = new FastMap<Integer, List<ActionKey>>();
  190. Connection con = null;
  191. try
  192. {
  193. con = L2DatabaseFactory.getInstance().getConnection();
  194. PreparedStatement stmt = con.prepareStatement("SELECT * FROM character_ui_actions WHERE `charId` = ? ORDER BY `cat`, `order`");
  195. stmt.setInt(1, _player.getObjectId());
  196. ResultSet rs = stmt.executeQuery();
  197. while (rs.next())
  198. {
  199. int cat = rs.getInt("cat");
  200. int cmd = rs.getInt("cmd");
  201. int key = rs.getInt("key");
  202. int tgKey1 = rs.getInt("tgKey1");
  203. int tgKey2 = rs.getInt("tgKey2");
  204. int show = rs.getInt("show");
  205. insertKey(cat, cmd, key, tgKey1, tgKey2, show);
  206. }
  207. stmt.close();
  208. rs.close();
  209. }
  210. catch (Exception e)
  211. {
  212. _log.warning("Exception: getKeysFromDB(): " + e.getMessage());
  213. e.printStackTrace();
  214. }
  215. finally
  216. {
  217. try
  218. {
  219. con.close();
  220. }
  221. catch (SQLException e)
  222. {
  223. // TODO Auto-generated catch block
  224. e.printStackTrace();
  225. }
  226. }
  227. if (_storedKeys.size() < 1)
  228. _storedKeys = UITable.getInstance().getKeys();
  229. }
  230. public void insertCategory(int cat, int cmd)
  231. {
  232. if (_storedCategories.containsKey(cat))
  233. _storedCategories.get(cat).add(cmd);
  234. else
  235. {
  236. List<Integer> tmp = new FastList<Integer>();
  237. tmp.add(cmd);
  238. _storedCategories.put(cat, tmp);
  239. }
  240. }
  241. public void insertKey(int cat, int cmdId, int key, int tgKey1, int tgKey2, int show)
  242. {
  243. ActionKey tmk = new ActionKey(cat, cmdId, key, tgKey1, tgKey2, show);
  244. if (_storedKeys.containsKey(cat))
  245. _storedKeys.get(cat).add(tmk);
  246. else
  247. {
  248. List<ActionKey> tmp = new FastList<ActionKey>();
  249. tmp.add(tmk);
  250. _storedKeys.put(cat, tmp);
  251. }
  252. }
  253. public boolean isSaved()
  254. {
  255. return _saved;
  256. }
  257. }