Fix incorrect behavior with ClientMessageEvents and CommandApi (#3182)

This commit is contained in:
Kevin 2023-07-18 07:54:01 -04:00 committed by GitHub
parent 2e061fd481
commit 32651619ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View file

@ -40,7 +40,7 @@ public abstract class ClientPlayNetworkHandlerMixin {
}
}
@ModifyVariable(method = "sendChatMessage", at = @At(value = "LOAD", ordinal = 0), ordinal = 0, argsOnly = true)
@ModifyVariable(method = "sendChatMessage", at = @At("HEAD"), ordinal = 0, argsOnly = true)
private String fabric_modifySendChatMessage(String content) {
content = ClientSendMessageEvents.MODIFY_CHAT.invoker().modifySendChatMessage(content);
ClientSendMessageEvents.CHAT.invoker().onSendChatMessage(content);
@ -55,7 +55,7 @@ public abstract class ClientPlayNetworkHandlerMixin {
}
}
@ModifyVariable(method = "sendChatCommand", at = @At(value = "LOAD", ordinal = 0), ordinal = 0, argsOnly = true)
@ModifyVariable(method = "sendChatCommand", at = @At("HEAD"), ordinal = 0, argsOnly = true)
private String fabric_modifySendCommandMessage(String command) {
command = ClientSendMessageEvents.MODIFY_COMMAND.invoker().modifySendCommandMessage(command);
ClientSendMessageEvents.COMMAND.invoker().onSendCommandMessage(command);

View file

@ -16,6 +16,7 @@
package net.fabricmc.fabric.test.message.client;
import com.mojang.brigadier.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -36,6 +37,11 @@ public class ChatTestClient implements ClientModInitializer {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(ClientCommandManager.literal("block").then(ClientCommandManager.literal("send").executes(context -> {
throw new AssertionError("This client command should be blocked!");
}))));
//Register the modified result command from ClientSendMessageEvents#MODIFY_COMMAND to ensure that MODIFY_COMMAND executes before the client command api
ClientCommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(ClientCommandManager.literal("sending").then(ClientCommandManager.literal("modified").then(ClientCommandManager.literal("command").then(ClientCommandManager.literal("message").executes(context -> {
LOGGER.info("Command modified by ClientSendMessageEvents#MODIFY_COMMAND successfully processed by fabric client command api");
return Command.SINGLE_SUCCESS;
}))))));
//Test client send message events
ClientSendMessageEvents.ALLOW_CHAT.register((message) -> {
if (message.contains("block send")) {