diff --git a/src/main/java/land/chipmunk/chipmunkmod/commands/SayCommand.java b/src/main/java/land/chipmunk/chipmunkmod/commands/SayCommand.java index 0f18e46..d7dd797 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/commands/SayCommand.java +++ b/src/main/java/land/chipmunk/chipmunkmod/commands/SayCommand.java @@ -3,14 +3,16 @@ package land.chipmunk.chipmunkmod.commands; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; -import static land.chipmunk.chipmunkmod.command.CommandManager.literal; -import static land.chipmunk.chipmunkmod.command.CommandManager.argument; -import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; -import static com.mojang.brigadier.arguments.StringArgumentType.getString; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; -import net.minecraft.text.Text; + +import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; +import static land.chipmunk.chipmunkmod.command.CommandManager.argument; +import static land.chipmunk.chipmunkmod.command.CommandManager.literal; public class SayCommand { + public static boolean saying = false; + public static void register (CommandDispatcher dispatcher) { dispatcher.register( literal("say") @@ -22,9 +24,14 @@ public class SayCommand { } public static int say (CommandContext context) { + saying = true; + final FabricClientCommandSource source = context.getSource(); + source.getClient().getNetworkHandler().sendChatMessage(getString(context, "message")); + saying = false; + return Command.SINGLE_SUCCESS; } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java index 97e764d..412298b 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ChatScreenMixin.java @@ -1,12 +1,5 @@ package land.chipmunk.chipmunkmod.mixin; -import com.google.gson.JsonObject; -import land.chipmunk.chipmunkmod.ChipmunkMod; -import land.chipmunk.chipmunkmod.command.CommandManager; -import land.chipmunk.chipmunkmod.data.ChomeNSBotCommand; -import land.chipmunk.chipmunkmod.modules.ChomeNSBotCommandSuggestions; -import land.chipmunk.chipmunkmod.modules.CustomChat; -import land.chipmunk.chipmunkmod.util.BotValidationUtilities; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.ChatInputSuggestor; import net.minecraft.client.gui.screen.Screen; @@ -21,82 +14,13 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import javax.net.ssl.HttpsURLConnection; -import java.io.IOException; -import java.io.OutputStream; -import java.net.URL; -import java.util.List; - -@Mixin(net.minecraft.client.gui.screen.ChatScreen.class) +@Mixin(value = net.minecraft.client.gui.screen.ChatScreen.class) public class ChatScreenMixin extends Screen { @Shadow protected TextFieldWidget chatField; @Shadow private String originalChatText; @Shadow ChatInputSuggestor chatInputSuggestor; @Shadow private int messageHistorySize = -1; - @Inject(at = @At("HEAD"), method = "sendMessage", cancellable = true) - public void sendMessage(String chatText, boolean addToHistory, CallbackInfoReturnable cir) { - if (addToHistory) MinecraftClient.getInstance().inGameHud.getChatHud().addToMessageHistory(chatText); - - final CommandManager commandManager = CommandManager.INSTANCE; - - if (ChipmunkMod.CONFIG.bots.testbot.webhookUrl != null && chatText.startsWith(ChipmunkMod.CONFIG.bots.testbot.prefix)) { - ChipmunkMod.executorService.submit(() -> { - try { - final URL url = new URL(ChipmunkMod.CONFIG.bots.testbot.webhookUrl); - - final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); - connection.addRequestProperty("Content-Type", "application/json"); - connection.addRequestProperty("User-Agent", "ChipmunkMod"); - connection.setDoOutput(true); - connection.setRequestMethod("POST"); - - final JsonObject jsonObject = new JsonObject(); - - jsonObject.addProperty("username", "ChipmunkMod UwU"); - jsonObject.addProperty("content", MinecraftClient.getInstance().getSession().getUsername()); - - final OutputStream stream = connection.getOutputStream(); - stream.write(jsonObject.toString().getBytes()); - stream.flush(); - stream.close(); - - connection.getInputStream().close(); - connection.disconnect(); - } catch (IOException e) { - e.printStackTrace(); - } - }); - } else if (chatText.startsWith(ChipmunkMod.CONFIG.bots.chomens.prefix)) { - final List commands = ChomeNSBotCommandSuggestions.INSTANCE.commands; - - final List moreOrTrustedCommands = commands.stream() - .filter((command) -> command.trustLevel != ChomeNSBotCommand.TrustLevel.PUBLIC) - .map((command) -> command.name.toLowerCase()) - .toList(); - - if (moreOrTrustedCommands.contains(chatText.toLowerCase().split("\\s")[0])) { - try { - BotValidationUtilities.chomens(chatText.substring(ChipmunkMod.CONFIG.bots.chomens.prefix.length())); - - cir.setReturnValue(true); - - return; - } catch (Exception ignored) {} - } - } - - if (chatText.startsWith(commandManager.prefix)) { - commandManager.executeCommand(chatText.substring(commandManager.prefix.length())); - - cir.setReturnValue(true); - } else if (!chatText.startsWith("/")) { - CustomChat.INSTANCE.chat(chatText); - - cir.setReturnValue(true); - } - } - public ChatScreenMixin(String originalChatText) { super(Text.translatable("chat_screen.title")); this.originalChatText = originalChatText; @@ -125,6 +49,25 @@ public class ChatScreenMixin extends Screen { ci.cancel(); } + @Inject(method = "sendMessage", at = @At("HEAD"), cancellable = true) + private void sendMessage (String chatText, boolean addToHistory, CallbackInfoReturnable cir) { + final MinecraftClient client = MinecraftClient.getInstance(); + + if (addToHistory) { + client.inGameHud.getChatHud().addToMessageHistory(chatText); + } + + if (chatText.startsWith("/")) { + client.player.networkHandler.sendChatCommand(chatText.substring(1)); + } else { + client.player.networkHandler.sendChatMessage(chatText); + } + + cir.setReturnValue(true); + + cir.cancel(); + } + @Unique private void onChatFieldUpdate(String chatText) { String string = this.chatField.getText(); diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java index 299145e..41dd707 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java @@ -1,11 +1,15 @@ package land.chipmunk.chipmunkmod.mixin; +import com.google.gson.JsonObject; import com.mojang.authlib.GameProfile; import land.chipmunk.chipmunkmod.ChipmunkMod; import land.chipmunk.chipmunkmod.command.CommandManager; +import land.chipmunk.chipmunkmod.commands.SayCommand; +import land.chipmunk.chipmunkmod.data.ChomeNSBotCommand; import land.chipmunk.chipmunkmod.listeners.Listener; import land.chipmunk.chipmunkmod.listeners.ListenerManager; import land.chipmunk.chipmunkmod.modules.*; +import land.chipmunk.chipmunkmod.util.BotValidationUtilities; import net.kyori.adventure.text.TextComponent; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; @@ -27,7 +31,13 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -@Mixin(net.minecraft.client.network.ClientPlayNetworkHandler.class) +import javax.net.ssl.HttpsURLConnection; +import java.io.IOException; +import java.io.OutputStream; +import java.net.URL; +import java.util.List; + +@Mixin(value = net.minecraft.client.network.ClientPlayNetworkHandler.class, priority = 1024) public class ClientPlayNetworkHandlerMixin { @Shadow private FeatureSet enabledFeatures; @Shadow private CombinedDynamicRegistries combinedDynamicRegistries; @@ -90,4 +100,65 @@ public class ClientPlayNetworkHandlerMixin { e.printStackTrace(); } } + + @Inject(method = "sendChatMessage", at = @At("HEAD"), cancellable = true) + private void injectedSendChatMessage (String chatText, CallbackInfo ci) { + final CommandManager commandManager = CommandManager.INSTANCE; + + if (ChipmunkMod.CONFIG.bots.testbot.webhookUrl != null && chatText.startsWith(ChipmunkMod.CONFIG.bots.testbot.prefix)) { + ChipmunkMod.executorService.submit(() -> { + try { + final URL url = new URL(ChipmunkMod.CONFIG.bots.testbot.webhookUrl); + + final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + connection.addRequestProperty("Content-Type", "application/json"); + connection.addRequestProperty("User-Agent", "ChipmunkMod"); + connection.setDoOutput(true); + connection.setRequestMethod("POST"); + + final JsonObject jsonObject = new JsonObject(); + + jsonObject.addProperty("username", "ChipmunkMod UwU"); + jsonObject.addProperty("content", MinecraftClient.getInstance().getSession().getUsername()); + + final OutputStream stream = connection.getOutputStream(); + stream.write(jsonObject.toString().getBytes()); + stream.flush(); + stream.close(); + + connection.getInputStream().close(); + connection.disconnect(); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } else if (chatText.startsWith(ChipmunkMod.CONFIG.bots.chomens.prefix)) { + final List commands = ChomeNSBotCommandSuggestions.INSTANCE.commands; + + final List moreOrTrustedCommands = commands.stream() + .filter((command) -> command.trustLevel != ChomeNSBotCommand.TrustLevel.PUBLIC) + .map((command) -> command.name.toLowerCase()) + .toList(); + + if (moreOrTrustedCommands.contains(chatText.toLowerCase().split("\\s")[0])) { + try { + BotValidationUtilities.chomens(chatText.substring(ChipmunkMod.CONFIG.bots.chomens.prefix.length())); + + ci.cancel(); + + return; + } catch (Exception ignored) {} + } + } + + if (chatText.startsWith(commandManager.prefix)) { + commandManager.executeCommand(chatText.substring(commandManager.prefix.length())); + + ci.cancel(); + } else if (!chatText.startsWith("/") && !SayCommand.saying) { + CustomChat.INSTANCE.chat(chatText); + + ci.cancel(); + } + } }