Search in sources :

Example 1 with ArgumentList

use of me.lucko.luckperms.common.command.utils.ArgumentList in project LuckPerms by lucko.

the class MetaRemoveChatMeta 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.getOrDefault(1, "null");
    ImmutableContextSet context = args.getContextOrDefault(2, plugin).immutableCopy();
    if (ArgumentPermissions.checkContext(plugin, sender, permission, context) || ArgumentPermissions.checkGroup(plugin, sender, target, context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    // Handle bulk removal
    if (meta.equalsIgnoreCase("null") || meta.equals("*")) {
        target.removeIf(DataType.NORMAL, context, this.type.nodeType().predicate(n -> n.getPriority() == priority && !n.hasExpiry()), false);
        Message.BULK_REMOVE_CHATMETA_SUCCESS.send(sender, target, this.type, priority, context);
        LoggedAction.build().source(sender).target(target).description("meta", "remove" + this.type.name().toLowerCase(Locale.ROOT), priority, "*", context).build().submit(plugin, sender);
        StorageAssistant.save(target, sender, plugin);
        return;
    }
    DataMutateResult result = target.unsetNode(DataType.NORMAL, this.type.builder(meta, priority).withContext(context).build());
    if (result.wasSuccessful()) {
        Message.REMOVE_CHATMETA_SUCCESS.send(sender, target, this.type, meta, priority, context);
        LoggedAction.build().source(sender).target(target).description("meta", "remove" + this.type.name().toLowerCase(Locale.ROOT), priority, meta, context).build().submit(plugin, sender);
        StorageAssistant.save(target, sender, plugin);
    } else {
        Message.DOES_NOT_HAVE_CHAT_META.send(sender, target, this.type, meta, priority, context);
    }
}
Also used : DataMutateResult(net.luckperms.api.model.data.DataMutateResult) CommandException(me.lucko.luckperms.common.command.abstraction.CommandException) ImmutableContextSet(net.luckperms.api.context.ImmutableContextSet) ChatMetaType(net.luckperms.api.node.ChatMetaType) LoggedAction(me.lucko.luckperms.common.actionlog.LoggedAction) PermissionHolder(me.lucko.luckperms.common.model.PermissionHolder) Sender(me.lucko.luckperms.common.sender.Sender) TabCompletions(me.lucko.luckperms.common.command.tabcomplete.TabCompletions) StorageAssistant(me.lucko.luckperms.common.command.utils.StorageAssistant) Message(me.lucko.luckperms.common.locale.Message) GenericChildCommand(me.lucko.luckperms.common.command.abstraction.GenericChildCommand) TabCompleter(me.lucko.luckperms.common.command.tabcomplete.TabCompleter) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) List(java.util.List) 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) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) ArgumentList(me.lucko.luckperms.common.command.utils.ArgumentList) DataType(net.luckperms.api.model.data.DataType) ImmutableContextSet(net.luckperms.api.context.ImmutableContextSet) DataMutateResult(net.luckperms.api.model.data.DataMutateResult)

Example 2 with ArgumentList

use of me.lucko.luckperms.common.command.utils.ArgumentList in project LuckPerms by lucko.

the class MetaUnset 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 key = args.get(0);
    MutableContextSet context = args.getContextOrDefault(1, plugin);
    if (ArgumentPermissions.checkContext(plugin, sender, permission, context) || ArgumentPermissions.checkGroup(plugin, sender, target, context) || ArgumentPermissions.checkArguments(plugin, sender, permission, key)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    if (target.removeIf(DataType.NORMAL, context, NodeType.META.predicate(n -> !n.hasExpiry() && n.getMetaKey().equalsIgnoreCase(key)), false)) {
        Message.UNSET_META_SUCCESS.send(sender, key, target, context);
        LoggedAction.build().source(sender).target(target).description("meta", "unset", key, context).build().submit(plugin, sender);
        StorageAssistant.save(target, sender, plugin);
    } else {
        Message.DOESNT_HAVE_META.send(sender, target, key, context);
    }
}
Also used : CommandException(me.lucko.luckperms.common.command.abstraction.CommandException) LoggedAction(me.lucko.luckperms.common.actionlog.LoggedAction) PermissionHolder(me.lucko.luckperms.common.model.PermissionHolder) NodeType(net.luckperms.api.node.NodeType) Sender(me.lucko.luckperms.common.sender.Sender) TabCompletions(me.lucko.luckperms.common.command.tabcomplete.TabCompletions) StorageAssistant(me.lucko.luckperms.common.command.utils.StorageAssistant) Message(me.lucko.luckperms.common.locale.Message) GenericChildCommand(me.lucko.luckperms.common.command.abstraction.GenericChildCommand) TabCompleter(me.lucko.luckperms.common.command.tabcomplete.TabCompleter) MutableContextSet(net.luckperms.api.context.MutableContextSet) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) List(java.util.List) Predicates(me.lucko.luckperms.common.util.Predicates) CommandSpec(me.lucko.luckperms.common.command.spec.CommandSpec) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) ArgumentList(me.lucko.luckperms.common.command.utils.ArgumentList) DataType(net.luckperms.api.model.data.DataType) MutableContextSet(net.luckperms.api.context.MutableContextSet)

