fabric: Begin adding support for some wrapped vanilla arguments

This commit is contained in:
Zach Levis 2021-01-03 14:25:41 -08:00 committed by Jason
parent 62caa2d641
commit 3be50956cc
13 changed files with 453 additions and 27 deletions

View file

@ -24,16 +24,34 @@
package cloud.commandframework.fabric.testmod;
import cloud.commandframework.Command;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.standard.IntegerArgument;
import cloud.commandframework.arguments.standard.StringArgument;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.fabric.FabricCommandManager;
import cloud.commandframework.meta.CommandMeta;
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonWriter;
import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.ArgumentTypes;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TextColor;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
public final class FabricExample implements ModInitializer {
private static final CommandArgument<ServerCommandSource, String> NAME = StringArgument.of("name");
private static final CommandArgument<ServerCommandSource, Integer> HUGS = IntegerArgument.<ServerCommandSource>newBuilder("hugs")
@ -47,7 +65,9 @@ public final class FabricExample implements ModInitializer {
final FabricCommandManager<ServerCommandSource> manager =
FabricCommandManager.createNative(CommandExecutionCoordinator.simpleCoordinator());
manager.command(manager.commandBuilder("cloudtest")
final Command.Builder<ServerCommandSource> base = manager.commandBuilder("cloudtest");
manager.command(base
.argument(NAME)
.argument(HUGS)
.handler(ctx -> {
@ -63,6 +83,37 @@ public final class FabricExample implements ModInitializer {
.styled(style -> style.withBold(true)), false);
}));
manager.command(base.literal("dump")
.meta(CommandMeta.DESCRIPTION, "Dump the client's Brigadier command tree (integrated server only)")
.meta(FabricCommandManager.META_REGISTRATION_ENVIRONMENT, CommandManager.RegistrationEnvironment.INTEGRATED)
.handler(ctx -> {
final Path target =
FabricLoader.getInstance().getGameDir().resolve(
"cloud-dump-"
+ Instant.now().toString().replace(':', '-')
+ ".json"
);
ctx.getSender().sendFeedback(new LiteralText("Dumping command output to ")
.append(new LiteralText(target.toString())
.styled(s -> s.withClickEvent(new ClickEvent(
ClickEvent.Action.OPEN_FILE,
target.toAbsolutePath().toString()
)))), false);
try (BufferedWriter writer = Files.newBufferedWriter(target); JsonWriter json = new JsonWriter(writer)) {
final CommandDispatcher<CommandSource> dispatcher = MinecraftClient.getInstance()
.getNetworkHandler()
.getCommandDispatcher();
final JsonObject object = ArgumentTypes.toJson(dispatcher, dispatcher.getRoot());
json.setIndent(" ");
Streams.write(object, json);
} catch (final IOException ex) {
ctx.getSender().sendError(new LiteralText("Unable to write file, see console for details: " + ex.getMessage()));
}
}));
}
}

View file

@ -5,7 +5,7 @@
"name": "Cloud Test mod",
"description": "Command framework and dispatcher for the JVM",
"authors": [ "Alexander Söderberg" ],
"authors": [ "Citomonstret", "zml" ],
"contact": {
"homepage": "https://commandframework.cloud/",
"sources": "https://github.com/Incendo/cloud"