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