diff --git a/api/src/main/java/net/luckperms/api/model/group/GroupManager.java b/api/src/main/java/net/luckperms/api/model/group/GroupManager.java index a0b324804..b8d681a50 100644 --- a/api/src/main/java/net/luckperms/api/model/group/GroupManager.java +++ b/api/src/main/java/net/luckperms/api/model/group/GroupManager.java @@ -102,6 +102,27 @@ public interface GroupManager { */ @NonNull CompletableFuture 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. + * + *

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}.

+ * + * @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 modifyGroup(@NonNull String name, @NonNull Consumer 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. * diff --git a/api/src/main/java/net/luckperms/api/model/user/UserManager.java b/api/src/main/java/net/luckperms/api/model/user/UserManager.java index bd5b7bb43..82ca25523 100644 --- a/api/src/main/java/net/luckperms/api/model/user/UserManager.java +++ b/api/src/main/java/net/luckperms/api/model/user/UserManager.java @@ -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 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. + * + *

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}.

+ * + * @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 modifyUser(@NonNull UUID uniqueId, @NonNull Consumer 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. * diff --git a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiGroupManager.java b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiGroupManager.java index b22af3f00..f08438177 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiGroupManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiGroupManager.java @@ -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> implements net.luckperms.api.model.group.GroupManager { public ApiGroupManager(LuckPermsPlugin plugin, GroupManager handle) { @@ -91,6 +92,19 @@ public class ApiGroupManager extends ApiAbstractManager modifyGroup(@NonNull String name, @NonNull Consumer 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 loadAllGroups() { return this.plugin.getStorage().loadAllGroups(); diff --git a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiUserManager.java b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiUserManager.java index 223c521c9..cba5a3a73 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiUserManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiUserManager.java @@ -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> implements net.luckperms.api.model.user.UserManager { public ApiUserManager(LuckPermsPlugin plugin, UserManager handle) { @@ -93,6 +94,19 @@ public class ApiUserManager extends ApiAbstractManager modifyUser(@NonNull UUID uniqueId, @NonNull Consumer 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 savePlayerData(@NonNull UUID uniqueId, @NonNull String username) { Objects.requireNonNull(uniqueId, "uuid");