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