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 net.md_5.bungee.api.ChatColor;
028import net.md_5.bungee.api.CommandSender;
029import net.md_5.bungee.api.ProxyServer;
030import net.md_5.bungee.api.connection.ProxiedPlayer;
031import net.md_5.bungee.api.plugin.Plugin;
032import net.md_5.bungee.api.plugin.PluginDescription;
033
034import java.lang.reflect.Method;
035import java.util.Collection;
036import java.util.Collections;
037import java.util.HashMap;
038import java.util.List;
039import java.util.Locale;
040import java.util.Map;
041import java.util.concurrent.TimeUnit;
042import java.util.logging.Level;
043import java.util.logging.Logger;
044
045public class BungeeCommandManager extends CommandManager<
046        CommandSender,
047        BungeeCommandIssuer,
048        ChatColor,
049        BungeeMessageFormatter,
050        BungeeCommandExecutionContext,
051        BungeeConditionContext
052        > {
053
054    protected final Plugin plugin;
055    protected Map<String, BungeeRootCommand> registeredCommands = new HashMap<>();
056    protected BungeeCommandContexts contexts;
057    protected BungeeCommandCompletions completions;
058    protected BungeeLocales locales;
059
060    public BungeeCommandManager(Plugin plugin) {
061        this.plugin = plugin;
062        this.formatters.put(MessageType.ERROR, defaultFormatter = new BungeeMessageFormatter(ChatColor.RED, ChatColor.YELLOW, ChatColor.RED));
063        this.formatters.put(MessageType.SYNTAX, new BungeeMessageFormatter(ChatColor.YELLOW, ChatColor.GREEN, ChatColor.WHITE));
064        this.formatters.put(MessageType.INFO, new BungeeMessageFormatter(ChatColor.BLUE, ChatColor.DARK_GREEN, ChatColor.GREEN));
065        this.formatters.put(MessageType.HELP, new BungeeMessageFormatter(ChatColor.AQUA, ChatColor.GREEN, ChatColor.YELLOW));
066
067        getLocales(); // auto load locales
068
069        this.validNamePredicate = ACFBungeeUtil::isValidName;
070
071        plugin.getProxy().getPluginManager().registerListener(plugin, new ACFBungeeListener(this, plugin));
072
073        //BungeeCord has no event for listening for client setting changes
074        plugin.getProxy().getScheduler().schedule(plugin, () -> {
075            ProxyServer.getInstance().getPlayers().forEach(this::readLocale);
076        }, 5, 5, TimeUnit.SECONDS);
077
078        // TODO more default dependencies for bungee
079        registerDependency(plugin.getClass(), plugin);
080        registerDependency(Plugin.class, plugin);
081        registerDependency(PluginDescription.class, plugin.getDescription());
082    }
083
084    public Plugin getPlugin() {
085        return this.plugin;
086    }
087
088    @Override
089    public synchronized CommandContexts<BungeeCommandExecutionContext> getCommandContexts() {
090        if (this.contexts == null) {
091            this.contexts = new BungeeCommandContexts(this);
092        }
093        return contexts;
094    }
095
096    @Override
097    public synchronized CommandCompletions<BungeeCommandCompletionContext> getCommandCompletions() {
098        if (this.completions == null) {
099            this.completions = new BungeeCommandCompletions(this);
100        }
101        return completions;
102    }
103
104    @Override
105    public BungeeLocales getLocales() {
106        if (this.locales == null) {
107            this.locales = new BungeeLocales(this);
108            this.locales.loadLanguages();
109        }
110        return locales;
111    }
112
113    public void readLocale(ProxiedPlayer player) {
114        if (!player.isConnected()) {
115            return;
116        }
117
118        //This can be null if we didn't receive a settings packet
119        Locale locale = player.getLocale();
120        if (locale != null) {
121            setIssuerLocale(player, player.getLocale());
122        }
123    }
124
125    @Override
126    public void registerCommand(BaseCommand command) {
127        command.onRegister(this);
128        for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
129            String commandName = entry.getKey().toLowerCase(Locale.ENGLISH);
130            BungeeRootCommand bungeeCommand = (BungeeRootCommand) entry.getValue();
131            if (!bungeeCommand.isRegistered) {
132                this.plugin.getProxy().getPluginManager().registerCommand(this.plugin, bungeeCommand);
133            }
134            bungeeCommand.isRegistered = true;
135            registeredCommands.put(commandName, bungeeCommand);
136        }
137    }
138
139    public void unregisterCommand(BaseCommand command) {
140        for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
141            String commandName = entry.getKey().toLowerCase(Locale.ENGLISH);
142            BungeeRootCommand bungeeCommand = (BungeeRootCommand) entry.getValue();
143            bungeeCommand.getSubCommands().values().removeAll(command.subCommands.values());
144            if (bungeeCommand.getSubCommands().isEmpty() && bungeeCommand.isRegistered) {
145                unregisterCommand(bungeeCommand);
146                bungeeCommand.isRegistered = false;
147                registeredCommands.remove(commandName);
148            }
149        }
150    }
151
152    public void unregisterCommand(BungeeRootCommand command) {
153        this.plugin.getProxy().getPluginManager().unregisterCommand(command);
154    }
155
156    public void unregisterCommands() {
157        for (Map.Entry<String, BungeeRootCommand> entry : registeredCommands.entrySet()) {
158            unregisterCommand(entry.getValue());
159        }
160    }
161
162    @Override
163    public boolean hasRegisteredCommands() {
164        return !registeredCommands.isEmpty();
165    }
166
167    @Override
168    public boolean isCommandIssuer(Class<?> aClass) {
169        return CommandSender.class.isAssignableFrom(aClass);
170    }
171
172    @Override
173    public BungeeCommandIssuer getCommandIssuer(Object issuer) {
174        if (!(issuer instanceof CommandSender)) {
175            throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer.");
176        }
177        return new BungeeCommandIssuer(this, (CommandSender) issuer);
178    }
179
180    @Override
181    public RootCommand createRootCommand(String cmd) {
182        return new BungeeRootCommand(this, cmd);
183    }
184
185    @Override
186    public Collection<RootCommand> getRegisteredRootCommands() {
187        return Collections.unmodifiableCollection(registeredCommands.values());
188    }
189
190    @Override
191    public BungeeCommandExecutionContext createCommandContext(RegisteredCommand command, CommandParameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) {
192        return new BungeeCommandExecutionContext(command, parameter, (BungeeCommandIssuer) sender, args, i, passedArgs);
193    }
194
195    @Override
196    public CommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args) {
197        return new BungeeCommandCompletionContext(command, (BungeeCommandIssuer) sender, input, config, args);
198    }
199
200    @Override
201    public RegisteredCommand createRegisteredCommand(BaseCommand command, String cmdName, Method method, String prefSubCommand) {
202        return new RegisteredCommand(command, cmdName, method, prefSubCommand);
203    }
204
205    @Override
206    public BungeeConditionContext createConditionContext(CommandIssuer issuer, String config) {
207        return new BungeeConditionContext((BungeeCommandIssuer) issuer, config);
208    }
209
210    @Override
211    public void log(LogLevel level, String message, Throwable throwable) {
212        Logger logger = this.plugin.getLogger();
213        Level logLevel = level == LogLevel.INFO ? Level.INFO : Level.SEVERE;
214        logger.log(logLevel, LogLevel.LOG_PREFIX + message);
215        if (throwable != null) {
216            for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) {
217                logger.log(logLevel, LogLevel.LOG_PREFIX + line);
218            }
219        }
220    }
221
222
223    @Override
224    public String getCommandPrefix(CommandIssuer issuer) {
225        return issuer.isPlayer() ? "/" : "";
226    }
227}