UIKeysSettings.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.datatables.UIData;
  30. /**
  31. * UI Keys Settings class.
  32. * @author mrTJO, Zoey76
  33. */
  34. public class UIKeysSettings
  35. {
  36. private static final Logger _log = Logger.getLogger(UIKeysSettings.class.getName());
  37. private final int _playerObjId;
  38. private Map<Integer, List<ActionKey>> _storedKeys;
  39. private Map<Integer, List<Integer>> _storedCategories;
  40. private boolean _saved = true;
  41. public UIKeysSettings(int playerObjId)
  42. {
  43. _playerObjId = playerObjId;
  44. loadFromDB();
  45. }
  46. public void storeAll(Map<Integer, List<Integer>> catMap, Map<Integer, List<ActionKey>> keyMap)
  47. {
  48. _saved = false;
  49. _storedCategories = catMap;
  50. _storedKeys = keyMap;
  51. }
  52. public void storeCategories(Map<Integer, List<Integer>> catMap)
  53. {
  54. _saved = false;
  55. _storedCategories = catMap;
  56. }
  57. public Map<Integer, List<Integer>> getCategories()
  58. {
  59. return _storedCategories;
  60. }
  61. public void storeKeys(Map<Integer, List<ActionKey>> keyMap)
  62. {
  63. _saved = false;
  64. _storedKeys = keyMap;
  65. }
  66. public Map<Integer, List<ActionKey>> getKeys()
  67. {
  68. return _storedKeys;
  69. }
  70. public void loadFromDB()
  71. {
  72. getCatsFromDB();
  73. getKeysFromDB();
  74. }
  75. /**
  76. * Save Categories and Mapped Keys into GameServer DataBase
  77. */
  78. public void saveInDB()
  79. {
  80. String query;
  81. if (_saved)
  82. {
  83. return;
  84. }
  85. query = "REPLACE INTO character_ui_categories (`charId`, `catId`, `order`, `cmdId`) VALUES ";
  86. for (int category : _storedCategories.keySet())
  87. {
  88. int order = 0;
  89. for (int key : _storedCategories.get(category))
  90. {
  91. query += "(" + _playerObjId + ", " + category + ", " + (order++) + ", " + key + "),";
  92. }
  93. }
  94. query = query.substring(0, query.length() - 1) + "; ";
  95. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  96. PreparedStatement statement = con.prepareStatement(query))
  97. {
  98. statement.execute();
  99. }
  100. catch (Exception e)
  101. {
  102. _log.log(Level.WARNING, "Exception: saveInDB(): " + e.getMessage(), e);
  103. }
  104. query = "REPLACE INTO character_ui_actions (`charId`, `cat`, `order`, `cmd`, `key`, `tgKey1`, `tgKey2`, `show`) VALUES";
  105. for (List<ActionKey> keyLst : _storedKeys.values())
  106. {
  107. int order = 0;
  108. for (ActionKey key : keyLst)
  109. {
  110. query += key.getSqlSaveString(_playerObjId, order++) + ",";
  111. }
  112. }
  113. query = query.substring(0, query.length() - 1) + ";";
  114. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  115. PreparedStatement statement = con.prepareStatement(query))
  116. {
  117. statement.execute();
  118. }
  119. catch (Exception e)
  120. {
  121. _log.log(Level.WARNING, "Exception: saveInDB(): " + e.getMessage(), e);
  122. }
  123. _saved = true;
  124. }
  125. public void getCatsFromDB()
  126. {
  127. if (_storedCategories != null)
  128. {
  129. return;
  130. }
  131. _storedCategories = new HashMap<>();
  132. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  133. PreparedStatement stmt = con.prepareStatement("SELECT * FROM character_ui_categories WHERE `charId` = ? ORDER BY `catId`, `order`"))
  134. {
  135. stmt.setInt(1, _playerObjId);
  136. try (ResultSet rs = stmt.executeQuery())
  137. {
  138. while (rs.next())
  139. {
  140. UIData.addCategory(_storedCategories, rs.getInt("catId"), rs.getInt("cmdId"));
  141. }
  142. }
  143. }
  144. catch (Exception e)
  145. {
  146. _log.log(Level.WARNING, "Exception: getCatsFromDB(): " + e.getMessage(), e);
  147. }
  148. if (_storedCategories.isEmpty())
  149. {
  150. _storedCategories = UIData.getInstance().getCategories();
  151. }
  152. }
  153. public void getKeysFromDB()
  154. {
  155. if (_storedKeys != null)
  156. {
  157. return;
  158. }
  159. _storedKeys = new HashMap<>();
  160. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  161. PreparedStatement stmt = con.prepareStatement("SELECT * FROM character_ui_actions WHERE `charId` = ? ORDER BY `cat`, `order`"))
  162. {
  163. stmt.setInt(1, _playerObjId);
  164. try (ResultSet rs = stmt.executeQuery())
  165. {
  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. UIData.addKey(_storedKeys, cat, new ActionKey(cat, cmd, key, tgKey1, tgKey2, show));
  175. }
  176. }
  177. }
  178. catch (Exception e)
  179. {
  180. _log.log(Level.WARNING, "Exception: getKeysFromDB(): " + e.getMessage(), e);
  181. }
  182. if (_storedKeys.isEmpty())
  183. {
  184. _storedKeys = UIData.getInstance().getKeys();
  185. }
  186. }
  187. public boolean isSaved()
  188. {
  189. return _saved;
  190. }
  191. }