fix and make it a LOT better

This commit is contained in:
Chayapak 2023-08-30 16:51:49 +07:00
parent bb30748a00
commit 4c1ff7a0aa
6 changed files with 112 additions and 79 deletions

View file

@ -3,6 +3,7 @@ package land.chipmunk.chipmunkmod.commands;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import land.chipmunk.chipmunkmod.modules.Chat;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.getString;
@ -11,8 +12,6 @@ import static land.chipmunk.chipmunkmod.command.CommandManager.argument;
import static land.chipmunk.chipmunkmod.command.CommandManager.literal; import static land.chipmunk.chipmunkmod.command.CommandManager.literal;
public class SayCommand { public class SayCommand {
public static boolean saying = false;
public static void register (CommandDispatcher<FabricClientCommandSource> dispatcher) { public static void register (CommandDispatcher<FabricClientCommandSource> dispatcher) {
dispatcher.register( dispatcher.register(
literal("say") literal("say")
@ -24,13 +23,7 @@ public class SayCommand {
} }
public static int say (CommandContext<FabricClientCommandSource> context) { public static int say (CommandContext<FabricClientCommandSource> context) {
saying = true; Chat.sendChatMessage(getString(context, "message"), true);
final FabricClientCommandSource source = context.getSource();
source.getClient().getNetworkHandler().sendChatMessage(getString(context, "message"));
saying = false;
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
} }

View file

@ -1,5 +1,10 @@
package land.chipmunk.chipmunkmod.mixin; package land.chipmunk.chipmunkmod.mixin;
import com.google.gson.JsonObject;
import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.data.ChomeNSBotCommand;
import land.chipmunk.chipmunkmod.modules.ChomeNSBotCommandSuggestions;
import land.chipmunk.chipmunkmod.util.BotValidationUtilities;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ChatInputSuggestor; import net.minecraft.client.gui.screen.ChatInputSuggestor;
import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.Screen;
@ -14,6 +19,12 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; 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(value = net.minecraft.client.gui.screen.ChatScreen.class) @Mixin(value = net.minecraft.client.gui.screen.ChatScreen.class)
public class ChatScreenMixin extends Screen { public class ChatScreenMixin extends Screen {
@Shadow protected TextFieldWidget chatField; @Shadow protected TextFieldWidget chatField;
@ -57,6 +68,53 @@ public class ChatScreenMixin extends Screen {
client.inGameHud.getChatHud().addToMessageHistory(chatText); client.inGameHud.getChatHud().addToMessageHistory(chatText);
} }
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<ChomeNSBotCommand> commands = ChomeNSBotCommandSuggestions.INSTANCE.commands;
final List<String> 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);
cir.cancel();
return;
} catch (Exception ignored) {}
}
}
if (chatText.startsWith("/")) { if (chatText.startsWith("/")) {
client.player.networkHandler.sendChatCommand(chatText.substring(1)); client.player.networkHandler.sendChatCommand(chatText.substring(1));
} else { } else {

View file

@ -1,15 +1,11 @@
package land.chipmunk.chipmunkmod.mixin; package land.chipmunk.chipmunkmod.mixin;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import land.chipmunk.chipmunkmod.ChipmunkMod; import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.command.CommandManager; 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.Listener;
import land.chipmunk.chipmunkmod.listeners.ListenerManager; import land.chipmunk.chipmunkmod.listeners.ListenerManager;
import land.chipmunk.chipmunkmod.modules.*; import land.chipmunk.chipmunkmod.modules.*;
import land.chipmunk.chipmunkmod.util.BotValidationUtilities;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.Screen;
@ -18,6 +14,13 @@ import net.minecraft.client.network.ServerInfo;
import net.minecraft.client.util.telemetry.WorldSession; import net.minecraft.client.util.telemetry.WorldSession;
import net.minecraft.command.CommandRegistryAccess; import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.network.ClientConnection; import net.minecraft.network.ClientConnection;
import net.minecraft.network.encryption.NetworkEncryptionUtils;
import net.minecraft.network.message.LastSeenMessagesCollector;
import net.minecraft.network.message.MessageBody;
import net.minecraft.network.message.MessageChain;
import net.minecraft.network.message.MessageSignatureData;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.play.ChatMessageC2SPacket;
import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket; import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket;
import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket; import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket;
import net.minecraft.network.packet.s2c.play.PlayerRemoveS2CPacket; import net.minecraft.network.packet.s2c.play.PlayerRemoveS2CPacket;
@ -31,16 +34,17 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import javax.net.ssl.HttpsURLConnection; import java.time.Instant;
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 = 1001) @Mixin(value = net.minecraft.client.network.ClientPlayNetworkHandler.class, priority = 1001)
public class ClientPlayNetworkHandlerMixin { public class ClientPlayNetworkHandlerMixin {
@Shadow private FeatureSet enabledFeatures; @Shadow private FeatureSet enabledFeatures;
@Shadow private CombinedDynamicRegistries<ClientDynamicRegistryType> combinedDynamicRegistries; @Shadow private CombinedDynamicRegistries<ClientDynamicRegistryType> combinedDynamicRegistries;
@Shadow private LastSeenMessagesCollector lastSeenMessagesCollector;
@Shadow private MessageChain.Packer messagePacker;
@Shadow
public void sendPacket(Packet<?> packet) {}
@Inject(method = "<init>", at = @At("TAIL")) @Inject(method = "<init>", at = @At("TAIL"))
private void init (MinecraftClient client, Screen screen, ClientConnection connection, ServerInfo serverInfo, GameProfile profile, WorldSession worldSession, CallbackInfo ci) { private void init (MinecraftClient client, Screen screen, ClientConnection connection, ServerInfo serverInfo, GameProfile profile, WorldSession worldSession, CallbackInfo ci) {
@ -102,63 +106,31 @@ public class ClientPlayNetworkHandlerMixin {
} }
@Inject(method = "sendChatMessage", at = @At("HEAD"), cancellable = true) @Inject(method = "sendChatMessage", at = @At("HEAD"), cancellable = true)
private void injectedSendChatMessage (String chatText, CallbackInfo ci) { private void sendChatMessage (String chatText, CallbackInfo ci) {
final CommandManager commandManager = CommandManager.INSTANCE; final CommandManager commandManager = CommandManager.INSTANCE;
if (ChipmunkMod.CONFIG.bots.testbot.webhookUrl != null && chatText.startsWith(ChipmunkMod.CONFIG.bots.testbot.prefix)) { final String secret = String.valueOf(Chat.secret);
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<ChomeNSBotCommand> commands = ChomeNSBotCommandSuggestions.INSTANCE.commands;
final List<String> 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)) { if (chatText.startsWith(commandManager.prefix)) {
commandManager.executeCommand(chatText.substring(commandManager.prefix.length())); commandManager.executeCommand(chatText.substring(commandManager.prefix.length()));
ci.cancel(); ci.cancel();
} else if (!chatText.startsWith("/") && !SayCommand.saying) { } else if (!chatText.startsWith("/") && !chatText.startsWith(secret)) {
CustomChat.INSTANCE.chat(chatText); CustomChat.INSTANCE.chat(chatText);
ci.cancel(); ci.cancel();
} }
if (chatText.startsWith(secret)) {
final String content = chatText.substring(secret.length());
Instant instant = Instant.now();
long l = NetworkEncryptionUtils.SecureRandomUtil.nextLong();
LastSeenMessagesCollector.LastSeenMessages lastSeenMessages = this.lastSeenMessagesCollector.collect();
MessageSignatureData messageSignatureData = this.messagePacker.pack(new MessageBody(content, instant, l, lastSeenMessages.lastSeen()));
this.sendPacket(new ChatMessageC2SPacket(content, instant, l, messageSignatureData, lastSeenMessages.update()));
ci.cancel();
}
} }
} }

View file

@ -0,0 +1,18 @@
package land.chipmunk.chipmunkmod.modules;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
public class Chat {
public static double secret = Math.random();
public static void sendChatMessage (String message) { sendChatMessage(message, false); }
public static void sendChatMessage (String message, boolean usePlayerChat) {
if (message == null) return;
final ClientPlayNetworkHandler networkHandler = MinecraftClient.getInstance().getNetworkHandler();
if (usePlayerChat) networkHandler.sendChatMessage(secret + message);
else networkHandler.sendChatMessage(message);
}
}

View file

@ -56,10 +56,7 @@ public class CustomChat {
} }
if (!enabled || !player.hasPermissionLevel(2) || !player.isCreative()) { if (!enabled || !player.hasPermissionLevel(2) || !player.isCreative()) {
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); Chat.sendChatMessage(message, true);
SayCommand.saying = true;
networkHandler.sendChatMessage(message);
SayCommand.saying = false;
return; return;
} }

View file

@ -4,6 +4,7 @@ import com.mojang.brigadier.Command;
import land.chipmunk.chipmunkmod.ChipmunkMod; import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.Configuration; import land.chipmunk.chipmunkmod.Configuration;
import land.chipmunk.chipmunkmod.commands.SayCommand; import land.chipmunk.chipmunkmod.commands.SayCommand;
import land.chipmunk.chipmunkmod.modules.Chat;
import land.chipmunk.chipmunkmod.modules.CustomChat; import land.chipmunk.chipmunkmod.modules.CustomChat;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.client.network.ClientPlayNetworkHandler;
@ -33,9 +34,7 @@ public class BotValidationUtilities {
BigInteger bigInt = new BigInteger(1, Arrays.copyOfRange(hash, 0, 4)); BigInteger bigInt = new BigInteger(1, Arrays.copyOfRange(hash, 0, 4));
String stringHash = bigInt.toString(Character.MAX_RADIX); String stringHash = bigInt.toString(Character.MAX_RADIX);
SayCommand.saying = true; Chat.sendChatMessage(prefix + command + " " + stringHash, true);
networkHandler.sendChatMessage(prefix + command + " " + stringHash);
SayCommand.saying = false;
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -60,9 +59,7 @@ public class BotValidationUtilities {
BigInteger bigInt = new BigInteger(1, Arrays.copyOfRange(hash, 0, 4)); BigInteger bigInt = new BigInteger(1, Arrays.copyOfRange(hash, 0, 4));
String stringHash = bigInt.toString(Character.MAX_RADIX); String stringHash = bigInt.toString(Character.MAX_RADIX);
SayCommand.saying = true; Chat.sendChatMessage(prefix + command + " " + stringHash, true);
networkHandler.sendChatMessage(prefix + command + " " + stringHash);
SayCommand.saying = false;
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -110,7 +107,7 @@ public class BotValidationUtilities {
" " + " " +
String.join(" ", restArguments); String.join(" ", restArguments);
CustomChat.INSTANCE.chat(toSend); Chat.sendChatMessage(toSend);
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -134,9 +131,7 @@ public class BotValidationUtilities {
BigInteger bigInt = new BigInteger(1, Arrays.copyOfRange(hash, 0, 4)); BigInteger bigInt = new BigInteger(1, Arrays.copyOfRange(hash, 0, 4));
String stringHash = bigInt.toString(Character.MAX_RADIX); String stringHash = bigInt.toString(Character.MAX_RADIX);
SayCommand.saying = true; Chat.sendChatMessage(prefix + command + " " + stringHash, true);
networkHandler.sendChatMessage(prefix + command + " " + stringHash);
SayCommand.saying = false;
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
} }