Search in sources :

Example 6 with DataType

use of net.luckperms.api.model.data.DataType in project LuckPerms by lucko.

the class GroupRename method execute.

@Override
public void execute(LuckPermsPlugin plugin, Sender sender, Group target, ArgumentList args, String label) {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    String newGroupName = args.get(0).toLowerCase(Locale.ROOT);
    if (!DataConstraints.GROUP_NAME_TEST.test(newGroupName)) {
        Message.GROUP_INVALID_ENTRY.send(sender, newGroupName);
        return;
    }
    if (plugin.getStorage().loadGroup(newGroupName).join().isPresent()) {
        Message.ALREADY_EXISTS.send(sender, newGroupName);
        return;
    }
    Group newGroup;
    try {
        newGroup = plugin.getStorage().createAndLoadGroup(newGroupName, CreationCause.COMMAND).get();
    } catch (Exception e) {
        plugin.getLogger().warn("Error whilst creating group", e);
        Message.CREATE_ERROR.send(sender, Component.text(newGroupName));
        return;
    }
    try {
        plugin.getStorage().deleteGroup(target, DeletionCause.COMMAND).get();
    } catch (Exception e) {
        plugin.getLogger().warn("Error whilst deleting group", e);
        Message.DELETE_ERROR.send(sender, target.getFormattedDisplayName());
        return;
    }
    newGroup.setNodes(DataType.NORMAL, target.normalData().asList(), false);
    Message.RENAME_SUCCESS.send(sender, target.getFormattedDisplayName(), newGroup.getFormattedDisplayName());
    LoggedAction.build().source(sender).target(target).description("rename", newGroup.getName()).build().submit(plugin, sender);
    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());
}
Also used : DeletionCause(net.luckperms.api.event.cause.DeletionCause) DataConstraints(me.lucko.luckperms.common.storage.misc.DataConstraints) CompletableFuture(java.util.concurrent.CompletableFuture) Constraint(me.lucko.luckperms.common.bulkupdate.comparison.Constraint) BulkUpdate(me.lucko.luckperms.common.bulkupdate.BulkUpdate) StorageAssistant(me.lucko.luckperms.common.command.utils.StorageAssistant) CreationCause(net.luckperms.api.event.cause.CreationCause) Query(me.lucko.luckperms.common.bulkupdate.query.Query) Message(me.lucko.luckperms.common.locale.Message) QueryField(me.lucko.luckperms.common.bulkupdate.query.QueryField) Component(net.kyori.adventure.text.Component) Locale(java.util.Locale) Predicates(me.lucko.luckperms.common.util.Predicates) CommandSpec(me.lucko.luckperms.common.command.spec.CommandSpec) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) ArgumentList(me.lucko.luckperms.common.command.utils.ArgumentList) StandardComparison(me.lucko.luckperms.common.bulkupdate.comparison.StandardComparison) LoggedAction(me.lucko.luckperms.common.actionlog.LoggedAction) CompletionSupplier(me.lucko.luckperms.common.command.tabcomplete.CompletionSupplier) Sender(me.lucko.luckperms.common.sender.Sender) UpdateAction(me.lucko.luckperms.common.bulkupdate.action.UpdateAction) TabCompleter(me.lucko.luckperms.common.command.tabcomplete.TabCompleter) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) List(java.util.List) Group(me.lucko.luckperms.common.model.Group) ChildCommand(me.lucko.luckperms.common.command.abstraction.ChildCommand) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) Inheritance(me.lucko.luckperms.common.node.types.Inheritance) BulkUpdateBuilder(me.lucko.luckperms.common.bulkupdate.BulkUpdateBuilder) DataType(net.luckperms.api.model.data.DataType) Group(me.lucko.luckperms.common.model.Group) BulkUpdate(me.lucko.luckperms.common.bulkupdate.BulkUpdate)

Aggregations

DataType (net.luckperms.api.model.data.DataType)6 InheritanceGraph (me.lucko.luckperms.common.inheritance.InheritanceGraph)4 ArrayList (java.util.ArrayList)2 Node (net.luckperms.api.node.Node)2 InheritanceNode (net.luckperms.api.node.types.InheritanceNode)2 List (java.util.List)1 Locale (java.util.Locale)1 OptionalInt (java.util.OptionalInt)1 TreeSet (java.util.TreeSet)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 LoggedAction (me.lucko.luckperms.common.actionlog.LoggedAction)1 BulkUpdate (me.lucko.luckperms.common.bulkupdate.BulkUpdate)1 BulkUpdateBuilder (me.lucko.luckperms.common.bulkupdate.BulkUpdateBuilder)1 UpdateAction (me.lucko.luckperms.common.bulkupdate.action.UpdateAction)1 Constraint (me.lucko.luckperms.common.bulkupdate.comparison.Constraint)1 StandardComparison (me.lucko.luckperms.common.bulkupdate.comparison.StandardComparison)1 Query (me.lucko.luckperms.common.bulkupdate.query.Query)1 QueryField (me.lucko.luckperms.common.bulkupdate.query.QueryField)1 ChildCommand (me.lucko.luckperms.common.command.abstraction.ChildCommand)1 ArgumentPermissions (me.lucko.luckperms.common.command.access.ArgumentPermissions)1