From 058671cd69832b8379ef61ab2f26fd1014680bf2 Mon Sep 17 00:00:00 2001 From: Kaspian <34658474+KaspianDev@users.noreply.github.com> Date: Sun, 8 Jan 2023 11:16:16 +0100 Subject: [PATCH] 5.2 (#28) * 5.2 Initial changes * Cleanup * Fixes for BUKKIT mode * Add 1.19.3 NMS mode. (needs cleaning) * Add preparation script * 5.2 Testing 3 * Fix compiling * Cleanup --- .github/workflows/release.yml | 6 ++ .gitignore | 2 + build.gradle | 22 ++--- gradle/wrapper/gradle-wrapper.properties | 4 +- gradlew.bat | 89 ------------------- prepare | 21 +++++ settings.gradle | 9 +- .../kaspiandev/antipopup/AntiPopup.java | 65 +++++++++----- .../kaspiandev/antipopup/CommandRegister.java | 9 +- .../kaspiandev/antipopup/LogFilter.java | 15 ++-- .../{URLListener.java => ChatListener.java} | 8 +- .../antipopup/listeners/KickListener.java | 19 ---- .../listeners/PacketEventsListener.java | 11 ++- .../antipopup/nms/AbstractInjector.java | 9 ++ .../antipopup/nms/PlayerListener.java | 26 ++++++ .../antipopup/nms/v1_19_3/PlayerInjector.java | 50 +++++++++++ src/main/resources/config.yml | 21 ++--- 17 files changed, 214 insertions(+), 172 deletions(-) delete mode 100644 gradlew.bat create mode 100755 prepare rename src/main/java/com/github/kaspiandev/antipopup/listeners/{URLListener.java => ChatListener.java} (75%) delete mode 100644 src/main/java/com/github/kaspiandev/antipopup/listeners/KickListener.java create mode 100644 src/main/java/com/github/kaspiandev/antipopup/nms/AbstractInjector.java create mode 100644 src/main/java/com/github/kaspiandev/antipopup/nms/PlayerListener.java create mode 100644 src/main/java/com/github/kaspiandev/antipopup/nms/v1_19_3/PlayerInjector.java diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5b92bae..bc5d520 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -17,9 +17,15 @@ jobs: with: java-version: 17 distribution: temurin + cache: gradle - name: Make gradlew executable run: chmod +x ./gradlew + + - uses: SpraxDev/Action-SpigotMC@v4 + with: + versions: 1.19.3 + remapped: true - name: Build artifacts run: ./gradlew build diff --git a/.gitignore b/.gitignore index 3c37caf..e7e8052 100644 --- a/.gitignore +++ b/.gitignore @@ -116,3 +116,5 @@ run/ # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) !gradle-wrapper.jar + +buildtools diff --git a/build.gradle b/build.gradle index b147f94..bfa61f8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,15 @@ plugins { id "java" id "com.github.johnrengelman.shadow" version "7.1.2" + id "io.papermc.paperweight.userdev" version "1.4.1" } group = "com.github.kaspiandev" -version = "5.1" +version = "5.2" repositories { mavenCentral() + mavenLocal() maven { url "https://repo.codemc.io/repository/maven-snapshots/" } maven { name = "spigotmc-repo" @@ -24,23 +26,23 @@ repositories { } dependencies { - compileOnly "org.spigotmc:spigot-api:1.19.2-R0.1-SNAPSHOT" + compileOnly "org.spigotmc:spigot:1.19.3-R0.1-SNAPSHOT:remapped-mojang" compileOnly "dev.dejvokep:boosted-yaml-spigot:1.3" compileOnly "org.apache.commons:commons-configuration2:2.8.0" - compileOnly "com.viaversion:viaversion:4.4.1" + compileOnly "com.viaversion:viaversion:4.5.1" compileOnly "org.apache.logging.log4j:log4j-core:2.19.0" implementation "org.bstats:bstats-bukkit:3.0.0" - implementation "com.github.retrooper.packetevents:spigot:2.0.0-20221209.211024-75" + implementation "com.github.retrooper.packetevents:spigot:2.0.0-20230103.215431-105" + + paperweightDevelopmentBundle "io.papermc.paper:dev-bundle:1.19.3-R0.1-SNAPSHOT" } tasks { - build { - dependsOn(shadowJar) + assemble { + dependsOn(reobfJar) } shadowJar { - archiveName = "AntiPopup-${version}.jar" - minimize() relocate("io.github.retrooper.packetevents", "com.github.kaspiandev.antipopup.libs.io.github.retrooper.packetevents") relocate("com.github.retrooper.packetevents", "com.github.kaspiandev.antipopup.libs.com.github.retrooper.packetevents") @@ -53,8 +55,6 @@ tasks { def targetJavaVersion = 17 -jar.enabled = false - java { def javaVersion = JavaVersion.toVersion(targetJavaVersion) sourceCompatibility = javaVersion @@ -71,4 +71,4 @@ processResources { filesMatching("plugin.yml") { expand props } -} +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2e6e589..afef0da 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists +zipStorePath=wrapper/dists \ No newline at end of file diff --git a/gradlew.bat b/gradlew.bat deleted file mode 100644 index ac1b06f..0000000 --- a/gradlew.bat +++ /dev/null @@ -1,89 +0,0 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega diff --git a/prepare b/prepare new file mode 100755 index 0000000..b6f127f --- /dev/null +++ b/prepare @@ -0,0 +1,21 @@ +#!/bin/bash + +btUrl="https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar" + +mkdir buildtools +cd buildtools + +curl -o BuildTools.jar $btUrl + +echo "" +echo "----------------------------------------" +echo "Downloading complete. Building started." +echo "----------------------------------------" +sleep 3 + +java -jar BuildTools.jar --rev 1.19.3 --remapped + +echo "" +echo "----------------------------------------" +echo "Done." +echo "----------------------------------------" diff --git a/settings.gradle b/settings.gradle index 7b932aa..80603d0 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,8 @@ -rootProject.name = 'AntiPopup' +pluginManagement { + repositories { + gradlePluginPortal() + maven { url = "https://repo.papermc.io/repository/maven-public/" } + } +} + +rootProject.name = 'AntiPopup' \ No newline at end of file diff --git a/src/main/java/com/github/kaspiandev/antipopup/AntiPopup.java b/src/main/java/com/github/kaspiandev/antipopup/AntiPopup.java index 4dd47e3..8c549e6 100644 --- a/src/main/java/com/github/kaspiandev/antipopup/AntiPopup.java +++ b/src/main/java/com/github/kaspiandev/antipopup/AntiPopup.java @@ -1,9 +1,10 @@ package com.github.kaspiandev.antipopup; import com.github.kaspiandev.antipopup.api.Api; -import com.github.kaspiandev.antipopup.listeners.KickListener; +import com.github.kaspiandev.antipopup.listeners.ChatListener; import com.github.kaspiandev.antipopup.listeners.PacketEventsListener; -import com.github.kaspiandev.antipopup.listeners.URLListener; +import com.github.kaspiandev.antipopup.nms.PlayerListener; +import com.github.kaspiandev.antipopup.nms.v1_19_3.PlayerInjector; import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.manager.server.ServerVersion; import dev.dejvokep.boostedyaml.YamlDocument; @@ -30,9 +31,8 @@ public final class AntiPopup extends JavaPlugin { - public static YamlDocument config; - static Plugin instance; - static Metrics metrics; + private static YamlDocument yamlDoc; + private static Plugin instance; @Override public void onLoad() { @@ -46,7 +46,7 @@ public void onLoad() { public void onEnable() { instance = this; try { - config = YamlDocument.create(new File(getDataFolder(), "config.yml"), + yamlDoc = YamlDocument.create(new File(getDataFolder(), "config.yml"), Objects.requireNonNull(getResource("config.yml")), GeneralSettings.DEFAULT, LoaderSettings.builder().setAutoUpdate(true).build(), DumperSettings.DEFAULT, @@ -58,8 +58,8 @@ public void onEnable() { throw new RuntimeException(ex); } - if (config.getBoolean("bstats", false)) { - metrics = new Metrics(this, 16308); + if (yamlDoc.getBoolean("bstats")) { + Metrics metrics = new Metrics(this, 16308); metrics.addCustomChart(new SimplePie("runs_viaversion", () -> Bukkit.getPluginManager().isPluginEnabled("ViaVersion") ? "Yes" : "No")); getLogger().info("Loaded optional metrics."); @@ -76,21 +76,37 @@ public void onEnable() { } } + if (yamlDoc.getBoolean("clickable-urls")) { + getServer().getPluginManager().registerEvents(new ChatListener(), this); + getLogger().info("Enabled URL support."); + } + PacketEvents.getAPI().getEventManager().registerListener(new PacketEventsListener()); PacketEvents.getAPI().init(); getLogger().info("Initiated PacketEvents."); - getServer().getPluginManager().registerEvents(new KickListener(), this); - if (config.getBoolean("enable-urls")) { - getServer().getPluginManager().registerEvents(new URLListener(), this); + if (yamlDoc.getBoolean("setup-mode") + && PacketEvents.getAPI().getServerManager().getVersion().equals(ServerVersion.V_1_19_3)) { + yamlDoc.set("mode", "NMS"); + yamlDoc.set("setup-mode", false); + try { + yamlDoc.save(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + if (yamlDoc.getString("mode").equals("NMS")) { + if (PacketEvents.getAPI().getServerManager().getVersion().equals(ServerVersion.V_1_19_3)) { + getServer().getPluginManager().registerEvents(new PlayerListener(new PlayerInjector()), this); + } } - getLogger().info("Listeners registered."); Objects.requireNonNull(this.getCommand("antipopup")).setExecutor(new CommandRegister()); getLogger().info("Commands registered."); - if (config.getBoolean("filter-not-secure", true) - || config.getBoolean("sync-time-suppress", false)) { + if (yamlDoc.getBoolean("filter-not-secure") + || yamlDoc.getBoolean("sync-time-suppress")) { ((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()).addFilter(new LogFilter()); getLogger().info("Logger filter enabled."); } else { @@ -98,8 +114,8 @@ public void onEnable() { } Bukkit.getScheduler().runTaskLater(this, () -> { - if (config.getBoolean("auto-setup", false)) new Api(instance).setupAntiPopup(80); - if (config.getBoolean("first-run")) { + if (yamlDoc.getBoolean("auto-setup")) new Api(instance).setupAntiPopup(80); + if (yamlDoc.getBoolean("first-run")) { try { FileInputStream in = new FileInputStream("server.properties"); Properties props = new Properties(); @@ -114,13 +130,12 @@ public void onEnable() { getLogger().warning("------------------------------------------------------"); } in.close(); - config.set("first-run", false); - config.save(); + yamlDoc.save(); } catch (IOException io) { io.printStackTrace(); } } - if (config.getBoolean("ask-bstats")) { + if (yamlDoc.getBoolean("ask-bstats")) { try { getLogger().warning("--------------------[ READ ME PLEASE ]--------------------"); getLogger().warning("This is your first startup with AntiPopup."); @@ -129,8 +144,8 @@ public void onEnable() { getLogger().warning("Because I respect your freedom it's disabled by default."); getLogger().warning("Thanks for using AntiPopup! (you will not see this again)"); getLogger().warning("----------------------------------------------------------"); - config.set("ask-bstats", false); - config.save(); + yamlDoc.set("ask-bstats", false); + yamlDoc.save(); } catch (IOException io) { io.printStackTrace(); } @@ -143,4 +158,12 @@ public void onDisable() { PacketEvents.getAPI().terminate(); getLogger().info("Disabled PacketEvents."); } + + public static Plugin getInstance() { + return instance; + } + + public static YamlDocument getYamlDoc() { + return yamlDoc; + } } \ No newline at end of file diff --git a/src/main/java/com/github/kaspiandev/antipopup/CommandRegister.java b/src/main/java/com/github/kaspiandev/antipopup/CommandRegister.java index 002f406..f1ed371 100644 --- a/src/main/java/com/github/kaspiandev/antipopup/CommandRegister.java +++ b/src/main/java/com/github/kaspiandev/antipopup/CommandRegister.java @@ -1,20 +1,23 @@ package com.github.kaspiandev.antipopup; import com.github.kaspiandev.antipopup.api.Api; +import dev.dejvokep.boostedyaml.YamlDocument; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import java.io.IOException; -import static com.github.kaspiandev.antipopup.AntiPopup.config; -import static com.github.kaspiandev.antipopup.AntiPopup.instance; import static org.bukkit.Bukkit.getLogger; public class CommandRegister implements CommandExecutor { + private final Plugin instance = AntiPopup.getInstance(); + private final YamlDocument yamlDoc = AntiPopup.getYamlDoc(); + @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @@ -29,7 +32,7 @@ public boolean onCommand(@NotNull CommandSender sender, api.setupAntiPopup(100); } else if ("reload".equals(args[0])) { try { - config.reload(); + yamlDoc.reload(); getLogger().info("Config has been reloaded."); } catch (IOException e) { throw new RuntimeException(e); diff --git a/src/main/java/com/github/kaspiandev/antipopup/LogFilter.java b/src/main/java/com/github/kaspiandev/antipopup/LogFilter.java index c827596..056020d 100644 --- a/src/main/java/com/github/kaspiandev/antipopup/LogFilter.java +++ b/src/main/java/com/github/kaspiandev/antipopup/LogFilter.java @@ -1,5 +1,6 @@ package com.github.kaspiandev.antipopup; +import dev.dejvokep.boostedyaml.YamlDocument; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Marker; @@ -7,16 +8,17 @@ import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.Logger; import org.apache.logging.log4j.message.Message; +import org.bukkit.plugin.Plugin; -import static com.github.kaspiandev.antipopup.AntiPopup.config; + public class LogFilter implements Filter { -public class LogFilter implements Filter { + private final YamlDocument yamlDoc = AntiPopup.getYamlDoc(); @Override public Filter.Result filter(LogEvent event) { // We wanna eliminate possibilities of false catches. - if (config.getBoolean("filter-not-secure", true) + if (yamlDoc.getBoolean("filter-not-secure") && event.getMessage().getFormattedMessage().contains("[Not Secure] ") && event.getLoggerName().equals("net.minecraft.server.MinecraftServer") && event.getLevel() == Level.INFO) { @@ -25,11 +27,6 @@ public Filter.Result filter(LogEvent event) { .replace("[Not Secure] ", "")); return Filter.Result.DENY; } - if (config.getBoolean("sync-time-suppress", false) - && event.getMessage().getFormattedMessage().contains("sent out-of-order chat:")) { - return Filter.Result.DENY; - - } return Filter.Result.NEUTRAL; } @@ -138,4 +135,4 @@ public boolean isStopped() { return false; } -} +} \ No newline at end of file diff --git a/src/main/java/com/github/kaspiandev/antipopup/listeners/URLListener.java b/src/main/java/com/github/kaspiandev/antipopup/listeners/ChatListener.java similarity index 75% rename from src/main/java/com/github/kaspiandev/antipopup/listeners/URLListener.java rename to src/main/java/com/github/kaspiandev/antipopup/listeners/ChatListener.java index 06bb1d2..b184ea5 100644 --- a/src/main/java/com/github/kaspiandev/antipopup/listeners/URLListener.java +++ b/src/main/java/com/github/kaspiandev/antipopup/listeners/ChatListener.java @@ -7,21 +7,25 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; -public class URLListener implements Listener { +import java.util.Set; + +public class ChatListener implements Listener { // Must be monitor @EventHandler(priority = EventPriority.MONITOR) public void onMessage(AsyncPlayerChatEvent event) { if (event.isCancelled()) return; + if (event.getRecipients().isEmpty()) return; event.setCancelled(true); Player sender = event.getPlayer(); String message = String.format(event.getFormat(), sender.getName(), event.getMessage()); Bukkit.getConsoleSender().sendMessage(message); - for (Player player : event.getRecipients()) { player.sendMessage(sender.getUniqueId(), message); } + Bukkit.getServer().getPluginManager().callEvent(new AsyncPlayerChatEvent( + true, sender, event.getMessage(), Set.of())); } } \ No newline at end of file diff --git a/src/main/java/com/github/kaspiandev/antipopup/listeners/KickListener.java b/src/main/java/com/github/kaspiandev/antipopup/listeners/KickListener.java deleted file mode 100644 index 006fcb1..0000000 --- a/src/main/java/com/github/kaspiandev/antipopup/listeners/KickListener.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.github.kaspiandev.antipopup.listeners; - -import com.github.kaspiandev.antipopup.AntiPopup; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerKickEvent; - -public class KickListener implements Listener { - - @EventHandler(priority = EventPriority.LOWEST) - public void onKick(PlayerKickEvent event) { - // Couldnt test - if (AntiPopup.config.getBoolean("prevent-time-sync-kick", false)) { - if (event.getReason().equals("Out-of-order chat packet received. Did your system time change?")) - event.setCancelled(true); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/github/kaspiandev/antipopup/listeners/PacketEventsListener.java b/src/main/java/com/github/kaspiandev/antipopup/listeners/PacketEventsListener.java index d283224..11b23f9 100644 --- a/src/main/java/com/github/kaspiandev/antipopup/listeners/PacketEventsListener.java +++ b/src/main/java/com/github/kaspiandev/antipopup/listeners/PacketEventsListener.java @@ -7,15 +7,19 @@ import com.github.retrooper.packetevents.protocol.chat.message.ChatMessage; import com.github.retrooper.packetevents.protocol.chat.message.ChatMessage_v1_19_1; import com.github.retrooper.packetevents.protocol.packettype.PacketType; +import com.github.retrooper.packetevents.protocol.player.ClientVersion; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerChatMessage; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerServerData; import com.github.retrooper.packetevents.wrapper.status.server.WrapperStatusServerResponse; import com.google.gson.JsonObject; +import dev.dejvokep.boostedyaml.YamlDocument; import java.util.UUID; public class PacketEventsListener extends PacketListenerAbstract { + private final YamlDocument yamlDoc = AntiPopup.getYamlDoc(); + public PacketEventsListener() { super(PacketListenerPriority.LOW); } @@ -23,6 +27,7 @@ public PacketEventsListener() { @Override public void onPacketSend(PacketSendEvent event) { if (event.getPacketType() == PacketType.Status.Server.RESPONSE) { + if (event.getUser().getClientVersion().isOlderThan(ClientVersion.V_1_19_1)) return; WrapperStatusServerResponse wrapper = new WrapperStatusServerResponse(event); JsonObject newObj = wrapper.getComponent(); newObj.addProperty("preventsChatReports", true); @@ -33,11 +38,11 @@ public void onPacketSend(PacketSendEvent event) { serverData.setEnforceSecureChat(true); } if (event.getPacketType() == PacketType.Play.Server.CHAT_MESSAGE - && AntiPopup.config.getBoolean("strip-signature", true)) { + && yamlDoc.getBoolean("strip-signature") + && yamlDoc.getString("mode").equals("PACKET")) { WrapperPlayServerChatMessage chatMessage = new WrapperPlayServerChatMessage(event); ChatMessage message = chatMessage.getMessage(); if (message instanceof ChatMessage_v1_19_1 v1_19_1) { - // We wanna trick the system by giving it random crap v1_19_1.setSignature(new byte[0]); v1_19_1.setSalt(0); v1_19_1.setSenderUUID(new UUID(0L, 0L)); @@ -45,7 +50,7 @@ public void onPacketSend(PacketSendEvent event) { } } if (event.getPacketType() == PacketType.Play.Server.PLAYER_CHAT_HEADER - && AntiPopup.config.getBoolean("dont-send-header", true)) { + && yamlDoc.getBoolean("dont-send-header")) { event.setCancelled(true); } } diff --git a/src/main/java/com/github/kaspiandev/antipopup/nms/AbstractInjector.java b/src/main/java/com/github/kaspiandev/antipopup/nms/AbstractInjector.java new file mode 100644 index 0000000..192c113 --- /dev/null +++ b/src/main/java/com/github/kaspiandev/antipopup/nms/AbstractInjector.java @@ -0,0 +1,9 @@ +package com.github.kaspiandev.antipopup.nms; + +import org.bukkit.entity.Player; + +public interface AbstractInjector { + + void inject(Player player); + void uninject(Player player); +} \ No newline at end of file diff --git a/src/main/java/com/github/kaspiandev/antipopup/nms/PlayerListener.java b/src/main/java/com/github/kaspiandev/antipopup/nms/PlayerListener.java new file mode 100644 index 0000000..008106c --- /dev/null +++ b/src/main/java/com/github/kaspiandev/antipopup/nms/PlayerListener.java @@ -0,0 +1,26 @@ +package com.github.kaspiandev.antipopup.nms; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +public class PlayerListener implements Listener { + + private final AbstractInjector injector; + + public PlayerListener(AbstractInjector injector) { + this.injector = injector; + } + + @EventHandler + public void onJoin(PlayerJoinEvent event) { + injector.inject(event.getPlayer()); + } + + @EventHandler + public void onLeave(PlayerQuitEvent event) { + injector.uninject(event.getPlayer()); + } + +} \ No newline at end of file diff --git a/src/main/java/com/github/kaspiandev/antipopup/nms/v1_19_3/PlayerInjector.java b/src/main/java/com/github/kaspiandev/antipopup/nms/v1_19_3/PlayerInjector.java new file mode 100644 index 0000000..10396a6 --- /dev/null +++ b/src/main/java/com/github/kaspiandev/antipopup/nms/v1_19_3/PlayerInjector.java @@ -0,0 +1,50 @@ +package com.github.kaspiandev.antipopup.nms.v1_19_3; + +import com.github.kaspiandev.antipopup.nms.AbstractInjector; +import io.netty.channel.*; +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 org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_19_R2.CraftServer; +import org.bukkit.craftbukkit.v1_19_R2.entity.CraftPlayer; +import org.bukkit.entity.Player; + +import java.util.Optional; + +public class PlayerInjector implements AbstractInjector { + + 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()); + } + Optional chatType = chatPacket.chatType().resolve( + ((CraftServer) Bukkit.getServer()).getServer().registryAccess()); + + ((CraftPlayer) player).getHandle().connection.send( + new ClientboundSystemChatPacket(chatType.orElseThrow().decorate(content), false)); + return; + } + super.write(ctx, packet, promise); + } + }; + + ChannelPipeline pipeline = ((CraftPlayer) player).getHandle() + .connection.getConnection().channel.pipeline(); + pipeline.addBefore("packet_handler", "antipopup_handler", duplexHandler); + } + + public void uninject(Player player) { + Channel channel = ((CraftPlayer) player).getHandle().connection.getConnection().channel; + channel.eventLoop().submit(() -> { + channel.pipeline().remove(player.getName()); + return null; + }); + } +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index bb0bc7c..b318c52 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,4 +1,4 @@ -config-version: 23 +config-version: 28 # Should we collect anonymous data about your server? # false by default. @@ -10,14 +10,6 @@ bstats: false # incident to Github issues of AntiPopup. filter-not-secure: true -# Should we suppress messages about unsynced time? -sync-time-suppress: false - -# Give it a try if players get kicked -# with message about unsynced message time. -# UNTESTED -prevent-time-sync-kick: false - # Should we prevent messages from being reported? # Leave as true unless you have a different plugin stripping those. strip-signature: true @@ -30,10 +22,15 @@ dont-send-header: true # Useful for all kinds of setups or server hosts. auto-setup: false -# Should we add back URLs in chat messages? -# Might cause incompabilities. -enable-urls: false +# NMS - Only for 1.19.3 +# PACKET - Only up to 1.19.2 +mode: PACKET + +# Readd clickable URLs in messages. +# Could cause incompabilities! +clickable-urls: false # Do not touch ones below! first-run: true ask-bstats: true +setup-mode: true \ No newline at end of file