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.defCommand.hasPermission(source);
076    }
077
078    @Override
079    public Optional<Text> getShortDescription(@NotNull CommandSource source) {
080        return Optional.empty();
081    }
082
083    @Override
084    public Optional<Text> getHelp(@NotNull CommandSource source) {
085        return Optional.empty();
086    }
087
088    @Override
089    public Text getUsage(@NotNull CommandSource source) {
090        return Text.of();
091    }
092
093    private CommandResult executeSponge(CommandIssuer sender, String commandLabel, String[] args) {
094        BaseCommand cmd = execute(sender, commandLabel, args);
095        return ((SpongeCommandOperationContext) cmd.lastCommandOperationContext).getResult();
096    }
097
098    public void addChild(BaseCommand command) {
099        if (this.defCommand == null || !command.subCommands.get(BaseCommand.DEFAULT).isEmpty()) {
100            this.defCommand = command;
101        }
102        addChildShared(this.children, this.subCommands, command);
103    }
104
105    @Override
106    public BaseCommand getDefCommand(){
107        return defCommand;
108    }
109
110    @Override
111    public CommandManager getManager() {
112        return manager;
113    }
114
115    @Override
116    public SetMultimap<String, RegisteredCommand> getSubCommands() {
117        return subCommands;
118    }
119
120    @Override
121    public List<BaseCommand> getChildren() {
122        return children;
123    }
124}