Example 3 with ArgumentList

use of me.lucko.luckperms.common.command.utils.ArgumentList in project LuckPerms by lucko.

the class HolderShowTracks method execute.

@Override
public void execute(LuckPermsPlugin plugin, Sender sender, T target, ArgumentList args, String label) {
    if (ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    try {
        plugin.getStorage().loadAllTracks().get();
    } catch (Exception e) {
        plugin.getLogger().warn("Error whilst loading tracks", e);
        Message.TRACKS_LOAD_ERROR.send(sender);
        return;
    }
    List<Map.Entry<Track, Component>> lines = new ArrayList<>();
    if (target.getType() == HolderType.USER) {
        // if the holder is a user, we want to query parent groups for tracks
        Set<InheritanceNode> nodes = target.normalData().inheritanceAsList().stream().filter(Node::getValue).filter(n -> !n.hasExpiry()).collect(Collectors.toSet());
        for (InheritanceNode node : nodes) {
            String groupName = node.getGroupName();
            List<Track> tracks = plugin.getTrackManager().getAll().values().stream().filter(t -> t.containsGroup(groupName)).collect(Collectors.toList());
            for (Track track : tracks) {
                Component line = Component.text().append(Message.formatContextSetBracketed(node.getContexts(), Component.empty())).append(Component.newline()).append(Message.formatTrackPath(track.getGroups(), groupName)).build();
                lines.add(Maps.immutableEntry(track, line));
            }
        }
    } else {
        // otherwise, just lookup for the actual group
        String groupName = ((Group) target).getName();
        List<Track> tracks = plugin.getTrackManager().getAll().values().stream().filter(t -> t.containsGroup(groupName)).collect(Collectors.toList());
        for (Track track : tracks) {
            lines.add(Maps.immutableEntry(track, Message.formatTrackPath(track.getGroups(), groupName)));
        }
    }
    if (lines.isEmpty()) {
        Message.LIST_TRACKS_EMPTY.send(sender, target);
        return;
    }
    Message.LIST_TRACKS.send(sender, target);
    for (Map.Entry<Track, Component> line : lines) {
        Message.LIST_TRACKS_ENTRY.send(sender, line.getKey().getName(), line.getValue());
    }
}
Also used : InheritanceNode(net.luckperms.api.node.types.InheritanceNode) PermissionHolder(me.lucko.luckperms.common.model.PermissionHolder) Set(java.util.Set) Sender(me.lucko.luckperms.common.sender.Sender) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Message(me.lucko.luckperms.common.locale.Message) ArrayList(java.util.ArrayList) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) Component(net.kyori.adventure.text.Component) Map(java.util.Map) Node(net.luckperms.api.node.Node) Group(me.lucko.luckperms.common.model.Group) Predicates(me.lucko.luckperms.common.util.Predicates) CommandSpec(me.lucko.luckperms.common.command.spec.CommandSpec) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) ChildCommand(me.lucko.luckperms.common.command.abstraction.ChildCommand) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) HolderType(me.lucko.luckperms.common.model.HolderType) ArgumentList(me.lucko.luckperms.common.command.utils.ArgumentList) Group(me.lucko.luckperms.common.model.Group) InheritanceNode(net.luckperms.api.node.types.InheritanceNode) InheritanceNode(net.luckperms.api.node.types.InheritanceNode) Node(net.luckperms.api.node.Node) ArrayList(java.util.ArrayList) Component(net.kyori.adventure.text.Component) Map(java.util.Map) Track(me.lucko.luckperms.common.model.Track)

Example 4 with ArgumentList

use of me.lucko.luckperms.common.command.utils.ArgumentList in project LuckPerms by lucko.

