001/*
002 * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
003 *
004 *  Permission is hereby granted, free of charge, to any person obtaining
005 *  a copy of this software and associated documentation files (the
006 *  "Software"), to deal in the Software without restriction, including
007 *  without limitation the rights to use, copy, modify, merge, publish,
008 *  distribute, sublicense, and/or sell copies of the Software, and to
009 *  permit persons to whom the Software is furnished to do so, subject to
010 *  the following conditions:
011 *
012 *  The above copyright notice and this permission notice shall be
013 *  included in all copies or substantial portions of the Software.
014 *
015 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
016 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
017 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
018 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
019 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
020 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
021 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
022 */
023
024package co.aikar.commands;
025
026import com.google.common.collect.HashMultimap;
027import com.google.common.collect.SetMultimap;
028import org.jetbrains.annotations.NotNull;
029import org.spongepowered.api.command.CommandCallable;
030import org.spongepowered.api.command.CommandException;
031import org.spongepowered.api.command.CommandResult;
032import org.spongepowered.api.command.CommandSource;
033import org.spongepowered.api.text.Text;
034import org.spongepowered.api.world.Location;
035import org.spongepowered.api.world.World;
036
037import javax.annotation.Nullable;
038import java.util.ArrayList;
039import java.util.List;
040import java.util.Optional;
041
042public class SpongeRootCommand implements CommandCallable, RootCommand {
043
044    private final SpongeCommandManager manager;
045    private final String name;
046    private BaseCommand defCommand;
047    private SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create();
048    private List<BaseCommand> children = new ArrayList<>();
049    boolean isRegistered = false;
050
051    SpongeRootCommand(SpongeCommandManager manager, String name) {
052        this.manager = manager;
053        this.name = name;
054    }
055
056    @Override
057    public String getCommandName() {
058        return name;
059    }
060
061    @Override
062    public CommandResult process(@NotNull CommandSource source, @NotNull String arguments) throws CommandException {
063        String[] args = arguments.isEmpty() ? new String[0] : arguments.split(" ");
064        return this.executeSponge(manager.getCommandIssuer(source), this.name, args);
065    }
066
067    @Override
068    public List<String> getSuggestions(@NotNull CommandSource source, @NotNull String arguments, @Nullable Location<World> location) throws CommandException {
069        String[] args = arguments.isEmpty() ? new String[]{""} : arguments.split(" ");
070        return getTabCompletions(manager.getCommandIssuer(source), this.name, args);
071    }
072
073    @Override
074    public boolean testPermission(@NotNull CommandSource source) {
075        return this.hasAnyPermission(manager.getCommandIssuer(source));
076    }
077
078    @Override
079    public Optional<Text> getShortDescription(@NotNull CommandSource source) {
080        String description = getDescription();
081        return description != null ? Optional.of(Text.of(description)) : Optional.empty();
082    }
083
084    @Override
085    public Optional<Text> getHelp(@NotNull CommandSource source) {
086        return Optional.empty();
087    }
088
089    @Override
090    public Text getUsage(@NotNull CommandSource source) {
091        String usage = getUsage();
092        return usage != null ? Text.of(usage) : Text.of();
093    }
094
095    private CommandResult executeSponge(CommandIssuer sender, String commandLabel, String[] args) {
096        BaseCommand cmd = execute(sender, commandLabel, args);
097        SpongeCommandOperationContext lastContext = (SpongeCommandOperationContext) cmd.getLastCommandOperationContext();
098        return lastContext != null ? lastContext.getResult() : CommandResult.success();
099    }
100
101    public void addChild(BaseCommand command) {
102        if (this.defCommand == null || !command.subCommands.get(BaseCommand.DEFAULT).isEmpty()) {
103            this.defCommand = command;
104        }
105        addChildShared(this.children, this.subCommands, command);
106    }
107
108    @Override
109    public BaseCommand getDefCommand() {
110        return defCommand;
111    }
112
113    @Override
114    public CommandManager getManager() {
115        return manager;
116    }
117
118    @Override
119    public SetMultimap<String, RegisteredCommand> getSubCommands() {
120        return subCommands;
121    }
122
123    @Override
124    public List<BaseCommand> getChildren() {
125        return children;
126    }
127}