Fix usernames showing as "null" and NPE related to primaryGroup not copying correctly

This commit is contained in:
Luck 2016-08-29 12:11:03 +01:00
parent 2cc65160b4
commit f5995c0afa
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
6 changed files with 62 additions and 1 deletions

View File

@ -84,7 +84,10 @@ public class UserMainCommand extends MainCommand<User> {
}
}
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);
}

View File

@ -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<String> callback) {
doAsync(() -> {
String result = getName(uuid);
doSync(() -> callback.onComplete(result));
});
}
}

View File

@ -612,6 +612,16 @@ public class FlatfileDatastore extends Datastore {
return UUID.fromString(uuidCache.get(username));
}
@Override
public String getName(UUID uuid) {
for (Map.Entry<String, String> e : uuidCache.entrySet()) {
if (e.getValue().equalsIgnoreCase(uuid.toString())) {
return e.getKey();
}
}
return null;
}
interface WriteOperation {
boolean onRun(JsonWriter writer) throws IOException;
}

View File

@ -413,6 +413,20 @@ public class MongoDBDatastore extends Datastore {
}, null);
}
@Override
public String getName(UUID uuid) {
return call(() -> {
MongoCollection<Document> c = database.getCollection("uuid");
try (MongoCursor<Document> cursor = c.find(new Document("_id", uuid)).iterator()) {
if (cursor.hasNext()) {
return cursor.next().get("name", String.class);
}
}
return null;
}, null);
}
private static <T> T call(Callable<T> c, T def) {
try {
return c.call();

View File

@ -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);

View File

@ -63,6 +63,7 @@ public abstract class UserManager extends AbstractManager<UUID, User> {
public void copy(User from, User to) {
to.setNodes(from.getNodes());
to.setPrimaryGroup(from.getPrimaryGroup());
giveDefaultIfNeeded(to, true);
to.refreshPermissions();
}