Search in sources :

Example 1 with MutableContextSet

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

the class UserDemote method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List<String> args, String label) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), user)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    final String trackName = args.get(0).toLowerCase();
    if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
        Message.TRACK_INVALID_ENTRY.send(sender, trackName);
        return CommandResult.INVALID_ARGS;
    }
    Track track = StorageAssistant.loadTrack(trackName, sender, plugin);
    if (track == null) {
        return CommandResult.LOADING_ERROR;
    }
    if (track.getSize() <= 1) {
        Message.TRACK_EMPTY.send(sender, track.getName());
        return CommandResult.STATE_ERROR;
    }
    boolean silent = args.remove("-s");
    MutableContextSet context = ArgumentParser.parseContext(1, args, plugin);
    if (ArgumentPermissions.checkContext(plugin, sender, getPermission().get(), context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    // Load applicable groups
    Set<Node> nodes = user.getEnduringNodes().values().stream().filter(Node::isGroupNode).filter(Node::getValuePrimitive).filter(node -> node.getFullContexts().makeImmutable().equals(context.makeImmutable())).collect(Collectors.toSet());
    nodes.removeIf(g -> !track.containsGroup(g.getGroupName()));
    if (nodes.isEmpty()) {
        Message.USER_TRACK_ERROR_NOT_CONTAIN_GROUP.send(sender, user.getFriendlyName(), track.getName());
        return CommandResult.FAILURE;
    }
    if (nodes.size() != 1) {
        Message.TRACK_AMBIGUOUS_CALL.send(sender, user.getFriendlyName());
        return CommandResult.FAILURE;
    }
    final Node oldNode = Iterables.getFirst(nodes, null);
    final String old = oldNode.getGroupName();
    final String previous;
    try {
        previous = track.getPrevious(old);
    } catch (IllegalArgumentException e) {
        Message.TRACK_DOES_NOT_CONTAIN.send(sender, track.getName(), old);
        return CommandResult.STATE_ERROR;
    }
    if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), oldNode.getGroupName())) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    if (previous == null) {
        user.unsetPermission(oldNode);
        Message.USER_DEMOTE_ENDOFTRACK.send(sender, track.getName(), user.getFriendlyName(), old);
        ExtendedLogEntry.build().actor(sender).acted(user).action("demote", track.getName(), context).build().submit(plugin, sender);
        StorageAssistant.save(user, sender, plugin);
        plugin.getEventFactory().handleUserDemote(user, track, old, null, sender);
        return CommandResult.SUCCESS;
    }
    Group previousGroup = plugin.getStorage().loadGroup(previous).join().orElse(null);
    if (previousGroup == null) {
        Message.USER_DEMOTE_ERROR_MALFORMED.send(sender, previous);
        return CommandResult.LOADING_ERROR;
    }
    user.unsetPermission(oldNode);
    user.setPermission(NodeFactory.buildGroupNode(previousGroup.getName()).withExtraContext(context).build());
    if (context.isEmpty() && user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME).equalsIgnoreCase(old)) {
        user.getPrimaryGroup().setStoredValue(previousGroup.getName());
    }
    Message.USER_DEMOTE_SUCCESS.send(sender, user.getFriendlyName(), track.getName(), old, previousGroup.getFriendlyName(), MessageUtils.contextSetToString(context));
    if (!silent) {
        Message.EMPTY.send(sender, MessageUtils.listToArrowSep(track.getGroups(), previousGroup.getName(), old, true));
    }
    ExtendedLogEntry.build().actor(sender).acted(user).action("demote", track.getName(), context).build().submit(plugin, sender);
    StorageAssistant.save(user, sender, plugin);
    plugin.getEventFactory().handleUserDemote(user, track, old, previousGroup.getName(), sender);
    return CommandResult.SUCCESS;
}
Also used : Iterables(com.google.common.collect.Iterables) CommandException(me.lucko.luckperms.common.command.abstraction.CommandException) SubCommand(me.lucko.luckperms.common.command.abstraction.SubCommand) LocaleManager(me.lucko.luckperms.common.locale.LocaleManager) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) StorageAssistant(me.lucko.luckperms.common.command.utils.StorageAssistant) ArgumentParser(me.lucko.luckperms.common.command.utils.ArgumentParser) CommandSpec(me.lucko.luckperms.common.locale.command.CommandSpec) Message(me.lucko.luckperms.common.locale.message.Message) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) ExtendedLogEntry(me.lucko.luckperms.common.actionlog.ExtendedLogEntry) MessageUtils(me.lucko.luckperms.common.command.utils.MessageUtils) Set(java.util.Set) NodeFactory(me.lucko.luckperms.common.node.NodeFactory) Sender(me.lucko.luckperms.common.sender.Sender) Collectors(java.util.stream.Collectors) CommandResult(me.lucko.luckperms.common.command.CommandResult) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) TabCompletions(me.lucko.luckperms.common.command.utils.TabCompletions) Group(me.lucko.luckperms.common.model.Group) Node(me.lucko.luckperms.api.Node) DataConstraints(me.lucko.luckperms.common.storage.DataConstraints) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) User(me.lucko.luckperms.common.model.User) Predicates(me.lucko.luckperms.common.utils.Predicates) Group(me.lucko.luckperms.common.model.Group) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) Node(me.lucko.luckperms.api.Node) Track(me.lucko.luckperms.common.model.Track)

Example 2 with MutableContextSet

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

the class UserPromote method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List<String> args, String label) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), user)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    final String trackName = args.get(0).toLowerCase();
    if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
        Message.TRACK_INVALID_ENTRY.send(sender, trackName);
        return CommandResult.INVALID_ARGS;
    }
    Track track = StorageAssistant.loadTrack(trackName, sender, plugin);
    if (track == null) {
        return CommandResult.LOADING_ERROR;
    }
    if (track.getSize() <= 1) {
        Message.TRACK_EMPTY.send(sender, track.getName());
        return CommandResult.STATE_ERROR;
    }
    boolean silent = args.remove("-s");
    MutableContextSet context = ArgumentParser.parseContext(1, args, plugin);
    if (ArgumentPermissions.checkContext(plugin, sender, getPermission().get(), context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    // Load applicable groups
    Set<Node> nodes = user.getEnduringNodes().values().stream().filter(Node::isGroupNode).filter(Node::getValuePrimitive).filter(node -> node.getFullContexts().makeImmutable().equals(context.makeImmutable())).collect(Collectors.toSet());
    nodes.removeIf(g -> !track.containsGroup(g.getGroupName()));
    if (nodes.isEmpty()) {
        String first = track.getGroups().get(0);
        Group nextGroup = plugin.getGroupManager().getIfLoaded(first);
        if (nextGroup == null) {
            Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, first);
            return CommandResult.LOADING_ERROR;
        }
        if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), nextGroup.getName())) {
            Message.COMMAND_NO_PERMISSION.send(sender);
            return CommandResult.NO_PERMISSION;
        }
        user.setPermission(NodeFactory.buildGroupNode(nextGroup.getId()).withExtraContext(context).build());
        Message.USER_TRACK_ADDED_TO_FIRST.send(sender, user.getFriendlyName(), nextGroup.getFriendlyName(), MessageUtils.contextSetToString(context));
        ExtendedLogEntry.build().actor(sender).acted(user).action("promote", track.getName(), context).build().submit(plugin, sender);
        StorageAssistant.save(user, sender, plugin);
        plugin.getEventFactory().handleUserPromote(user, track, null, first, sender);
        return CommandResult.SUCCESS;
    }
    if (nodes.size() != 1) {
        Message.TRACK_AMBIGUOUS_CALL.send(sender, user.getFriendlyName());
        return CommandResult.FAILURE;
    }
    final Node oldNode = nodes.stream().findAny().get();
    final String old = oldNode.getGroupName();
    final String next;
    try {
        next = track.getNext(old);
    } catch (IllegalArgumentException e) {
        Message.TRACK_DOES_NOT_CONTAIN.send(sender, track.getName(), old);
        return CommandResult.STATE_ERROR;
    }
    if (next == null) {
        Message.USER_PROMOTE_ERROR_ENDOFTRACK.send(sender, track.getName(), user.getFriendlyName());
        return CommandResult.STATE_ERROR;
    }
    if (!plugin.getStorage().loadGroup(next).join().isPresent()) {
        Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, next);
        return CommandResult.STATE_ERROR;
    }
    Group nextGroup = plugin.getGroupManager().getIfLoaded(next);
    if (nextGroup == null) {
        Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, next);
        return CommandResult.LOADING_ERROR;
    }
    if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), nextGroup.getName())) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    user.unsetPermission(oldNode);
    user.setPermission(NodeFactory.buildGroupNode(nextGroup.getName()).withExtraContext(context).build());
    if (context.isEmpty() && user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME).equalsIgnoreCase(old)) {
        user.getPrimaryGroup().setStoredValue(nextGroup.getName());
    }
    Message.USER_PROMOTE_SUCCESS.send(sender, user.getFriendlyName(), track.getName(), old, nextGroup.getFriendlyName(), MessageUtils.contextSetToString(context));
    if (!silent) {
        Message.EMPTY.send(sender, MessageUtils.listToArrowSep(track.getGroups(), old, nextGroup.getName(), false));
    }
    ExtendedLogEntry.build().actor(sender).acted(user).action("promote", track.getName(), context).build().submit(plugin, sender);
    StorageAssistant.save(user, sender, plugin);
    plugin.getEventFactory().handleUserPromote(user, track, old, nextGroup.getName(), sender);
    return CommandResult.SUCCESS;
}
Also used : CommandException(me.lucko.luckperms.common.command.abstraction.CommandException) SubCommand(me.lucko.luckperms.common.command.abstraction.SubCommand) LocaleManager(me.lucko.luckperms.common.locale.LocaleManager) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) StorageAssistant(me.lucko.luckperms.common.command.utils.StorageAssistant) ArgumentParser(me.lucko.luckperms.common.command.utils.ArgumentParser) CommandSpec(me.lucko.luckperms.common.locale.command.CommandSpec) Message(me.lucko.luckperms.common.locale.message.Message) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) ExtendedLogEntry(me.lucko.luckperms.common.actionlog.ExtendedLogEntry) MessageUtils(me.lucko.luckperms.common.command.utils.MessageUtils) Set(java.util.Set) NodeFactory(me.lucko.luckperms.common.node.NodeFactory) Sender(me.lucko.luckperms.common.sender.Sender) Collectors(java.util.stream.Collectors) CommandResult(me.lucko.luckperms.common.command.CommandResult) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) TabCompletions(me.lucko.luckperms.common.command.utils.TabCompletions) Group(me.lucko.luckperms.common.model.Group) Node(me.lucko.luckperms.api.Node) DataConstraints(me.lucko.luckperms.common.storage.DataConstraints) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) User(me.lucko.luckperms.common.model.User) Predicates(me.lucko.luckperms.common.utils.Predicates) Group(me.lucko.luckperms.common.model.Group) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) Node(me.lucko.luckperms.api.Node) Track(me.lucko.luckperms.common.model.Track)

Example 3 with MutableContextSet

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

the class ContextSetJsonSerializer method deserializeContextSet.

public static ContextSet deserializeContextSet(JsonElement element) {
    Preconditions.checkArgument(element.isJsonObject());
    JsonObject data = element.getAsJsonObject();
    if (data.entrySet().isEmpty()) {
        return ContextSet.empty();
    }
    MutableContextSet map = MutableContextSet.create();
    for (Map.Entry<String, JsonElement> e : data.entrySet()) {
        String k = e.getKey();
        JsonElement v = e.getValue();
        if (v.isJsonArray()) {
            JsonArray values = v.getAsJsonArray();
            for (JsonElement value : values) {
                map.add(k, value.getAsString());
            }
        } else {
            map.add(k, v.getAsString());
        }
    }
    return map;
}
Also used : JsonArray(com.google.gson.JsonArray) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Map(java.util.Map)

Example 4 with MutableContextSet

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

the class VaultPermissionHook method contextForLookup.

// utility method for getting a contexts instance for a given vault lookup.
Contexts contextForLookup(User user, String world) {
    MutableContextSet context;
    Player player = user == null ? null : this.plugin.getBootstrap().getPlayer(user.getUuid());
    if (player != null) {
        context = this.plugin.getContextManager().getApplicableContext(player).mutableCopy();
    } else {
        context = this.plugin.getContextManager().getStaticContext().mutableCopy();
    }
    // if world is not null, we want to do a lookup in that specific world
    if (world != null && !world.isEmpty()) {
        // remove already accumulated worlds
        context.removeAll(Contexts.WORLD_KEY);
        // add the vault world
        context.add(Contexts.WORLD_KEY, world.toLowerCase());
    }
    // if we're using a special vault server
    if (useVaultServer()) {
        // remove the normal server context from the set
        context.remove(Contexts.SERVER_KEY, getServer());
        // add the vault specific server
        if (!getVaultServer().equals("global")) {
            context.add(Contexts.SERVER_KEY, getVaultServer());
        }
    }
    return Contexts.of(context, isIncludeGlobal(), true, true, true, true, false);
}
Also used : Player(org.bukkit.entity.Player) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet)

