Add modifyUser and modifyGroup convenience API methods

This commit is contained in:
Luck 2020-05-10 12:40:20 +01:00
parent a19cb71394
commit bcfcb64931
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
4 changed files with 71 additions and 1 deletions

View File

@ -102,6 +102,27 @@ public interface GroupManager {
*/
@NonNull CompletableFuture<Void> deleteGroup(@NonNull Group group);
/**
* Loads (or creates) a group from the plugin's storage provider, applies the given
* {@code action}, then saves the group's data back to storage.
*
* <p>This method effectively calls {@link #createAndLoadGroup(String)}, followed by the
* {@code action}, then {@link #saveGroup(Group)}, and returns an encapsulation of the whole
* process as a {@link CompletableFuture}. </p>
*
* @param name the name of the group
* @param action the action to apply to the group
* @return a future to encapsulate the operation
* @since 5.1
*/
default @NonNull CompletableFuture<Void> modifyGroup(@NonNull String name, @NonNull Consumer<? super Group> action) {
/* This default method is overridden in the implementation, and is just here
to demonstrate what this method does in the API sources. */
return createAndLoadGroup(name)
.thenApplyAsync(group -> { action.accept(group); return group; })
.thenCompose(this::saveGroup);
}
/**
* Loads all groups into memory.
*

View File

@ -63,7 +63,7 @@ public interface UserManager {
/**
* Loads a user from the plugin's storage provider into memory.
*
* @param uniqueId the uuid of the user
* @param uniqueId the uuid of the user
* @param username the username, if known
* @return the resultant user
* @throws NullPointerException if the uuid is null
@ -115,6 +115,27 @@ public interface UserManager {
*/
@NonNull CompletableFuture<Void> saveUser(@NonNull User user);
/**
* Loads a user from the plugin's storage provider, applies the given {@code action},
* then saves the user's data back to storage.
*
* <p>This method effectively calls {@link #loadUser(UUID)}, followed by the {@code action},
* then {@link #saveUser(User)}, and returns an encapsulation of the whole process as a
* {@link CompletableFuture}. </p>
*
* @param uniqueId the uuid of the user
* @param action the action to apply to the user
* @return a future to encapsulate the operation
* @since 5.1
*/
default @NonNull CompletableFuture<Void> modifyUser(@NonNull UUID uniqueId, @NonNull Consumer<? super User> action) {
/* This default method is overridden in the implementation, and is just here
to demonstrate what this method does in the API sources. */
return loadUser(uniqueId)
.thenApplyAsync(user -> { action.accept(user); return user; })
.thenCompose(this::saveUser);
}
/**
* Saves data about a player to the uuid caching system.
*

View File

@ -51,6 +51,7 @@ import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
public class ApiGroupManager extends ApiAbstractManager<Group, net.luckperms.api.model.group.Group, GroupManager<?>> implements net.luckperms.api.model.group.GroupManager {
public ApiGroupManager(LuckPermsPlugin plugin, GroupManager<?> handle) {
@ -91,6 +92,19 @@ public class ApiGroupManager extends ApiAbstractManager<Group, net.luckperms.api
return this.plugin.getStorage().deleteGroup(ApiGroup.cast(group), DeletionCause.API);
}
@Override
public @NonNull CompletableFuture<Void> modifyGroup(@NonNull String name, @NonNull Consumer<? super net.luckperms.api.model.group.Group> action) {
Objects.requireNonNull(name, "name");
Objects.requireNonNull(action, "action");
return this.plugin.getStorage().createAndLoadGroup(name, CreationCause.API)
.thenApplyAsync(group -> {
action.accept(group.getApiProxy());
return group;
}, this.plugin.getBootstrap().getScheduler().async())
.thenCompose(group -> this.plugin.getStorage().saveGroup(group));
}
@Override
public @NonNull CompletableFuture<Void> loadAllGroups() {
return this.plugin.getStorage().loadAllGroups();

View File

@ -51,6 +51,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
public class ApiUserManager extends ApiAbstractManager<User, net.luckperms.api.model.user.User, UserManager<?>> implements net.luckperms.api.model.user.UserManager {
public ApiUserManager(LuckPermsPlugin plugin, UserManager<?> handle) {
@ -93,6 +94,19 @@ public class ApiUserManager extends ApiAbstractManager<User, net.luckperms.api.m
return this.plugin.getStorage().saveUser(ApiUser.cast(user));
}
@Override
public @NonNull CompletableFuture<Void> modifyUser(@NonNull UUID uniqueId, @NonNull Consumer<? super net.luckperms.api.model.user.User> action) {
Objects.requireNonNull(uniqueId, "uniqueId");
Objects.requireNonNull(action, "action");
return this.plugin.getStorage().loadUser(uniqueId, null)
.thenApplyAsync(user -> {
action.accept(user.getApiProxy());
return user;
}, this.plugin.getBootstrap().getScheduler().async())
.thenCompose(user -> this.plugin.getStorage().saveUser(user));
}
@Override
public @NonNull CompletableFuture<PlayerSaveResult> savePlayerData(@NonNull UUID uniqueId, @NonNull String username) {
Objects.requireNonNull(uniqueId, "uuid");