mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-09 18:08:37 +01:00
Cleanup (moved some packet methods to DeclareCommandsPacket) and added CommandResult#getInput
This commit is contained in:
parent
1a2fd4f743
commit
053ef06ee1
@ -157,7 +157,7 @@ public final class CommandManager {
|
|||||||
player.callEvent(PlayerCommandEvent.class, playerCommandEvent);
|
player.callEvent(PlayerCommandEvent.class, playerCommandEvent);
|
||||||
|
|
||||||
if (playerCommandEvent.isCancelled())
|
if (playerCommandEvent.isCancelled())
|
||||||
return CommandResult.withType(CommandResult.Type.CANCELLED);
|
return CommandResult.of(CommandResult.Type.CANCELLED, command);
|
||||||
|
|
||||||
command = playerCommandEvent.getCommand();
|
command = playerCommandEvent.getCommand();
|
||||||
}
|
}
|
||||||
@ -178,13 +178,13 @@ public final class CommandManager {
|
|||||||
if (unknownCommandCallback != null) {
|
if (unknownCommandCallback != null) {
|
||||||
this.unknownCommandCallback.apply(sender, command);
|
this.unknownCommandCallback.apply(sender, command);
|
||||||
}
|
}
|
||||||
return CommandResult.withType(CommandResult.Type.CANCELLED);
|
return CommandResult.of(CommandResult.Type.CANCELLED, command);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute the legacy-command
|
// Execute the legacy-command
|
||||||
final String[] args = command.substring(command.indexOf(StringUtils.SPACE) + 1).split(StringUtils.SPACE);
|
final String[] args = command.substring(command.indexOf(StringUtils.SPACE) + 1).split(StringUtils.SPACE);
|
||||||
commandProcessor.process(sender, commandName, args);
|
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()) {
|
for (String alias : command.getAliases()) {
|
||||||
DeclareCommandsPacket.Node node = new DeclareCommandsPacket.Node();
|
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.name = alias;
|
||||||
node.redirectedNode = mainNodeIndex;
|
node.redirectedNode = mainNodeIndex;
|
||||||
nodes.add(node);
|
nodes.add(node);
|
||||||
@ -359,7 +360,8 @@ public final class CommandManager {
|
|||||||
// Server suggestion (ask_server)
|
// Server suggestion (ask_server)
|
||||||
{
|
{
|
||||||
DeclareCommandsPacket.Node tabNode = new DeclareCommandsPacket.Node();
|
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.name = tracking ? "tab_completion" : "args";
|
||||||
tabNode.parser = "brigadier:string";
|
tabNode.parser = "brigadier:string";
|
||||||
tabNode.properties = packetWriter -> packetWriter.writeVarInt(2); // Greedy phrase
|
tabNode.properties = packetWriter -> packetWriter.writeVarInt(2); // Greedy phrase
|
||||||
@ -372,7 +374,8 @@ public final class CommandManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node();
|
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.name = name;
|
||||||
literalNode.children = new int[]{nodes.size() - 1};
|
literalNode.children = new int[]{nodes.size() - 1};
|
||||||
|
|
||||||
@ -517,37 +520,9 @@ public final class CommandManager {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private DeclareCommandsPacket.Node createMainNode(@NotNull String name, boolean executable) {
|
private DeclareCommandsPacket.Node createMainNode(@NotNull String name, boolean executable) {
|
||||||
DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node();
|
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;
|
literalNode.name = name;
|
||||||
|
|
||||||
return literalNode;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,7 @@ public class CommandDispatcher {
|
|||||||
final Command command = findCommand(commandName);
|
final Command command = findCommand(commandName);
|
||||||
// Check if the command exists
|
// Check if the command exists
|
||||||
if (command == null) {
|
if (command == null) {
|
||||||
return CommandResult.withType(CommandResult.Type.UNKNOWN);
|
return CommandResult.of(CommandResult.Type.UNKNOWN, commandName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes the command's name + the space after
|
// Removes the command's name + the space after
|
||||||
@ -94,6 +94,7 @@ public class CommandDispatcher {
|
|||||||
|
|
||||||
// Find the used syntax
|
// Find the used syntax
|
||||||
CommandResult result = new CommandResult();
|
CommandResult result = new CommandResult();
|
||||||
|
result.input = commandString;
|
||||||
ParsedCommand parsedCommand = findParsedCommand(command, args);
|
ParsedCommand parsedCommand = findParsedCommand(command, args);
|
||||||
if (parsedCommand != null && parsedCommand.executor != null) {
|
if (parsedCommand != null && parsedCommand.executor != null) {
|
||||||
// Syntax found
|
// Syntax found
|
||||||
|
@ -6,6 +6,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
public class CommandResult {
|
public class CommandResult {
|
||||||
|
|
||||||
protected Type type = Type.UNKNOWN;
|
protected Type type = Type.UNKNOWN;
|
||||||
|
protected String input;
|
||||||
protected ParsedCommand parsedCommand;
|
protected ParsedCommand parsedCommand;
|
||||||
protected CommandData commandData;
|
protected CommandData commandData;
|
||||||
|
|
||||||
@ -14,6 +15,11 @@ public class CommandResult {
|
|||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public String getInput() {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public ParsedCommand getParsedCommand() {
|
public ParsedCommand getParsedCommand() {
|
||||||
return parsedCommand;
|
return parsedCommand;
|
||||||
@ -48,9 +54,10 @@ public class CommandResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static CommandResult withType(@NotNull Type type) {
|
public static CommandResult of(@NotNull Type type, @NotNull String input) {
|
||||||
CommandResult result = new CommandResult();
|
CommandResult result = new CommandResult();
|
||||||
result.type = type;
|
result.type = type;
|
||||||
|
result.input = input;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package net.minestom.server.command.builder.arguments;
|
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.ArgumentCallback;
|
||||||
import net.minestom.server.command.builder.Command;
|
import net.minestom.server.command.builder.Command;
|
||||||
import net.minestom.server.command.builder.CommandExecutor;
|
import net.minestom.server.command.builder.CommandExecutor;
|
||||||
@ -22,8 +20,6 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
*/
|
*/
|
||||||
public abstract class Argument<T> {
|
public abstract class Argument<T> {
|
||||||
|
|
||||||
protected final static CommandManager COMMAND_MANAGER = MinecraftServer.getCommandManager();
|
|
||||||
|
|
||||||
private final String id;
|
private final String id;
|
||||||
private final boolean allowSpace;
|
private final boolean allowSpace;
|
||||||
private final boolean useRemaining;
|
private final boolean useRemaining;
|
||||||
@ -96,7 +92,7 @@ public abstract class Argument<T> {
|
|||||||
boolean executable, boolean redirect, boolean suggestion) {
|
boolean executable, boolean redirect, boolean suggestion) {
|
||||||
DeclareCommandsPacket.Node argumentNode = new DeclareCommandsPacket.Node();
|
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();
|
argumentNode.name = argument.getId();
|
||||||
|
|
||||||
return argumentNode;
|
return argumentNode;
|
||||||
|
@ -13,7 +13,7 @@ public class ArgumentCommand extends Argument<CommandResult> {
|
|||||||
public static final int INVALID_COMMAND_ERROR = 1;
|
public static final int INVALID_COMMAND_ERROR = 1;
|
||||||
|
|
||||||
public ArgumentCommand(@NotNull String id) {
|
public ArgumentCommand(@NotNull String id) {
|
||||||
super(id, true, true); // TODO don't use remaining?
|
super(id, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package net.minestom.server.command.builder.arguments;
|
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.NodeMaker;
|
||||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
||||||
@ -26,7 +25,8 @@ public class ArgumentLiteral extends Argument<String> {
|
|||||||
@Override
|
@Override
|
||||||
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
|
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
|
||||||
DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node();
|
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();
|
literalNode.name = getId();
|
||||||
|
|
||||||
nodeMaker.setCurrentNodes(new DeclareCommandsPacket.Node[]{literalNode});
|
nodeMaker.setCurrentNodes(new DeclareCommandsPacket.Node[]{literalNode});
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package net.minestom.server.command.builder.arguments;
|
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.NodeMaker;
|
||||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
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++) {
|
for (int i = 0; i < nodes.length; i++) {
|
||||||
DeclareCommandsPacket.Node argumentNode = new DeclareCommandsPacket.Node();
|
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];
|
argumentNode.name = this.getRestrictions()[i];
|
||||||
wordConsumer.accept(argumentNode);
|
wordConsumer.accept(argumentNode);
|
||||||
nodes[i] = argumentNode;
|
nodes[i] = argumentNode;
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user