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 com.velocitypowered.api.command.CommandMeta; 028import com.velocitypowered.api.command.CommandSource; 029import com.velocitypowered.api.plugin.Plugin; 030import com.velocitypowered.api.plugin.PluginContainer; 031import com.velocitypowered.api.proxy.Player; 032import com.velocitypowered.api.proxy.ProxyServer; 033import net.kyori.adventure.text.format.NamedTextColor; 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037import java.lang.reflect.Method; 038import java.util.Collection; 039import java.util.Collections; 040import java.util.HashMap; 041import java.util.List; 042import java.util.Locale; 043import java.util.Map; 044 045public class VelocityCommandManager extends 046 CommandManager<CommandSource, VelocityCommandIssuer, NamedTextColor, VelocityMessageFormatter, VelocityCommandExecutionContext, VelocityConditionContext> { 047 048 protected final ProxyServer proxy; 049 protected final PluginContainer plugin; 050 protected Map<String, VelocityRootCommand> registeredCommands = new HashMap<>(); 051 protected VelocityCommandContexts contexts; 052 protected VelocityCommandCompletions completions; 053 protected VelocityLocales locales; 054 055 public VelocityCommandManager(ProxyServer proxy, Object plugin) { 056 this.proxy = proxy; 057 this.plugin = proxy.getPluginManager().getPlugin(plugin.getClass().getAnnotation(Plugin.class).id()).get(); 058 this.formatters.put(MessageType.ERROR, defaultFormatter = new VelocityMessageFormatter(NamedTextColor.RED, NamedTextColor.YELLOW, NamedTextColor.RED)); 059 this.formatters.put(MessageType.SYNTAX, new VelocityMessageFormatter(NamedTextColor.YELLOW, NamedTextColor.GREEN, NamedTextColor.WHITE)); 060 this.formatters.put(MessageType.INFO, new VelocityMessageFormatter(NamedTextColor.BLUE, NamedTextColor.DARK_GREEN, NamedTextColor.GREEN)); 061 this.formatters.put(MessageType.HELP, new VelocityMessageFormatter(NamedTextColor.AQUA, NamedTextColor.GREEN, NamedTextColor.YELLOW)); 062 063 getLocales(); 064 065 proxy.getEventManager().register(plugin, new ACFVelocityListener(this, this.plugin, proxy)); 066 067 registerDependency(plugin.getClass(), plugin); 068 registerDependency(Plugin.class, plugin); 069 registerDependency(ProxyServer.class, proxy); 070 } 071 072 public ProxyServer getProxy() { 073 return this.proxy; 074 } 075 076 public PluginContainer getPlugin() { 077 return this.plugin; 078 } 079 080 @Override 081 public synchronized CommandContexts<VelocityCommandExecutionContext> getCommandContexts() { 082 if (this.contexts == null) { 083 this.contexts = new VelocityCommandContexts(proxy, this); 084 } 085 return contexts; 086 } 087 088 @Override 089 public synchronized CommandCompletions<VelocityCommandCompletionContext> getCommandCompletions() { 090 if (this.completions == null) { 091 this.completions = new VelocityCommandCompletions(proxy, this); 092 } 093 return completions; 094 } 095 096 @Override 097 public VelocityLocales getLocales() { 098 if (this.locales == null) { 099 this.locales = new VelocityLocales(this); 100 this.locales.loadLanguages(); 101 } 102 return locales; 103 } 104 105 public void readLocale(Player player) { 106 if (!player.isActive()) { 107 return; 108 } 109 110 //This can be null if we didn't receive a settings packet 111 Locale locale = player.getPlayerSettings().getLocale(); 112 if (locale != null) { 113 setIssuerLocale(player, player.getPlayerSettings().getLocale()); 114 } 115 } 116 117 @Override 118 public void registerCommand(BaseCommand command) { 119 registerCommand(command, false); 120 } 121 122 public void registerCommand(BaseCommand command, boolean force) { 123 command.onRegister(this); 124 for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) { 125 String commandName = entry.getKey().toLowerCase(Locale.ENGLISH); 126 VelocityRootCommand velocityCommand = (VelocityRootCommand) entry.getValue(); 127 if (!velocityCommand.isRegistered) { 128 if (force) { 129 proxy.getCommandManager().unregister(commandName); 130 } 131 CommandMeta meta = proxy.getCommandManager().metaBuilder(commandName).build(); 132 proxy.getCommandManager().register(meta, velocityCommand); 133 } 134 velocityCommand.isRegistered = true; 135 registeredCommands.put(commandName, velocityCommand); 136 } 137 } 138 139 public void unregisterCommand(BaseCommand command) { 140 for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) { 141 String commandName = entry.getKey().toLowerCase(Locale.ENGLISH); 142 VelocityRootCommand velocityCommand = (VelocityRootCommand) entry.getValue(); 143 velocityCommand.getSubCommands().values().removeAll(command.subCommands.values()); 144 if (velocityCommand.getSubCommands().isEmpty() && velocityCommand.isRegistered) { 145 unregisterCommand(velocityCommand); 146 velocityCommand.isRegistered = false; 147 registeredCommands.remove(commandName); 148 } 149 } 150 } 151 152 public void unregisterCommand(VelocityRootCommand command) { 153 proxy.getCommandManager().unregister(command.getCommandName()); 154 } 155 156 public void unregisterCommands() { 157 for (Map.Entry<String, VelocityRootCommand> entry : registeredCommands.entrySet()) { 158 unregisterCommand(entry.getValue()); 159 } 160 } 161 162 @Override 163 public boolean hasRegisteredCommands() { 164 return !registeredCommands.isEmpty(); 165 } 166 167 @Override 168 public boolean isCommandIssuer(Class<?> aClass) { 169 return CommandSource.class.isAssignableFrom(aClass); 170 } 171 172 @Override 173 public VelocityCommandIssuer getCommandIssuer(Object issuer) { 174 if (!(issuer instanceof CommandSource)) { 175 throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer."); 176 } 177 return new VelocityCommandIssuer(this, (CommandSource) issuer); 178 } 179 180 @Override 181 public RootCommand createRootCommand(String cmd) { 182 return new VelocityRootCommand(this, cmd); 183 } 184 185 @Override 186 public Collection<RootCommand> getRegisteredRootCommands() { 187 return Collections.unmodifiableCollection(registeredCommands.values()); 188 } 189 190 @Override 191 public VelocityCommandExecutionContext createCommandContext(RegisteredCommand command, CommandParameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) { 192 return new VelocityCommandExecutionContext(command, parameter, (VelocityCommandIssuer) sender, args, i, passedArgs); 193 } 194 195 @Override 196 public CommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args) { 197 return new VelocityCommandCompletionContext(command, (VelocityCommandIssuer) sender, input, config, args); 198 } 199 200 @Override 201 public RegisteredCommand createRegisteredCommand(BaseCommand command, String cmdName, Method method, String prefSubCommand) { 202 return new RegisteredCommand(command, cmdName, method, prefSubCommand); 203 } 204 205 @Override 206 public VelocityConditionContext createConditionContext(CommandIssuer issuer, String config) { 207 return new VelocityConditionContext((VelocityCommandIssuer) issuer, config); 208 } 209 210 @Override 211 public void log(LogLevel level, String message, Throwable throwable) { 212 // TODO: Find better solution 213 Logger logger = LoggerFactory.getLogger(plugin.getClass()); 214 if (level == LogLevel.INFO) { 215 logger.info(LogLevel.LOG_PREFIX + message); 216 } else { 217 logger.warn(LogLevel.LOG_PREFIX + message); 218 } 219 220 if (throwable != null) { 221 for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) { 222 if (level == LogLevel.INFO) { 223 logger.info(LogLevel.LOG_PREFIX + line); 224 } else { 225 logger.warn(LogLevel.LOG_PREFIX + line); 226 } 227 } 228 } 229 } 230 231 232 @Override 233 public String getCommandPrefix(CommandIssuer issuer) { 234 return issuer.isPlayer() ? "/" : ""; 235 } 236}