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.annotation.Conditions;
027import co.aikar.commands.apachecommonslang.ApacheCommonsExceptionUtil;
028import co.aikar.timings.lib.MCTiming;
029import co.aikar.timings.lib.TimingManager;
030import com.google.common.collect.Maps;
031import org.bukkit.Bukkit;
032import org.bukkit.ChatColor;
033import org.bukkit.Server;
034import org.bukkit.command.Command;
035import org.bukkit.command.CommandMap;
036import org.bukkit.command.CommandSender;
037import org.bukkit.command.SimpleCommandMap;
038import org.bukkit.entity.Player;
039import org.bukkit.plugin.Plugin;
040import org.bukkit.scheduler.BukkitTask;
041import org.jetbrains.annotations.NotNull;
042
043import java.lang.reflect.Field;
044import java.lang.reflect.Method;
045import java.lang.reflect.Parameter;
046import java.util.HashMap;
047import java.util.List;
048import java.util.Locale;
049import java.util.Map;
050import java.util.Objects;
051import java.util.UUID;
052import java.util.logging.Level;
053import java.util.logging.Logger;
054
055@SuppressWarnings("WeakerAccess")
056public class BukkitCommandManager extends CommandManager<
057        CommandSender,
058        BukkitCommandIssuer,
059        ChatColor,
060        BukkitMessageFormatter,
061        BukkitCommandExecutionContext,
062        BukkitCommandCompletionContext,
063        BukkitConditionContext,
064        BukkitParameterConditionContext<?>
065        > {
066
067    @SuppressWarnings("WeakerAccess")
068    protected final Plugin plugin;
069    private final CommandMap commandMap;
070    private final TimingManager timingManager;
071    private final BukkitTask localeTask;
072    private final Logger logger;
073    protected Map<String, Command> knownCommands = new HashMap<>();
074    protected Map<String, BukkitRootCommand> registeredCommands = new HashMap<>();
075    protected BukkitCommandContexts contexts;
076    protected BukkitCommandCompletions completions;
077    MCTiming commandTiming;
078    protected BukkitLocales locales;
079    private boolean cantReadLocale = false;
080    protected Map<UUID, Locale> issuersLocale = Maps.newConcurrentMap();
081
082    @SuppressWarnings("JavaReflectionMemberAccess")
083    public BukkitCommandManager(Plugin plugin) {
084        this.plugin = plugin;
085        this.logger = Logger.getLogger(this.plugin.getName());
086        this.timingManager = TimingManager.of(plugin);
087        this.commandTiming = this.timingManager.of("Commands");
088        this.commandMap = hookCommandMap();
089        this.formatters.put(MessageType.ERROR, defaultFormatter = new BukkitMessageFormatter(ChatColor.RED, ChatColor.YELLOW, ChatColor.RED));
090        this.formatters.put(MessageType.SYNTAX, new BukkitMessageFormatter(ChatColor.YELLOW, ChatColor.GREEN, ChatColor.WHITE));
091        this.formatters.put(MessageType.INFO, new BukkitMessageFormatter(ChatColor.BLUE, ChatColor.DARK_GREEN, ChatColor.GREEN));
092        this.formatters.put(MessageType.HELP, new BukkitMessageFormatter(ChatColor.AQUA, ChatColor.GREEN, ChatColor.YELLOW));
093        Bukkit.getPluginManager().registerEvents(new ACFBukkitListener(this, plugin), plugin);
094        getLocales(); // auto load locales
095        this.localeTask = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
096            if (cantReadLocale) {
097                return;
098            }
099            Bukkit.getOnlinePlayers().forEach(this::readPlayerLocale);
100        }, 5, 5);
101    }
102
103    @NotNull private CommandMap hookCommandMap() {
104        CommandMap commandMap = null;
105        try {
106            Server server = Bukkit.getServer();
107            Method getCommandMap = server.getClass().getDeclaredMethod("getCommandMap");
108            getCommandMap.setAccessible(true);
109            commandMap = (CommandMap) getCommandMap.invoke(server);
110            if (!SimpleCommandMap.class.isAssignableFrom(commandMap.getClass())) {
111                this.log(LogLevel.ERROR, "ERROR: CommandMap has been hijacked! Offending command map is located at: " + commandMap.getClass().getName());
112                this.log(LogLevel.ERROR, "We are going to try to hijack it back and resolve this, but you are now in dangerous territory.");
113                this.log(LogLevel.ERROR, "We can not guarantee things are going to work.");
114                Field cmField = server.getClass().getDeclaredField("commandMap");
115                commandMap = new ProxyCommandMap(this, commandMap);
116                cmField.set(server, commandMap);
117                this.log(LogLevel.INFO, "Injected Proxy Command Map... good luck...");
118            }
119            Field knownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
120            knownCommands.setAccessible(true);
121            //noinspection unchecked
122            this.knownCommands = (Map<String, Command>) knownCommands.get(commandMap);
123        } catch (Exception e) {
124            this.log(LogLevel.ERROR, "Failed to get Command Map. ACF will not function.");
125            ACFUtil.sneaky(e);
126        }
127        return commandMap;
128    }
129
130    public Plugin getPlugin() {
131        return this.plugin;
132    }
133
134    @Override
135    public boolean isCommandIssuer(Class<?> type) {
136        return CommandSender.class.isAssignableFrom(type);
137    }
138
139    @Override
140    public synchronized CommandContexts<BukkitCommandExecutionContext> getCommandContexts() {
141        if (this.contexts == null) {
142            this.contexts = new BukkitCommandContexts(this);
143        }
144        return contexts;
145    }
146
147    @Override
148    public synchronized CommandCompletions<BukkitCommandCompletionContext> getCommandCompletions() {
149        if (this.completions == null) {
150            this.completions = new BukkitCommandCompletions(this);
151        }
152        return completions;
153    }
154
155
156    @Override
157    public BukkitLocales getLocales() {
158        if (this.locales == null) {
159            this.locales = new BukkitLocales(this);
160            this.locales.loadLanguages();
161        }
162        return locales;
163    }
164
165
166    @Override
167    public boolean hasRegisteredCommands() {
168        return !registeredCommands.isEmpty();
169    }
170
171    public void registerCommand(BaseCommand command, boolean force) {
172        final String plugin = this.plugin.getName().toLowerCase();
173        command.onRegister(this);
174        for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
175            String commandName = entry.getKey().toLowerCase();
176            BukkitRootCommand bukkitCommand = (BukkitRootCommand) entry.getValue();
177            if (!bukkitCommand.isRegistered) {
178                if (force && knownCommands.containsKey(commandName)) {
179                    Command oldCommand = commandMap.getCommand(commandName);
180                    knownCommands.remove(commandName);
181                    for (Map.Entry<String, Command> ce : knownCommands.entrySet()) {
182                        String key = ce.getKey();
183                        Command value = ce.getValue();
184                        if (key.contains(":") && oldCommand.equals(value)) {
185                            String[] split = ACFPatterns.COLON.split(key, 2);
186                            if (split.length > 1) {
187                                oldCommand.unregister(commandMap);
188                                oldCommand.setLabel(split[0] + ":" + command.getName());
189                                oldCommand.register(commandMap);
190                            }
191                        }
192                    }
193                }
194                commandMap.register(commandName, plugin, bukkitCommand);
195            }
196            bukkitCommand.isRegistered = true;
197            registeredCommands.put(commandName, bukkitCommand);
198        }
199    }
200
201    @Override
202    public void registerCommand(BaseCommand command) {
203        registerCommand(command, false);
204    }
205
206    public void unregisterCommand(BaseCommand command) {
207        for (RootCommand rootcommand : command.registeredCommands.values()) {
208            BukkitRootCommand bukkitCommand = (BukkitRootCommand) rootcommand;
209            bukkitCommand.getSubCommands().values().removeAll(command.subCommands.values());
210            if (bukkitCommand.isRegistered && bukkitCommand.getSubCommands().isEmpty()) {
211                unregisterCommand(bukkitCommand);
212                bukkitCommand.isRegistered = false;
213            }
214        }
215    }
216
217    /**
218     * @deprecated Use unregisterCommand(BaseCommand) - this will be visibility reduced later.
219     * @param command
220     */
221    @Deprecated
222    public void unregisterCommand(BukkitRootCommand command) {
223        final String plugin = this.plugin.getName().toLowerCase();
224        command.unregister(commandMap);
225        String key = command.getName();
226        Command registered = knownCommands.get(key);
227        if (command.equals(registered)) {
228            knownCommands.remove(key);
229        }
230        knownCommands.remove(plugin + ":" + key);
231    }
232
233    public void unregisterCommands() {
234        for (Map.Entry<String, BukkitRootCommand> entry : registeredCommands.entrySet()) {
235            unregisterCommand(entry.getValue());
236        }
237        this.registeredCommands.clear();
238    }
239
240
241    private Field getEntityField(Player player) throws NoSuchFieldException {
242        Class cls = player.getClass();
243        while (cls != Object.class) {
244            if (cls.getName().endsWith("CraftEntity")) {
245                Field field = cls.getDeclaredField("entity");
246                field.setAccessible(true);
247                return field;
248            }
249            cls = cls.getSuperclass();
250        }
251        return null;
252    }
253
254    void readPlayerLocale(Player player) {
255        if (!player.isOnline() || cantReadLocale) {
256            return;
257        }
258        try {
259            Field entityField = getEntityField(player);
260            if (entityField == null) {
261                return;
262            }
263            Object nmsPlayer = entityField.get(player);
264            if (nmsPlayer != null) {
265                Field localeField = nmsPlayer.getClass().getField("locale");
266                Object localeString = localeField.get(nmsPlayer);
267                if (localeString != null && localeString instanceof String) {
268                    String[] split = ACFPatterns.UNDERSCORE.split((String) localeString);
269                    Locale locale = split.length > 1 ? new Locale(split[0], split[1]) : new Locale(split[0]);
270                    Locale prev = issuersLocale.put(player.getUniqueId(), locale);
271                    if (!Objects.equals(locale, prev)) {
272                        this.notifyLocaleChange(getCommandIssuer(player), prev, locale);
273                    }
274                }
275            }
276        } catch (Exception e) {
277            cantReadLocale = true;
278            this.localeTask.cancel();
279            this.log(LogLevel.INFO, "Can't read players locale, you will be unable to automatically detect players language. Only Bukkit 1.7+ is supported for this.", e);
280        }
281    }
282
283    public TimingManager getTimings() {
284        return timingManager;
285    }
286
287    @Override
288    public RootCommand createRootCommand(String cmd) {
289        return new BukkitRootCommand(this, cmd);
290    }
291
292    @Override
293    public BukkitCommandIssuer getCommandIssuer(Object issuer) {
294        if (!(issuer instanceof CommandSender)) {
295            throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer.");
296        }
297        return new BukkitCommandIssuer(this, (CommandSender) issuer);
298    }
299
300    @Override
301    public BukkitCommandExecutionContext createCommandContext(RegisteredCommand command, Parameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) {
302        return new BukkitCommandExecutionContext(command, parameter, (BukkitCommandIssuer) sender, args, i, passedArgs);
303    }
304
305    @Override
306    public BukkitCommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args) {
307        return new BukkitCommandCompletionContext(command, sender, input, config, args);
308    }
309
310    @Override
311    public RegisteredCommand createRegisteredCommand(BaseCommand command, String cmdName, Method method, String prefSubCommand) {
312        return new BukkitRegisteredCommand(command, cmdName, method, prefSubCommand);
313    }
314
315    @Override
316    public BukkitConditionContext createConditionContext(CommandOperationContext context, Conditions conditions) {
317        return new BukkitConditionContext(context.getRegisteredCommand(), (BukkitCommandIssuer) context.getCommandIssuer(), conditions);
318    }
319
320    @Override
321    public <P> BukkitParameterConditionContext createConditionContext(CommandOperationContext context, BukkitCommandExecutionContext execContext, Conditions conditions) {
322        return new BukkitParameterConditionContext<P>(context.getRegisteredCommand(), (BukkitCommandIssuer) context.getCommandIssuer(), execContext, conditions);
323    }
324
325    @Override
326    public void log(LogLevel level, String message, Throwable throwable) {
327        Level logLevel = level == LogLevel.INFO ? Level.INFO : Level.SEVERE;
328        logger.log(logLevel, LogLevel.LOG_PREFIX + message);
329        if (throwable != null) {
330            for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) {
331                logger.log(logLevel, LogLevel.LOG_PREFIX + line);
332            }
333        }
334    }
335
336    @Override
337    public Locale getIssuerLocale(CommandIssuer issuer) {
338        if (usingPerIssuerLocale() && issuer.getIssuer() instanceof Player) {
339            UUID uniqueId = ((Player) issuer.getIssuer()).getUniqueId();
340            Locale locale = issuersLocale.get(uniqueId);
341            if (locale != null) {
342                return locale;
343            }
344        }
345        return super.getIssuerLocale(issuer);
346    }
347}