2
0

L2UIKeysSettings.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.Level;
  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.log(Level.WARNING, "Exception: saveInDB(): " + e.getMessage(), e);
  105. }
  106. query = "REPLACE INTO character_ui_actions (`charId`, `cat`, `order`, `cmd`, `key`, `tgKey1`, `tgKey2`, `show`) VALUES";
  107. for (List<ActionKey> keyLst : _storedKeys.values())
  108. {
  109. int order = 0;
  110. for (ActionKey key : keyLst)
  111. {
  112. query += key.getSqlSaveString(playerId, order++) + ",";
  113. }
  114. }
  115. query = query.substring(0, query.length() - 1) + ";";
  116. try
  117. {
  118. if (con == null)
  119. con = L2DatabaseFactory.getInstance().getConnection();
  120. statement = con.prepareStatement(query);
  121. statement.execute();
  122. statement.close();
  123. }
  124. catch (Exception e)
  125. {
  126. _log.log(Level.WARNING, "Exception: saveInDB(): " + e.getMessage(), e);
  127. }
  128. finally
  129. {
  130. L2DatabaseFactory.close(con);
  131. }
  132. _saved = true;
  133. }
  134. public void getCatsFromDB()
  135. {
  136. if (_storedCategories != null)
  137. return;
  138. _storedCategories = new FastMap<Integer, List<Integer>>();
  139. Connection con = null;
  140. try
  141. {
  142. con = L2DatabaseFactory.getInstance().getConnection();
  143. PreparedStatement stmt = con.prepareStatement("SELECT * FROM character_ui_categories WHERE `charId` = ? ORDER BY `catId`, `order`");
  144. stmt.setInt(1, _player.getObjectId());
  145. ResultSet rs = stmt.executeQuery();
  146. while (rs.next())
  147. {
  148. int cat = rs.getInt("catId");
  149. int cmd = rs.getInt("cmdId");
  150. insertCategory(cat, cmd);
  151. }
  152. stmt.close();
  153. rs.close();
  154. }
  155. catch (Exception e)
  156. {
  157. _log.log(Level.WARNING, "Exception: getCatsFromDB(): " + e.getMessage(), e);
  158. }
  159. finally
  160. {
  161. L2DatabaseFactory.close(con);
  162. }
  163. if (_storedCategories.size() < 1)
  164. _storedCategories = UITable.getInstance().getCategories();
  165. }
  166. public void getKeysFromDB()
  167. {
  168. if (_storedKeys != null)
  169. return;
  170. _storedKeys = new FastMap<Integer, List<ActionKey>>();
  171. Connection con = null;
  172. try
  173. {
  174. con = L2DatabaseFactory.getInstance().getConnection();
  175. PreparedStatement stmt = con.prepareStatement("SELECT * FROM character_ui_actions WHERE `charId` = ? ORDER BY `cat`, `order`");
  176. stmt.setInt(1, _player.getObjectId());
  177. ResultSet rs = stmt.executeQuery();
  178. while (rs.next())
  179. {
  180. int cat = rs.getInt("cat");
  181. int cmd = rs.getInt("cmd");
  182. int key = rs.getInt("key");
  183. int tgKey1 = rs.getInt("tgKey1");
  184. int tgKey2 = rs.getInt("tgKey2");
  185. int show = rs.getInt("show");
  186. insertKey(cat, cmd, key, tgKey1, tgKey2, show);
  187. }
  188. stmt.close();
  189. rs.close();
  190. }
  191. catch (Exception e)
  192. {
  193. _log.log(Level.WARNING, "Exception: getKeysFromDB(): " + e.getMessage(), e);
  194. }
  195. finally
  196. {
  197. L2DatabaseFactory.close(con);
  198. }
  199. if (_storedKeys.size() < 1)
  200. _storedKeys = UITable.getInstance().getKeys();
  201. }
  202. public void insertCategory(int cat, int cmd)
  203. {
  204. if (_storedCategories.containsKey(cat))
  205. _storedCategories.get(cat).add(cmd);
  206. else
  207. {
  208. List<Integer> tmp = new FastList<Integer>();
  209. tmp.add(cmd);
  210. _storedCategories.put(cat, tmp);
  211. }
  212. }
  213. public void insertKey(int cat, int cmdId, int key, int tgKey1, int tgKey2, int show)
  214. {
  215. ActionKey tmk = new ActionKey(cat, cmdId, key, tgKey1, tgKey2, show);
  216. if (_storedKeys.containsKey(cat))
  217. _storedKeys.get(cat).add(tmk);
  218. else
  219. {
  220. List<ActionKey> tmp = new FastList<ActionKey>();
  221. tmp.add(tmk);
  222. _storedKeys.put(cat, tmp);
  223. }
  224. }
  225. public boolean isSaved()
  226. {
  227. return _saved;
  228. }
  229. }