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 java.lang.reflect.Method; 027import java.util.Collection; 028import java.util.Collections; 029import java.util.HashMap; 030import java.util.List; 031import java.util.Locale; 032import java.util.Map; 033 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037import com.velocitypowered.api.command.CommandSource; 038import com.velocitypowered.api.plugin.Plugin; 039import com.velocitypowered.api.plugin.PluginContainer; 040import com.velocitypowered.api.proxy.Player; 041import com.velocitypowered.api.proxy.ProxyServer; 042 043import co.aikar.commands.apachecommonslang.ApacheCommonsExceptionUtil; 044import net.kyori.text.format.TextColor; 045 046public class VelocityCommandManager extends 047 CommandManager<CommandSource, VelocityCommandIssuer, TextColor, VelocityMessageFormatter, VelocityCommandExecutionContext, VelocityConditionContext> { 048 049 protected final ProxyServer proxy; 050 protected final PluginContainer plugin; 051 protected Map<String, VelocityRootCommand> registeredCommands = new HashMap<>(); 052 protected VelocityCommandContexts contexts; 053 protected VelocityCommandCompletions completions; 054 protected VelocityLocales locales; 055 056 public VelocityCommandManager(ProxyServer proxy, Object plugin) { 057 this.proxy = proxy; 058 this.plugin = proxy.getPluginManager().getPlugin(plugin.getClass().getAnnotation(Plugin.class).id()).get(); 059 this.formatters.put(MessageType.ERROR, defaultFormatter = new VelocityMessageFormatter(TextColor.RED, TextColor.YELLOW, TextColor.RED)); 060 this.formatters.put(MessageType.SYNTAX, new VelocityMessageFormatter(TextColor.YELLOW, TextColor.GREEN, TextColor.WHITE)); 061 this.formatters.put(MessageType.INFO, new VelocityMessageFormatter(TextColor.BLUE, TextColor.DARK_GREEN, TextColor.GREEN)); 062 this.formatters.put(MessageType.HELP, new VelocityMessageFormatter(TextColor.AQUA, TextColor.GREEN, TextColor.YELLOW)); 063 064 getLocales(); 065 066 proxy.getEventManager().register(plugin, new ACFVelocityListener(this, this.plugin, proxy)); 067 068 registerDependency(plugin.getClass(), plugin); 069 registerDependency(Plugin.class, plugin); 070 registerDependency(ProxyServer.class, proxy); 071 } 072 073 public ProxyServer getProxy() { 074 return this.proxy; 075 } 076 077 public PluginContainer getPlugin() { 078 return this.plugin; 079 } 080 081 @Override 082 public synchronized CommandContexts<VelocityCommandExecutionContext> getCommandContexts() { 083 if (this.contexts == null) { 084 this.contexts = new VelocityCommandContexts(proxy, this); 085 } 086 return contexts; 087 } 088 089 @Override 090 public synchronized CommandCompletions<VelocityCommandCompletionContext> getCommandCompletions() { 091 if (this.completions == null) { 092 this.completions = new VelocityCommandCompletions(proxy, this); 093 } 094 return completions; 095 } 096 097 @Override 098 public VelocityLocales getLocales() { 099 if (this.locales == null) { 100 this.locales = new VelocityLocales(this); 101 this.locales.loadLanguages(); 102 } 103 return locales; 104 } 105 106 public void readLocale(Player player) { 107 if (!player.isActive()) { 108 return; 109 } 110 111 //This can be null if we didn't received a settings packet 112 Locale locale = player.getPlayerSettings().getLocale(); 113 if (locale != null) { 114 setIssuerLocale(player, player.getPlayerSettings().getLocale()); 115 } 116 } 117 118 @Override 119 public void registerCommand(BaseCommand command) { 120 registerCommand(command, false); 121 } 122 123 public void registerCommand(BaseCommand command, boolean force) { 124 command.onRegister(this); 125 for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) { 126 String commandName = entry.getKey().toLowerCase(Locale.ENGLISH); 127 VelocityRootCommand velocityCommand = (VelocityRootCommand) entry.getValue(); 128 if (!velocityCommand.isRegistered) { 129 if (force) { 130 proxy.getCommandManager().unregister(commandName); 131 } 132 proxy.getCommandManager().register(velocityCommand, commandName); 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}