diff --git a/build.gradle b/build.gradle index 16689ec..e66b03d 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ tasks { allprojects { group = "com.github.kaspiandev.antipopup" - version = "9.2" + version = "10" repositories { mavenCentral() diff --git a/gradle.properties b/gradle.properties index 34138dd..2aae38e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ userdevVer=1.7.1 shadowVer=8.1.7 tinyRemapperVer=0.10.3 -packetEventsVer=2.4.0 +packetEventsVer=2.6.0 diff --git a/settings.gradle b/settings.gradle index d8cd58b..5810b5e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -7,5 +7,5 @@ pluginManagement { rootProject.name = "AntiPopup" include "spigot", "shared", "nms", "v1.19.2", "v1.19.3", "v1.19.4", - "v1.20.1", "v1.20.2", "v1.20.4", "v1.20.6", "v1.21", "velocity" + "v1.20.1", "v1.20.2", "v1.20.4", "v1.20.6", "v1.21", "v1.21.2", "velocity" diff --git a/spigot/build.gradle b/spigot/build.gradle index ff45ee9..ccb4103 100644 --- a/spigot/build.gradle +++ b/spigot/build.gradle @@ -21,6 +21,7 @@ dependencies { implementation project(path: ":v1.20.4", configuration: "reobf") implementation project(path: ":v1.20.6", configuration: "reobf") implementation project(path: ":v1.21", configuration: "reobf") + implementation project(path: ":v1.21.2", configuration: "reobf") compileOnly "org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT" compileOnly "com.viaversion:viaversion:5.0.1" diff --git a/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/AntiPopup.java b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/AntiPopup.java index a39470c..f3d6948 100644 --- a/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/AntiPopup.java +++ b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/AntiPopup.java @@ -12,6 +12,7 @@ import com.github.kaspiandev.antipopup.nms.v1_20_4.PlayerInjector_v1_20_4; import com.github.kaspiandev.antipopup.nms.v1_20_6.PlayerInjector_v1_20_6; import com.github.kaspiandev.antipopup.nms.v1_21.PlayerInjector_v1_21; +import com.github.kaspiandev.antipopup.nms.v1_21_2.PlayerInjector_v1_21_2; import com.github.kaspiandev.antipopup.spigot.api.Api; import com.github.kaspiandev.antipopup.spigot.hook.HookManager; import com.github.kaspiandev.antipopup.spigot.hook.viaversion.ViaVersionHook; @@ -119,7 +120,8 @@ public void onEnable() { if (config.isBlockChatReports()) { if (!config.isExperimentalMode()) { PlayerListener playerListener = switch (serverManager.getVersion()) { - case V_1_21 -> new PlayerListener(new PlayerInjector_v1_21()); + case V_1_21_2, V_1_21_3 -> new PlayerListener(new PlayerInjector_v1_21_2()); + case V_1_21, V_1_21_1 -> new PlayerListener(new PlayerInjector_v1_21()); case V_1_20_5, V_1_20_6 -> new PlayerListener(new PlayerInjector_v1_20_6()); case V_1_20_3, V_1_20_4 -> new PlayerListener(new PlayerInjector_v1_20_4()); case V_1_20_2 -> new PlayerListener(new PlayerInjector_v1_20_2()); diff --git a/v1.21.2/build.gradle b/v1.21.2/build.gradle new file mode 100644 index 0000000..e03a464 --- /dev/null +++ b/v1.21.2/build.gradle @@ -0,0 +1,21 @@ +plugins { + id "java" + id "io.papermc.paperweight.userdev" version "${userdevVer}" +} + +dependencies { + compileOnly project(path: ":shared") + compileOnly project(path: ":nms") + + paperweightDevelopmentBundle "io.papermc.paper:dev-bundle:1.21.3-R0.1-SNAPSHOT" + pluginRemapper "net.fabricmc:tiny-remapper:${tinyRemapperVer}:fat" +} + +java { + disableAutoTargetJvm() + toolchain { + languageVersion.set(JavaLanguageVersion.of(21)) + } + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 +} diff --git a/v1.21.2/src/main/java/com/github/kaspiandev/antipopup/nms/v1_21_2/PlayerInjector_v1_21_2.java b/v1.21.2/src/main/java/com/github/kaspiandev/antipopup/nms/v1_21_2/PlayerInjector_v1_21_2.java new file mode 100644 index 0000000..947c59e --- /dev/null +++ b/v1.21.2/src/main/java/com/github/kaspiandev/antipopup/nms/v1_21_2/PlayerInjector_v1_21_2.java @@ -0,0 +1,86 @@ +package com.github.kaspiandev.antipopup.nms.v1_21_2; + +import com.github.kaspiandev.antipopup.spigot.nms.PacketInjector; +import io.netty.channel.*; +import net.minecraft.network.Connection; +import net.minecraft.network.chat.ChatType; +import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket; +import net.minecraft.network.protocol.game.ClientboundSystemChatPacket; +import net.minecraft.server.network.ServerCommonPacketListenerImpl; +import net.minecraft.server.network.ServerGamePacketListenerImpl; +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Player; + +import java.lang.reflect.Field; +import java.lang.reflect.InaccessibleObjectException; + +@SuppressWarnings("unused") +public class PlayerInjector_v1_21_2 implements PacketInjector { + + private static final String HANDLER_NAME = "antipopup_handler"; + private static Field connectionField; + + static { + try { + for (Field field : ServerCommonPacketListenerImpl.class.getDeclaredFields()) { + if (field.getType().equals(Connection.class)) { + field.setAccessible(true); + connectionField = field; + break; + } + } + } catch (SecurityException | InaccessibleObjectException e) { + throw new RuntimeException(e); + } + } + + public void inject(Player player) { + ChannelDuplexHandler duplexHandler = new ChannelDuplexHandler() { + @Override + public void write(ChannelHandlerContext ctx, Object packet, ChannelPromise promise) throws Exception { + if (packet instanceof ClientboundPlayerChatPacket chatPacket) { + Component content = chatPacket.unsignedContent(); + if (content == null) { + content = Component.literal(chatPacket.body().content()); + } + ChatType.Bound chatType = chatPacket.chatType(); + + ((CraftPlayer) player).getHandle().connection.send( + new ClientboundSystemChatPacket(chatType.decorate(content), false)); + return; + } + super.write(ctx, packet, promise); + } + }; + + ServerGamePacketListenerImpl listener = ((CraftPlayer) player).getHandle().connection; + Channel channel = getConnection(listener).channel; + ChannelPipeline pipeline = channel.pipeline(); + + if (pipeline.get(HANDLER_NAME) != null) { + pipeline.remove(HANDLER_NAME); + } + + channel.eventLoop().submit(() -> { + channel.pipeline().addBefore("packet_handler", HANDLER_NAME, duplexHandler); + }); + } + + public void uninject(Player player) { + ServerGamePacketListenerImpl listener = ((CraftPlayer) player).getHandle().connection; + Channel channel = getConnection(listener).channel; + channel.eventLoop().submit(() -> { + channel.pipeline().remove(HANDLER_NAME); + }); + } + + private Connection getConnection(ServerGamePacketListenerImpl listener) { + try { + return (Connection) connectionField.get(listener); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + +}