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.Timing; 028import co.aikar.timings.Timings; 029import org.slf4j.Logger; 030import org.spongepowered.api.Sponge; 031import org.spongepowered.api.command.CommandSource; 032import org.spongepowered.api.plugin.PluginContainer; 033import org.spongepowered.api.text.format.TextColor; 034import org.spongepowered.api.text.format.TextColors; 035 036import java.lang.reflect.Method; 037import java.util.Collection; 038import java.util.Collections; 039import java.util.HashMap; 040import java.util.List; 041import java.util.Locale; 042import java.util.Map; 043 044@SuppressWarnings("WeakerAccess") 045public class SpongeCommandManager extends CommandManager< 046 CommandSource, 047 SpongeCommandIssuer, 048 TextColor, 049 SpongeMessageFormatter, 050 SpongeCommandExecutionContext, 051 SpongeConditionContext 052 > { 053 054 protected final PluginContainer plugin; 055 protected Map<String, SpongeRootCommand> registeredCommands = new HashMap<>(); 056 protected SpongeCommandContexts contexts; 057 protected SpongeCommandCompletions completions; 058 private Timing commandTiming; 059 protected SpongeLocales locales; 060 061 public SpongeCommandManager(PluginContainer plugin) { 062 this.plugin = plugin; 063 String pluginName = "acf-" + plugin.getName(); 064 getLocales().addMessageBundles("acf-minecraft", pluginName, pluginName.toLowerCase(Locale.ENGLISH)); 065 this.commandTiming = Timings.of(plugin, "Commands"); 066 067 this.formatters.put(MessageType.ERROR, defaultFormatter = new SpongeMessageFormatter(TextColors.RED, TextColors.YELLOW, TextColors.RED)); 068 this.formatters.put(MessageType.SYNTAX, new SpongeMessageFormatter(TextColors.YELLOW, TextColors.GREEN, TextColors.WHITE)); 069 this.formatters.put(MessageType.INFO, new SpongeMessageFormatter(TextColors.BLUE, TextColors.DARK_GREEN, TextColors.GREEN)); 070 this.formatters.put(MessageType.HELP, new SpongeMessageFormatter(TextColors.AQUA, TextColors.GREEN, TextColors.YELLOW)); 071 getLocales(); // auto load locales 072 073 Sponge.getEventManager().registerListeners(plugin, new ACFSpongeListener(this)); 074 075 //TODO more default dependencies for sponge 076 registerDependency(plugin.getClass(), plugin); 077 } 078 079 public PluginContainer getPlugin() { 080 return plugin; 081 } 082 083 @Override 084 public boolean isCommandIssuer(Class<?> type) { 085 return CommandSource.class.isAssignableFrom(type); 086 } 087 088 @Override 089 public synchronized CommandContexts<SpongeCommandExecutionContext> getCommandContexts() { 090 if (this.contexts == null) { 091 this.contexts = new SpongeCommandContexts(this); 092 } 093 return contexts; 094 } 095 096 @Override 097 public synchronized CommandCompletions<SpongeCommandCompletionContext> getCommandCompletions() { 098 if (this.completions == null) { 099 this.completions = new SpongeCommandCompletions(this); 100 } 101 return completions; 102 } 103 104 @Override 105 public SpongeLocales getLocales() { 106 if (this.locales == null) { 107 this.locales = new SpongeLocales(this); 108 this.locales.loadLanguages(); 109 } 110 return locales; 111 } 112 113 @Override 114 public boolean hasRegisteredCommands() { 115 return !registeredCommands.isEmpty(); 116 } 117 118 @Override 119 public void registerCommand(BaseCommand command) { 120 command.onRegister(this); 121 122 for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) { 123 String commandName = entry.getKey().toLowerCase(Locale.ENGLISH); 124 SpongeRootCommand spongeCommand = (SpongeRootCommand) entry.getValue(); 125 if (!spongeCommand.isRegistered) { 126 Sponge.getCommandManager().register(this.plugin, spongeCommand, commandName); 127 } 128 spongeCommand.isRegistered = true; 129 registeredCommands.put(commandName, spongeCommand); 130 } 131 } 132 133 public Timing createTiming(final String name) { 134 return Timings.of(this.plugin, name, this.commandTiming); 135 } 136 137 @Override 138 public RootCommand createRootCommand(String cmd) { 139 return new SpongeRootCommand(this, cmd); 140 } 141 142 @Override 143 public Collection<RootCommand> getRegisteredRootCommands() { 144 return Collections.unmodifiableCollection(registeredCommands.values()); 145 } 146 147 @Override 148 public SpongeCommandIssuer getCommandIssuer(Object issuer) { 149 if (!(issuer instanceof CommandSource)) { 150 throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer."); 151 } 152 return new SpongeCommandIssuer(this, (CommandSource) issuer); 153 } 154 155 @Override 156 public SpongeCommandExecutionContext createCommandContext(RegisteredCommand command, CommandParameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) { 157 return new SpongeCommandExecutionContext(command, parameter, (SpongeCommandIssuer) sender, args, i, passedArgs); 158 } 159 160 @Override 161 public CommandCompletionContext createCompletionContext(RegisteredCommand command, CommandIssuer sender, String input, String config, String[] args) { 162 return new SpongeCommandCompletionContext(command, (SpongeCommandIssuer) sender, input, config, args); 163 } 164 165 @Override 166 public RegisteredCommand createRegisteredCommand(BaseCommand command, String cmdName, Method method, String prefSubCommand) { 167 return new SpongeRegisteredCommand(command, cmdName, method, prefSubCommand); 168 } 169 170 @Override 171 public void log(final LogLevel level, final String message, final Throwable throwable) { 172 Logger logger = this.plugin.getLogger(); 173 switch(level) { 174 case INFO: 175 logger.info(LogLevel.LOG_PREFIX + message); 176 if (throwable != null) { 177 for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) { 178 logger.info(LogLevel.LOG_PREFIX + line); 179 } 180 } 181 return; 182 case ERROR: 183 logger.error(LogLevel.LOG_PREFIX + message); 184 if (throwable != null) { 185 for (String line : ACFPatterns.NEWLINE.split(ApacheCommonsExceptionUtil.getFullStackTrace(throwable))) { 186 logger.error(LogLevel.LOG_PREFIX + line); 187 } 188 } 189 } 190 } 191 192 @Override 193 CommandOperationContext createCommandOperationContext(BaseCommand command, CommandIssuer issuer, String commandLabel, String[] args, boolean isAsync) { 194 return new SpongeCommandOperationContext( 195 this, 196 issuer, 197 command, 198 commandLabel, 199 args, 200 isAsync 201 ); 202 } 203 204 @Override 205 public SpongeConditionContext createConditionContext(CommandIssuer issuer, String config) { 206 return new SpongeConditionContext((SpongeCommandIssuer) issuer, config); 207 } 208 209 @Override 210 public String getCommandPrefix(CommandIssuer issuer) { 211 return issuer.isPlayer() ? "/" : ""; 212 } 213}