Store commands as nodes and execute them
This commit is contained in:
@@ -4,25 +4,26 @@ import com.google.common.collect.Maps;
|
|||||||
import net.minecraft.commands.builder.CommandBuilder;
|
import net.minecraft.commands.builder.CommandBuilder;
|
||||||
import net.minecraft.commands.exceptions.CommandException;
|
import net.minecraft.commands.exceptions.CommandException;
|
||||||
import net.minecraft.commands.exceptions.UnknownCommandException;
|
import net.minecraft.commands.exceptions.UnknownCommandException;
|
||||||
|
import net.minecraft.commands.tree.LiteralCommandNode;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class CommandDispatcher {
|
public class CommandDispatcher {
|
||||||
private final Map<String, Runnable> commands = Maps.newHashMap();
|
private final Map<String, LiteralCommandNode> commands = Maps.newHashMap();
|
||||||
|
|
||||||
public void register(CommandBuilder command) {
|
public void register(CommandBuilder command) {
|
||||||
if (commands.containsKey(command.getName())) {
|
if (commands.containsKey(command.getName())) {
|
||||||
throw new IllegalArgumentException("New command " + command.getName() + " conflicts with existing command " + 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 {
|
public void execute(String command) throws CommandException {
|
||||||
Runnable runnable = commands.get(command);
|
LiteralCommandNode node = commands.get(command);
|
||||||
if (runnable == null) {
|
if (node == null) {
|
||||||
throw new UnknownCommandException();
|
throw new UnknownCommandException();
|
||||||
}
|
}
|
||||||
|
|
||||||
runnable.run();
|
node.getExecutor().run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class CommandBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public LiteralCommandNode build() {
|
public LiteralCommandNode build() {
|
||||||
LiteralCommandNode result = new LiteralCommandNode(getName());
|
LiteralCommandNode result = new LiteralCommandNode(getName(), getExecutor());
|
||||||
|
|
||||||
for (ArgumentBuilder argument : arguments) {
|
for (ArgumentBuilder argument : arguments) {
|
||||||
result.addChild(argument.build());
|
result.addChild(argument.build());
|
||||||
|
|||||||
@@ -2,12 +2,18 @@ package net.minecraft.commands.tree;
|
|||||||
|
|
||||||
public class LiteralCommandNode extends CommandNode {
|
public class LiteralCommandNode extends CommandNode {
|
||||||
private final String literal;
|
private final String literal;
|
||||||
|
private final Runnable executor;
|
||||||
|
|
||||||
public LiteralCommandNode(String literal) {
|
public LiteralCommandNode(String literal, Runnable executor) {
|
||||||
this.literal = literal;
|
this.literal = literal;
|
||||||
|
this.executor = executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLiteral() {
|
public String getLiteral() {
|
||||||
return literal;
|
return literal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Runnable getExecutor() {
|
||||||
|
return executor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user