L2UIKeysSettings.java 6.2 KB

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