Archived
0

move Commander

This commit is contained in:
2018-05-23 01:20:19 +03:00
parent d187a3a431
commit ca1d3914c8
2 changed files with 4 additions and 6 deletions

View File

@@ -0,0 +1,11 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-05-22
*/
package mc.core.chat;
import mc.core.player.Player;
public interface CommandExecutor {
void execute(Player sender, String command, String... args);
}

View File

@@ -0,0 +1,42 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-05-23
*/
package mc.core.chat;
import lombok.extern.slf4j.Slf4j;
import mc.core.player.Player;
import org.slf4j.Marker;
import org.slf4j.helpers.BasicMarkerFactory;
import java.util.Collections;
import java.util.Map;
@Slf4j
public class CommanderChatProcessor extends SimpleChatProcessor {
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 Map<String, CommandExecutor> commands = Collections.emptyMap();
@Override
public void process(Player player, String message) {
if (message.startsWith("/")) {
log.info(COMMAND_MARKER, "<{}> {}", player.getName(), message);
int idx = message.indexOf(' ');
if (idx == -1) {
idx = message.length();
}
String command = message.substring(1, idx).toLowerCase();
if (commands.containsKey(command)) {
String[] args = message.substring(idx).split(" ");
commands.get(command).execute(player, command, args);
} else {
player.getChannel().sendChatMessage(String.format(UNKNOW_COMMAND_MSG, command));
}
} else {
super.process(player, message);
}
}
}