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.lib.MCTiming;
028import co.aikar.timings.lib.TimingManager;
029import org.bukkit.Bukkit;
030import org.bukkit.ChatColor;
031import org.bukkit.Server;
032import org.bukkit.command.Command;
033import org.bukkit.command.CommandMap;
034import org.bukkit.command.CommandSender;
035import org.bukkit.command.SimpleCommandMap;
036import org.bukkit.event.EventHandler;
037import org.bukkit.event.Listener;
038import org.bukkit.event.server.PluginDisableEvent;
039import org.bukkit.plugin.Plugin;
040import org.jetbrains.annotations.NotNull;
041
042import java.lang.reflect.Field;
043import java.lang.reflect.Method;
044import java.lang.reflect.Parameter;
045import java.util.HashMap;
046import java.util.List;
047import java.util.Map;
048import java.util.logging.Level;
049import java.util.logging.Logger;
050
051@SuppressWarnings("WeakerAccess")
052public class BukkitCommandManager extends CommandManager<CommandSender, ChatColor, BukkitMessageFormatter> {
053
054    @SuppressWarnings("WeakerAccess")
055    protected final Plugin plugin;
056    private final CommandMap commandMap;
057    private final TimingManager timingManager;
058    protected Map<String, Command> knownCommands = new HashMap<>();
059    protected Map<String, BukkitRootCommand> registeredCommands = new HashMap<>();
060    protected BukkitCommandContexts contexts;
061    protected BukkitCommandCompletions completions;
062    MCTiming commandTiming;
063    protected BukkitLocales locales;
064
065    @SuppressWarnings("JavaReflectionMemberAccess")
066    public BukkitCommandManager(Plugin plugin) {
067        this.plugin = plugin;
068        this.timingManager = TimingManager.of(plugin);
069        this.commandTiming = this.timingManager.of("Commands");
070        this.commandMap = hookCommandMap();
071        this.formatters.put(MessageType.ERROR, defaultFormatter = new BukkitMessageFormatter(ChatColor.RED, ChatColor.YELLOW, ChatColor.RED));
072        this.formatters.put(MessageType.SYNTAX, new BukkitMessageFormatter(ChatColor.YELLOW, ChatColor.GREEN, ChatColor.WHITE));
073        this.formatters.put(MessageType.INFO, new BukkitMessageFormatter(ChatColor.BLUE, ChatColor.DARK_GREEN, ChatColor.GREEN));
074        this.formatters.put(MessageType.HELP, new BukkitMessageFormatter(ChatColor.AQUA, ChatColor.GREEN, ChatColor.YELLOW));
075        Bukkit.getPluginManager().registerEvents(new ACFBukkitListener(plugin), plugin);
076        getLocales(); // auto load locales
077    }
078
079    @NotNull private CommandMap hookCommandMap() {
080        CommandMap commandMap = null;
081        try {
082            Server server = Bukkit.getServer();
083            Method getCommandMap = server.getClass().getDeclaredMethod("getCommandMap");
084            getCommandMap.setAccessible(true);
085            commandMap = (CommandMap) getCommandMap.invoke(server);
086            if (!SimpleCommandMap.class.isAssignableFrom(commandMap.getClass())) {
087                this.log(LogLevel.ERROR, "ERROR: CommandMap has been hijacked! Offending command map is located at: " + commandMap.getClass().getName());
088                this.log(LogLevel.ERROR, "We are going to try to hijack it back and resolve this, but you are now in dangerous territory.");
089                this.log(LogLevel.ERROR, "We can not guarantee things are going to work.");
090                Field cmField = server.getClass().getDeclaredField("commandMap");
091                commandMap = new ProxyCommandMap(this, commandMap);
092                cmField.set(server, commandMap);
093                this.log(LogLevel.INFO, "Injected Proxy Command Map... good luck...");
094            }
095            Field knownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
096            knownCommands.setAccessible(true);
097            //noinspection unchecked
098            this.knownCommands = (Map<String, Command>) knownCommands.get(commandMap);
099        } catch (Exception e) {
100            this.log(LogLevel.ERROR, "Failed to get Command Map. ACF will not function.");
101            ACFUtil.sneaky(e);
102        }
103        return commandMap;
104    }
105
106    public Plugin getPlugin() {
107        return this.plugin;
108    }
109
110    @Override
111    public boolean isCommandIssuer(Class<?> type) {
112        return CommandSender.class.isAssignableFrom(type);
113    }
114
115    @Override
116    public synchronized CommandContexts<BukkitCommandExecutionContext> getCommandContexts() {
117        if (this.contexts == null) {
118            this.contexts = new BukkitCommandContexts(this);
119        }
120        return contexts;
121    }
122
123    @Override
124    public synchronized CommandCompletions<BukkitCommandCompletionContext> getCommandCompletions() {
125        if (this.completions == null) {
126            this.completions = new BukkitCommandCompletions(this);
127        }
128        return completions;
129    }
130
131
132    @Override
133    public BukkitLocales getLocales() {
134        if (this.locales == null) {
135            this.locales = new BukkitLocales(this);
136            this.locales.loadLanguages();
137        }
138        return locales;
139    }
140
141
142    @Override
143    public boolean hasRegisteredCommands() {
144        return !registeredCommands.isEmpty();
145    }
146
147    public void registerCommand(BaseCommand command, boolean force) {
148        final String plugin = this.plugin.getName().toLowerCase();
149        command.onRegister(this);
150        for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
151            String commandName = entry.getKey().toLowerCase();
152            BukkitRootCommand bukkitCommand = (BukkitRootCommand) entry.getValue();
153            if (!bukkitCommand.isRegistered) {
154                if (force && knownCommands.containsKey(commandName)) {
155                    Command oldCommand = commandMap.getCommand(commandName);
156                    knownCommands.remove(commandName);
157                    for (Map.Entry<String, Command> ce : knownCommands.entrySet()) {
158                        String key = ce.getKey();
159                        Command value = ce.getValue();
160                        if (key.contains(":") && oldCommand.equals(value)) {
161                            String[] split = ACFPatterns.COLON.split(key, 2);
162                            if (split.length > 1) {
163                                oldCommand.unregister(commandMap);
164                                oldCommand.setLabel(split[0] + ":" + command.getName());
165                                oldCommand.register(commandMap);
166                            }
167                        }
168                    }
169                }
170                commandMap.register(commandName, plugin, bukkitCommand);
171            }
172            bukkitCommand.isRegistered = true;
173            registeredCommands.put(commandName, bukkitCommand);
174        }
175    }
176
177    @Override
178    public void registerCommand(BaseCommand command) {
179        registerCommand(command, false);
180    }
181
182    public void unregisterCommand(BaseCommand command) {
183        for (RootCommand rootcommand : command.registeredCommands.values()) {
184            BukkitRootCommand bukkitCommand = (BukkitRootCommand) rootcommand;
185            bukkitCommand.getSubCommands().values().removeAll(command.subCommands.values());
186            if (bukkitCommand.isRegistered && bukkitCommand.getSubCommands().isEmpty()) {
187                unregisterCommand(bukkitCommand);
188                bukkitCommand.isRegistered = false;
189            }
190        }
191    }
192
193    /**
194     * @deprecated Use unregisterCommand(BaseCommand) - this will be visibility reduced later.
195     * @param command
196     */
197    @Deprecated
198    public void unregisterCommand(BukkitRootCommand command) {
199        final String plugin = this.plugin.getName().toLowerCase();
200        command.unregister(commandMap);
201        String key = command.getName();
202        Command registered = knownCommands.get(key);
203        if (command.equals(registered)) {
204            knownCommands.remove(key);
205        }
206        knownCommands.remove(plugin + ":" + key);
207    }
208
209    public void unregisterCommands() {
210        for (Map.Entry<String, BukkitRootCommand> entry : registeredCommands.entrySet()) {
211            unregisterCommand(entry.getValue());
212        }
213        this.registeredCommands.clear();
214    }
215
216    private class ACFBukkitListener implements Listener {
217        private final Plugin plugin;
218
219        public ACFBukkitListener(Plugin plugin) {
220            this.plugin = plugin;
221        }
222
223        @EventHandler
224        public void onPluginDisable(PluginDisableEvent event) {
225            if (!(plugin.getName().equalsIgnoreCase(event.getPlugin().getName()))) {
226                return;
227            }
228            unregisterCommands();
229        }
230    }
231
232    public TimingManager getTimings() {
233        return timingManager;
234    }
235
236    @Override
237    public RootCommand createRootCommand(String cmd) {
238        return new BukkitRootCommand(this, cmd);
239    }
240
241    @Override
242    public CommandIssuer getCommandIssuer(Object issuer) {
243        if (!(issuer instanceof CommandSender)) {
244            throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer.");
245        }
246        return new BukkitCommandIssuer(this, (CommandSender) issuer);
247    }
248
249    @Override
250    public <R extends CommandExecutionContext> R createCommandContext(RegisteredCommand command, Parameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) {
251        //noinspection unchecked
252        return (R) new BukkitCommandExecutionContext(command, parameter, (BukkitCommandIssuer) sender, args, i, passedArgs);
253    }
254
255    @Override
256    public CommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args) {
257        return new BukkitCommandCompletionContext(command, sender, input, config, args);
258    }
259
260    @Override
261    public RegisteredCommand createRegisteredCommand(BaseCommand command, String cmdName, Method method, String prefSubCommand) {
262        return new BukkitRegisteredCommand(command, cmdName, method, prefSubCommand);
263    }
264
265    @Override
266    public void log(LogLevel level, String message, Throwable throwable) {
267        Logger logger = this.plugin.getLogger();
268        Level logLevel = level == LogLevel.INFO ? Level.INFO : Level.SEVERE;
269        logger.log(logLevel, LogLevel.LOG_PREFIX + message);
270        if (throwable != null) {
271            for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) {
272                logger.log(logLevel, LogLevel.LOG_PREFIX + line);
273            }
274        }
275    }
276}