Cleanup (moved some packet methods to DeclareCommandsPacket) and added CommandResult#getInput

This commit is contained in:
themode 2021-02-11 03:00:13 +01:00
parent 1a2fd4f743
commit 053ef06ee1
8 changed files with 54 additions and 47 deletions

View File

@ -157,7 +157,7 @@ public final class CommandManager {
player.callEvent(PlayerCommandEvent.class, playerCommandEvent);
if (playerCommandEvent.isCancelled())
return CommandResult.withType(CommandResult.Type.CANCELLED);
return CommandResult.of(CommandResult.Type.CANCELLED, command);
command = playerCommandEvent.getCommand();
}
@ -178,13 +178,13 @@ public final class CommandManager {
if (unknownCommandCallback != null) {
this.unknownCommandCallback.apply(sender, command);
}
return CommandResult.withType(CommandResult.Type.CANCELLED);
return CommandResult.of(CommandResult.Type.CANCELLED, command);
}
// Execute the legacy-command
final String[] args = command.substring(command.indexOf(StringUtils.SPACE) + 1).split(StringUtils.SPACE);
commandProcessor.process(sender, commandName, args);
return CommandResult.withType(CommandResult.Type.SUCCESS);
return CommandResult.of(CommandResult.Type.SUCCESS, command);
}
}
}
@ -328,7 +328,8 @@ public final class CommandManager {
for (String alias : command.getAliases()) {
DeclareCommandsPacket.Node node = new DeclareCommandsPacket.Node();
node.flags = getFlag(NodeType.LITERAL, false, true, false);
node.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL,
false, true, false);
node.name = alias;
node.redirectedNode = mainNodeIndex;
nodes.add(node);
@ -359,7 +360,8 @@ public final class CommandManager {
// Server suggestion (ask_server)
{
DeclareCommandsPacket.Node tabNode = new DeclareCommandsPacket.Node();
tabNode.flags = getFlag(NodeType.ARGUMENT, true, false, tracking);
tabNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.ARGUMENT,
true, false, tracking);
tabNode.name = tracking ? "tab_completion" : "args";
tabNode.parser = "brigadier:string";
tabNode.properties = packetWriter -> packetWriter.writeVarInt(2); // Greedy phrase
@ -372,7 +374,8 @@ public final class CommandManager {
}
DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node();
literalNode.flags = getFlag(NodeType.LITERAL, true, false, false);
literalNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL,
true, false, false);
literalNode.name = name;
literalNode.children = new int[]{nodes.size() - 1};
@ -517,37 +520,9 @@ public final class CommandManager {
@NotNull
private DeclareCommandsPacket.Node createMainNode(@NotNull String name, boolean executable) {
DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node();
literalNode.flags = getFlag(NodeType.LITERAL, executable, false, false);
literalNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL, executable, false, false);
literalNode.name = name;
return literalNode;
}
public byte getFlag(@NotNull NodeType type, boolean executable, boolean redirect, boolean suggestionType) {
byte result = (byte) type.mask;
if (executable) {
result |= 0x04;
}
if (redirect) {
result |= 0x08;
}
if (suggestionType) {
result |= 0x10;
}
return result;
}
public enum NodeType {
ROOT(0), LITERAL(0b1), ARGUMENT(0b10), NONE(0x11);
private final int mask;
NodeType(int mask) {
this.mask = mask;
}
}
}

View File

