FIX custom chat overriding other mods

This commit is contained in:
Chayapak 2023-08-28 17:04:33 +07:00
parent 198ec68b43
commit 9d7d16c643
3 changed files with 104 additions and 83 deletions

View file

@ -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<FabricClientCommandSource> dispatcher) {
dispatcher.register(
literal("say")
@ -22,9 +24,14 @@ public class SayCommand {
}
public static int say (CommandContext<FabricClientCommandSource> context) {
saying = true;
final FabricClientCommandSource source = context.getSource();
source.getClient().getNetworkHandler().sendChatMessage(getString(context, "message"));
saying = false;
return Command.SINGLE_SUCCESS;
}
}

View file

@ -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<Boolean> 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<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);
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<Boolean> 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();

View file

@ -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<ClientDynamicRegistryType> 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<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)) {
commandManager.executeCommand(chatText.substring(commandManager.prefix.length()));
ci.cancel();
} else if (!chatText.startsWith("/") && !SayCommand.saying) {
CustomChat.INSTANCE.chat(chatText);
ci.cancel();
}
}
}