Search in sources :

Example 1 with PlayerSaveResult

use of me.lucko.luckperms.common.storage.PlayerSaveResult in project LuckPerms by lucko.

the class FileUuidCache method addMapping.

/**
 * Adds a mapping to the cache
 *
 * @param uuid the uuid of the player
 * @param username the username of the player
 */
public PlayerSaveResult addMapping(UUID uuid, String username) {
    // perform the insert
    String oldUsername = this.lookupMap.put(uuid, username);
    PlayerSaveResult result = PlayerSaveResult.determineBaseResult(username, oldUsername);
    Set<UUID> conflicting = new HashSet<>(this.lookupMap.lookupUuid(username));
    conflicting.remove(uuid);
    if (!conflicting.isEmpty()) {
        // remove the mappings for conflicting uuids
        for (UUID conflict : conflicting) {
            this.lookupMap.remove(conflict);
        }
        result = result.withOtherUuidsPresent(conflicting);
    }
    return result;
}
Also used : PlayerSaveResult(me.lucko.luckperms.common.storage.PlayerSaveResult) UUID(java.util.UUID) HashSet(java.util.HashSet)

Example 2 with PlayerSaveResult

use of me.lucko.luckperms.common.storage.PlayerSaveResult in project LuckPerms by lucko.

the class MongoDao method savePlayerData.

@Override
public PlayerSaveResult savePlayerData(UUID uuid, String username) {
    username = username.toLowerCase();
    MongoCollection<Document> c = this.database.getCollection(this.prefix + "uuid");
    // find any existing mapping
    String oldUsername = getPlayerName(uuid);
    // do the insert
    if (!username.equalsIgnoreCase(oldUsername)) {
        c.replaceOne(new Document("_id", uuid), new Document("_id", uuid).append("name", username), new UpdateOptions().upsert(true));
    }
    PlayerSaveResult result = PlayerSaveResult.determineBaseResult(username, oldUsername);
    Set<UUID> conflicting = new HashSet<>();
    try (MongoCursor<Document> cursor = c.find(new Document("name", username)).iterator()) {
        if (cursor.hasNext()) {
            conflicting.add(cursor.next().get("_id", UUID.class));
        }
    }
    conflicting.remove(uuid);
    if (!conflicting.isEmpty()) {
        // remove the mappings for conflicting uuids
        c.deleteMany(Filters.and(conflicting.stream().map(u -> Filters.eq("_id", u)).collect(Collectors.toList())));
        result = result.withOtherUuidsPresent(conflicting);
    }
    return result;
}
Also used : PlayerSaveResult(me.lucko.luckperms.common.storage.PlayerSaveResult) LogEntry(me.lucko.luckperms.api.LogEntry) Document(org.bson.Document) MongoClientURI(com.mongodb.MongoClientURI) AbstractDao(me.lucko.luckperms.common.storage.dao.AbstractDao) MongoCredential(com.mongodb.MongoCredential) MongoCollection(com.mongodb.client.MongoCollection) UserIdentifier(me.lucko.luckperms.common.references.UserIdentifier) MongoDatabase(com.mongodb.client.MongoDatabase) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) BulkUpdate(me.lucko.luckperms.common.bulkupdate.BulkUpdate) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) LegacyNodeFactory(me.lucko.luckperms.common.node.LegacyNodeFactory) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Strings(com.google.common.base.Strings) PlayerSaveResult(me.lucko.luckperms.common.storage.PlayerSaveResult) NodeModel(me.lucko.luckperms.common.node.NodeModel) MongoCursor(com.mongodb.client.MongoCursor) MongoClientOptions(com.mongodb.MongoClientOptions) Map(java.util.Map) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) HeldPermission(me.lucko.luckperms.api.HeldPermission) UpdateOptions(com.mongodb.client.model.UpdateOptions) ServerAddress(com.mongodb.ServerAddress) ExtendedLogEntry(me.lucko.luckperms.common.actionlog.ExtendedLogEntry) GroupManager(me.lucko.luckperms.common.managers.group.GroupManager) StorageCredentials(me.lucko.luckperms.common.storage.StorageCredentials) NodeHeldPermission(me.lucko.luckperms.common.node.NodeHeldPermission) Set(java.util.Set) ImmutableContextSet(me.lucko.luckperms.api.context.ImmutableContextSet) Log(me.lucko.luckperms.common.actionlog.Log) NodeFactory(me.lucko.luckperms.common.node.NodeFactory) UUID(java.util.UUID) ContextSet(me.lucko.luckperms.api.context.ContextSet) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) TrackManager(me.lucko.luckperms.common.managers.track.TrackManager) MongoClient(com.mongodb.MongoClient) Group(me.lucko.luckperms.common.model.Group) Optional(java.util.Optional) Node(me.lucko.luckperms.api.Node) User(me.lucko.luckperms.common.model.User) Document(org.bson.Document) UUID(java.util.UUID) UpdateOptions(com.mongodb.client.model.UpdateOptions) HashSet(java.util.HashSet)

Example 3 with PlayerSaveResult

use of me.lucko.luckperms.common.storage.PlayerSaveResult in project LuckPerms by lucko.

