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.CommandException;
034import org.bukkit.command.CommandMap;
035import org.bukkit.command.CommandSender;
036import org.bukkit.command.PluginIdentifiableCommand;
037import org.bukkit.command.SimpleCommandMap;
038import org.bukkit.configuration.file.FileConfiguration;
039import org.bukkit.entity.Player;
040import org.bukkit.help.GenericCommandHelpTopic;
041import org.bukkit.inventory.ItemFactory;
042import org.bukkit.plugin.Plugin;
043import org.bukkit.plugin.PluginManager;
044import org.bukkit.plugin.java.JavaPlugin;
045import org.bukkit.scheduler.BukkitScheduler;
046import org.bukkit.scheduler.BukkitTask;
047import org.bukkit.scoreboard.ScoreboardManager;
048import org.jetbrains.annotations.NotNull;
049
050import java.lang.reflect.Field;
051import java.lang.reflect.Method;
052import java.util.Collection;
053import java.util.Collections;
054import java.util.HashMap;
055import java.util.HashSet;
056import java.util.List;
057import java.util.Locale;
058import java.util.Map;
059import java.util.Objects;
060import java.util.UUID;
061import java.util.concurrent.ConcurrentHashMap;
062import java.util.logging.Level;
063import java.util.logging.Logger;
064import java.util.regex.Matcher;
065import java.util.regex.Pattern;
066
067@SuppressWarnings("WeakerAccess")
068public class BukkitCommandManager extends CommandManager<
069        CommandSender,
070        BukkitCommandIssuer,
071        ChatColor,
072        BukkitMessageFormatter,
073        BukkitCommandExecutionContext,
074        BukkitConditionContext
075        > {
076
077    @SuppressWarnings("WeakerAccess")
078    protected final Plugin plugin;
079    private final CommandMap commandMap;
080    private final TimingManager timingManager;
081    private final BukkitTask localeTask;
082    private final Logger logger;
083    public final Integer mcMinorVersion;
084    public final Integer mcPatchVersion;
085    protected Map<String, Command> knownCommands = new HashMap<>();
086    protected Map<String, BukkitRootCommand> registeredCommands = new HashMap<>();
087    protected BukkitCommandContexts contexts;
088    protected BukkitCommandCompletions completions;
089    MCTiming commandTiming;
090    protected BukkitLocales locales;
091    protected Map<UUID, String> issuersLocaleString = new ConcurrentHashMap<>();
092    private boolean cantReadLocale = false;
093    protected boolean autoDetectFromClient = true;
094
095    public BukkitCommandManager(Plugin plugin) {
096        this.plugin = plugin;
097        String prefix = this.plugin.getDescription().getPrefix();
098        this.logger = Logger.getLogger(prefix != null ? prefix : this.plugin.getName());
099        this.timingManager = TimingManager.of(plugin);
100        this.commandTiming = this.timingManager.of("Commands");
101        this.commandMap = hookCommandMap();
102        this.formatters.put(MessageType.ERROR, defaultFormatter = new BukkitMessageFormatter(ChatColor.RED, ChatColor.YELLOW, ChatColor.RED));
103        this.formatters.put(MessageType.SYNTAX, new BukkitMessageFormatter(ChatColor.YELLOW, ChatColor.GREEN, ChatColor.WHITE));
104        this.formatters.put(MessageType.INFO, new BukkitMessageFormatter(ChatColor.BLUE, ChatColor.DARK_GREEN, ChatColor.GREEN));
105        this.formatters.put(MessageType.HELP, new BukkitMessageFormatter(ChatColor.AQUA, ChatColor.GREEN, ChatColor.YELLOW));
106        Pattern versionPattern = Pattern.compile("\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?\\)");
107        Matcher matcher = versionPattern.matcher(Bukkit.getVersion());
108        if (matcher.find()) {
109            this.mcMinorVersion = ACFUtil.parseInt(matcher.toMatchResult().group(2), 0);
110            this.mcPatchVersion = ACFUtil.parseInt(matcher.toMatchResult().group(3), 0);
111        } else {
112            this.mcMinorVersion = -1;
113            this.mcPatchVersion = -1;
114        }
115        Bukkit.getHelpMap().registerHelpTopicFactory(BukkitRootCommand.class, command -> {
116            if (hasUnstableAPI("help")) {
117                return new ACFBukkitHelpTopic(this, (BukkitRootCommand) command);
118            } else {
119                return new GenericCommandHelpTopic(command);
120            }
121        });
122
123        Bukkit.getPluginManager().registerEvents(new ACFBukkitListener(this, plugin), plugin);
124
125        getLocales(); // auto load locales
126        this.localeTask = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
127            if (this.cantReadLocale || !this.autoDetectFromClient) {
128                return;
129            }
130            Bukkit.getOnlinePlayers().forEach(this::readPlayerLocale);
131        }, 30, 30);
132
133        registerDependency(plugin.getClass(), plugin);
134        registerDependency(Logger.class, plugin.getLogger());
135        registerDependency(FileConfiguration.class, plugin.getConfig());
136        registerDependency(FileConfiguration.class, "config", plugin.getConfig());
137        registerDependency(Plugin.class, plugin);
138        registerDependency(JavaPlugin.class, plugin);
139        registerDependency(PluginManager.class, Bukkit.getPluginManager());
140        registerDependency(Server.class, Bukkit.getServer());
141        registerDependency(BukkitScheduler.class, Bukkit.getScheduler());
142        registerDependency(ScoreboardManager.class, Bukkit.getScoreboardManager());
143        registerDependency(ItemFactory.class, Bukkit.getItemFactory());
144    }
145
146    @NotNull
147    private CommandMap hookCommandMap() {
148        CommandMap commandMap = null;
149        try {
150            Server server = Bukkit.getServer();
151            Method getCommandMap = server.getClass().getDeclaredMethod("getCommandMap");
152            getCommandMap.setAccessible(true);
153            commandMap = (CommandMap) getCommandMap.invoke(server);
154            if (!SimpleCommandMap.class.isAssignableFrom(commandMap.getClass())) {
155                this.log(LogLevel.ERROR, "ERROR: CommandMap has been hijacked! Offending command map is located at: " + commandMap.getClass().getName());
156                this.log(LogLevel.ERROR, "We are going to try to hijack it back and resolve this, but you are now in dangerous territory.");
157                this.log(LogLevel.ERROR, "We can not guarantee things are going to work.");
158                Field cmField = server.getClass().getDeclaredField("commandMap");
159                commandMap = new ProxyCommandMap(this, commandMap);
160                cmField.set(server, commandMap);
161                this.log(LogLevel.INFO, "Injected Proxy Command Map... good luck...");
162            }
163            Field knownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
164            knownCommands.setAccessible(true);
165            //noinspection unchecked
166            this.knownCommands = (Map<String, Command>) knownCommands.get(commandMap);
167        } catch (Exception e) {
168            this.log(LogLevel.ERROR, "Failed to get Command Map. ACF will not function.");
169            ACFUtil.sneaky(e);
170        }
171        return commandMap;
172    }
173
174    public Plugin getPlugin() {
175        return this.plugin;
176    }
177
178    @Override
179    public boolean isCommandIssuer(Class<?> type) {
180        return CommandSender.class.isAssignableFrom(type);
181    }
182
183    @Override
184    public synchronized CommandContexts<BukkitCommandExecutionContext> getCommandContexts() {
185        if (this.contexts == null) {
186            this.contexts = new BukkitCommandContexts(this);
187        }
188        return contexts;
189    }
190
191    @Override
192    public synchronized CommandCompletions<BukkitCommandCompletionContext> getCommandCompletions() {
193        if (this.completions == null) {
194            this.completions = new BukkitCommandCompletions(this);
195        }
196        return completions;
197    }
198
199
200    @Override
201    public BukkitLocales getLocales() {
202        if (this.locales == null) {
203            this.locales = new BukkitLocales(this);
204            this.locales.loadLanguages();
205        }
206        return locales;
207    }
208
209
210    @Override
211    public boolean hasRegisteredCommands() {
212        return !registeredCommands.isEmpty();
213    }
214
215    public void registerCommand(BaseCommand command, boolean force) {
216        final String plugin = this.plugin.getName().toLowerCase(Locale.ENGLISH);
217        command.onRegister(this);
218        for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
219            String commandName = entry.getKey().toLowerCase(Locale.ENGLISH);
220            BukkitRootCommand bukkitCommand = (BukkitRootCommand) entry.getValue();
221            if (!bukkitCommand.isRegistered) {
222                Command oldCommand = commandMap.getCommand(commandName);
223                if (oldCommand instanceof PluginIdentifiableCommand && ((PluginIdentifiableCommand) oldCommand).getPlugin() == this.plugin) {
224                    knownCommands.remove(commandName);
225                    oldCommand.unregister(commandMap);
226                } else if (oldCommand != null && force) {
227                    knownCommands.remove(commandName);
228                    for (Map.Entry<String, Command> ce : knownCommands.entrySet()) {
229                        String key = ce.getKey();
230                        Command value = ce.getValue();
231                        if (key.contains(":") && oldCommand.equals(value)) {
232                            String[] split = ACFPatterns.COLON.split(key, 2);
233                            if (split.length > 1) {
234                                oldCommand.unregister(commandMap);
235                                oldCommand.setLabel(split[0] + ":" + command.getName());
236                                oldCommand.register(commandMap);
237                            }
238                        }
239                    }
240                }
241                commandMap.register(commandName, plugin, bukkitCommand);
242            }
243            bukkitCommand.isRegistered = true;
244            registeredCommands.put(commandName, bukkitCommand);
245        }
246    }
247
248    @Override
249    public void registerCommand(BaseCommand command) {
250        registerCommand(command, false);
251    }
252
253    public void unregisterCommand(BaseCommand command) {
254        for (RootCommand rootcommand : command.registeredCommands.values()) {
255            BukkitRootCommand bukkitCommand = (BukkitRootCommand) rootcommand;
256            bukkitCommand.getSubCommands().values().removeAll(command.subCommands.values());
257            if (bukkitCommand.isRegistered && bukkitCommand.getSubCommands().isEmpty()) {
258                unregisterCommand(bukkitCommand);
259                bukkitCommand.isRegistered = false;
260            }
261        }
262    }
263
264    /**
265     * @param command
266     * @deprecated Use unregisterCommand(BaseCommand) - this will be visibility reduced later.
267     */
268    @Deprecated
269    public void unregisterCommand(BukkitRootCommand command) {
270        final String plugin = this.plugin.getName().toLowerCase(Locale.ENGLISH);
271        command.unregister(commandMap);
272        String key = command.getName();
273        Command registered = knownCommands.get(key);
274        if (command.equals(registered)) {
275            knownCommands.remove(key);
276        }
277        knownCommands.remove(plugin + ":" + key);
278        registeredCommands.remove(key);
279    }
280
281    public void unregisterCommands() {
282        for (String key : new HashSet<>(registeredCommands.keySet())) {
283            unregisterCommand(registeredCommands.get(key));
284        }
285    }
286
287
288    private Field getEntityField(Player player) throws NoSuchFieldException {
289        Class cls = player.getClass();
290        while (cls != Object.class) {
291            if (cls.getName().endsWith("CraftEntity")) {
292                Field field = cls.getDeclaredField("entity");
293                field.setAccessible(true);
294                return field;
295            }
296            cls = cls.getSuperclass();
297        }
298        return null;
299    }
300
301    public Locale setPlayerLocale(Player player, Locale locale) {
302        return this.setIssuerLocale(player, locale);
303    }
304
305    void readPlayerLocale(Player player) {
306        if (!player.isOnline() || cantReadLocale) {
307            return;
308        }
309        try {
310            Field entityField = getEntityField(player);
311            if (entityField == null) {
312                return;
313            }
314            Object nmsPlayer = entityField.get(player);
315            if (nmsPlayer != null) {
316                Field localeField = nmsPlayer.getClass().getDeclaredField("locale");
317                localeField.setAccessible(true);
318                Object localeString = localeField.get(nmsPlayer);
319                if (localeString instanceof String) {
320                    UUID playerUniqueId = player.getUniqueId();
321                    if (!localeString.equals(issuersLocaleString.get(playerUniqueId))) {
322                        String[] split = ACFPatterns.UNDERSCORE.split((String) localeString);
323                        Locale locale = split.length > 1 ? new Locale(split[0], split[1]) : new Locale(split[0]);
324                        Locale prev = issuersLocale.put(playerUniqueId, locale);
325                        issuersLocaleString.put(playerUniqueId, (String) localeString);
326                        if (!Objects.equals(locale, prev)) {
327                            this.notifyLocaleChange(getCommandIssuer(player), prev, locale);
328                        }
329                    }
330                }
331            }
332        } catch (Exception e) {
333            cantReadLocale = true;
334            this.localeTask.cancel();
335            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);
336        }
337    }
338
339    public TimingManager getTimings() {
340        return timingManager;
341    }
342
343    @Override
344    public RootCommand createRootCommand(String cmd) {
345        return new BukkitRootCommand(this, cmd);
346    }
347
348    @Override
349    public Collection<RootCommand> getRegisteredRootCommands() {
350        return Collections.unmodifiableCollection(registeredCommands.values());
351    }
352
353    @Override
354    public BukkitCommandIssuer getCommandIssuer(Object issuer) {
355        if (!(issuer instanceof CommandSender)) {
356            throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer.");
357        }
358        return new BukkitCommandIssuer(this, (CommandSender) issuer);
359    }
360
361    @Override
362    public BukkitCommandExecutionContext createCommandContext(RegisteredCommand command, CommandParameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) {
363        return new BukkitCommandExecutionContext(command, parameter, (BukkitCommandIssuer) sender, args, i, passedArgs);
364    }
365
366    @Override
367    public BukkitCommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args) {
368        return new BukkitCommandCompletionContext(command, (BukkitCommandIssuer) sender, input, config, args);
369    }
370
371    @Override
372    public RegisteredCommand createRegisteredCommand(BaseCommand command, String cmdName, Method method, String prefSubCommand) {
373        return new BukkitRegisteredCommand(command, cmdName, method, prefSubCommand);
374    }
375
376    @Override
377    public BukkitConditionContext createConditionContext(CommandIssuer issuer, String config) {
378        return new BukkitConditionContext((BukkitCommandIssuer) issuer, config);
379    }
380
381
382    @Override
383    public void log(LogLevel level, String message, Throwable throwable) {
384        Level logLevel = level == LogLevel.INFO ? Level.INFO : Level.SEVERE;
385        logger.log(logLevel, LogLevel.LOG_PREFIX + message);
386        if (throwable != null) {
387            for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) {
388                logger.log(logLevel, LogLevel.LOG_PREFIX + line);
389            }
390        }
391    }
392
393    public boolean usePerIssuerLocale(boolean usePerIssuerLocale, boolean autoDetectFromClient) {
394        boolean old = this.usePerIssuerLocale;
395        this.usePerIssuerLocale = usePerIssuerLocale;
396        this.autoDetectFromClient = autoDetectFromClient;
397        return old;
398    }
399
400    @Override
401    public String getCommandPrefix(CommandIssuer issuer) {
402        return issuer.isPlayer() ? "/" : "";
403    }
404
405    @Override
406    protected boolean handleUncaughtException(BaseCommand scope, RegisteredCommand registeredCommand, CommandIssuer sender, List<String> args, Throwable t) {
407        if (t instanceof CommandException && t.getCause() != null && t.getMessage().startsWith("Unhandled exception")) {
408            t = t.getCause();
409        }
410        return super.handleUncaughtException(scope, registeredCommand, sender, args, t);
411    }
412}