Path: blob/1.20.x/src/test_old/java/net/minecraftforge/debug/chat/ClientCommandTest.java
6513 views
/*1* Copyright (c) Forge Development LLC and contributors2* SPDX-License-Identifier: LGPL-2.1-only3*/45package net.minecraftforge.debug.chat;67import com.mojang.brigadier.builder.LiteralArgumentBuilder;8import com.mojang.brigadier.context.CommandContext;9import net.minecraft.commands.CommandSourceStack;10import net.minecraft.commands.Commands;11import net.minecraft.commands.SharedSuggestionProvider;12import net.minecraft.commands.arguments.ObjectiveArgument;13import net.minecraft.commands.arguments.ResourceLocationArgument;14import net.minecraft.commands.arguments.TeamArgument;15import net.minecraft.commands.arguments.coordinates.BlockPosArgument;16import net.minecraft.commands.synchronization.SuggestionProviders;17import net.minecraft.network.chat.Component;18import net.minecraftforge.debug.client.TestScreen;19import net.minecraftforge.client.event.RegisterClientCommandsEvent;20import net.minecraftforge.common.MinecraftForge;21import net.minecraftforge.fml.common.Mod;22import net.minecraftforge.registries.ForgeRegistries;2324import java.util.List;2526@Mod("client_command_test")27public class ClientCommandTest28{29public ClientCommandTest()30{31MinecraftForge.EVENT_BUS.addListener(this::init);32}3334private void init(RegisterClientCommandsEvent event)35{36event.getDispatcher().register(37Commands.literal("clientcommandtest")38// Used for checking suggestion providers that aren't registered39.then(Commands.literal("rawsuggest")40.then(Commands.argument("block", ResourceLocationArgument.id())41.suggests((c, b) -> SharedSuggestionProvider.suggestResource(ForgeRegistries.BLOCKS.getKeys(), b))42.executes(this::testCommand)))43// Used for checking suggestion providers that are registered44.then(Commands.literal("registeredsuggest").then(45Commands.argument("block", ResourceLocationArgument.id())46.suggests(SuggestionProviders.ALL_RECIPES)47.executes(this::testCommand)))48// Used for checking if attempting to get the server on the client side errors49.then(Commands.literal("server")50.executes((context) -> {51context.getSource().getServer();52context.getSource().sendSuccess(() ->Component.literal("Successfully called getServer should have errored"), false);53return 1;54}))55// Used for checking if attempting to get the server level on the client side errors56.then(Commands.literal("level")57.executes((context) -> {58context.getSource().getLevel();59context.getSource().sendSuccess(() ->Component.literal("Successfully called getLevel should have errored"), false);60return 1;61}))62// Used for checking if getting a known objective argument works on the client side63.then(Commands.literal("get_objective")64.then(Commands.argument("objective", ObjectiveArgument.objective())65.executes((context) -> {66final Component msg = Component.literal("Regular: ")67.append(ObjectiveArgument.getObjective(context, "objective").getFormattedDisplayName());68context.getSource().sendSuccess(() -> msg, false);69return 1;70})))71// Used for checking if getting a known advancement works on the client side72.then(Commands.literal("get_advancement")73.then(Commands.argument("advancement", ResourceLocationArgument.id())74.executes((context) -> {75final Component msg = ResourceLocationArgument.getAdvancement(context, "advancement").value().name().get();76context.getSource().sendSuccess(() -> msg, false);77return 1;78})))79// Used for checking if getting a known recipe works on the client side80.then(Commands.literal("get_recipe")81.then(Commands.argument("recipe", ResourceLocationArgument.id())82.executes((context) -> {83final Component msg = ResourceLocationArgument.getRecipe(context, "recipe").value().getResultItem(context.getSource().registryAccess()).getDisplayName();84context.getSource()85.sendSuccess(() -> msg, false);86return 1;87})))88// Used for checking if getting a team works on the client side89.then(Commands.literal("get_team")90.then(Commands.argument("team", TeamArgument.team())91.executes((context) -> {92final Component msg = TeamArgument.getTeam(context, "team").getFormattedDisplayName();93context.getSource().sendSuccess(() -> msg, false);94return 1;95})))96// Used for checking if a block position is valid works on the client side97.then(Commands.literal("get_loaded_blockpos")98.then(Commands.argument("blockpos", BlockPosArgument.blockPos())99.executes((context) -> {100final Component msg = Component.literal(BlockPosArgument.getLoadedBlockPos(context, "blockpos").toString());101context.getSource()102.sendSuccess(() -> msg, false);103return 1;104})))105// Used for checking if a command can have a requirement106.then(Commands.literal("requires")107.requires((source) -> false)108.executes((context) -> {109context.getSource().sendSuccess(() ->Component.literal("Executed command"), false);110return 1;111}))112// Used for testing the screen after using commands113.then(Commands.literal("screentest")114.executes((stack) -> TestScreen.open())));115116// Used for testing that client command redirects can only be used with client commands117LiteralArgumentBuilder<CommandSourceStack> fork = Commands.literal("clientcommandfork");118fork.fork(event.getDispatcher().getRoot(), (context) -> List.of(context.getSource(), context.getSource()))119.executes((context) -> {120context.getSource().sendSuccess(() ->Component.literal("Executing forked command"), false);121return 1;122});123event.getDispatcher().register(fork);124}125126private int testCommand(CommandContext<CommandSourceStack> context)127{128context.getSource().sendSuccess(() ->Component.literal("Input: " + ResourceLocationArgument.getId(context, "block")), false);129context.getSource().sendSuccess(() ->Component.literal("Teams: " + context.getSource().getAllTeams()), false);130context.getSource().sendSuccess(() ->Component.literal("Players: " + context.getSource().getOnlinePlayerNames()), false);131context.getSource().sendSuccess(() ->Component.literal("First recipe: " + context.getSource().getRecipeNames().findFirst().get()), false);132context.getSource().sendSuccess(() ->Component.literal("Levels: " + context.getSource().levels()), false);133context.getSource().sendSuccess(() ->Component.literal("Registry Access: " + context.getSource().registryAccess()), false);134return 0;135}136}137138139