    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.lang.reflect.Method;
    027import java.util.Collection;
    028import java.util.Collections;
    029import java.util.HashMap;
    030import java.util.List;
    031import java.util.Locale;
    032import java.util.Map;
    033
    034import org.slf4j.Logger;
    035import org.slf4j.LoggerFactory;
    036
    037import com.velocitypowered.api.command.CommandSource;
    038import com.velocitypowered.api.plugin.Plugin;
    039import com.velocitypowered.api.plugin.PluginContainer;
    040import com.velocitypowered.api.proxy.Player;
    041import com.velocitypowered.api.proxy.ProxyServer;
    042
    043import co.aikar.commands.apachecommonslang.ApacheCommonsExceptionUtil;
    044import net.kyori.text.format.TextColor;
    045
    046public class VelocityCommandManager extends
    047        CommandManager<CommandSource, VelocityCommandIssuer, TextColor, VelocityMessageFormatter, VelocityCommandExecutionContext, VelocityConditionContext> {
    048
    049    protected final ProxyServer proxy;
    050    protected final PluginContainer plugin;
    051    protected Map<String, VelocityRootCommand> registeredCommands = new HashMap<>();
    052    protected VelocityCommandContexts contexts;
    053    protected VelocityCommandCompletions completions;
    054    protected VelocityLocales locales;
    055
    056    public VelocityCommandManager(ProxyServer proxy, Object plugin) {
    057        this.proxy = proxy;
    058        this.plugin = proxy.getPluginManager().getPlugin(plugin.getClass().getAnnotation(Plugin.class).id()).get();
    059        this.formatters.put(MessageType.ERROR, defaultFormatter = new VelocityMessageFormatter(TextColor.RED, TextColor.YELLOW, TextColor.RED));
    060        this.formatters.put(MessageType.SYNTAX, new VelocityMessageFormatter(TextColor.YELLOW, TextColor.GREEN, TextColor.WHITE));
    061        this.formatters.put(MessageType.INFO, new VelocityMessageFormatter(TextColor.BLUE, TextColor.DARK_GREEN, TextColor.GREEN));
    062        this.formatters.put(MessageType.HELP, new VelocityMessageFormatter(TextColor.AQUA, TextColor.GREEN, TextColor.YELLOW));
    063
    064        getLocales();
    065
    066        proxy.getEventManager().register(plugin, new ACFVelocityListener(this, this.plugin, proxy));
    067
    068        registerDependency(plugin.getClass(), plugin);
    069        registerDependency(Plugin.class, plugin);
    070        registerDependency(ProxyServer.class, proxy);
    071    }
    072
    073    public ProxyServer getProxy() {
    074        return this.proxy;
    075    }
    076
    077    public PluginContainer getPlugin() {
    078        return this.plugin;
    079    }
    080
    081    @Override
    082    public synchronized CommandContexts<VelocityCommandExecutionContext> getCommandContexts() {
    083        if (this.contexts == null) {
    084            this.contexts = new VelocityCommandContexts(proxy, this);
    085        }
    086        return contexts;
    087    }
    088
    089    @Override
    090    public synchronized CommandCompletions<VelocityCommandCompletionContext> getCommandCompletions() {
    091        if (this.completions == null) {
    092            this.completions = new VelocityCommandCompletions(proxy, this);
    093        }
    094        return completions;
    095    }
    096
    097    @Override
    098    public VelocityLocales getLocales() {
    099        if (this.locales == null) {
    100            this.locales = new VelocityLocales(this);
    101            this.locales.loadLanguages();
    102        }
    103        return locales;
    104    }
    105
    106    public void readLocale(Player player) {
    107        if (!player.isActive()) {
    108            return;
    109        }
    110
    111        //This can be null if we didn't received a settings packet
    112        Locale locale = player.getPlayerSettings().getLocale();
    113        if (locale != null) {
    114            setIssuerLocale(player, player.getPlayerSettings().getLocale());
    115        }
    116    }
    117
    118    @Override
    119    public void registerCommand(BaseCommand command) {
    120        command.onRegister(this);
    121        for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
    122            String commandName = entry.getKey().toLowerCase();
    123            VelocityRootCommand velocityCommand = (VelocityRootCommand) entry.getValue();
    124            if (!velocityCommand.isRegistered) {
    125                proxy.getCommandManager().register(velocityCommand, commandName);
    126            }
    127            velocityCommand.isRegistered = true;
    128            registeredCommands.put(commandName, velocityCommand);
    129        }
    130    }
    131
    132    public void unregisterCommand(BaseCommand command) {
    133        for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
    134            String commandName = entry.getKey().toLowerCase();
    135            VelocityRootCommand velocityCommand = (VelocityRootCommand) entry.getValue();
    136            velocityCommand.getSubCommands().values().removeAll(command.subCommands.values());
    137            if (velocityCommand.getSubCommands().isEmpty() && velocityCommand.isRegistered)  {
    138                unregisterCommand(velocityCommand);
    139                velocityCommand.isRegistered = false;
    140                registeredCommands.remove(commandName);
    141            }
    142        }
    143    }
    144
    145    public void unregisterCommand(VelocityRootCommand command) {
    146        proxy.getCommandManager().unregister(command.getCommandName());
    147    }
    148
    149    public void unregisterCommands() {
    150        for (Map.Entry<String, VelocityRootCommand> entry : registeredCommands.entrySet()) {
    151            unregisterCommand(entry.getValue());
    152        }
    153    }
    154
    155    @Override
    156    public boolean hasRegisteredCommands() {
    157        return !registeredCommands.isEmpty();
    158    }
    159
    160    @Override
    161    public boolean isCommandIssuer(Class<?> aClass) {
    162        return CommandSource.class.isAssignableFrom(aClass);
    163    }
    164
    165    @Override
    166    public VelocityCommandIssuer getCommandIssuer(Object issuer) {
    167        if (!(issuer instanceof CommandSource)) {
    168            throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer.");
    169        }
    170        return new VelocityCommandIssuer(this, (CommandSource) issuer);
    171    }
    172
    173    @Override
    174    public RootCommand createRootCommand(String cmd) {
    175        return new VelocityRootCommand(this, cmd);
    176    }
    177
    178    @Override
    179    public Collection<RootCommand> getRegisteredRootCommands() {
    180        return Collections.unmodifiableCollection(registeredCommands.values());
    181    }
    182
    183    @Override
    184    public VelocityCommandExecutionContext createCommandContext(RegisteredCommand command, CommandParameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) {
    185        return new VelocityCommandExecutionContext(command, parameter, (VelocityCommandIssuer) sender, args, i, passedArgs);
    186    }
    187
    188    @Override
    189    public CommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args) {
    190        return new VelocityCommandCompletionContext(command, (VelocityCommandIssuer) sender, input, config, args);
    191    }
    192
    193    @Override
    194    public RegisteredCommand createRegisteredCommand(BaseCommand command, String cmdName, Method method, String prefSubCommand) {
    195        return new RegisteredCommand(command, cmdName, method, prefSubCommand);
    196    }
    197
    198    @Override
    199    public VelocityConditionContext createConditionContext(CommandIssuer issuer, String config) {
    200        return new VelocityConditionContext((VelocityCommandIssuer) issuer, config);
    201    }
    202
    203    @Override
    204    public void log(LogLevel level, String message, Throwable throwable) {
    205        // TODO: Find better solution
    206        Logger logger = LoggerFactory.getLogger(plugin.getClass());
    207        if (level == LogLevel.INFO) {
    208            logger.info(LogLevel.LOG_PREFIX + message);
    209        } else {
    210            logger.warn(LogLevel.LOG_PREFIX + message);
    211        }
    212
    213        if (throwable != null) {
    214            for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) {
    215                if (level == LogLevel.INFO) {
    216                    logger.info(LogLevel.LOG_PREFIX + line);
    217                } else {
    218                    logger.warn(LogLevel.LOG_PREFIX + line);
    219                }
    220            }
    221        }
    222    }
    223
    224
    225    @Override
    226    public String getCommandPrefix(CommandIssuer issuer) {
    227        return issuer.isPlayer() ? "/" : "";
    228    }
    229}