Command executor
This commit is contained in:
7
commander/build.gradle
Normal file
7
commander/build.gradle
Normal file
@@ -0,0 +1,7 @@
|
||||
group 'mc'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
dependencies {
|
||||
/* Core */
|
||||
compile_excludeCopy project(':core')
|
||||
}
|
||||
11
commander/src/main/java/mc/commander/CommandExecutor.java
Normal file
11
commander/src/main/java/mc/commander/CommandExecutor.java
Normal file
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-22
|
||||
*/
|
||||
package mc.commander;
|
||||
|
||||
import mc.core.player.Player;
|
||||
|
||||
public interface CommandExecutor {
|
||||
void execute(Player sender, String command, String... args);
|
||||
}
|
||||
44
commander/src/main/java/mc/commander/Commander.java
Normal file
44
commander/src/main/java/mc/commander/Commander.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-22
|
||||
*/
|
||||
package mc.commander;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.chat.ChatStyle;
|
||||
import mc.core.chat.SimpleChatProcessor;
|
||||
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 Commander 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,3 +4,4 @@ include('core') // Core
|
||||
include('proto125') // Protocol 1.2.5
|
||||
include('proto125_netty') // Protocol 1.2.5 (Netty impl.)
|
||||
include('flat_world')
|
||||
include('commander')
|
||||
|
||||
Reference in New Issue
Block a user