diff --git a/common/src/main/java/me/lucko/luckperms/common/command/utils/StorageAssistant.java b/common/src/main/java/me/lucko/luckperms/common/command/utils/StorageAssistant.java index 7cc180d56..8e4bdbba1 100644 --- a/common/src/main/java/me/lucko/luckperms/common/command/utils/StorageAssistant.java +++ b/common/src/main/java/me/lucko/luckperms/common/command/utils/StorageAssistant.java @@ -37,6 +37,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.sender.Sender; import java.util.Optional; +import java.util.concurrent.CompletableFuture; /** * Utility methods for saving users, groups and tracks. @@ -88,40 +89,28 @@ public final class StorageAssistant { } } - public static void save(Group group, Sender sender, LuckPermsPlugin plugin) { + public static CompletableFuture save(Group group, Sender sender, LuckPermsPlugin plugin) { try { plugin.getStorage().saveGroup(group).get(); } catch (Exception e) { plugin.getLogger().warn("Error whilst saving group", e); Message.GROUP_SAVE_ERROR.send(sender, group); - return; + return failedFuture(e); } - plugin.getGroupManager().invalidateAllGroupCaches(); - plugin.getUserManager().invalidateAllUserCaches(); - - Optional messagingService = plugin.getMessagingService(); - if (messagingService.isPresent() && plugin.getConfiguration().get(ConfigKeys.AUTO_PUSH_UPDATES)) { - messagingService.get().getUpdateBuffer().request(); - } + return invalidateCachesAndPushUpdates(plugin); } - public static void save(Track track, Sender sender, LuckPermsPlugin plugin) { + public static CompletableFuture save(Track track, Sender sender, LuckPermsPlugin plugin) { try { plugin.getStorage().saveTrack(track).get(); } catch (Exception e) { plugin.getLogger().warn("Error whilst saving track", e); Message.TRACK_SAVE_ERROR.send(sender, track.getName()); - return; + return failedFuture(e); } - plugin.getGroupManager().invalidateAllGroupCaches(); - plugin.getUserManager().invalidateAllUserCaches(); - - Optional messagingService = plugin.getMessagingService(); - if (messagingService.isPresent() && plugin.getConfiguration().get(ConfigKeys.AUTO_PUSH_UPDATES)) { - messagingService.get().getUpdateBuffer().request(); - } + return invalidateCachesAndPushUpdates(plugin); } public static void save(PermissionHolder holder, Sender sender, LuckPermsPlugin plugin) { @@ -136,4 +125,21 @@ public final class StorageAssistant { } } + public static CompletableFuture invalidateCachesAndPushUpdates(LuckPermsPlugin plugin) { + plugin.getGroupManager().invalidateAllGroupCaches(); + plugin.getUserManager().invalidateAllUserCaches(); + + Optional messagingService = plugin.getMessagingService(); + if (messagingService.isPresent() && plugin.getConfiguration().get(ConfigKeys.AUTO_PUSH_UPDATES)) { + return messagingService.get().getUpdateBuffer().request(); + } else { + return CompletableFuture.completedFuture(null); + } + } + + private static CompletableFuture failedFuture(Throwable ex) { + CompletableFuture future = new CompletableFuture<>(); + future.completeExceptionally(ex); + return future; + } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupRename.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupRename.java index 0186e3dcf..23c625fce 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupRename.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/GroupRename.java @@ -55,6 +55,7 @@ import net.luckperms.api.event.cause.DeletionCause; import net.luckperms.api.model.data.DataType; import java.util.List; +import java.util.concurrent.CompletableFuture; public class GroupRename extends ChildCommand { public GroupRename() { @@ -104,24 +105,27 @@ public class GroupRename extends ChildCommand { .description("rename", newGroup.getName()) .build().submit(plugin, sender); - if (!args.remove("--update-parent-lists")) { - StorageAssistant.save(newGroup, sender, plugin); - } else { - // the group is now renamed, proceed to update its representing inheritance nodes - BulkUpdate operation = BulkUpdateBuilder.create() - .trackStatistics(false) - .dataType(me.lucko.luckperms.common.bulkupdate.DataType.ALL) - .action(UpdateAction.of(QueryField.PERMISSION, Inheritance.key(newGroupName))) - .query(Query.of(QueryField.PERMISSION, Constraint.of(StandardComparison.EQUAL, Inheritance.key(target.getName())))) - .build(); - plugin.getStorage().applyBulkUpdate(operation).whenCompleteAsync((v, ex) -> { - plugin.getSyncTaskBuffer().requestDirectly(); - if (ex != null) { - ex.printStackTrace(); - } - }, plugin.getBootstrap().getScheduler().async()) - .thenRunAsync(() -> StorageAssistant.save(newGroup, sender, plugin)); - } + StorageAssistant.save(newGroup, sender, plugin) + .thenCompose((v) -> { + if (args.remove("--update-parent-lists")) { + // the group is now renamed, proceed to update its representing inheritance nodes + BulkUpdate operation = BulkUpdateBuilder.create() + .trackStatistics(false) + .dataType(me.lucko.luckperms.common.bulkupdate.DataType.ALL) + .action(UpdateAction.of(QueryField.PERMISSION, Inheritance.key(newGroupName))) + .query(Query.of(QueryField.PERMISSION, Constraint.of(StandardComparison.EQUAL, Inheritance.key(target.getName())))) + .build(); + return plugin.getStorage().applyBulkUpdate(operation); + } else { + return CompletableFuture.completedFuture(v); + } + }).whenCompleteAsync((v, ex) -> { + if (ex != null) { + ex.printStackTrace(); + } + + plugin.getSyncTaskBuffer().requestDirectly(); + }, plugin.getBootstrap().getScheduler().async()); } @Override