From fca65be520aad29fc9c5a43408f0b32634045d7c Mon Sep 17 00:00:00 2001 From: RacoonDog <32882447+RacoonDog@users.noreply.github.com> Date: Sun, 11 Feb 2024 06:19:25 -0500 Subject: [PATCH] prevent running meteor commands from clickevent Co-Authored-By: Damian <43420467+DAMcraft@users.noreply.github.com> --- .../commands/commands/NbtCommand.java | 3 ++- .../meteorclient/mixin/ScreenMixin.java | 3 ++- .../systems/modules/misc/BetterChat.java | 3 ++- .../utils/misc/text/MeteorClickEvent.java | 20 +++++++++++++++++++ .../meteorclient/utils/player/ChatUtils.java | 3 ++- 5 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/main/java/meteordevelopment/meteorclient/utils/misc/text/MeteorClickEvent.java diff --git a/src/main/java/meteordevelopment/meteorclient/commands/commands/NbtCommand.java b/src/main/java/meteordevelopment/meteorclient/commands/commands/NbtCommand.java index 49ee8d36a1..2b8a8935de 100644 --- a/src/main/java/meteordevelopment/meteorclient/commands/commands/NbtCommand.java +++ b/src/main/java/meteordevelopment/meteorclient/commands/commands/NbtCommand.java @@ -11,6 +11,7 @@ import meteordevelopment.meteorclient.commands.Command; import meteordevelopment.meteorclient.commands.arguments.CompoundNbtTagArgumentType; import meteordevelopment.meteorclient.systems.config.Config; +import meteordevelopment.meteorclient.utils.misc.text.MeteorClickEvent; import net.minecraft.command.CommandSource; import net.minecraft.command.argument.NbtPathArgumentType; import net.minecraft.item.ItemStack; @@ -84,7 +85,7 @@ public void build(LiteralArgumentBuilder builder) { MutableText copyButton = Text.literal("NBT"); copyButton.setStyle(copyButton.getStyle() .withFormatting(Formatting.UNDERLINE) - .withClickEvent(new ClickEvent( + .withClickEvent(new MeteorClickEvent( ClickEvent.Action.RUN_COMMAND, this.toString("copy") )) diff --git a/src/main/java/meteordevelopment/meteorclient/mixin/ScreenMixin.java b/src/main/java/meteordevelopment/meteorclient/mixin/ScreenMixin.java index 3fb6f8ecc2..13767eb998 100644 --- a/src/main/java/meteordevelopment/meteorclient/mixin/ScreenMixin.java +++ b/src/main/java/meteordevelopment/meteorclient/mixin/ScreenMixin.java @@ -12,6 +12,7 @@ import meteordevelopment.meteorclient.systems.modules.movement.GUIMove; import meteordevelopment.meteorclient.systems.modules.render.NoRender; import meteordevelopment.meteorclient.utils.Utils; +import meteordevelopment.meteorclient.utils.misc.text.MeteorClickEvent; import net.minecraft.client.gui.screen.ChatScreen; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.Style; @@ -35,7 +36,7 @@ private void onRenderBackground(CallbackInfo info) { @Inject(method = "handleTextClick", at = @At(value = "INVOKE", target = "Lorg/slf4j/Logger;error(Ljava/lang/String;Ljava/lang/Object;)V", ordinal = 1, remap = false), cancellable = true) private void onRunCommand(Style style, CallbackInfoReturnable cir) { - if (style.getClickEvent().getValue().startsWith(Config.get().prefix.get())) { + if (style.getClickEvent() instanceof MeteorClickEvent clickEvent && clickEvent.getValue().startsWith(Config.get().prefix.get())) { try { Commands.dispatch(style.getClickEvent().getValue().substring(Config.get().prefix.get().length())); cir.setReturnValue(true); diff --git a/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/BetterChat.java b/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/BetterChat.java index 14c6889448..8fe57fc9dc 100644 --- a/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/BetterChat.java +++ b/src/main/java/meteordevelopment/meteorclient/systems/modules/misc/BetterChat.java @@ -22,6 +22,7 @@ import meteordevelopment.meteorclient.systems.modules.Module; import meteordevelopment.meteorclient.utils.Utils; import meteordevelopment.meteorclient.utils.misc.MeteorIdentifier; +import meteordevelopment.meteorclient.utils.misc.text.MeteorClickEvent; import meteordevelopment.meteorclient.utils.player.ChatUtils; import meteordevelopment.meteorclient.utils.render.color.Color; import meteordevelopment.orbit.EventHandler; @@ -518,7 +519,7 @@ private MutableText getSendButton(String message) { sendButton.setStyle(sendButton.getStyle() .withFormatting(Formatting.DARK_RED) - .withClickEvent(new ClickEvent( + .withClickEvent(new MeteorClickEvent( ClickEvent.Action.RUN_COMMAND, Commands.get("say").toString(message) )) diff --git a/src/main/java/meteordevelopment/meteorclient/utils/misc/text/MeteorClickEvent.java b/src/main/java/meteordevelopment/meteorclient/utils/misc/text/MeteorClickEvent.java new file mode 100644 index 0000000000..acc9e6ed9f --- /dev/null +++ b/src/main/java/meteordevelopment/meteorclient/utils/misc/text/MeteorClickEvent.java @@ -0,0 +1,20 @@ +/* + * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client). + * Copyright (c) Meteor Development. + */ + +package meteordevelopment.meteorclient.utils.misc.text; + +import net.minecraft.text.ClickEvent; +import net.minecraft.text.Style; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +/** + * This class does nothing except ensure that {@link ClickEvent}'s containing Meteor Client commands can only be executed if they come from the client. + * @see meteordevelopment.meteorclient.mixin.ScreenMixin#onRunCommand(Style, CallbackInfoReturnable) + */ +public class MeteorClickEvent extends ClickEvent { + public MeteorClickEvent(Action action, String value) { + super(action, value); + } +} diff --git a/src/main/java/meteordevelopment/meteorclient/utils/player/ChatUtils.java b/src/main/java/meteordevelopment/meteorclient/utils/player/ChatUtils.java index d4eace822e..ba05ab99f3 100644 --- a/src/main/java/meteordevelopment/meteorclient/utils/player/ChatUtils.java +++ b/src/main/java/meteordevelopment/meteorclient/utils/player/ChatUtils.java @@ -11,6 +11,7 @@ import meteordevelopment.meteorclient.pathing.BaritoneUtils; import meteordevelopment.meteorclient.systems.config.Config; import meteordevelopment.meteorclient.utils.PostInit; +import meteordevelopment.meteorclient.utils.misc.text.MeteorClickEvent; import net.minecraft.text.*; import net.minecraft.util.Formatting; import net.minecraft.util.Pair; @@ -248,7 +249,7 @@ public static MutableText formatCoords(Vec3d pos) { )); if (BaritoneUtils.IS_AVAILABLE) { - style = style.withClickEvent(new ClickEvent( + style = style.withClickEvent(new MeteorClickEvent( ClickEvent.Action.RUN_COMMAND, String.format("%sgoto %d %d %d", BaritoneUtils.getPrefix(), (int) pos.x, (int) pos.y, (int) pos.z) ));