Cache names in NameManager

This commit is contained in:
Andrzej Pomirski 2014-05-06 11:55:33 +02:00
parent a0f824d526
commit bd0a40e16c

View File

@ -6,6 +6,8 @@ import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Database.ConnectionManager;
import com.Acrobot.ChestShop.Permission;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
@ -16,6 +18,8 @@ import org.bukkit.entity.Player;
import java.io.File;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
@ -26,7 +30,14 @@ import java.util.UUID;
public class NameManager {
private static Dao<Account, String> accounts;
private static BiMap<String, UUID> usernameToUUID = HashBiMap.create();
private static Map<String, String> shortToLongName = new HashMap<String, String>();
public static UUID getUUID(String username) {
if (usernameToUUID.containsKey(username)) {
return usernameToUUID.get(username);
}
String shortenedName = NameUtil.stripUsername(username);
Account account = null;
@ -42,10 +53,20 @@ public class NameManager {
return null;
}
return account.getUuid();
UUID uuid = account.getUuid();
if (uuid != null) {
usernameToUUID.put(username, uuid);
}
return uuid;
}
public static String getUsername(UUID uuid) {
if (usernameToUUID.containsValue(uuid)) {
return usernameToUUID.inverse().get(uuid);
}
Account account = null;
try {
@ -59,11 +80,22 @@ public class NameManager {
return "";
}
return account.getName();
String name = account.getName();
if (name != null) {
usernameToUUID.put(name, uuid);
}
return name;
}
public static String getFullUsername(String username) {
String shortName = NameUtil.stripUsername(username);
if (shortToLongName.containsKey(shortName)) {
return shortToLongName.get(shortName);
}
Account account = null;
try {
@ -77,32 +109,47 @@ public class NameManager {
return username;
}
return account.getName();
String name = account.getName();
if (name != null) {
shortToLongName.put(shortName, name);
}
return name;
}
public static void storeUsername(Player player) {
UUID uuid = player.getUniqueId();
public static void storeUsername(final Player player) {
final UUID uuid = player.getUniqueId();
Account account = null;
try {
account = accounts.queryBuilder().selectColumns("name").where().eq("uuid", uuid).queryForFirst();
} catch (SQLException e) {
e.printStackTrace();
return;
if (!usernameToUUID.inverse().containsKey(uuid)) {
usernameToUUID.inverse().put(uuid, player.getName());
}
if (account != null) {
return;
}
Bukkit.getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), new Runnable() {
@Override
public void run() {
Account account = null;
account = new Account(player.getName(), player.getUniqueId());
try {
account = accounts.queryBuilder().selectColumns("name").where().eq("uuid", uuid).queryForFirst();
} catch (SQLException e) {
e.printStackTrace();
return;
}
try {
accounts.create(account);
} catch (SQLException e) {
e.printStackTrace();
}
if (account != null) {
return;
}
account = new Account(player.getName(), player.getUniqueId());
try {
accounts.create(account);
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
public static boolean canUseName(Player player, String name) {