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;
|
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;
|
2015-05-22 13:26:07 +02:00
|
|
|
import com.Acrobot.ChestShop.Database.DaoCreator;
|
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;
|
2014-04-12 16:52:21 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2016-07-07 18:15:43 +02:00
|
|
|
import org.bukkit.OfflinePlayer;
|
2014-04-06 19:51:47 +02:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
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;
|
2016-07-07 18:15:43 +02:00
|
|
|
import java.util.logging.Level;
|
2014-04-06 19:51:47 +02:00
|
|
|
|
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)
|
|
|
|
*/
|
2016-07-07 18:15:43 +02:00
|
|
|
@SuppressWarnings("UnusedAssignment") // I deliberately set the variables to null while initializing
|
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;
|
|
|
|
|
2015-03-11 18:02:30 +01:00
|
|
|
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>();
|
|
|
|
|
2015-03-11 18:02:30 +01:00
|
|
|
public static String getLastSeenName(UUID uuid) {
|
|
|
|
if (lastSeenName.containsKey(uuid)) {
|
|
|
|
return lastSeenName.get(uuid);
|
|
|
|
}
|
|
|
|
|
2016-07-07 18:15:43 +02:00
|
|
|
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
|
|
|
|
String lastSeen = player.getName();
|
|
|
|
if (lastSeen != null) {
|
2015-03-21 16:11:33 +01:00
|
|
|
lastSeenName.put(uuid, lastSeen);
|
|
|
|
return lastSeen;
|
|
|
|
}
|
|
|
|
|
2015-03-11 18:02:30 +01:00
|
|
|
Account account = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
account = accounts.queryBuilder().selectColumns("lastSeenName", "name").where().eq("uuid", uuid).queryForFirst();
|
2016-07-07 18:15:43 +02:00
|
|
|
} catch (SQLException ex) {
|
|
|
|
Bukkit.getLogger().log(Level.WARNING, "[ChestShop] Failed to find last seen name for " + uuid + ":", ex);
|
2015-03-11 18:02:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (account == null) {
|
2016-07-07 18:15:43 +02:00
|
|
|
return null;
|
2015-03-11 18:02:30 +01:00
|
|
|
}
|
|
|
|
|
2016-07-07 18:15:43 +02:00
|
|
|
lastSeen = account.getLastSeenName();
|
|
|
|
if (lastSeen != null) {
|
|
|
|
lastSeenName.put(uuid, lastSeen);
|
|
|
|
return lastSeen;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastSeen = account.getName();
|
|
|
|
if (lastSeen != null) {
|
|
|
|
lastSeenName.put(uuid, lastSeen);
|
|
|
|
return lastSeen;
|
2015-03-11 18:02:30 +01:00
|
|
|
}
|
|
|
|
|
2016-07-07 18:15:43 +02:00
|
|
|
return null;
|
2015-03-11 18:02:30 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2015-03-11 18:02:30 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-04-10 22:52:21 +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;
|
|
|
|
}
|
|
|
|
|
2014-04-10 22:52:21 +02:00
|
|
|
String shortName = NameUtil.stripUsername(username);
|
2014-05-06 11:55:33 +02:00
|
|
|
|
|
|
|
if (shortToLongName.containsKey(shortName)) {
|
|
|
|
return shortToLongName.get(shortName);
|
|
|
|
}
|
|
|
|
|
2014-04-10 22:52:21 +02:00
|
|
|
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
|
|
|
|
2015-07-05 21:56:42 +02:00
|
|
|
public static void storeUsername(final PlayerDTO player) {
|
2014-05-06 11:55:33 +02:00
|
|
|
final UUID uuid = player.getUniqueId();
|
2014-04-06 19:51:47 +02:00
|
|
|
|
2015-03-11 18:02:30 +01: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
|
|
|
|
2015-03-11 18:02:30 +01:00
|
|
|
if (account != null) {
|
2015-07-05 21:56:42 +02:00
|
|
|
if (account.getName() != null && account.getShortName() == null) {
|
|
|
|
String shortenedName = NameUtil.stripUsername(account.getName());
|
|
|
|
|
|
|
|
account.setShortName(shortenedName);
|
|
|
|
}
|
|
|
|
|
|
|
|
account.setUuid(uuid); //HOW IS IT EVEN POSSIBLE THAT UUID IS NOT SET EVEN IF WE HAVE FOUND THE PLAYER?!
|
2015-03-11 18:02:30 +01:00
|
|
|
account.setLastSeenName(player.getName());
|
2014-05-06 11:55:33 +02:00
|
|
|
|
2015-03-11 18:02:30 +01:00
|
|
|
try {
|
|
|
|
accounts.createOrUpdate(account);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2014-05-06 11:55:33 +02:00
|
|
|
|
2015-03-11 18:02:30 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-05-06 11:55:33 +02:00
|
|
|
|
2015-03-11 18:02:30 +01:00
|
|
|
account = new Account(player.getName(), player.getUniqueId());
|
2014-05-06 11:55:33 +02:00
|
|
|
|
2015-03-11 18:02:30 +01: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
|
|
|
}
|
|
|
|
|
2014-07-03 18:49:12 +02:00
|
|
|
public static void dropUsername(final Player player) {
|
2016-09-20 21:40:41 +02:00
|
|
|
if (player == null) return;
|
|
|
|
|
2014-07-03 18:49:12 +02:00
|
|
|
final UUID uuid = player.getUniqueId();
|
2016-09-20 21:40:41 +02:00
|
|
|
if (uuid == null) return;
|
2014-07-03 18:49:12 +02:00
|
|
|
|
2016-09-20 21:40:41 +02:00
|
|
|
try {
|
|
|
|
if (usernameToUUID.containsValue(uuid)) {
|
|
|
|
usernameToUUID.inverse().remove(uuid);
|
|
|
|
}
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
// Sigh...
|
2014-07-03 18:49:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-03-21 16:11:33 +01:00
|
|
|
if (ChestShopSign.isAdminShop(name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-05-22 13:26:07 +02:00
|
|
|
return Properties.ADMIN_SHOP_NAME.equals(getUsername(uuid));
|
2014-06-14 20:46:59 +02:00
|
|
|
}
|
|
|
|
|
2014-04-06 19:51:47 +02:00
|
|
|
public static void load() {
|
|
|
|
try {
|
2015-05-22 13:26:07 +02:00
|
|
|
accounts = DaoCreator.getDaoAndCreateTable(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
|
|
|
}
|