L2UIKeysSettings.java 6.2 KB

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