Archived
0
This repository has been archived on 2022-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
Files
mc-server/vanilla_commands/src/main/java/mc/commands/HelpCommand.java
2018-06-24 15:16:30 +03:00

71 lines
2.1 KiB
Java

/*
* DmitriyMX <dimon550@gmail.com>
* 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<String[]> getAliases() {
return Optional.of(new String[]{"?"});
}
@Override
public Optional<String> 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);
});
}
}