Search in sources :

Example 1 with LiteralArgumentBuilder

use of com.mojang.brigadier.builder.LiteralArgumentBuilder in project MinecraftForge by MinecraftForge.

the class ClientCommandTest method init.

private void init(RegisterClientCommandsEvent event) {
    event.getDispatcher().register(Commands.literal("clientcommandtest").then(Commands.literal("rawsuggest").then(Commands.argument("block", ResourceLocationArgument.id()).suggests((c, b) -> SharedSuggestionProvider.suggestResource(ForgeRegistries.BLOCKS.getKeys(), b)).executes(this::testCommand))).then(Commands.literal("registeredsuggest").then(Commands.argument("block", ResourceLocationArgument.id()).suggests(SuggestionProviders.AVAILABLE_BIOMES).executes(this::testCommand))).then(Commands.literal("server").executes((context) -> {
        context.getSource().getServer();
        context.getSource().sendSuccess(new TextComponent("Successfully called getServer should have errored"), false);
        return 1;
    })).then(Commands.literal("level").executes((context) -> {
        context.getSource().getLevel();
        context.getSource().sendSuccess(new TextComponent("Successfully called getLevel should have errored"), false);
        return 1;
    })).then(Commands.literal("get_objective").then(Commands.argument("objective", ObjectiveArgument.objective()).executes((context) -> {
        context.getSource().sendSuccess(new TextComponent("Regular: ").append(ObjectiveArgument.getObjective(context, "objective").getFormattedDisplayName()), false);
        return 1;
    }))).then(Commands.literal("get_advancement").then(Commands.argument("advancement", ResourceLocationArgument.id()).executes((context) -> {
        context.getSource().sendSuccess(ResourceLocationArgument.getAdvancement(context, "advancement").getChatComponent(), false);
        return 1;
    }))).then(Commands.literal("get_recipe").then(Commands.argument("recipe", ResourceLocationArgument.id()).executes((context) -> {
        context.getSource().sendSuccess(ResourceLocationArgument.getRecipe(context, "recipe").getResultItem().getDisplayName(), false);
        return 1;
    }))).then(Commands.literal("get_team").then(Commands.argument("team", TeamArgument.team()).executes((context) -> {
        context.getSource().sendSuccess(TeamArgument.getTeam(context, "team").getFormattedDisplayName(), false);
        return 1;
    }))).then(Commands.literal("get_loaded_blockpos").then(Commands.argument("blockpos", BlockPosArgument.blockPos()).executes((context) -> {
        context.getSource().sendSuccess(new TextComponent(BlockPosArgument.getLoadedBlockPos(context, "blockpos").toString()), false);
        return 1;
    }))).then(Commands.literal("requires").requires((source) -> false).executes((context) -> {
        context.getSource().sendSuccess(new TextComponent("Executed command"), false);
        return 1;
    })));
    // Used for testing that client command redirects can only be used with client commands
    LiteralArgumentBuilder<CommandSourceStack> fork = Commands.literal("clientcommandfork");
    fork.fork(event.getDispatcher().getRoot(), (context) -> List.of(context.getSource(), context.getSource())).executes((context) -> {
        context.getSource().sendSuccess(new TextComponent("Executing forked command"), false);
        return 1;
    });
    event.getDispatcher().register(fork);
}
Also used : RegisterClientCommandsEvent(net.minecraftforge.client.event.RegisterClientCommandsEvent) SharedSuggestionProvider(net.minecraft.commands.SharedSuggestionProvider) CommandContext(com.mojang.brigadier.context.CommandContext) CommandSourceStack(net.minecraft.commands.CommandSourceStack) Commands(net.minecraft.commands.Commands) TeamArgument(net.minecraft.commands.arguments.TeamArgument) LiteralArgumentBuilder(com.mojang.brigadier.builder.LiteralArgumentBuilder) ResourceLocationArgument(net.minecraft.commands.arguments.ResourceLocationArgument) ObjectiveArgument(net.minecraft.commands.arguments.ObjectiveArgument) TextComponent(net.minecraft.network.chat.TextComponent) MinecraftForge(net.minecraftforge.common.MinecraftForge) List(java.util.List) SuggestionProviders(net.minecraft.commands.synchronization.SuggestionProviders) BlockPosArgument(net.minecraft.commands.arguments.coordinates.BlockPosArgument) Mod(net.minecraftforge.fml.common.Mod) ForgeRegistries(net.minecraftforge.registries.ForgeRegistries) TextComponent(net.minecraft.network.chat.TextComponent) CommandSourceStack(net.minecraft.commands.CommandSourceStack)

Aggregations

LiteralArgumentBuilder (com.mojang.brigadier.builder.LiteralArgumentBuilder)1 CommandContext (com.mojang.brigadier.context.CommandContext)1 List (java.util.List)1 CommandSourceStack (net.minecraft.commands.CommandSourceStack)1 Commands (net.minecraft.commands.Commands)1 SharedSuggestionProvider (net.minecraft.commands.SharedSuggestionProvider)1 ObjectiveArgument (net.minecraft.commands.arguments.ObjectiveArgument)1 ResourceLocationArgument (net.minecraft.commands.arguments.ResourceLocationArgument)1 TeamArgument (net.minecraft.commands.arguments.TeamArgument)1 BlockPosArgument (net.minecraft.commands.arguments.coordinates.BlockPosArgument)1 SuggestionProviders (net.minecraft.commands.synchronization.SuggestionProviders)1 TextComponent (net.minecraft.network.chat.TextComponent)1 RegisterClientCommandsEvent (net.minecraftforge.client.event.RegisterClientCommandsEvent)1 MinecraftForge (net.minecraftforge.common.MinecraftForge)1 Mod (net.minecraftforge.fml.common.Mod)1 ForgeRegistries (net.minecraftforge.registries.ForgeRegistries)1