Store commands as nodes and execute them

This commit is contained in:
Nathan Adams
2014-09-18 10:58:49 +02:00
parent 4f20d02a97
commit 2b267a26f6
3 changed files with 14 additions and 7 deletions
@@ -4,25 +4,26 @@ import com.google.common.collect.Maps;
import net.minecraft.commands.builder.CommandBuilder;
import net.minecraft.commands.exceptions.CommandException;
import net.minecraft.commands.exceptions.UnknownCommandException;
import net.minecraft.commands.tree.LiteralCommandNode;
import java.util.Map;
public class CommandDispatcher {
private final Map<String, Runnable> commands = Maps.newHashMap();
private final Map<String, LiteralCommandNode> commands = Maps.newHashMap();
public void register(CommandBuilder command) {
if (commands.containsKey(command.getName())) {
throw new IllegalArgumentException("New command " + command.getName() + " conflicts with existing command " + command.getName());
}
commands.put(command.getName(), command.getExecutor());
commands.put(command.getName(), command.build());
}
public void execute(String command) throws CommandException {
Runnable runnable = commands.get(command);
if (runnable == null) {
LiteralCommandNode node = commands.get(command);
if (node == null) {
throw new UnknownCommandException();
}
runnable.run();
node.getExecutor().run();
}
}