/* * DmitriyMX * 2018-05-23 */ package mc.commands; import lombok.extern.slf4j.Slf4j; import mc.core.chat.CommandExecutor; import mc.core.chat.CommanderChatProcessor; import mc.core.chat.MessageType; import mc.core.player.Player; import mc.core.text.Text; import mc.core.text.TextColor; import mc.core.text.TextTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import java.util.Optional; @Slf4j public class HelpCommand implements CommandExecutor { private static final TextTemplate messageFormat = TextTemplate.builder() .arg("command", TextColor.RED) .append(Text.of(TextColor.GRAY, " - ")) .arg("description", TextColor.WHITE) .build(); @Autowired private ApplicationContext applicationContext; private CommanderChatProcessor commanderChatProcessor; @Override public String getName() { return "help"; } @Override public Optional getAliases() { return Optional.of(new String[]{"?"}); } @Override public Optional getUsage() { return Optional.empty(); } @Override public String getDescription() { return "shows this message"; } @Override public void execute(Player sender, String... args) { if (commanderChatProcessor == null) { commanderChatProcessor = applicationContext.getBean(CommanderChatProcessor.class); if (commanderChatProcessor == null) { log.error("Error get bean of type \"CommanderChatProcessor\". WTF?!"); sender.getChannel().sendChatMessage(Text.of(TextColor.RED, "!!-Server error-!!")); return; } } commanderChatProcessor.getAllCommands().forEach(commandExecutor -> { Text message = messageFormat.apply( "command", commandExecutor.getUsage().orElse(commandExecutor.getName()), "description", commandExecutor.getDescription()); sender.getChannel().sendChatMessage(message, MessageType.SYSTEM_MESSAGE); }); } }