the class ParentSetTrack 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;
    }
    final String trackName = args.get(0).toLowerCase(Locale.ROOT);
    if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
        Message.TRACK_INVALID_ENTRY.send(sender, trackName);
        return;
    }
    Track track = StorageAssistant.loadTrack(trackName, sender, plugin);
    if (track == null) {
        return;
    }
    if (track.getSize() <= 1) {
        Message.TRACK_EMPTY.send(sender, track.getName());
        return;
    }
    int index = args.getIntOrDefault(1, -1);
    String groupName;
    if (index > 0) {
        List<String> trackGroups = track.getGroups();
        if (index - 1 >= trackGroups.size()) {
            Message.DOES_NOT_EXIST.send(sender, String.valueOf(index));
            return;
        }
        groupName = track.getGroups().get(index - 1);
    } else {
        groupName = args.getLowercase(1, DataConstraints.GROUP_NAME_TEST);
        if (!track.containsGroup(groupName)) {
            Message.TRACK_DOES_NOT_CONTAIN.send(sender, track.getName(), groupName);
            return;
        }
    }
    ImmutableContextSet context = args.getContextOrDefault(2, plugin).immutableCopy();
    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, track.getName(), group.getName())) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    target.removeIf(DataType.NORMAL, context, NodeType.INHERITANCE.predicate(n -> track.containsGroup(n.getGroupName())), false);
    target.setNode(DataType.NORMAL, Inheritance.builder(group.getName()).withContext(context).build(), true);
    Message.SET_TRACK_PARENT_SUCCESS.send(sender, target, track.getName(), group, context);
    LoggedAction.build().source(sender).target(target).description("parent", "settrack", track.getName(), groupName, context).build().submit(plugin, sender);
    StorageAssistant.save(target, sender, plugin);
}
Also used : CommandException(me.lucko.luckperms.common.command.abstraction.CommandException) ImmutableContextSet(net.luckperms.api.context.ImmutableContextSet) LoggedAction(me.lucko.luckperms.common.actionlog.LoggedAction) PermissionHolder(me.lucko.luckperms.common.model.PermissionHolder) NodeType(net.luckperms.api.node.NodeType) DataConstraints(me.lucko.luckperms.common.storage.misc.DataConstraints) Sender(me.lucko.luckperms.common.sender.Sender) TabCompletions(me.lucko.luckperms.common.command.tabcomplete.TabCompletions) StorageAssistant(me.lucko.luckperms.common.command.utils.StorageAssistant) Message(me.lucko.luckperms.common.locale.Message) GenericChildCommand(me.lucko.luckperms.common.command.abstraction.GenericChildCommand) TabCompleter(me.lucko.luckperms.common.command.tabcomplete.TabCompleter) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) Locale(java.util.Locale) Group(me.lucko.luckperms.common.model.Group) Predicates(me.lucko.luckperms.common.util.Predicates) CommandSpec(me.lucko.luckperms.common.command.spec.CommandSpec) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) Inheritance(me.lucko.luckperms.common.node.types.Inheritance) ArgumentList(me.lucko.luckperms.common.command.utils.ArgumentList) DataType(net.luckperms.api.model.data.DataType) Group(me.lucko.luckperms.common.model.Group) ImmutableContextSet(net.luckperms.api.context.ImmutableContextSet) Track(me.lucko.luckperms.common.model.Track)

Example 5 with ArgumentList

use of me.lucko.luckperms.common.command.utils.ArgumentList in project LuckPerms by lucko.

the class UserDemote method execute.

