Vanilla commands: help, list
This commit is contained in:
@@ -6,6 +6,12 @@ package mc.core.chat;
|
|||||||
|
|
||||||
import mc.core.player.Player;
|
import mc.core.player.Player;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface CommandExecutor {
|
public interface CommandExecutor {
|
||||||
void execute(Player sender, String command, String... args);
|
String getName();
|
||||||
|
Optional<String[]> getAliases();
|
||||||
|
Optional<String> getUsage();
|
||||||
|
String getDescription();
|
||||||
|
void execute(Player sender, String... args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,15 +8,54 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import mc.core.player.Player;
|
import mc.core.player.Player;
|
||||||
import org.slf4j.Marker;
|
import org.slf4j.Marker;
|
||||||
import org.slf4j.helpers.BasicMarkerFactory;
|
import org.slf4j.helpers.BasicMarkerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
import java.util.Collections;
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class CommanderChatProcessor extends SimpleChatProcessor {
|
public class CommanderChatProcessor extends SimpleChatProcessor {
|
||||||
private static final Marker COMMAND_MARKER = new BasicMarkerFactory().getMarker("Command");
|
private static final Marker COMMAND_MARKER = new BasicMarkerFactory().getMarker("Command");
|
||||||
private static final String UNKNOW_COMMAND_MSG = ChatStyle.RED + "Unknown command \"" + ChatStyle.WHITE + "%s" + ChatStyle.RED + "\"";
|
private static final String UNKNOW_COMMAND_MSG = ChatStyle.RED + "Unknown command \"" + ChatStyle.WHITE + "%s" + ChatStyle.RED + "\"";
|
||||||
private Map<String, CommandExecutor> commands = Collections.emptyMap();
|
@Autowired
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
private Map<String, CommandExecutor> commands = new HashMap<>();
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
Map<String, CommandExecutor> beans = applicationContext.getBeansOfType(CommandExecutor.class);
|
||||||
|
beans.values().forEach(commandExecutor -> {
|
||||||
|
log.trace("Add command \"{}\" ({})", commandExecutor.getName(), commandExecutor.getClass().getName());
|
||||||
|
if (commands.containsKey(commandExecutor.getName())) {
|
||||||
|
log.warn("Override command \"{}\"", commandExecutor.getName());
|
||||||
|
log.debug("{} -> {}",
|
||||||
|
commands.get(commandExecutor.getName()).getClass().getName(),
|
||||||
|
commandExecutor.getClass().getName()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
commands.put(commandExecutor.getName(), commandExecutor);
|
||||||
|
|
||||||
|
if (commandExecutor.getAliases().isPresent()) {
|
||||||
|
Arrays.stream(commandExecutor.getAliases().get()).forEach(aliase -> {
|
||||||
|
log.trace("Add aliase \"{}\" ({})", aliase, commandExecutor.getClass().getName());
|
||||||
|
if (commands.containsKey(aliase)) {
|
||||||
|
log.warn("Override aliase \"{}\"", aliase);
|
||||||
|
log.debug("{} -> {}",
|
||||||
|
commands.get(aliase).getClass().getName(),
|
||||||
|
commandExecutor.getClass().getName()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
commands.put(aliase, commandExecutor);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
log.debug("Load {} commands", commands.size());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void process(Player player, String message) {
|
public void process(Player player, String message) {
|
||||||
@@ -31,7 +70,7 @@ public class CommanderChatProcessor extends SimpleChatProcessor {
|
|||||||
String command = message.substring(1, idx).toLowerCase();
|
String command = message.substring(1, idx).toLowerCase();
|
||||||
if (commands.containsKey(command)) {
|
if (commands.containsKey(command)) {
|
||||||
String[] args = message.substring(idx).split(" ");
|
String[] args = message.substring(idx).split(" ");
|
||||||
commands.get(command).execute(player, command, args);
|
commands.get(command).execute(player, args);
|
||||||
} else {
|
} else {
|
||||||
player.getChannel().sendChatMessage(String.format(UNKNOW_COMMAND_MSG, command));
|
player.getChannel().sendChatMessage(String.format(UNKNOW_COMMAND_MSG, command));
|
||||||
}
|
}
|
||||||
@@ -39,4 +78,8 @@ public class CommanderChatProcessor extends SimpleChatProcessor {
|
|||||||
super.process(player, message);
|
super.process(player, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Collection<CommandExecutor> getAllCommands() {
|
||||||
|
return commands.values();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,4 +4,4 @@ include('core') // Core
|
|||||||
include('proto125') // Protocol 1.2.5
|
include('proto125') // Protocol 1.2.5
|
||||||
include('proto125_netty') // Protocol 1.2.5 (Netty impl.)
|
include('proto125_netty') // Protocol 1.2.5 (Netty impl.)
|
||||||
include('flat_world')
|
include('flat_world')
|
||||||
include('commander')
|
include('vanilla_commands')
|
||||||
|
|||||||
62
vanilla_commands/src/main/java/mc/commands/HelpCommand.java
Normal file
62
vanilla_commands/src/main/java/mc/commands/HelpCommand.java
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2018-05-23
|
||||||
|
*/
|
||||||
|
package mc.commands;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import mc.core.chat.ChatStyle;
|
||||||
|
import mc.core.chat.CommandExecutor;
|
||||||
|
import mc.core.chat.CommanderChatProcessor;
|
||||||
|
import mc.core.player.Player;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class HelpCommand implements CommandExecutor {
|
||||||
|
@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(ChatStyle.RED + "!!-Server error-!!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final String messageFormat = ChatStyle.RED + "%s " + ChatStyle.GRAY + "- " + ChatStyle.WHITE + "%s";
|
||||||
|
commanderChatProcessor.getAllCommands().forEach(commandExecutor -> {
|
||||||
|
sender.getChannel().sendChatMessage(String.format(messageFormat,
|
||||||
|
commandExecutor.getUsage().orElse(commandExecutor.getName()),
|
||||||
|
commandExecutor.getDescription()
|
||||||
|
));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
49
vanilla_commands/src/main/java/mc/commands/ListCommand.java
Normal file
49
vanilla_commands/src/main/java/mc/commands/ListCommand.java
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2018-05-23
|
||||||
|
*/
|
||||||
|
package mc.commands;
|
||||||
|
|
||||||
|
import mc.core.chat.ChatStyle;
|
||||||
|
import mc.core.chat.CommandExecutor;
|
||||||
|
import mc.core.player.Player;
|
||||||
|
import mc.core.player.PlayerManager;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
|
public class ListCommand implements CommandExecutor {
|
||||||
|
@Autowired
|
||||||
|
private PlayerManager playerManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "list";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<String[]> getAliases() {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<String> getUsage() {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "lists all currently connected players";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(Player sender, String... args) {
|
||||||
|
StringJoiner sj = new StringJoiner(", ");
|
||||||
|
playerManager.getPlayers().forEach(pl -> sj.add(pl.getName()));
|
||||||
|
|
||||||
|
sender.getChannel().sendChatMessage(
|
||||||
|
ChatStyle.GREEN + "Online(" + playerManager.getCountOnlinePlayers() + "): "
|
||||||
|
+ ChatStyle.DARK_GREEN + sj.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user