Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.Den_Abr.Crates;
- import java.util.concurrent.*;
- import org.bukkit.configuration.*;
- import org.bukkit.scheduler.*;
- import java.util.*;
- import org.bukkit.plugin.*;
- public class KeyCache
- {
- private static String host;
- private static String user;
- private static String password;
- private static String database;
- private static int port;
- private static MySQL mysql;
- private static Map<String, Map<String, Integer>> keys;
- static {
- KeyCache.keys = new ConcurrentHashMap<String, Map<String, Integer>>();
- }
- public static void init(final ConfigurationSection cs) {
- KeyCache.keys.clear();
- KeyCache.host = cs.getString("host");
- KeyCache.user = cs.getString("user");
- KeyCache.password = cs.getString("password");
- KeyCache.database = cs.getString("database");
- KeyCache.port = cs.getInt("port");
- KeyCache.mysql = new MySQL(KeyCache.user, KeyCache.password, KeyCache.host, KeyCache.port, KeyCache.database);
- createTables();
- }
- private static void createTables() {
- final String sqlQuery = "CREATE TABLE IF NOT EXISTS `player_keys` ( `id` int(11) NOT NULL AUTO_INCREMENT, `player` varchar(255) NOT NULL, `key` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `id` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
- KeyCache.mysql.createTable(sqlQuery);
- }
- public static boolean removeKey(final String player, final String key) {
- if (getKeys(player, key) <= 1) {
- KeyCache.keys.get(key).remove(player.toLowerCase());
- }
- else {
- KeyCache.keys.get(key).put(player.toLowerCase(), getKeys(player, key) - 1);
- }
- return KeyCache.mysql.update("DELETE FROM `player_keys` WHERE `player` = '" + player + "' AND `key` = '" + key + "' LIMIT 1");
- }
- public static int getKeys(final String player, final String key) {
- if (KeyCache.keys.containsKey(key) && KeyCache.keys.get(key).containsKey(player.toLowerCase())) {
- return KeyCache.keys.get(key).get(player.toLowerCase());
- }
- return 0;
- }
- public static void grabKeys(final int period) {
- new BukkitRunnable() {
- public void run() {
- final String query = "SELECT * FROM `player_keys`";
- final List<Object[]> resultList = KeyCache.mysql.select(query);
- final ConcurrentHashMap<String, Map<String, Integer>> results = new ConcurrentHashMap<String, Map<String, Integer>>();
- for (final Object[] r : resultList) {
- final String player = ((String)r[1]).toLowerCase();
- final String keyName = (String)r[2];
- if (!results.containsKey(keyName)) {
- results.put(keyName, new HashMap<String, Integer>());
- results.get(keyName).put(player, 1);
- }
- else {
- final Map<String, Integer> keyMap = results.get(keyName);
- if (keyMap.containsKey(player)) {
- keyMap.put(player, keyMap.get(player) + 1);
- }
- else {
- keyMap.put(player, 1);
- }
- }
- }
- KeyCache.keys.clear();
- KeyCache.keys.putAll(results);
- }
- }.runTaskTimerAsynchronously((Plugin)CratesPlugin.getInstance(), 0L, (long)(period * 20 * 60));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement