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 java.util.ArrayList;
038import java.util.HashSet;
039import java.util.List;
040import java.util.Optional;
041import java.util.Set;
042
043import javax.annotation.Nullable;
044
045public class SpongeRootCommand implements CommandCallable, RootCommand {
046
047    private final SpongeCommandManager manager;
048    private final String name;
049    private BaseCommand defCommand;
050    private SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create();
051    private List<BaseCommand> children = new ArrayList<>();
052    boolean isRegistered = false;
053
054    SpongeRootCommand(SpongeCommandManager manager, String name) {
055        this.manager = manager;
056        this.name = name;
057    }
058
059    @Override
060    public String getCommandName() {
061        return name;
062    }
063
064    @Override
065    public CommandResult process(@NotNull CommandSource source, @NotNull String arguments) throws CommandException {
066        String[] args = arguments.isEmpty() ? new String[0] : arguments.split(" ");
067        return this.executeSponge(manager.getCommandIssuer(source), this.name, args);
068    }
069
070    @Override
071    public List<String> getSuggestions(@NotNull CommandSource source, @NotNull String arguments, @Nullable Location<World> location) throws CommandException {
072        String[] args = arguments.isEmpty() ? new String[0] : arguments.split(" ");
073        return tabComplete(manager.getCommandIssuer(source), this.name, args);
074    }
075
076    @Override
077    public boolean testPermission(@NotNull CommandSource source) {
078        return this.defCommand.hasPermission(source);
079    }
080
081    @Override
082    public Optional<Text> getShortDescription(@NotNull CommandSource source) {
083        return Optional.empty();
084    }
085
086    @Override
087    public Optional<Text> getHelp(@NotNull CommandSource source) {
088        return Optional.empty();
089    }
090
091    @Override
092    public Text getUsage(@NotNull CommandSource source) {
093        return Text.of();
094    }
095
096    private List<String> tabComplete(CommandIssuer sender, String alias, String[] args) throws IllegalArgumentException {
097        Set<String> completions = new HashSet<>();
098        this.children.forEach(child -> completions.addAll(child.tabComplete(sender, alias, args)));
099        return new ArrayList<>(completions);
100    }
101
102    private CommandResult executeSponge(CommandIssuer sender, String commandLabel, String[] args) {
103        BaseCommand cmd = execute(sender, commandLabel, args);
104        return ((SpongeCommandOperationContext) cmd.lastCommandOperationContext).getResult();
105    }
106
107    public void addChild(BaseCommand command) {
108        if (this.defCommand == null || !command.subCommands.get("__default").isEmpty()) {
109            this.defCommand = command;
110        }
111        addChildShared(this.children, this.subCommands, command);
112    }
113
114    @Override
115    public BaseCommand getDefCommand(){
116        return defCommand;
117    }
118
119    @Override
120    public CommandManager getManager() {
121        return manager;
122    }
123
124    @Override
125    public SetMultimap<String, RegisteredCommand> getSubCommands() {
126        return subCommands;
127    }
128}