|
| 1 | +package com.github.kaspiandev.antipopup.nms.v1_21_4; |
| 2 | + |
| 3 | +import com.github.kaspiandev.antipopup.spigot.nms.PacketInjector; |
| 4 | +import io.netty.channel.*; |
| 5 | +import net.minecraft.network.Connection; |
| 6 | +import net.minecraft.network.chat.ChatType; |
| 7 | +import net.minecraft.network.chat.Component; |
| 8 | +import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket; |
| 9 | +import net.minecraft.network.protocol.game.ClientboundSystemChatPacket; |
| 10 | +import net.minecraft.server.network.ServerCommonPacketListenerImpl; |
| 11 | +import net.minecraft.server.network.ServerGamePacketListenerImpl; |
| 12 | +import org.bukkit.craftbukkit.entity.CraftPlayer; |
| 13 | +import org.bukkit.entity.Player; |
| 14 | + |
| 15 | +import java.lang.reflect.Field; |
| 16 | +import java.lang.reflect.InaccessibleObjectException; |
| 17 | + |
| 18 | +@SuppressWarnings("unused") |
| 19 | +public class PlayerInjector_v1_21_4 implements PacketInjector { |
| 20 | + |
| 21 | + private static final String HANDLER_NAME = "antipopup_handler"; |
| 22 | + private static Field connectionField; |
| 23 | + |
| 24 | + static { |
| 25 | + try { |
| 26 | + for (Field field : ServerCommonPacketListenerImpl.class.getDeclaredFields()) { |
| 27 | + if (field.getType().equals(Connection.class)) { |
| 28 | + field.setAccessible(true); |
| 29 | + connectionField = field; |
| 30 | + break; |
| 31 | + } |
| 32 | + } |
| 33 | + } catch (SecurityException | InaccessibleObjectException e) { |
| 34 | + throw new RuntimeException(e); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public void inject(Player player) { |
| 39 | + ChannelDuplexHandler duplexHandler = new ChannelDuplexHandler() { |
| 40 | + @Override |
| 41 | + public void write(ChannelHandlerContext ctx, Object packet, ChannelPromise promise) throws Exception { |
| 42 | + if (packet instanceof ClientboundPlayerChatPacket chatPacket) { |
| 43 | + Component content = chatPacket.unsignedContent(); |
| 44 | + if (content == null) { |
| 45 | + content = Component.literal(chatPacket.body().content()); |
| 46 | + } |
| 47 | + ChatType.Bound chatType = chatPacket.chatType(); |
| 48 | + |
| 49 | + ((CraftPlayer) player).getHandle().connection.send( |
| 50 | + new ClientboundSystemChatPacket(chatType.decorate(content), false)); |
| 51 | + return; |
| 52 | + } |
| 53 | + super.write(ctx, packet, promise); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + ServerGamePacketListenerImpl listener = ((CraftPlayer) player).getHandle().connection; |
| 58 | + Channel channel = getConnection(listener).channel; |
| 59 | + ChannelPipeline pipeline = channel.pipeline(); |
| 60 | + |
| 61 | + if (pipeline.get(HANDLER_NAME) != null) { |
| 62 | + pipeline.remove(HANDLER_NAME); |
| 63 | + } |
| 64 | + |
| 65 | + channel.eventLoop().submit(() -> { |
| 66 | + channel.pipeline().addBefore("packet_handler", HANDLER_NAME, duplexHandler); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + public void uninject(Player player) { |
| 71 | + ServerGamePacketListenerImpl listener = ((CraftPlayer) player).getHandle().connection; |
| 72 | + Channel channel = getConnection(listener).channel; |
| 73 | + channel.eventLoop().submit(() -> { |
| 74 | + channel.pipeline().remove(HANDLER_NAME); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + private Connection getConnection(ServerGamePacketListenerImpl listener) { |
| 79 | + try { |
| 80 | + return (Connection) connectionField.get(listener); |
| 81 | + } catch (IllegalAccessException e) { |
| 82 | + throw new RuntimeException(e); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments