Search in sources :

Example 1 with PromotionResult

use of net.luckperms.api.track.PromotionResult in project LuckPerms by lucko.

the class UserPromote 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 addToFirst = !args.remove("--dont-add-to-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> nextGroupPermissionChecker = s -> !ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), s) && !ArgumentPermissions.checkGroup(plugin, sender, s, context);
    PromotionResult result = track.promote(target, context, nextGroupPermissionChecker, sender, addToFirst);
    switch(result.getStatus()) {
        case MALFORMED_TRACK:
            Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, result.getGroupTo().get());
            return;
        case UNDEFINED_FAILURE:
            Message.COMMAND_NO_PERMISSION.send(sender);
            return;
        case AMBIGUOUS_CALL:
            Message.TRACK_AMBIGUOUS_CALL.send(sender, target);
            return;
        case END_OF_TRACK:
            Message.USER_PROMOTE_ERROR_ENDOFTRACK.send(sender, track.getName(), target);
            return;
        case ADDED_TO_FIRST_GROUP:
            {
                if (!addToFirst && !result.getGroupTo().isPresent()) {
                    Message.USER_PROMOTE_NOT_ON_TRACK.send(sender, target, track.getName());
                    return;
                }
                Message.USER_TRACK_ADDED_TO_FIRST.send(sender, target, track.getName(), result.getGroupTo().get(), context);
                LoggedAction.build().source(sender).target(target).description("promote", 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_PROMOTE_SUCCESS.send(sender, target, track.getName(), groupFrom, groupTo, context);
                if (!dontShowTrackProgress) {
                    Message.TRACK_PATH_HIGHLIGHTED_PROGRESSION.send(sender, track.getGroups(), groupFrom, groupTo, false);
                }
                LoggedAction.build().source(sender).target(target).description("promote", 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) PromotionResult(net.luckperms.api.track.PromotionResult) 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) 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) PromotionResult(net.luckperms.api.track.PromotionResult) Track(me.lucko.luckperms.common.model.Track)

Aggregations

List (java.util.List)1 Locale (java.util.Locale)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 LoggedAction (me.lucko.luckperms.common.actionlog.LoggedAction)1 ChildCommand (me.lucko.luckperms.common.command.abstraction.ChildCommand)1 CommandException (me.lucko.luckperms.common.command.abstraction.CommandException)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 Message (me.lucko.luckperms.common.locale.Message)1 Track (me.lucko.luckperms.common.model.Track)1 User (me.lucko.luckperms.common.model.User)1 LuckPermsPlugin (me.lucko.luckperms.common.plugin.LuckPermsPlugin)1 Sender (me.lucko.luckperms.common.sender.Sender)1 DataConstraints (me.lucko.luckperms.common.storage.misc.DataConstraints)1