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