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