Search in sources :

Example 1 with TemporaryNodeMergeStrategy

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

the class MetaAddTempChatMeta method execute.

@Override
public void execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder target, ArgumentList args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    int priority = args.getPriority(0);
    String meta = args.get(1);
    Duration duration = args.getDuration(2);
    TemporaryNodeMergeStrategy modifier = args.getTemporaryModifierAndRemove(3).orElseGet(() -> plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR));
    MutableContextSet context = args.getContextOrDefault(3, plugin);
    if (ArgumentPermissions.checkContext(plugin, sender, permission, context) || ArgumentPermissions.checkGroup(plugin, sender, target, context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    DataMutateResult.WithMergedNode result = target.setNode(DataType.NORMAL, this.type.builder(meta, priority).expiry(duration).withContext(context).build(), modifier);
    if (result.getResult().wasSuccessful()) {
        duration = result.getMergedNode().getExpiryDuration();
        Message.ADD_TEMP_CHATMETA_SUCCESS.send(sender, target, this.type, meta, priority, duration, context);
        LoggedAction.build().source(sender).target(target).description("meta", "addtemp" + this.type.name().toLowerCase(Locale.ROOT), priority, meta, duration, context).build().submit(plugin, sender);
        StorageAssistant.save(target, sender, plugin);
    } else {
        Message.ALREADY_HAS_TEMP_CHAT_META.send(sender, target, this.type, meta, priority, context);
    }
}
Also used : TemporaryNodeMergeStrategy(net.luckperms.api.model.data.TemporaryNodeMergeStrategy) MutableContextSet(net.luckperms.api.context.MutableContextSet) Duration(java.time.Duration) DataMutateResult(net.luckperms.api.model.data.DataMutateResult)

Example 2 with TemporaryNodeMergeStrategy

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

the class MetaSetTempChatMeta method execute.

@Override
public void execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder target, ArgumentList args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    int priority = args.getIntOrDefault(0, Integer.MIN_VALUE);
    String meta;
    Duration duration;
    TemporaryNodeMergeStrategy modifier;
    MutableContextSet context;
    if (priority == Integer.MIN_VALUE) {
        // priority wasn't defined, meta is at index 0, duration at index 1
        meta = args.get(0);
        duration = args.getDuration(1);
        modifier = args.getTemporaryModifierAndRemove(2).orElseGet(() -> plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR));
        context = args.getContextOrDefault(2, plugin);
    } else {
        // priority was defined, meta should be at index 1, duration at index 2
        if (args.size() <= 2) {
            sendDetailedUsage(sender);
            return;
        }
        meta = args.get(1);
        duration = args.getDuration(2);
        modifier = args.getTemporaryModifierAndRemove(3).orElseGet(() -> plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR));
        context = args.getContextOrDefault(3, plugin);
    }
    if (ArgumentPermissions.checkContext(plugin, sender, permission, context) || ArgumentPermissions.checkGroup(plugin, sender, target, context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    // remove all other prefixes/suffixes set in these contexts
    target.removeIf(DataType.NORMAL, context, this.type.nodeType()::matches, false);
    // determine the priority to set at
    if (priority == Integer.MIN_VALUE) {
        MetaAccumulator metaAccumulator = target.accumulateMeta(QueryOptionsImpl.DEFAULT_CONTEXTUAL.toBuilder().context(context).build());
        priority = metaAccumulator.getChatMeta(this.type).keySet().stream().mapToInt(e -> e).max().orElse(0) + 1;
        if (target instanceof Group) {
            OptionalInt weight = target.getWeight();
            if (weight.isPresent() && weight.getAsInt() > priority) {
                priority = weight.getAsInt();
            }
        }
    }
    DataMutateResult.WithMergedNode result = target.setNode(DataType.NORMAL, this.type.builder(meta, priority).expiry(duration).withContext(context).build(), modifier);
    if (result.getResult().wasSuccessful()) {
        duration = result.getMergedNode().getExpiryDuration();
        Message.ADD_TEMP_CHATMETA_SUCCESS.send(sender, target, this.type, meta, priority, duration, context);
        LoggedAction.build().source(sender).target(target).description("meta", "settemp" + this.type.name().toLowerCase(Locale.ROOT), priority, meta, duration, context).build().submit(plugin, sender);
        StorageAssistant.save(target, sender, plugin);
    } else {
        Message.ALREADY_HAS_TEMP_CHAT_META.send(sender, target, this.type, meta, priority, context);
    }
}
Also used : Group(me.lucko.luckperms.common.model.Group) TemporaryNodeMergeStrategy(net.luckperms.api.model.data.TemporaryNodeMergeStrategy) MetaAccumulator(me.lucko.luckperms.common.cacheddata.type.MetaAccumulator) MutableContextSet(net.luckperms.api.context.MutableContextSet) Duration(java.time.Duration) OptionalInt(java.util.OptionalInt) DataMutateResult(net.luckperms.api.model.data.DataMutateResult)

Example 3 with TemporaryNodeMergeStrategy

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

the class ArgumentList method getTemporaryModifierAndRemove.

public Optional<TemporaryNodeMergeStrategy> getTemporaryModifierAndRemove(int index) {
    if (indexOutOfBounds(index)) {
        return Optional.empty();
    }
    TemporaryNodeMergeStrategy strategy = parseTemporaryModifier(get(index));
    if (strategy == null) {
        return Optional.empty();
    }
    remove(index);
    return Optional.of(strategy);
}
Also used : TemporaryNodeMergeStrategy(net.luckperms.api.model.data.TemporaryNodeMergeStrategy)

Example 4 with TemporaryNodeMergeStrategy

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

the class ParentAddTemp method execute.

@Override
public void execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder target, ArgumentList args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    String groupName = args.getLowercase(0, DataConstraints.GROUP_NAME_TEST);
    Duration duration = args.getDuration(1);
    TemporaryNodeMergeStrategy modifier = args.getTemporaryModifierAndRemove(2).orElseGet(() -> plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR));
    MutableContextSet context = args.getContextOrDefault(2, plugin);
    Group group = StorageAssistant.loadGroup(groupName, sender, plugin, false);
    if (group == null) {
        return;
    }
    if (ArgumentPermissions.checkContext(plugin, sender, permission, context) || ArgumentPermissions.checkGroup(plugin, sender, target, context) || ArgumentPermissions.checkGroup(plugin, sender, group, context) || ArgumentPermissions.checkArguments(plugin, sender, permission, group.getName())) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    if (group.getName().equalsIgnoreCase(target.getIdentifier().getName())) {
        Message.ALREADY_TEMP_INHERITS.send(sender, target, group, context);
        return;
    }
    DataMutateResult.WithMergedNode result = target.setNode(DataType.NORMAL, Inheritance.builder(group.getName()).expiry(duration).withContext(context).build(), modifier);
    if (result.getResult().wasSuccessful()) {
        duration = result.getMergedNode().getExpiryDuration();
        Message.SET_TEMP_INHERIT_SUCCESS.send(sender, target, group, duration, context);
        LoggedAction.build().source(sender).target(target).description("parent", "addtemp", group.getName(), duration, context).build().submit(plugin, sender);
        StorageAssistant.save(target, sender, plugin);
    } else {
        Message.ALREADY_TEMP_INHERITS.send(sender, target, group, context);
    }
}
Also used : Group(me.lucko.luckperms.common.model.Group) TemporaryNodeMergeStrategy(net.luckperms.api.model.data.TemporaryNodeMergeStrategy) MutableContextSet(net.luckperms.api.context.MutableContextSet) Duration(java.time.Duration) DataMutateResult(net.luckperms.api.model.data.DataMutateResult)

