L2UIKeysSettings.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.logging.Logger;
  22. import javolution.util.FastList;
  23. import javolution.util.FastMap;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.datatables.UITable;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.gameserver.model.entity.ActionKey;
  28. /**
  29. *
  30. * @author mrTJO
  31. */
  32. public class L2UIKeysSettings
  33. {
  34. protected static final Logger _log = Logger.getLogger(L2UIKeysSettings.class.getName());
  35. private final L2PcInstance _player;
  36. Map<Integer, List<ActionKey>> _storedKeys;
  37. Map<Integer, List<Integer>> _storedCategories;
  38. boolean _saved = true;
  39. public L2UIKeysSettings(L2PcInstance player)
  40. {
  41. _player = player;
  42. loadFromDB();
  43. }
  44. public void storeAll(Map<Integer, List<Integer>> catMap, Map<Integer, List<ActionKey>> keyMap)
  45. {
  46. _saved = false;
  47. _storedCategories = catMap;
  48. _storedKeys = keyMap;
  49. }
  50. public void storeCategories(Map<Integer, List<Integer>> catMap)
  51. {
  52. _saved = false;
  53. _storedCategories = catMap;
  54. }
  55. public Map<Integer, List<Integer>> getCategories()
  56. {
  57. return _storedCategories;
  58. }
  59. public void storeKeys(Map<Integer, List<ActionKey>> keyMap)
  60. {
  61. _saved = false;
  62. _storedKeys = keyMap;
  63. }
  64. public Map<Integer, List<ActionKey>> getKeys()
  65. {
  66. return _storedKeys;
  67. }
  68. public void loadFromDB()
  69. {
  70. getCatsFromDB();
  71. getKeysFromDB();
  72. }
  73. /**
  74. * Save Categories and Mapped Keys into GameServer DataBase
  75. */
  76. public void saveInDB()
  77. {
  78. String query;
  79. int playerId = _player.getObjectId();
  80. if (_saved)
  81. return;
  82. query = "REPLACE INTO character_ui_categories (`charId`, `catId`, `order`, `cmdId`) VALUES ";
  83. for (int category : _storedCategories.keySet())
  84. {
  85. int order = 0;
  86. for (int key : _storedCategories.get(category))
  87. {
  88. query += "(" + playerId + ", " + category + ", " + (order++) + ", " + key + "),";
  89. }
  90. }
  91. query = query.substring(0, query.length() - 1) + "; ";
  92. Connection con = null;
  93. PreparedStatement statement;
  94. try
  95. {
  96. if (con == null)
  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. if (_storedCategories.size() < 1)
  171. _storedCategories = UITable.getInstance().getCategories();
  172. }
  173. public void getKeysFromDB()
  174. {
  175. if (_storedKeys != null)
  176. return;
  177. _storedKeys = new FastMap<Integer, List<ActionKey>>();
  178. Connection con = null;
  179. try
  180. {
  181. con = L2DatabaseFactory.getInstance().getConnection();
  182. PreparedStatement stmt = con.prepareStatement("SELECT * FROM character_ui_actions WHERE `charId` = ? ORDER BY `cat`, `order`");
  183. stmt.setInt(1, _player.getObjectId());
  184. ResultSet rs = stmt.executeQuery();
  185. while (rs.next())
  186. {
  187. int cat = rs.getInt("cat");
  188. int cmd = rs.getInt("cmd");
  189. int key = rs.getInt("key");
  190. int tgKey1 = rs.getInt("tgKey1");
  191. int tgKey2 = rs.getInt("tgKey2");
  192. int show = rs.getInt("show");
  193. insertKey(cat, cmd, key, tgKey1, tgKey2, show);
  194. }
  195. stmt.close();
  196. rs.close();
  197. }
  198. catch (Exception e)
  199. {
  200. _log.warning("Exception: getKeysFromDB(): " + e.getMessage());
  201. e.printStackTrace();
  202. }
  203. if (_storedKeys.size() < 1)
  204. _storedKeys = UITable.getInstance().getKeys();
  205. }
  206. public void insertCategory(int cat, int cmd)
  207. {
  208. if (_storedCategories.containsKey(cat))
  209. _storedCategories.get(cat).add(cmd);
  210. else
  211. {
  212. List<Integer> tmp = new FastList<Integer>();
  213. tmp.add(cmd);
  214. _storedCategories.put(cat, tmp);
  215. }
  216. }
  217. public void insertKey(int cat, int cmdId, int key, int tgKey1, int tgKey2, int show)
  218. {
  219. ActionKey tmk = new ActionKey(cat, cmdId, key, tgKey1, tgKey2, show);
  220. if (_storedKeys.containsKey(cat))
  221. _storedKeys.get(cat).add(tmk);
  222. else
  223. {
  224. List<ActionKey> tmp = new FastList<ActionKey>();
  225. tmp.add(tmk);
  226. _storedKeys.put(cat, tmp);
  227. }
  228. }
  229. public boolean isSaved()
  230. {
  231. return _saved;
  232. }
  233. }