mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-21 07:31:47 +01:00
commit
b50c2831bf
@ -1,10 +1,7 @@
|
|||||||
package net.minestom.server.command.builder.arguments;
|
package net.minestom.server.command.builder.arguments;
|
||||||
|
|
||||||
import com.google.common.annotations.Beta;
|
import com.google.common.annotations.Beta;
|
||||||
import net.minestom.server.command.builder.ArgumentCallback;
|
import net.minestom.server.command.builder.*;
|
||||||
import net.minestom.server.command.builder.Command;
|
|
||||||
import net.minestom.server.command.builder.CommandExecutor;
|
|
||||||
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.command.builder.suggestion.SuggestionCallback;
|
import net.minestom.server.command.builder.suggestion.SuggestionCallback;
|
||||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
||||||
@ -220,6 +217,19 @@ public abstract class Argument<T> {
|
|||||||
return suggestionCallback != null;
|
return suggestionCallback != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps this argument's output to another result.
|
||||||
|
*
|
||||||
|
* @param mapper The mapper to use (this argument's input --> desired output)
|
||||||
|
* @param <O> The type of output expected.
|
||||||
|
*
|
||||||
|
* @return A new ArgumentMap that can get this complex object type.
|
||||||
|
*/
|
||||||
|
@Beta
|
||||||
|
public <O> ArgumentMap<T, O> map(ArgumentMap.ArgumentMapper<T, O> mapper) {
|
||||||
|
return new ArgumentMap<>(this, mapper);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
|
@ -0,0 +1,58 @@
|
|||||||
|
package net.minestom.server.command.builder.arguments;
|
||||||
|
|
||||||
|
import net.minestom.server.command.builder.NodeMaker;
|
||||||
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an argument that maps an existing argument to a value.
|
||||||
|
*
|
||||||
|
* @param <I> The input (any object)
|
||||||
|
* @param <O> The output (any object)
|
||||||
|
*/
|
||||||
|
public class ArgumentMap<I, O> extends Argument<O> {
|
||||||
|
|
||||||
|
final Argument<I> argument;
|
||||||
|
final ArgumentMapper<I, O> mapper;
|
||||||
|
|
||||||
|
protected ArgumentMap(Argument<I> argument, ArgumentMapper<I, O> mapper) {
|
||||||
|
super(argument.getId());
|
||||||
|
|
||||||
|
this.argument = argument;
|
||||||
|
this.mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull O parse(@NotNull String input) throws ArgumentSyntaxException {
|
||||||
|
return mapper.accept(argument.parse(input));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
|
||||||
|
argument.processNodes(nodeMaker, executable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a lambda that can turn an input into an output
|
||||||
|
* that also allows the throwing of ArgumentSyntaxException
|
||||||
|
*
|
||||||
|
* @param <I> The input expected from the Argument
|
||||||
|
* @param <O> The desired output type from this lambda.
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface ArgumentMapper<I, O> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accept's I data from the argument and returns O output
|
||||||
|
*
|
||||||
|
* @param i The input processed from an argument
|
||||||
|
*
|
||||||
|
* @return The complex data type that came as a result from this argument
|
||||||
|
* @throws ArgumentSyntaxException If the input can not be turned into the desired output
|
||||||
|
* (E.X. an invalid extension name)
|
||||||
|
*/
|
||||||
|
O accept(I i) throws ArgumentSyntaxException;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -5,7 +5,7 @@ import net.minestom.server.MinecraftServer;
|
|||||||
import net.minestom.server.command.CommandSender;
|
import net.minestom.server.command.CommandSender;
|
||||||
import net.minestom.server.command.builder.Command;
|
import net.minestom.server.command.builder.Command;
|
||||||
import net.minestom.server.command.builder.CommandContext;
|
import net.minestom.server.command.builder.CommandContext;
|
||||||
import net.minestom.server.command.builder.arguments.ArgumentString;
|
import net.minestom.server.command.builder.arguments.ArgumentMap;
|
||||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
import net.minestom.server.command.builder.arguments.ArgumentType;
|
||||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.extensions.Extension;
|
import net.minestom.server.extensions.Extension;
|
||||||
@ -18,14 +18,20 @@ import java.nio.charset.StandardCharsets;
|
|||||||
|
|
||||||
public class UnloadExtensionCommand extends Command {
|
public class UnloadExtensionCommand extends Command {
|
||||||
|
|
||||||
private final ArgumentString extensionName;
|
private final ArgumentMap<String, Extension> extensionName;
|
||||||
|
|
||||||
public UnloadExtensionCommand() {
|
public UnloadExtensionCommand() {
|
||||||
super("unload");
|
super("unload");
|
||||||
|
|
||||||
setDefaultExecutor(this::usage);
|
setDefaultExecutor(this::usage);
|
||||||
|
|
||||||
extensionName = ArgumentType.String("extensionName");
|
extensionName = ArgumentType.String("extensionName").map((input) -> {
|
||||||
|
Extension extension = MinecraftServer.getExtensionManager().getExtension(input);
|
||||||
|
|
||||||
|
if (extension == null) throw new ArgumentSyntaxException("The specified extension was not found", input, 1);
|
||||||
|
|
||||||
|
return extension;
|
||||||
|
});
|
||||||
|
|
||||||
setArgumentCallback(this::extensionCallback, extensionName);
|
setArgumentCallback(this::extensionCallback, extensionName);
|
||||||
|
|
||||||
@ -37,29 +43,25 @@ public class UnloadExtensionCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void execute(CommandSender sender, CommandContext context) {
|
private void execute(CommandSender sender, CommandContext context) {
|
||||||
final String name = context.get(extensionName);
|
final Extension ext = context.get(extensionName);
|
||||||
sender.sendMessage(Component.text("extensionName = " + name + "...."));
|
sender.sendMessage(Component.text("extensionName = " + ext.getOrigin().getName() + "...."));
|
||||||
|
|
||||||
ExtensionManager extensionManager = MinecraftServer.getExtensionManager();
|
ExtensionManager extensionManager = MinecraftServer.getExtensionManager();
|
||||||
Extension ext = extensionManager.getExtension(name);
|
|
||||||
if (ext != null) {
|
try {
|
||||||
|
extensionManager.unloadExtension(ext.getOrigin().getName());
|
||||||
|
} catch (Throwable t) {
|
||||||
try {
|
try {
|
||||||
extensionManager.unloadExtension(name);
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
} catch (Throwable t) {
|
t.printStackTrace();
|
||||||
try {
|
t.printStackTrace(new PrintStream(baos));
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
baos.flush();
|
||||||
t.printStackTrace();
|
baos.close();
|
||||||
t.printStackTrace(new PrintStream(baos));
|
String contents = baos.toString(StandardCharsets.UTF_8);
|
||||||
baos.flush();
|
contents.lines().map(Component::text).forEach(sender::sendMessage);
|
||||||
baos.close();
|
} catch (IOException e) {
|
||||||
String contents = baos.toString(StandardCharsets.UTF_8);
|
e.printStackTrace();
|
||||||
contents.lines().map(Component::text).forEach(sender::sendMessage);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
sender.sendMessage(Component.text("Extension '" + name + "' does not exist."));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user