Example 5 with TemporaryNodeMergeStrategy

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

the class PermissionSetTemp method execute.

@Override
public void execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder target, ArgumentList args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    String node = args.get(0);
    boolean value = args.getBooleanOrInsert(1, true);
    Duration duration = args.getDuration(2);
    TemporaryNodeMergeStrategy modifier = args.getTemporaryModifierAndRemove(3).orElseGet(() -> plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR));
    MutableContextSet context = args.getContextOrDefault(3, plugin);
    if (node.isEmpty()) {
        Message.PERMISSION_INVALID_ENTRY_EMPTY.send(sender);
    }
    if (ArgumentPermissions.checkContext(plugin, sender, permission, context) || ArgumentPermissions.checkGroup(plugin, sender, target, context) || ArgumentPermissions.checkArguments(plugin, sender, permission, node)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    Node builtNode = NodeBuilders.determineMostApplicable(node).value(value).withContext(context).expiry(duration).build();
    if (builtNode instanceof InheritanceNode) {
        if (ArgumentPermissions.checkGroup(plugin, sender, ((InheritanceNode) builtNode).getGroupName(), context)) {
            Message.COMMAND_NO_PERMISSION.send(sender);
            return;
        }
    }
    DataMutateResult.WithMergedNode result = target.setNode(DataType.NORMAL, builtNode, modifier);
    if (result.getResult().wasSuccessful()) {
        duration = result.getMergedNode().getExpiryDuration();
        Message.SETPERMISSION_TEMP_SUCCESS.send(sender, node, value, target, duration, context);
        LoggedAction.build().source(sender).target(target).description("permission", "settemp", node, value, duration, context).build().submit(plugin, sender);
        StorageAssistant.save(target, sender, plugin);
    } else {
        Message.ALREADY_HAS_TEMP_PERMISSION.send(sender, target, node, context);
    }
}
Also used : TemporaryNodeMergeStrategy(net.luckperms.api.model.data.TemporaryNodeMergeStrategy) MutableContextSet(net.luckperms.api.context.MutableContextSet) Node(net.luckperms.api.node.Node) InheritanceNode(net.luckperms.api.node.types.InheritanceNode) InheritanceNode(net.luckperms.api.node.types.InheritanceNode) Duration(java.time.Duration) DataMutateResult(net.luckperms.api.model.data.DataMutateResult)

