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