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