Aggregations

TemporaryNodeMergeStrategy (net.luckperms.api.model.data.TemporaryNodeMergeStrategy)6 Duration (java.time.Duration)5 MutableContextSet (net.luckperms.api.context.MutableContextSet)5 DataMutateResult (net.luckperms.api.model.data.DataMutateResult)5 Group (me.lucko.luckperms.common.model.Group)2 Node (net.luckperms.api.node.Node)2 List (java.util.List)1 OptionalInt (java.util.OptionalInt)1 LoggedAction (me.lucko.luckperms.common.actionlog.LoggedAction)1 MetaAccumulator (me.lucko.luckperms.common.cacheddata.type.MetaAccumulator)1 CommandException (me.lucko.luckperms.common.command.abstraction.CommandException)1 GenericChildCommand (me.lucko.luckperms.common.command.abstraction.GenericChildCommand)1 ArgumentPermissions (me.lucko.luckperms.common.command.access.ArgumentPermissions)1 CommandPermission (me.lucko.luckperms.common.command.access.CommandPermission)1 CommandSpec (me.lucko.luckperms.common.command.spec.CommandSpec)1 TabCompleter (me.lucko.luckperms.common.command.tabcomplete.TabCompleter)1 TabCompletions (me.lucko.luckperms.common.command.tabcomplete.TabCompletions)1 ArgumentList (me.lucko.luckperms.common.command.utils.ArgumentList)1 StorageAssistant (me.lucko.luckperms.common.command.utils.StorageAssistant)1 ConfigKeys (me.lucko.luckperms.common.config.ConfigKeys)1