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