Archived
0

Command executor

This commit is contained in:
2018-05-22 23:51:50 +03:00
parent 778d8349d7
commit d187a3a431
4 changed files with 63 additions and 0 deletions

7
commander/build.gradle Normal file
View File

@@ -0,0 +1,7 @@
group 'mc'
version '1.0-SNAPSHOT'
dependencies {
/* Core */
compile_excludeCopy project(':core')
}

View 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);
}

View 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);
}
}
}

View File

@@ -4,3 +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')