diff --git a/patches/minecraft/net/minecraft/client/multiplayer/ClientPacketListener.java.patch b/patches/minecraft/net/minecraft/client/multiplayer/ClientPacketListener.java.patch index f5492b2a76..533255383b 100644 --- a/patches/minecraft/net/minecraft/client/multiplayer/ClientPacketListener.java.patch +++ b/patches/minecraft/net/minecraft/client/multiplayer/ClientPacketListener.java.patch @@ -71,3 +71,11 @@ SignableCommand signablecommand = SignableCommand.of(this.commands.parse(p_250092_, this.suggestionsProvider)); if (signablecommand.arguments().isEmpty()) { this.send(new ServerboundChatCommandPacket(p_250092_)); +@@ -2641,6 +_,7 @@ + } + + public void sendUnattendedCommand(String p_407213_, @Nullable Screen p_406262_) { ++ if (net.minecraftforge.client.ClientCommandHandler.runCommand(p_407213_)) return; + switch (this.verifyCommand(p_407213_)) { + case NO_ISSUES: + this.send(new ServerboundChatCommandPacket(p_407213_)); diff --git a/src/test/generated/client_command/data/forge/test_instance/client_command/test_command.json b/src/test/generated/client_command/data/forge/test_instance/client_command/test_command.json new file mode 100644 index 0000000000..48341653cc --- /dev/null +++ b/src/test/generated/client_command/data/forge/test_instance/client_command/test_command.json @@ -0,0 +1,7 @@ +{ + "type": "minecraft:function", + "environment": "minecraft:default", + "function": "forge:client_command/test_command", + "max_ticks": 100, + "structure": "forge:empty3x3x3" +} \ No newline at end of file diff --git a/src/test/generated/client_command_test/data/forge/test_instance/client_command_test/client_command_test.json b/src/test/generated/client_command_test/data/forge/test_instance/client_command_test/client_command_test.json new file mode 100644 index 0000000000..31fe155def --- /dev/null +++ b/src/test/generated/client_command_test/data/forge/test_instance/client_command_test/client_command_test.json @@ -0,0 +1,7 @@ +{ + "type": "minecraft:function", + "environment": "minecraft:default", + "function": "forge:client_command_test/client_command_test", + "max_ticks": 100, + "structure": "forge:empty3x3x3" +} \ No newline at end of file diff --git a/src/test/java/net/minecraftforge/debug/client/ClientCommandTest.java b/src/test/java/net/minecraftforge/debug/client/ClientCommandTest.java new file mode 100644 index 0000000000..5c60d8b78d --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/client/ClientCommandTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.debug.client; + +import net.minecraft.client.Minecraft; +import net.minecraft.commands.Commands; +import net.minecraft.gametest.framework.GameTestHelper; +import net.minecraftforge.client.event.RegisterClientCommandsEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.gametest.GameTest; +import net.minecraftforge.gametest.GameTestNamespace; +import net.minecraftforge.test.BaseTestMod; + +import java.util.Random; + +import com.mojang.brigadier.arguments.IntegerArgumentType; + +@GameTestNamespace("forge") +@Mod(ClientCommandTest.MODID) +public class ClientCommandTest extends BaseTestMod { + public static final String MODID = "client_command"; + private static final String COMMAND = "testClientCommand"; + private static int LAST_COMMAND = -1; + + public ClientCommandTest(FMLJavaModLoadingContext context) { + super(context, false, false); + RegisterClientCommandsEvent.BUS.addListener(this::addCommand); + } + + private void addCommand(RegisterClientCommandsEvent event) { + event.getDispatcher() + .register(Commands.literal(COMMAND) + .then(Commands.argument("id", IntegerArgumentType.integer()) + .executes(ctx -> LAST_COMMAND = IntegerArgumentType.getInteger(ctx, "id")) + ) + ); + } + + @GameTest + public static void testCommand(GameTestHelper helper) throws IllegalAccessException { + int random = new Random().nextInt(); + Minecraft.getInstance().getConnection().sendUnattendedCommand(COMMAND + ' ' + random, null); + helper.assertTrue(LAST_COMMAND == random, "Client command did not execute"); + helper.succeed(); + } +} diff --git a/src/test/resources/client_command_test/META-INF/mods.toml b/src/test/resources/client_command_test/META-INF/mods.toml new file mode 100644 index 0000000000..c361869e8e --- /dev/null +++ b/src/test/resources/client_command_test/META-INF/mods.toml @@ -0,0 +1 @@ +clientSideOnly=true \ No newline at end of file