ChestShop-3/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java

242 lines
6.9 KiB
Java
Raw Normal View History

2014-04-06 19:51:30 +02:00
package com.Acrobot.ChestShop.UUIDs;
2014-04-06 19:51:47 +02:00
import com.Acrobot.Breeze.Utils.NameUtil;
import com.Acrobot.ChestShop.ChestShop;
2014-04-12 16:52:21 +02:00
import com.Acrobot.ChestShop.Configuration.Properties;
2014-04-10 21:52:05 +02:00
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Database.ConnectionManager;
2014-04-12 13:57:39 +02:00
import com.Acrobot.ChestShop.Permission;
2014-08-13 17:22:52 +02:00
import com.Acrobot.ChestShop.Signs.ChestShopSign;
2014-05-06 11:55:33 +02:00
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
2014-04-10 21:52:05 +02:00
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
2014-04-12 16:52:21 +02:00
import org.bukkit.Bukkit;
2014-04-06 19:51:47 +02:00
import org.bukkit.entity.Player;
import java.io.File;
2014-04-10 21:52:05 +02:00
import java.sql.SQLException;
2014-05-06 11:55:33 +02:00
import java.util.HashMap;
import java.util.Map;
2014-04-06 19:51:47 +02:00
import java.util.UUID;
2014-04-06 19:51:30 +02:00
/**
2014-04-06 19:51:47 +02:00
* Lets you save/cache username and UUID relations
2014-04-11 17:23:04 +02:00
*
2014-04-06 19:51:30 +02:00
* @author Andrzej Pomirski (Acrobot)
*/
2014-04-12 13:57:39 +02:00
public class NameManager {
2014-04-10 21:52:05 +02:00
private static Dao<Account, String> accounts;
private static Map<UUID, String> lastSeenName = new HashMap<UUID, String>();
2014-05-06 11:55:33 +02:00
private static BiMap<String, UUID> usernameToUUID = HashBiMap.create();
private static Map<String, String> shortToLongName = new HashMap<String, String>();
public static String getLastSeenName(UUID uuid) {
if (lastSeenName.containsKey(uuid)) {
return lastSeenName.get(uuid);
}
Account account = null;
try {
account = accounts.queryBuilder().selectColumns("lastSeenName", "name").where().eq("uuid", uuid).queryForFirst();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
if (account == null) {
return "";
}
if (account.getLastSeenName() != null) {
lastSeenName.put(uuid, account.getLastSeenName());
} else if (account.getName() != null) {
lastSeenName.put(uuid, account.getName());
}
return account.getLastSeenName();
}
2014-04-10 21:52:05 +02:00
public static UUID getUUID(String username) {
2014-05-06 11:55:33 +02:00
if (usernameToUUID.containsKey(username)) {
return usernameToUUID.get(username);
}
2014-04-10 21:52:05 +02:00
String shortenedName = NameUtil.stripUsername(username);
Account account = null;
try {
account = accounts.queryBuilder().selectColumns("uuid").where().eq("shortName", shortenedName).queryForFirst();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
2014-04-06 19:51:47 +02:00
2014-04-10 21:52:05 +02:00
if (account == null) {
return Bukkit.getOfflinePlayer(username).getUniqueId();
2014-04-06 19:51:47 +02:00
}
2014-05-06 11:55:33 +02:00
UUID uuid = account.getUuid();
2015-02-04 20:14:35 +01:00
if (uuid != null && !usernameToUUID.containsValue(uuid)) {
2014-07-22 22:03:50 +02:00
usernameToUUID.put(account.getName(), uuid);
2014-05-06 11:55:33 +02:00
}
return uuid;
2014-04-06 19:51:47 +02:00
}
2014-04-10 21:52:05 +02:00
public static String getUsername(UUID uuid) {
2014-05-06 11:55:33 +02:00
if (usernameToUUID.containsValue(uuid)) {
return usernameToUUID.inverse().get(uuid);
}
2014-04-10 21:52:05 +02:00
Account account = null;
try {
account = accounts.queryBuilder().selectColumns("name").where().eq("uuid", uuid).queryForFirst();
} catch (SQLException e) {
e.printStackTrace();
return null;
2014-04-06 19:51:47 +02:00
}
2014-04-11 17:17:20 +02:00
if (account == null) {
2014-06-22 14:32:50 +02:00
String name = Bukkit.getOfflinePlayer(uuid).getName();
if (name != null) {
usernameToUUID.put(name, uuid);
return name;
}
2014-04-12 14:22:34 +02:00
return "";
2014-04-11 17:17:20 +02:00
}
2014-05-06 11:55:33 +02:00
String name = account.getName();
if (name != null) {
usernameToUUID.put(name, uuid);
}
return name;
2014-04-06 19:51:47 +02:00
}
public static String getFullUsername(String username) {
2014-08-13 17:22:52 +02:00
if (ChestShopSign.isAdminShop(username)) {
return Properties.ADMIN_SHOP_NAME;
}
String shortName = NameUtil.stripUsername(username);
2014-05-06 11:55:33 +02:00
if (shortToLongName.containsKey(shortName)) {
return shortToLongName.get(shortName);
}
Account account = null;
try {
account = accounts.queryBuilder().selectColumns("name").where().eq("shortName", shortName).queryForFirst();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
2014-04-11 17:17:20 +02:00
if (account == null) {
return username;
}
2014-05-06 11:55:33 +02:00
String name = account.getName();
2014-04-10 21:52:05 +02:00
2014-05-06 11:55:33 +02:00
if (name != null) {
shortToLongName.put(shortName, name);
2014-04-06 19:51:47 +02:00
}
2014-05-06 11:55:33 +02:00
return name;
}
2014-04-06 19:51:47 +02:00
2014-05-06 11:55:33 +02:00
public static void storeUsername(final Player player) {
final UUID uuid = player.getUniqueId();
2014-04-06 19:51:47 +02:00
Account account = null;
try {
account = accounts.queryBuilder().where().eq("uuid", uuid).queryForFirst();
} catch (SQLException e) {
e.printStackTrace();
return;
2014-04-10 21:52:05 +02:00
}
2014-05-06 11:55:33 +02:00
if (account != null) {
account.setLastSeenName(player.getName());
2014-05-06 11:55:33 +02:00
try {
accounts.createOrUpdate(account);
} catch (SQLException e) {
e.printStackTrace();
}
2014-05-06 11:55:33 +02:00
return;
}
2014-05-06 11:55:33 +02:00
account = new Account(player.getName(), player.getUniqueId());
2014-05-06 11:55:33 +02:00
if (!usernameToUUID.inverse().containsKey(uuid)) {
usernameToUUID.inverse().put(uuid, player.getName());
}
lastSeenName.put(uuid, player.getName());
try {
accounts.createOrUpdate(account);
} catch (SQLException e) {
e.printStackTrace();
}
2014-04-06 19:51:47 +02:00
}
public static void dropUsername(final Player player) {
final UUID uuid = player.getUniqueId();
if (usernameToUUID.containsValue(uuid)) {
usernameToUUID.inverse().remove(uuid);
}
String shortName = NameUtil.stripUsername(player.getName());
if (shortToLongName.containsKey(shortName)) {
shortToLongName.remove(shortName);
}
}
2014-04-12 13:57:39 +02:00
public static boolean canUseName(Player player, String name) {
String shortenedName = NameUtil.stripUsername(getUsername(player.getUniqueId()));
2015-02-04 20:14:35 +01:00
return shortenedName.equals(name) || Permission.otherName(player, name) || player.getUniqueId().equals(getUUID(name));
2014-04-12 13:57:39 +02:00
}
2014-06-14 20:46:59 +02:00
public static boolean isAdminShop(UUID uuid) {
return getUsername(uuid).equals(Properties.ADMIN_SHOP_NAME);
}
2014-04-06 19:51:47 +02:00
public static void load() {
2014-04-10 21:52:05 +02:00
File databaseFile = ChestShop.loadFile("users.db");
String uri = ConnectionManager.getURI(databaseFile);
ConnectionSource connection;
2014-04-06 19:51:47 +02:00
try {
2014-04-10 21:52:05 +02:00
connection = new JdbcConnectionSource(uri);
accounts = DaoManager.createDao(connection, Account.class);
2014-04-12 14:22:34 +02:00
TableUtils.createTableIfNotExists(connection, Account.class);
2014-04-12 16:52:21 +02:00
Account adminAccount = new Account(Properties.ADMIN_SHOP_NAME, Bukkit.getOfflinePlayer(Properties.ADMIN_SHOP_NAME).getUniqueId());
2014-07-28 20:51:04 +02:00
accounts.createOrUpdate(adminAccount);
2014-04-10 21:52:05 +02:00
} catch (SQLException e) {
2014-04-06 19:51:47 +02:00
e.printStackTrace();
}
}
2014-04-06 19:51:30 +02:00
}