Example 5 with MutableContextSet

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

the class MetaClear method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    MetaType type = null;
    if (!args.isEmpty()) {
        String typeId = args.get(0).toLowerCase();
        if (typeId.equals("any") || typeId.equals("all") || typeId.equals("*")) {
            type = MetaType.ANY;
        }
        if (typeId.equals("chat") || typeId.equals("chatmeta")) {
            type = MetaType.CHAT;
        }
        if (typeId.equals(NodeFactory.META_KEY)) {
            type = MetaType.META;
        }
        if (typeId.equals(NodeFactory.PREFIX_KEY) || typeId.equals("prefixes")) {
            type = MetaType.PREFIX;
        }
        if (typeId.equals(NodeFactory.SUFFIX_KEY) || typeId.equals("suffixes")) {
            type = MetaType.SUFFIX;
        }
        if (type != null) {
            args.remove(0);
        }
    }
    if (type == null) {
        type = MetaType.ANY;
    }
    int before = holder.getEnduringNodes().size();
    MutableContextSet context = ArgumentParser.parseContext(0, args, plugin);
    if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    if (context.isEmpty()) {
        holder.clearMeta(type);
    } else {
        holder.clearMeta(type, context);
    }
    int changed = before - holder.getEnduringNodes().size();
    if (changed == 1) {
        Message.META_CLEAR_SUCCESS_SINGULAR.send(sender, holder.getFriendlyName(), type.name().toLowerCase(), MessageUtils.contextSetToString(context), changed);
    } else {
        Message.META_CLEAR_SUCCESS.send(sender, holder.getFriendlyName(), type.name().toLowerCase(), MessageUtils.contextSetToString(context), changed);
    }
    ExtendedLogEntry.build().actor(sender).acted(holder).action("meta", "clear", context).build().submit(plugin, sender);
    StorageAssistant.save(holder, sender, plugin);
    return CommandResult.SUCCESS;
}
Also used : MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) MetaType(me.lucko.luckperms.common.node.MetaType)

Aggregations

MutableContextSet (me.lucko.luckperms.api.context.MutableContextSet)31 DataMutateResult (me.lucko.luckperms.api.DataMutateResult)12 Node (me.lucko.luckperms.api.Node)7 Group (me.lucko.luckperms.common.model.Group)6 TextComponent (net.kyori.text.TextComponent)6 HoverEvent (net.kyori.text.event.HoverEvent)6 Map (java.util.Map)5 TemporaryModifier (me.lucko.luckperms.common.model.TemporaryModifier)4 Track (me.lucko.luckperms.common.model.Track)4 User (me.lucko.luckperms.common.model.User)4 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ExtendedLogEntry (me.lucko.luckperms.common.actionlog.ExtendedLogEntry)2 CommandResult (me.lucko.luckperms.common.command.CommandResult)2 CommandException (me.lucko.luckperms.common.command.abstraction.CommandException)2 SubCommand (me.lucko.luckperms.common.command.abstraction.SubCommand)2 ArgumentPermissions (me.lucko.luckperms.common.command.access.ArgumentPermissions)2 CommandPermission (me.lucko.luckperms.common.command.access.CommandPermission)2 ArgumentParser (me.lucko.luckperms.common.command.utils.ArgumentParser)2