Search in sources :

Example 11 with MutableContextSet

use of net.luckperms.api.context.MutableContextSet in project LuckPerms by lucko.

the class PermissionUnsetTemp 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);
    Duration duration = args.getDurationOrDefault(1, null);
    int fromIndex = duration == null ? 1 : 2;
    MutableContextSet context = args.getContextOrDefault(fromIndex, 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).expiry(10L).withContext(context).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.unsetNode(DataType.NORMAL, builtNode, duration);
    if (result.getResult().wasSuccessful()) {
        Node mergedNode = result.getMergedNode();
        // noinspection ConstantConditions
        if (mergedNode != null) {
            Message.UNSET_TEMP_PERMISSION_SUBTRACT_SUCCESS.send(sender, mergedNode.getKey(), mergedNode.getValue(), target, mergedNode.getExpiryDuration(), context, duration);
            LoggedAction.build().source(sender).target(target).description("permission", "unsettemp", node, duration, context).build().submit(plugin, sender);
        } else {
            Message.UNSET_TEMP_PERMISSION_SUCCESS.send(sender, node, target, context);
            LoggedAction.build().source(sender).target(target).description("permission", "unsettemp", node, context).build().submit(plugin, sender);
        }
        StorageAssistant.save(target, sender, plugin);
    } else {
        Message.DOES_NOT_HAVE_TEMP_PERMISSION.send(sender, target, node, context);
    }
}
Also used : MutableContextSet(net.luckperms.api.context.MutableContextSet) InheritanceNode(net.luckperms.api.node.types.InheritanceNode) Node(net.luckperms.api.node.Node) InheritanceNode(net.luckperms.api.node.types.InheritanceNode) Duration(java.time.Duration) DataMutateResult(net.luckperms.api.model.data.DataMutateResult)

Example 12 with MutableContextSet

use of net.luckperms.api.context.MutableContextSet 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 13 with MutableContextSet

use of net.luckperms.api.context.MutableContextSet 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 14 with MutableContextSet

use of net.luckperms.api.context.MutableContextSet in project LuckPerms by lucko.

the class ArgumentList method parseContext.

private MutableContextSet parseContext(int fromIndex) {
    MutableContextSet contextSet = new MutableContextSetImpl();
    List<String> entries = subList(fromIndex, size());
    for (int i = 0; i < entries.size(); i++) {
        String entry = entries.get(i);
        int sep = entry.indexOf('=');
        String key;
        String value;
        if (sep != -1) {
            key = entry.substring(0, sep);
            value = entry.substring(sep + 1);
        } else {
            key = i == 1 ? DefaultContextKeys.WORLD_KEY : DefaultContextKeys.SERVER_KEY;
            value = entry;
        }
        if (!Context.isValidKey(key) || !Context.isValidValue(value)) {
            continue;
        }
        contextSet.add(key, value);
    }
    return contextSet;
}
Also used : MutableContextSet(net.luckperms.api.context.MutableContextSet) MutableContextSetImpl(me.lucko.luckperms.common.context.MutableContextSetImpl)

Example 15 with MutableContextSet

use of net.luckperms.api.context.MutableContextSet in project LuckPerms by lucko.

the class MetaSetChatMeta 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;
    MutableContextSet context;
    if (priority == Integer.MIN_VALUE) {
        // priority wasn't defined, meta is at index 0, contexts at index 1
        meta = args.get(0);
        context = args.getContextOrDefault(1, plugin);
    } else {
        // priority was defined, meta should be at index 1, contexts at index 2
        if (args.size() <= 1) {
            sendDetailedUsage(sender);
            return;
        }
        meta = args.get(1);
        context = args.getContextOrDefault(2, 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 result = target.setNode(DataType.NORMAL, this.type.builder(meta, priority).withContext(context).build(), true);
    if (result.wasSuccessful()) {
        Message.ADD_CHATMETA_SUCCESS.send(sender, target, this.type, meta, priority, context);
        LoggedAction.build().source(sender).target(target).description("meta", "set" + this.type.name().toLowerCase(Locale.ROOT), priority, meta, context).build().submit(plugin, sender);
        StorageAssistant.save(target, sender, plugin);
    } else {
        Message.ALREADY_HAS_CHAT_META.send(sender, target, this.type, meta, priority, context);
    }
}
Also used : Group(me.lucko.luckperms.common.model.Group) MetaAccumulator(me.lucko.luckperms.common.cacheddata.type.MetaAccumulator) MutableContextSet(net.luckperms.api.context.MutableContextSet) OptionalInt(java.util.OptionalInt) DataMutateResult(net.luckperms.api.model.data.DataMutateResult)

Aggregations

MutableContextSet (net.luckperms.api.context.MutableContextSet)28 DataMutateResult (net.luckperms.api.model.data.DataMutateResult)13 Node (net.luckperms.api.node.Node)8 Duration (java.time.Duration)7 List (java.util.List)6 LoggedAction (me.lucko.luckperms.common.actionlog.LoggedAction)6 CommandException (me.lucko.luckperms.common.command.abstraction.CommandException)6 ArgumentPermissions (me.lucko.luckperms.common.command.access.ArgumentPermissions)6 CommandPermission (me.lucko.luckperms.common.command.access.CommandPermission)6 CommandSpec (me.lucko.luckperms.common.command.spec.CommandSpec)6 TabCompleter (me.lucko.luckperms.common.command.tabcomplete.TabCompleter)6 TabCompletions (me.lucko.luckperms.common.command.tabcomplete.TabCompletions)6 ArgumentList (me.lucko.luckperms.common.command.utils.ArgumentList)6 StorageAssistant (me.lucko.luckperms.common.command.utils.StorageAssistant)6 Message (me.lucko.luckperms.common.locale.Message)6 Group (me.lucko.luckperms.common.model.Group)6 LuckPermsPlugin (me.lucko.luckperms.common.plugin.LuckPermsPlugin)6 Sender (me.lucko.luckperms.common.sender.Sender)6 Predicates (me.lucko.luckperms.common.util.Predicates)6 PermissionHolder (me.lucko.luckperms.common.model.PermissionHolder)5