@Override
public void execute(LuckPermsPlugin plugin, Sender sender, User target, ArgumentList args, String label) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), target)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    boolean removeFromFirst = !args.remove("--dont-remove-from-first");
    // if args is empty - use the only/default track
    if (args.isEmpty()) {
        Set<String> tracks = plugin.getTrackManager().getAll().keySet();
        if (tracks.size() == 1) {
            args.add(tracks.iterator().next());
        } else if (tracks.contains("default")) {
            args.add("default");
        } else {
            Message.USER_TRACK_ERROR_AMBIGUOUS_TRACK_SELECTION.send(sender);
            return;
        }
    }
    final String trackName = args.get(0).toLowerCase(Locale.ROOT);
    if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
        Message.TRACK_INVALID_ENTRY.send(sender, trackName);
        return;
    }
    Track track = StorageAssistant.loadTrack(trackName, sender, plugin);
    if (track == null) {
        return;
    }
    if (track.getSize() <= 1) {
        Message.TRACK_EMPTY.send(sender, track.getName());
        return;
    }
    boolean dontShowTrackProgress = args.remove("-s");
    MutableContextSet context = args.getContextOrDefault(1, plugin);
    if (ArgumentPermissions.checkContext(plugin, sender, getPermission().get(), context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return;
    }
    Predicate<String> previousGroupPermissionChecker = s -> !ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), s) && !ArgumentPermissions.checkGroup(plugin, sender, s, context);
    DemotionResult result = track.demote(target, context, previousGroupPermissionChecker, sender, removeFromFirst);
    switch(result.getStatus()) {
        case NOT_ON_TRACK:
            Message.USER_TRACK_ERROR_NOT_CONTAIN_GROUP.send(sender, target, track.getName());
            return;
        case AMBIGUOUS_CALL:
            Message.TRACK_AMBIGUOUS_CALL.send(sender, target);
            return;
        case UNDEFINED_FAILURE:
            Message.COMMAND_NO_PERMISSION.send(sender);
            return;
        case MALFORMED_TRACK:
            Message.USER_DEMOTE_ERROR_MALFORMED.send(sender, result.getGroupTo().get());
            return;
        case REMOVED_FROM_FIRST_GROUP:
            {
                if (!removeFromFirst && !result.getGroupFrom().isPresent()) {
                    Message.USER_DEMOTE_ENDOFTRACK_NOT_REMOVED.send(sender, track.getName(), target);
                    return;
                }
                Message.USER_DEMOTE_ENDOFTRACK.send(sender, track.getName(), target, result.getGroupFrom().get());
                LoggedAction.build().source(sender).target(target).description("demote", track.getName(), context).build().submit(plugin, sender);
                StorageAssistant.save(target, sender, plugin);
                return;
            }
        case SUCCESS:
            {
                String groupFrom = result.getGroupFrom().get();
                String groupTo = result.getGroupTo().get();
                Message.USER_DEMOTE_SUCCESS.send(sender, target, track.getName(), groupFrom, groupTo, context);
                if (!dontShowTrackProgress) {
                    Message.TRACK_PATH_HIGHLIGHTED_PROGRESSION.send(sender, track.getGroups(), groupTo, groupFrom, true);
                }
                LoggedAction.build().source(sender).target(target).description("demote", track.getName(), context).build().submit(plugin, sender);
                StorageAssistant.save(target, sender, plugin);
                return;
            }
        default:
            throw new AssertionError("Unknown status: " + result.getStatus());
    }
}
Also used : CommandException(me.lucko.luckperms.common.command.abstraction.CommandException) LoggedAction(me.lucko.luckperms.common.actionlog.LoggedAction) Predicate(java.util.function.Predicate) DataConstraints(me.lucko.luckperms.common.storage.misc.DataConstraints) Set(java.util.Set) Sender(me.lucko.luckperms.common.sender.Sender) TabCompletions(me.lucko.luckperms.common.command.tabcomplete.TabCompletions) StorageAssistant(me.lucko.luckperms.common.command.utils.StorageAssistant) Message(me.lucko.luckperms.common.locale.Message) TabCompleter(me.lucko.luckperms.common.command.tabcomplete.TabCompleter) MutableContextSet(net.luckperms.api.context.MutableContextSet) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) DemotionResult(net.luckperms.api.track.DemotionResult) 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) ChildCommand(me.lucko.luckperms.common.command.abstraction.ChildCommand) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) User(me.lucko.luckperms.common.model.User) ArgumentList(me.lucko.luckperms.common.command.utils.ArgumentList) MutableContextSet(net.luckperms.api.context.MutableContextSet) DemotionResult(net.luckperms.api.track.DemotionResult) Track(me.lucko.luckperms.common.model.Track)

Aggregations

List (java.util.List)22 ArgumentList (me.lucko.luckperms.common.command.utils.ArgumentList)22 Message (me.lucko.luckperms.common.locale.Message)18 LuckPermsPlugin (me.lucko.luckperms.common.plugin.LuckPermsPlugin)18 Sender (me.lucko.luckperms.common.sender.Sender)18 CommandSpec (me.lucko.luckperms.common.command.spec.CommandSpec)16 Predicates (me.lucko.luckperms.common.util.Predicates)16 CommandPermission (me.lucko.luckperms.common.command.access.CommandPermission)15 TabCompleter (me.lucko.luckperms.common.command.tabcomplete.TabCompleter)14 CommandException (me.lucko.luckperms.common.command.abstraction.CommandException)13 ArgumentPermissions (me.lucko.luckperms.common.command.access.ArgumentPermissions)13 TabCompletions (me.lucko.luckperms.common.command.tabcomplete.TabCompletions)12 Locale (java.util.Locale)11 LoggedAction (me.lucko.luckperms.common.actionlog.LoggedAction)11 StorageAssistant (me.lucko.luckperms.common.command.utils.StorageAssistant)11 PermissionHolder (me.lucko.luckperms.common.model.PermissionHolder)9 DataType (net.luckperms.api.model.data.DataType)9 GenericChildCommand (me.lucko.luckperms.common.command.abstraction.GenericChildCommand)8 Group (me.lucko.luckperms.common.model.Group)7 ArrayList (java.util.ArrayList)6