mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-10 02:17:41 +01:00
Added initial command return support
This commit is contained in:
parent
8334e100cf
commit
3c7bbc9d2d
@ -27,6 +27,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
@ -45,6 +46,7 @@ public final class CommandManager {
|
||||
|
||||
private volatile boolean running = true;
|
||||
|
||||
private final ServerSender serverSender = new ServerSender();
|
||||
private final ConsoleSender consoleSender = new ConsoleSender();
|
||||
|
||||
private final CommandDispatcher dispatcher = new CommandDispatcher();
|
||||
@ -154,7 +156,8 @@ public final class CommandManager {
|
||||
* @param command the raw command string (without the command prefix)
|
||||
* @return true if the command hadn't been cancelled and has been successful
|
||||
*/
|
||||
public boolean execute(@NotNull CommandSender sender, @NotNull String command) {
|
||||
@Nullable
|
||||
public NBTCompound execute(@NotNull CommandSender sender, @NotNull String command) {
|
||||
|
||||
// Command event
|
||||
if (sender instanceof Player) {
|
||||
@ -164,7 +167,7 @@ public final class CommandManager {
|
||||
player.callEvent(PlayerCommandEvent.class, playerCommandEvent);
|
||||
|
||||
if (playerCommandEvent.isCancelled())
|
||||
return false;
|
||||
return null;
|
||||
|
||||
command = playerCommandEvent.getCommand();
|
||||
}
|
||||
@ -173,9 +176,9 @@ public final class CommandManager {
|
||||
|
||||
{
|
||||
// Check for rich-command
|
||||
final boolean result = this.dispatcher.execute(sender, command);
|
||||
if (result) {
|
||||
return true;
|
||||
final NBTCompound commandData = this.dispatcher.execute(sender, command);
|
||||
if (commandData != null) {
|
||||
return commandData;
|
||||
} else {
|
||||
// Check for legacy-command
|
||||
final String[] splitCommand = command.split(StringUtils.SPACE);
|
||||
@ -185,17 +188,28 @@ public final class CommandManager {
|
||||
if (unknownCommandCallback != null) {
|
||||
this.unknownCommandCallback.apply(sender, command);
|
||||
}
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Execute the legacy-command
|
||||
final String[] args = command.substring(command.indexOf(StringUtils.SPACE) + 1).split(StringUtils.SPACE);
|
||||
|
||||
return commandProcessor.process(sender, commandName, args);
|
||||
commandProcessor.process(sender, commandName, args);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the command using a {@link ServerSender} to do not
|
||||
* print the command messages, and rely instead on the command return data.
|
||||
*
|
||||
* @see #execute(CommandSender, String)
|
||||
*/
|
||||
@Nullable
|
||||
public NBTCompound executeServerCommand(@NotNull String command) {
|
||||
return execute(serverSender, command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the callback executed once an unknown command is run.
|
||||
*
|
||||
|
31
src/main/java/net/minestom/server/command/ServerSender.java
Normal file
31
src/main/java/net/minestom/server/command/ServerSender.java
Normal file
@ -0,0 +1,31 @@
|
||||
package net.minestom.server.command;
|
||||
|
||||
import net.minestom.server.command.builder.Arguments;
|
||||
import net.minestom.server.permission.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Sender used in {@link CommandManager#executeServerCommand(String)}.
|
||||
* <p>
|
||||
* Be aware that {@link #sendMessage(String)} is empty on purpose because the purpose
|
||||
* of this sender is to process the data of {@link Arguments#getCommandReturn()}.
|
||||
*/
|
||||
public class ServerSender implements CommandSender {
|
||||
|
||||
private final Set<Permission> permissions = Collections.unmodifiableSet(new HashSet<>());
|
||||
|
||||
@Override
|
||||
public void sendMessage(@NotNull String message) {
|
||||
// Empty on purpose
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Set<Permission> getAllPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
}
|
@ -35,6 +35,8 @@ public final class Arguments {
|
||||
|
||||
private Map<String, Object> args = new HashMap<>();
|
||||
|
||||
private NBTCompound commandReturn;
|
||||
|
||||
@NotNull
|
||||
public <T> T get(@NotNull Argument<T> argument) {
|
||||
return (T) getObject(argument.getId());
|
||||
@ -250,6 +252,15 @@ public final class Arguments {
|
||||
});
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public NBTCompound getCommandReturn() {
|
||||
return commandReturn;
|
||||
}
|
||||
|
||||
public void setCommandReturn(@Nullable NBTCompound commandReturn) {
|
||||
this.commandReturn = commandReturn;
|
||||
}
|
||||
|
||||
protected void setArg(@NotNull String id, Object value) {
|
||||
this.args.put(id, value);
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
@ -79,18 +80,19 @@ public class CommandDispatcher {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the command exists, and execute it.
|
||||
* Checks if the command exists, and execute it.
|
||||
*
|
||||
* @param source the command source
|
||||
* @param commandString the command with the argument(s)
|
||||
* @return true if the command executed successfully, false if the command doesn't exist
|
||||
* @return the command data, null if none
|
||||
*/
|
||||
public boolean execute(@NotNull CommandSender source, @NotNull String commandString) {
|
||||
@Nullable
|
||||
public NBTCompound execute(@NotNull CommandSender source, @NotNull String commandString) {
|
||||
CommandResult result = parse(commandString);
|
||||
if (result != null) {
|
||||
result.execute(source, commandString);
|
||||
return result.execute(source, commandString);
|
||||
}
|
||||
return result != null;
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@ -377,8 +379,10 @@ public class CommandDispatcher {
|
||||
*
|
||||
* @param source the command source
|
||||
* @param commandString the command string
|
||||
* @return the command data, null if none
|
||||
*/
|
||||
public void execute(@NotNull CommandSender source, @NotNull String commandString) {
|
||||
@Nullable
|
||||
public NBTCompound execute(@NotNull CommandSender source, @NotNull String commandString) {
|
||||
// Global listener
|
||||
command.globalListener(source, arguments, commandString);
|
||||
// Command condition check
|
||||
@ -386,7 +390,7 @@ public class CommandDispatcher {
|
||||
if (condition != null) {
|
||||
final boolean result = condition.canUse(source, commandString);
|
||||
if (!result)
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
// Condition is respected
|
||||
if (executor != null) {
|
||||
@ -408,6 +412,8 @@ public class CommandDispatcher {
|
||||
// Execute the faulty argument callback
|
||||
callback.apply(source, argumentSyntaxException);
|
||||
}
|
||||
|
||||
return arguments.getCommandReturn();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user