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

132 lines
3.7 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-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-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;
public static UUID getUUID(String username) {
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 null;
2014-04-06 19:51:47 +02:00
}
2014-04-10 21:52:05 +02:00
return account.getUuid();
2014-04-06 19:51:47 +02:00
}
2014-04-10 21:52:05 +02:00
public static String getUsername(UUID uuid) {
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-04-12 14:22:34 +02:00
return "";
2014-04-11 17:17:20 +02:00
}
2014-04-10 21:52:05 +02:00
return account.getName();
2014-04-06 19:51:47 +02:00
}
public static String getFullUsername(String username) {
String shortName = NameUtil.stripUsername(username);
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;
}
return account.getName();
}
2014-04-10 21:52:05 +02:00
public static void storeUsername(Player player) {
UUID uuid = player.getUniqueId();
2014-04-06 19:51:47 +02:00
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;
2014-04-06 19:51:47 +02:00
}
2014-04-10 21:52:05 +02:00
if (account != null) {
return;
}
2014-04-06 19:51:47 +02:00
2014-04-10 21:52:05 +02:00
account = new Account(player.getName(), player.getUniqueId());
2014-04-06 19:51:47 +02:00
2014-04-10 21:52:05 +02:00
try {
accounts.create(account);
} catch (SQLException e) {
e.printStackTrace();
}
2014-04-06 19:51:47 +02:00
}
2014-04-12 13:57:39 +02:00
public static boolean canUseName(Player player, String name) {
String shortenedName = NameUtil.stripUsername(getUsername(player.getUniqueId()));
return shortenedName.equals(name) || Permission.otherName(player, 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());
accounts.createIfNotExists(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
}