123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- /*
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.model;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.List;
- import java.util.Map;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javolution.util.FastList;
- import javolution.util.FastMap;
- import com.l2jserver.L2DatabaseFactory;
- import com.l2jserver.gameserver.datatables.UITable;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.model.entity.ActionKey;
- /**
- *
- * @author mrTJO
- */
- public class L2UIKeysSettings
- {
- protected static final Logger _log = Logger.getLogger(L2UIKeysSettings.class.getName());
-
- private final L2PcInstance _player;
-
- Map<Integer, List<ActionKey>> _storedKeys;
- Map<Integer, List<Integer>> _storedCategories;
-
- boolean _saved = true;
-
- public L2UIKeysSettings(L2PcInstance player)
- {
- _player = player;
- loadFromDB();
- }
-
- public void storeAll(Map<Integer, List<Integer>> catMap, Map<Integer, List<ActionKey>> keyMap)
- {
- _saved = false;
- _storedCategories = catMap;
- _storedKeys = keyMap;
- }
-
- public void storeCategories(Map<Integer, List<Integer>> catMap)
- {
- _saved = false;
- _storedCategories = catMap;
- }
-
- public Map<Integer, List<Integer>> getCategories()
- {
- return _storedCategories;
- }
-
- public void storeKeys(Map<Integer, List<ActionKey>> keyMap)
- {
- _saved = false;
- _storedKeys = keyMap;
- }
-
- public Map<Integer, List<ActionKey>> getKeys()
- {
- return _storedKeys;
- }
-
- public void loadFromDB()
- {
- getCatsFromDB();
- getKeysFromDB();
- }
-
- /**
- * Save Categories and Mapped Keys into GameServer DataBase
- */
- public void saveInDB()
- {
- String query;
- int playerId = _player.getObjectId();
-
- if (_saved)
- return;
-
- query = "REPLACE INTO character_ui_categories (`charId`, `catId`, `order`, `cmdId`) VALUES ";
- for (int category : _storedCategories.keySet())
- {
- int order = 0;
- for (int key : _storedCategories.get(category))
- {
- query += "(" + playerId + ", " + category + ", " + (order++) + ", " + key + "),";
- }
- }
- query = query.substring(0, query.length() - 1) + "; ";
-
- Connection con = null;
- PreparedStatement statement;
-
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
-
- statement = con.prepareStatement(query);
- statement.execute();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: saveInDB(): " + e.getMessage(), e);
- }
-
- query = "REPLACE INTO character_ui_actions (`charId`, `cat`, `order`, `cmd`, `key`, `tgKey1`, `tgKey2`, `show`) VALUES";
- for (List<ActionKey> keyLst : _storedKeys.values())
- {
- int order = 0;
- for (ActionKey key : keyLst)
- {
- query += key.getSqlSaveString(playerId, order++) + ",";
- }
- }
- query = query.substring(0, query.length() - 1) + ";";
-
- try
- {
- if (con == null)
- con = L2DatabaseFactory.getInstance().getConnection();
-
- statement = con.prepareStatement(query);
- statement.execute();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: saveInDB(): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- _saved = true;
- }
-
- public void getCatsFromDB()
- {
-
- if (_storedCategories != null)
- return;
-
- _storedCategories = new FastMap<>();
-
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement stmt = con.prepareStatement("SELECT * FROM character_ui_categories WHERE `charId` = ? ORDER BY `catId`, `order`");
- stmt.setInt(1, _player.getObjectId());
- ResultSet rs = stmt.executeQuery();
-
- while (rs.next())
- {
- int cat = rs.getInt("catId");
- int cmd = rs.getInt("cmdId");
- insertCategory(cat, cmd);
- }
- stmt.close();
- rs.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: getCatsFromDB(): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
-
- if (_storedCategories.size() < 1)
- _storedCategories = UITable.getInstance().getCategories();
- }
-
- public void getKeysFromDB()
- {
- if (_storedKeys != null)
- return;
-
- _storedKeys = new FastMap<>();
-
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement stmt = con.prepareStatement("SELECT * FROM character_ui_actions WHERE `charId` = ? ORDER BY `cat`, `order`");
- stmt.setInt(1, _player.getObjectId());
- ResultSet rs = stmt.executeQuery();
-
- while (rs.next())
- {
- int cat = rs.getInt("cat");
- int cmd = rs.getInt("cmd");
- int key = rs.getInt("key");
- int tgKey1 = rs.getInt("tgKey1");
- int tgKey2 = rs.getInt("tgKey2");
- int show = rs.getInt("show");
- insertKey(cat, cmd, key, tgKey1, tgKey2, show);
- }
- stmt.close();
- rs.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception: getKeysFromDB(): " + e.getMessage(), e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
-
- if (_storedKeys.size() < 1)
- _storedKeys = UITable.getInstance().getKeys();
- }
-
- public void insertCategory(int cat, int cmd)
- {
- if (_storedCategories.containsKey(cat))
- _storedCategories.get(cat).add(cmd);
- else
- {
- List<Integer> tmp = new FastList<>();
- tmp.add(cmd);
- _storedCategories.put(cat, tmp);
- }
- }
-
- public void insertKey(int cat, int cmdId, int key, int tgKey1, int tgKey2, int show)
- {
- ActionKey tmk = new ActionKey(cat, cmdId, key, tgKey1, tgKey2, show);
- if (_storedKeys.containsKey(cat))
- _storedKeys.get(cat).add(tmk);
- else
- {
- List<ActionKey> tmp = new FastList<>();
- tmp.add(tmk);
- _storedKeys.put(cat, tmp);
- }
- }
-
- public boolean isSaved()
- {
- return _saved;
- }
- }
|