From f5995c0afad4fad40c9bb577221ecdba62d3806b Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 29 Aug 2016 12:11:03 +0100 Subject: [PATCH] Fix usernames showing as "null" and NPE related to primaryGroup not copying correctly --- .../commands/user/UserMainCommand.java | 5 +++- .../me/lucko/luckperms/storage/Datastore.java | 8 ++++++ .../storage/methods/FlatfileDatastore.java | 10 ++++++++ .../storage/methods/MongoDBDatastore.java | 14 +++++++++++ .../storage/methods/SQLDatastore.java | 25 +++++++++++++++++++ .../me/lucko/luckperms/users/UserManager.java | 1 + 6 files changed, 62 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java index 5c2790865..c71064ad2 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java @@ -84,7 +84,10 @@ public class UserMainCommand extends MainCommand { } } - if (!plugin.getDatastore().loadUser(u, "null")) { + String name = plugin.getDatastore().getName(u); + if (name == null) name = "null"; + + if (!plugin.getDatastore().loadUser(u, name)) { Message.LOADING_ERROR.send(sender); } diff --git a/common/src/main/java/me/lucko/luckperms/storage/Datastore.java b/common/src/main/java/me/lucko/luckperms/storage/Datastore.java index 03564ae20..b1c65aafb 100644 --- a/common/src/main/java/me/lucko/luckperms/storage/Datastore.java +++ b/common/src/main/java/me/lucko/luckperms/storage/Datastore.java @@ -87,6 +87,7 @@ public abstract class Datastore { public abstract boolean deleteTrack(Track track); public abstract boolean saveUUIDData(String username, UUID uuid); public abstract UUID getUUID(String username); + public abstract String getName(UUID uuid); @@ -219,4 +220,11 @@ public abstract class Datastore { doSync(() -> callback.onComplete(result)); }); } + + public void getName(UUID uuid, Callback callback) { + doAsync(() -> { + String result = getName(uuid); + doSync(() -> callback.onComplete(result)); + }); + } } diff --git a/common/src/main/java/me/lucko/luckperms/storage/methods/FlatfileDatastore.java b/common/src/main/java/me/lucko/luckperms/storage/methods/FlatfileDatastore.java index 95c3fca5f..160204c05 100644 --- a/common/src/main/java/me/lucko/luckperms/storage/methods/FlatfileDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/storage/methods/FlatfileDatastore.java @@ -612,6 +612,16 @@ public class FlatfileDatastore extends Datastore { return UUID.fromString(uuidCache.get(username)); } + @Override + public String getName(UUID uuid) { + for (Map.Entry e : uuidCache.entrySet()) { + if (e.getValue().equalsIgnoreCase(uuid.toString())) { + return e.getKey(); + } + } + return null; + } + interface WriteOperation { boolean onRun(JsonWriter writer) throws IOException; } diff --git a/common/src/main/java/me/lucko/luckperms/storage/methods/MongoDBDatastore.java b/common/src/main/java/me/lucko/luckperms/storage/methods/MongoDBDatastore.java index 285b65364..031fa769a 100644 --- a/common/src/main/java/me/lucko/luckperms/storage/methods/MongoDBDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/storage/methods/MongoDBDatastore.java @@ -413,6 +413,20 @@ public class MongoDBDatastore extends Datastore { }, null); } + @Override + public String getName(UUID uuid) { + return call(() -> { + MongoCollection c = database.getCollection("uuid"); + + try (MongoCursor cursor = c.find(new Document("_id", uuid)).iterator()) { + if (cursor.hasNext()) { + return cursor.next().get("name", String.class); + } + } + return null; + }, null); + } + private static T call(Callable c, T def) { try { return c.call(); diff --git a/common/src/main/java/me/lucko/luckperms/storage/methods/SQLDatastore.java b/common/src/main/java/me/lucko/luckperms/storage/methods/SQLDatastore.java index 1f7aa5fd2..f9592f8b4 100644 --- a/common/src/main/java/me/lucko/luckperms/storage/methods/SQLDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/storage/methods/SQLDatastore.java @@ -71,6 +71,7 @@ abstract class SQLDatastore extends Datastore { private static final String UUIDCACHE_INSERT = "INSERT INTO lp_uuid VALUES(?, ?)"; private static final String UUIDCACHE_SELECT = "SELECT uuid FROM lp_uuid WHERE name=?"; + private static final String UUIDCACHE_SELECT_NAME = "SELECT name FROM lp_uuid WHERE uuid=?"; private static final String UUIDCACHE_UPDATE = "UPDATE lp_uuid SET uuid=? WHERE name=?"; private static final String ACTION_INSERT = "INSERT INTO lp_actions(`time`, `actor_uuid`, `actor_name`, `type`, `acted_uuid`, `acted_name`, `action`) VALUES(?, ?, ?, ?, ?, ?, ?)"; @@ -541,6 +542,30 @@ abstract class SQLDatastore extends Datastore { return success ? uuid[0] : null; } + @Override + public String getName(UUID uuid) { + final String u = uuid.toString(); + final String[] name = {null}; + + boolean success = runQuery(new QueryRS(UUIDCACHE_SELECT_NAME) { + @Override + void onRun(PreparedStatement preparedStatement) throws SQLException { + preparedStatement.setString(1, u); + } + + @Override + boolean onResult(ResultSet resultSet) throws SQLException { + if (resultSet.next()) { + name[0] = resultSet.getString("name"); + return true; + } + return false; + } + }); + + return success ? name[0] : null; + } + private class Query extends QueryPS { Query(String query) { super(query); diff --git a/common/src/main/java/me/lucko/luckperms/users/UserManager.java b/common/src/main/java/me/lucko/luckperms/users/UserManager.java index ee2578937..3ab87e379 100644 --- a/common/src/main/java/me/lucko/luckperms/users/UserManager.java +++ b/common/src/main/java/me/lucko/luckperms/users/UserManager.java @@ -63,6 +63,7 @@ public abstract class UserManager extends AbstractManager { public void copy(User from, User to) { to.setNodes(from.getNodes()); to.setPrimaryGroup(from.getPrimaryGroup()); + giveDefaultIfNeeded(to, true); to.refreshPermissions(); }