@ -86,7 +86,7 @@ public class CommandDispatcher {
final Command command = findCommand(commandName);
// Check if the command exists
if (command == null) {
return CommandResult.withType(CommandResult.Type.UNKNOWN);
return CommandResult.of(CommandResult.Type.UNKNOWN, commandName);
}
// Removes the command's name + the space after
@ -94,6 +94,7 @@ public class CommandDispatcher {
// Find the used syntax
CommandResult result = new CommandResult();
result.input = commandString;
ParsedCommand parsedCommand = findParsedCommand(command, args);
if (parsedCommand != null && parsedCommand.executor != null) {
// Syntax found

View File

@ -6,6 +6,7 @@ import org.jetbrains.annotations.Nullable;
public class CommandResult {
protected Type type = Type.UNKNOWN;
protected String input;
protected ParsedCommand parsedCommand;
protected CommandData commandData;
@ -14,6 +15,11 @@ public class CommandResult {
return type;
}
@NotNull
public String getInput() {
return input;
}
@Nullable
public ParsedCommand getParsedCommand() {
return parsedCommand;
@ -48,9 +54,10 @@ public class CommandResult {
}
@NotNull
public static CommandResult withType(@NotNull Type type) {
public static CommandResult of(@NotNull Type type, @NotNull String input) {
CommandResult result = new CommandResult();
result.type = type;
result.input = input;
return result;
}

View File

@ -1,7 +1,5 @@
package net.minestom.server.command.builder.arguments;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandManager;
import net.minestom.server.command.builder.ArgumentCallback;
import net.minestom.server.command.builder.Command;
import net.minestom.server.command.builder.CommandExecutor;
@ -22,8 +20,6 @@ import org.jetbrains.annotations.Nullable;
*/
public abstract class Argument<T> {
protected final static CommandManager COMMAND_MANAGER = MinecraftServer.getCommandManager();
private final String id;
private final boolean allowSpace;
private final boolean useRemaining;
@ -96,7 +92,7 @@ public abstract class Argument<T> {
boolean executable, boolean redirect, boolean suggestion) {
DeclareCommandsPacket.Node argumentNode = new DeclareCommandsPacket.Node();
argumentNode.flags = COMMAND_MANAGER.getFlag(CommandManager.NodeType.ARGUMENT, executable, redirect, suggestion);
argumentNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.ARGUMENT, executable, redirect, suggestion);
argumentNode.name = argument.getId();
return argumentNode;

View File

@ -13,7 +13,7 @@ public class ArgumentCommand extends Argument<CommandResult> {
public static final int INVALID_COMMAND_ERROR = 1;
public ArgumentCommand(@NotNull String id) {
super(id, true, true); // TODO don't use remaining?
super(id, true, true);
}
@NotNull

View File

@ -1,6 +1,5 @@
package net.minestom.server.command.builder.arguments;
import net.minestom.server.command.CommandManager;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
@ -26,7 +25,8 @@ public class ArgumentLiteral extends Argument<String> {
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node();
literalNode.flags = COMMAND_MANAGER.getFlag(CommandManager.NodeType.LITERAL, executable, false, false);
literalNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL,
executable, false, false);
literalNode.name = getId();
nodeMaker.setCurrentNodes(new DeclareCommandsPacket.Node[]{literalNode});

View File

@ -1,6 +1,5 @@
package net.minestom.server.command.builder.arguments;
import net.minestom.server.command.CommandManager;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
@ -93,7 +92,8 @@ public class ArgumentWord extends Argument<String> {
for (int i = 0; i < nodes.length; i++) {
DeclareCommandsPacket.Node argumentNode = new DeclareCommandsPacket.Node();
argumentNode.flags = COMMAND_MANAGER.getFlag(CommandManager.NodeType.LITERAL, executable, false, false);
argumentNode.flags = DeclareCommandsPacket.getFlag(DeclareCommandsPacket.NodeType.LITERAL,
executable, false, false);
argumentNode.name = this.getRestrictions()[i];
wordConsumer.accept(argumentNode);
nodes[i] = argumentNode;

View File

@ -74,4 +74,32 @@ public class DeclareCommandsPacket implements ServerPacket {
}
public static byte getFlag(@NotNull NodeType type, boolean executable, boolean redirect, boolean suggestionType) {
byte result = (byte) type.mask;
if (executable) {
result |= 0x04;
}
if (redirect) {
result |= 0x08;
}
if (suggestionType) {
result |= 0x10;
}
return result;
}
public enum NodeType {
ROOT(0), LITERAL(0b1), ARGUMENT(0b10), NONE(0x11);
private final int mask;
NodeType(int mask) {
this.mask = mask;
}
}
}