the class SqlDao method savePlayerData.

@Override
public PlayerSaveResult savePlayerData(UUID uuid, String username) throws SQLException {
    username = username.toLowerCase();
    // find any existing mapping
    String oldUsername = getPlayerName(uuid);
    // do the insert
    if (!username.equalsIgnoreCase(oldUsername)) {
        try (Connection c = this.provider.getConnection()) {
            if (oldUsername != null) {
                try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_UPDATE_USERNAME_FOR_UUID))) {
                    ps.setString(1, username);
                    ps.setString(2, uuid.toString());
                    ps.execute();
                }
            } else {
                try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_INSERT))) {
                    ps.setString(1, uuid.toString());
                    ps.setString(2, username);
                    ps.setString(3, NodeFactory.DEFAULT_GROUP_NAME);
                    ps.execute();
                }
            }
        }
    }
    PlayerSaveResult result = PlayerSaveResult.determineBaseResult(username, oldUsername);
    Set<UUID> conflicting = new HashSet<>();
    try (Connection c = this.provider.getConnection()) {
        try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_SELECT_ALL_UUIDS_BY_USERNAME))) {
            ps.setString(1, username);
            ps.setString(2, uuid.toString());
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    conflicting.add(UUID.fromString(rs.getString("uuid")));
                }
            }
        }
    }
    if (!conflicting.isEmpty()) {
        // remove the mappings for conflicting uuids
        try (Connection c = this.provider.getConnection()) {
            try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_DELETE_ALL_UUIDS_BY_USERNAME))) {
                ps.setString(1, username);
                ps.setString(2, uuid.toString());
                ps.execute();
            }
        }
        result = result.withOtherUuidsPresent(conflicting);
    }
    return result;
}
Also used : PlayerSaveResult(me.lucko.luckperms.common.storage.PlayerSaveResult) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) UUID(java.util.UUID) HashSet(java.util.HashSet)

Example 4 with PlayerSaveResult

use of me.lucko.luckperms.common.storage.PlayerSaveResult in project LuckPerms by lucko.

the class AbstractConnectionListener method loadUser.

public User loadUser(UUID u, String username) {
    final long startTime = System.currentTimeMillis();
    // register with the housekeeper to avoid accidental unloads
    this.plugin.getUserManager().getHouseKeeper().registerUsage(u);
    // save uuid data.
    PlayerSaveResult saveResult = this.plugin.getStorage().savePlayerData(u, username).join();
    if (saveResult.includes(PlayerSaveResult.Status.CLEAN_INSERT)) {
        this.plugin.getEventFactory().handleUserFirstLogin(u, username);
    }
    if (saveResult.includes(PlayerSaveResult.Status.OTHER_UUIDS_PRESENT_FOR_USERNAME)) {
        this.plugin.getLogger().warn("LuckPerms already has data for player '" + username + "' - but this data is stored under a different uuid.");
        this.plugin.getLogger().warn("'" + username + "' has previously used the unique ids " + saveResult.getOtherUuids() + " but is now connecting with '" + u + "'");
        this.plugin.getLogger().warn("This is usually because the server is not authenticating correctly. If you're using BungeeCord, please ensure that IP-Forwarding is setup correctly!");
    }
    User user = this.plugin.getStorage().noBuffer().loadUser(u, username).join();
    if (user == null) {
        throw new NullPointerException("User is null");
    } else {
        // Setup defaults for the user
        boolean save = false;
        for (AssignmentRule rule : this.plugin.getConfiguration().get(ConfigKeys.DEFAULT_ASSIGNMENTS)) {
            if (rule.apply(user)) {
                save = true;
            }
        }
        // If they were given a default, persist the new assignments back to the storage.
        if (save) {
            this.plugin.getStorage().noBuffer().saveUser(user).join();
        }
        // Does some minimum pre-calculations to (maybe) speed things up later.
        user.preCalculateData();
    }
    final long time = System.currentTimeMillis() - startTime;
    if (time >= 1000) {
        this.plugin.getLogger().warn("Processing login for " + username + " took " + time + "ms.");
    }
    return user;
}
Also used : PlayerSaveResult(me.lucko.luckperms.common.storage.PlayerSaveResult) User(me.lucko.luckperms.common.model.User) AssignmentRule(me.lucko.luckperms.common.assignments.AssignmentRule)

Aggregations

PlayerSaveResult (me.lucko.luckperms.common.storage.PlayerSaveResult)4 HashSet (java.util.HashSet)3 UUID (java.util.UUID)3 User (me.lucko.luckperms.common.model.User)2 Strings (com.google.common.base.Strings)1 MongoClient (com.mongodb.MongoClient)1 MongoClientOptions (com.mongodb.MongoClientOptions)1 MongoClientURI (com.mongodb.MongoClientURI)1 MongoCredential (com.mongodb.MongoCredential)1 ServerAddress (com.mongodb.ServerAddress)1 MongoCollection (com.mongodb.client.MongoCollection)1 MongoCursor (com.mongodb.client.MongoCursor)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 Filters (com.mongodb.client.model.Filters)1 UpdateOptions (com.mongodb.client.model.UpdateOptions)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1