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 co.aikar.commands.apachecommonslang.ApacheCommonsExceptionUtil;
027import co.aikar.timings.Timing;
028import co.aikar.timings.Timings;
029import org.slf4j.Logger;
030import org.spongepowered.api.Sponge;
031import org.spongepowered.api.command.CommandSource;
032import org.spongepowered.api.plugin.PluginContainer;
033import org.spongepowered.api.text.format.TextColor;
034import org.spongepowered.api.text.format.TextColors;
035
036import java.lang.reflect.Method;
037import java.util.Collection;
038import java.util.Collections;
039import java.util.HashMap;
040import java.util.List;
041import java.util.Locale;
042import java.util.Map;
043
044@SuppressWarnings("WeakerAccess")
045public class SpongeCommandManager extends CommandManager<
046        CommandSource,
047        SpongeCommandIssuer,
048        TextColor,
049        SpongeMessageFormatter,
050        SpongeCommandExecutionContext,
051        SpongeConditionContext
052    > {
053
054    protected final PluginContainer plugin;
055    protected Map<String, SpongeRootCommand> registeredCommands = new HashMap<>();
056    protected SpongeCommandContexts contexts;
057    protected SpongeCommandCompletions completions;
058    private Timing commandTiming;
059    protected SpongeLocales locales;
060
061    public SpongeCommandManager(PluginContainer plugin) {
062        this.plugin = plugin;
063        String pluginName = "acf-" + plugin.getName();
064        getLocales().addMessageBundles("acf-minecraft", pluginName, pluginName.toLowerCase(Locale.ENGLISH));
065        this.commandTiming = Timings.of(plugin, "Commands");
066
067        this.formatters.put(MessageType.ERROR, defaultFormatter = new SpongeMessageFormatter(TextColors.RED, TextColors.YELLOW, TextColors.RED));
068        this.formatters.put(MessageType.SYNTAX, new SpongeMessageFormatter(TextColors.YELLOW, TextColors.GREEN, TextColors.WHITE));
069        this.formatters.put(MessageType.INFO, new SpongeMessageFormatter(TextColors.BLUE, TextColors.DARK_GREEN, TextColors.GREEN));
070        this.formatters.put(MessageType.HELP, new SpongeMessageFormatter(TextColors.AQUA, TextColors.GREEN, TextColors.YELLOW));
071        getLocales(); // auto load locales
072
073        this.validNamePredicate = ACFSpongeUtil::isValidName;
074
075        Sponge.getEventManager().registerListeners(plugin, new ACFSpongeListener(this));
076
077        //TODO more default dependencies for sponge
078        registerDependency(plugin.getClass(), plugin);
079    }
080
081    public PluginContainer getPlugin() {
082        return plugin;
083    }
084
085    @Override
086    public boolean isCommandIssuer(Class<?> type) {
087        return CommandSource.class.isAssignableFrom(type);
088    }
089
090    @Override
091    public synchronized CommandContexts<SpongeCommandExecutionContext> getCommandContexts() {
092        if (this.contexts == null) {
093            this.contexts = new SpongeCommandContexts(this);
094        }
095        return contexts;
096    }
097
098    @Override
099    public synchronized CommandCompletions<SpongeCommandCompletionContext> getCommandCompletions() {
100        if (this.completions == null) {
101            this.completions = new SpongeCommandCompletions(this);
102        }
103        return completions;
104    }
105
106    @Override
107    public SpongeLocales getLocales() {
108        if (this.locales == null) {
109            this.locales = new SpongeLocales(this);
110            this.locales.loadLanguages();
111        }
112        return locales;
113    }
114
115    @Override
116    public boolean hasRegisteredCommands() {
117        return !registeredCommands.isEmpty();
118    }
119
120    @Override
121    public void registerCommand(BaseCommand command) {
122        command.onRegister(this);
123
124        for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
125            String commandName = entry.getKey().toLowerCase(Locale.ENGLISH);
126            SpongeRootCommand spongeCommand = (SpongeRootCommand) entry.getValue();
127            if (!spongeCommand.isRegistered) {
128                Sponge.getCommandManager().register(this.plugin, spongeCommand, commandName);
129            }
130            spongeCommand.isRegistered = true;
131            registeredCommands.put(commandName, spongeCommand);
132        }
133    }
134
135    public Timing createTiming(final String name) {
136        return Timings.of(this.plugin, name, this.commandTiming);
137    }
138
139    @Override
140    public RootCommand createRootCommand(String cmd) {
141        return new SpongeRootCommand(this, cmd);
142    }
143    
144    @Override
145    public Collection<RootCommand> getRegisteredRootCommands() {
146        return Collections.unmodifiableCollection(registeredCommands.values());
147    }
148
149    @Override
150    public SpongeCommandIssuer getCommandIssuer(Object issuer) {
151        if (!(issuer instanceof CommandSource)) {
152            throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer.");
153        }
154        return new SpongeCommandIssuer(this, (CommandSource) issuer);
155    }
156
157    @Override
158    public SpongeCommandExecutionContext createCommandContext(RegisteredCommand command, CommandParameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) {
159        return new SpongeCommandExecutionContext(command, parameter, (SpongeCommandIssuer) sender, args, i, passedArgs);
160    }
161
162    @Override
163    public CommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args) {
164        return new SpongeCommandCompletionContext(command, (SpongeCommandIssuer) sender, input, config, args);
165    }
166
167    @Override
168    public RegisteredCommand createRegisteredCommand(BaseCommand command, String cmdName, Method method, String prefSubCommand) {
169        return new SpongeRegisteredCommand(command, cmdName, method, prefSubCommand);
170    }
171
172    @Override
173    public void log(final LogLevel level, final String message, final Throwable throwable) {
174        Logger logger = this.plugin.getLogger();
175        switch(level) {
176            case INFO:
177                logger.info(LogLevel.LOG_PREFIX + message);
178                if (throwable != null) {
179                    for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) {
180                        logger.info(LogLevel.LOG_PREFIX + line);
181                    }
182                }
183                return;
184            case ERROR:
185                logger.error(LogLevel.LOG_PREFIX + message);
186                if (throwable != null) {
187                    for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) {
188                        logger.error(LogLevel.LOG_PREFIX + line);
189                    }
190                }
191        }
192    }
193
194    @Override
195    CommandOperationContext createCommandOperationContext(BaseCommand command, CommandIssuer issuer, String commandLabel, String[] args, boolean isAsync) {
196        return new SpongeCommandOperationContext(
197                this,
198                issuer,
199                command,
200                commandLabel,
201                args,
202                isAsync
203        );
204    }
205
206    @Override
207    public SpongeConditionContext createConditionContext(CommandIssuer issuer, String config) {
208        return new SpongeConditionContext((SpongeCommandIssuer) issuer, config);
209    }
210
211    @Override
212    public String getCommandPrefix(CommandIssuer issuer) {
213        return issuer.isPlayer() ? "/" : "";
214    }
215}