From 5552542461f0d791a1b5340cabdf1570b5f5e7bc Mon Sep 17 00:00:00 2001 From: Chipmunk <65827213+ChipmunkMC@users.noreply.github.com> Date: Sun, 30 Apr 2023 21:10:14 -0400 Subject: [PATCH] length fix hack --- .../chipmunkbot/plugins/ChatPlugin.java | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java index 58f9488..2b40cc4 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java @@ -5,17 +5,23 @@ import land.chipmunk.chipmunkbot.data.chat.PlayerMessage; import land.chipmunk.chipmunkbot.data.chat.SystemChatParser; import land.chipmunk.chipmunkbot.data.MutablePlayerListEntry; import land.chipmunk.chipmunkbot.util.UUIDUtilities; -import land.chipmunk.chipmunkbot.util.ComponentUtilities; // sus import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPlayerChatPacket; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundDisguisedChatPacket; +import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundGameProfilePacket; // TODO: Move uuid detection elsewhere import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatPacket; import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket; +import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetCreativeModeSlotPacket; import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.event.session.SessionListener; +import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.StringTag; +import com.github.steveice10.mc.protocol.MinecraftProtocol; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import lombok.Getter; @@ -25,12 +31,14 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; import java.time.Instant; +import java.nio.charset.StandardCharsets; import land.chipmunk.chipmunkbot.systemChat.*; public class ChatPlugin extends SessionAdapter { private final ChipmunkBot client; @Getter private List listeners = new ArrayList<>(); + private UUID uuid; // TODO: Move uuid detection elsewhere private List systemChatParsers; @@ -58,6 +66,7 @@ public class ChatPlugin extends SessionAdapter { if (packet instanceof ClientboundSystemChatPacket) packetReceived(session, (ClientboundSystemChatPacket) packet); else if (packet instanceof ClientboundPlayerChatPacket) packetReceived(session, (ClientboundPlayerChatPacket) packet); else if (packet instanceof ClientboundDisguisedChatPacket) packetReceived(session, (ClientboundDisguisedChatPacket) packet); + else if (packet instanceof ClientboundGameProfilePacket) packetReceived(session, (ClientboundGameProfilePacket) packet); // TODO: Move uuid detection elsewhere } public void packetReceived (Session session, ClientboundSystemChatPacket packet) { @@ -105,12 +114,41 @@ public class ChatPlugin extends SessionAdapter { } } + public void packetReceived (Session session, ClientboundGameProfilePacket packet) { + uuid = packet.getProfile().getId(); + } + // ? Should this be here? + // TODO: Break up the method to make it less messy public void tellraw (Component message, String targets) { final int maxLength = client.core().maxCommandLength(); - String command = "minecraft:tellraw " + targets + " " + GsonComponentSerializer.gson().serialize(message); - if (command.length() > maxLength) command = ("minecraft:w " + targets + " " + ComponentUtilities.stringify(message)); - if (command.length() > maxLength) command = command.substring(0, maxLength); + + final String raw = GsonComponentSerializer.gson().serialize(message); + String command = "minecraft:tellraw " + targets + " " + raw; + + if (command.length() > maxLength) { + String tagString; + boolean inspect; + CompoundTag itemTag = new CompoundTag(""); + + if (message instanceof TextComponent && message.style().isEmpty() && (message.children() == null || message.children().size() == 0)) { + tagString = ((TextComponent) message).content(); + inspect = false; + } else { + tagString = raw; + inspect = true; + } + + if (tagString.getBytes(StandardCharsets.UTF_8).length > 65535) return; + + final Session session = client.session(); + + session.send(new ServerboundSetCreativeModeSlotPacket(26, new ItemStack(1 /* stone */, 1, itemTag))); + client.core().run("minecraft:tellraw " + targets + " {\"nbt\":\"SelectedItem.tag.m\",\"entity\":\"" + uuid.toString() + "\",\"inspect\":" + inspect + "}"); // TODO: Use GSON instead of concatenating strings, and hardcode less of this (it shouldn't matter here but yes) + + return; + } + client.core().run(command); } public void tellraw (Component message) { tellraw(message, "@a"); }