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