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