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 java.util.ArrayList;
027import java.util.List;
028
029import com.google.common.collect.HashMultimap;
030import com.google.common.collect.SetMultimap;
031import com.velocitypowered.api.command.Command;
032import com.velocitypowered.api.command.CommandSource;
033
034public class VelocityRootCommand implements Command, RootCommand {
035
036    private final VelocityCommandManager manager;
037    private final String name;
038    private BaseCommand defCommand;
039    private SetMultimap<String, RegisteredCommand> subCommands = HashMultimap.create();
040    private List<BaseCommand> children = new ArrayList<>();
041    boolean isRegistered = false;
042
043    VelocityRootCommand(VelocityCommandManager manager, String name) {
044        this.manager = manager;
045        this.name = name;
046    }
047
048    @Override
049    public String getCommandName() {
050        return name;
051    }
052
053    @Override
054    public void addChild(BaseCommand command) {
055        if (this.defCommand == null || !command.subCommands.get(BaseCommand.DEFAULT).isEmpty()) {
056            this.defCommand = command;
057
058        }
059        this.addChildShared(this.children, this.subCommands, command);
060    }
061
062    @Override
063    public CommandManager getManager() {
064        return manager;
065    }
066
067    @Override
068    public SetMultimap<String, RegisteredCommand> getSubCommands() {
069        return subCommands;
070    }
071
072    @Override
073    public List<BaseCommand> getChildren() {
074        return children;
075    }
076
077    @Override
078    public void execute(CommandSource source, String[] args) {
079        execute(manager.getCommandIssuer(source), getCommandName(), args);
080    }
081
082    @Override
083    public List<String> suggest(CommandSource source, String[] args) {
084        return getTabCompletions(manager.getCommandIssuer(source), getCommandName(), args);
085    }
086
087    @Override
088    public BaseCommand getDefCommand() {
089        return defCommand;
090    }
091}