move Commander
This commit is contained in:
11
core/src/main/java/mc/core/chat/CommandExecutor.java
Normal file
11
core/src/main/java/mc/core/chat/CommandExecutor.java
Normal 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);
|
||||
}
|
||||
42
core/src/main/java/mc/core/chat/CommanderChatProcessor.java
Normal file
42
core/src/main/java/mc/core/chat/CommanderChatProcessor.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user