mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-06-26 14:24:45 +02:00
Use ORMlite for the UUID->name mapping
This commit is contained in:
parent
03320b7db6
commit
da7e236c0e
|
@ -69,6 +69,14 @@ public class NameUtil {
|
|||
}
|
||||
}
|
||||
|
||||
public static String stripUsername(String username) {
|
||||
if (username.length() > 15) {
|
||||
return username.substring(0, 15);
|
||||
}
|
||||
|
||||
return username;
|
||||
}
|
||||
|
||||
public static boolean isInvalid(UUID uuid) {
|
||||
return uuid.equals(INVALID_UUID);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,58 @@
|
|||
package com.Acrobot.ChestShop.Database;
|
||||
|
||||
import com.Acrobot.Breeze.Utils.NameUtil;
|
||||
import com.j256.ormlite.field.DatabaseField;
|
||||
import com.j256.ormlite.table.DatabaseTable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* A mapping for an account
|
||||
* @author Andrzej Pomirski (Acrobot)
|
||||
*/
|
||||
@DatabaseTable(tableName = "accounts")
|
||||
public class Account {
|
||||
|
||||
@DatabaseField(id = true)
|
||||
private String name;
|
||||
|
||||
@DatabaseField(index = true)
|
||||
private String shortName;
|
||||
|
||||
@DatabaseField
|
||||
private UUID uuid;
|
||||
|
||||
public Account() {
|
||||
//empty constructor, needed for ORMLite
|
||||
}
|
||||
|
||||
public Account(String name, UUID uuid) {
|
||||
this.name = name;
|
||||
this.shortName = NameUtil.stripUsername(name);
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getShortName() {
|
||||
return shortName;
|
||||
}
|
||||
|
||||
public void setShortName(String shortName) {
|
||||
this.shortName = shortName;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,14 @@
|
|||
package com.Acrobot.ChestShop.Database;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @author Andrzej Pomirski (Acrobot)
|
||||
*/
|
||||
public class ConnectionManager {
|
||||
private static final String URI_STRING = "jdbc:sqlite:%s";
|
||||
|
||||
public static String getURI(File databaseFile) {
|
||||
return String.format(URI_STRING, databaseFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
package com.Acrobot.ChestShop.Listeners.Player;
|
||||
|
||||
import com.Acrobot.Breeze.Utils.NameUtil;
|
||||
import com.Acrobot.ChestShop.ChestShop;
|
||||
import com.Acrobot.ChestShop.Permission;
|
||||
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
||||
import com.Acrobot.ChestShop.UUIDs.UUIDSaver;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
|
||||
/**
|
||||
|
@ -25,4 +28,9 @@ public class PlayerConnect implements Listener {
|
|||
ChestShop.getBukkitLogger().severe(event.getAddress() + " tried to log in on Admin Shop's account!");
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public static void onPlayerConnect(PlayerJoinEvent event) {
|
||||
UUIDSaver.storeUsername(event.getPlayer());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,13 @@ package com.Acrobot.ChestShop.UUIDs;
|
|||
|
||||
import com.Acrobot.Breeze.Utils.NameUtil;
|
||||
import com.Acrobot.ChestShop.ChestShop;
|
||||
import com.Acrobot.ChestShop.Database.Account;
|
||||
import com.Acrobot.ChestShop.Database.ConnectionManager;
|
||||
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;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.Configuration;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
@ -10,63 +17,89 @@ import org.bukkit.entity.Player;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.Acrobot.Breeze.Utils.NameUtil.*;
|
||||
|
||||
/**
|
||||
* Lets you save/cache username and UUID relations
|
||||
* @author Andrzej Pomirski (Acrobot)
|
||||
*/
|
||||
public class UUIDSaver {
|
||||
private static FileConfiguration uuidStorage;
|
||||
private static Dao<Account, String> accounts;
|
||||
|
||||
public static UUID getUUID(Player player) {
|
||||
if (uuidStorage.getString(player.getUniqueId().toString()) == null) {
|
||||
uuidStorage.set(player.getUniqueId().toString(), player.getPlayer());
|
||||
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;
|
||||
}
|
||||
|
||||
return player.getUniqueId();
|
||||
if (account == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return account.getUuid();
|
||||
}
|
||||
|
||||
public static String getUsername(Player player) {
|
||||
if (uuidStorage.getString(player.getUniqueId().toString()) != null) {
|
||||
uuidStorage.set(player.getUniqueId().toString(), player.getName());
|
||||
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;
|
||||
}
|
||||
|
||||
return uuidStorage.getString(player.getUniqueId().toString());
|
||||
return account.getName();
|
||||
}
|
||||
|
||||
public static String getUsername(final UUID uuid) {
|
||||
String username = uuidStorage.getString(uuid.toString());
|
||||
public static void storeUsername(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
|
||||
if (username != null) {
|
||||
return username;
|
||||
Account account = null;
|
||||
|
||||
try {
|
||||
account = accounts.queryBuilder().selectColumns("name").where().eq("uuid", uuid).queryForFirst();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String name = NameUtil.getName(uuid);
|
||||
if (account != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (name != null) {
|
||||
uuidStorage.set(uuid.toString(), name);
|
||||
}
|
||||
}
|
||||
});
|
||||
account = new Account(player.getName(), player.getUniqueId());
|
||||
|
||||
return uuidStorage.getString(uuid.toString());
|
||||
try {
|
||||
accounts.create(account);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void load() {
|
||||
File uuidStorageFile = ChestShop.loadFile("uuid.storage");
|
||||
uuidStorage = YamlConfiguration.loadConfiguration(uuidStorageFile);
|
||||
}
|
||||
|
||||
public static void save() {
|
||||
File uuidStorageFile = ChestShop.loadFile("uuid.storage");
|
||||
File databaseFile = ChestShop.loadFile("users.db");
|
||||
String uri = ConnectionManager.getURI(databaseFile);
|
||||
ConnectionSource connection;
|
||||
|
||||
try {
|
||||
uuidStorage.save(uuidStorageFile);
|
||||
} catch (IOException e) {
|
||||
connection = new JdbcConnectionSource(uri);
|
||||
accounts = DaoManager.createDao(connection, Account.class);
|
||||
|
||||
TableUtils.createTable(connection, Account.class);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user