diff --git a/build.gradle b/build.gradle index 75b3521..a6da89b 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ tasks { allprojects { group = "com.github.kaspiandev.antipopup" - version = "8" + version = "8.1" repositories { mavenCentral() @@ -42,6 +42,11 @@ allprojects { url = "https://repo.codemc.io/repository/maven-releases/" } } + + java { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 + } } tasks.register("buildAndMove", Copy) { diff --git a/spigot/build.gradle b/spigot/build.gradle index 4bbc17b..cc27e8d 100644 --- a/spigot/build.gradle +++ b/spigot/build.gradle @@ -22,7 +22,7 @@ dependencies { implementation project(path: ":v1.20.6", configuration: "reobf") compileOnly "org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT" - compileOnly "com.viaversion:viaversion:4.6.1" + compileOnly "com.viaversion:viaversion:4.10.1" compileOnly "org.apache.logging.log4j:log4j-core:2.19.0" implementation "org.bstats:bstats-bukkit:3.0.0" @@ -44,17 +44,6 @@ tasks { } } -def targetJavaVersion = 21 - -java { - def javaVersion = JavaVersion.toVersion(targetJavaVersion) - sourceCompatibility = javaVersion - targetCompatibility = javaVersion - if (JavaVersion.current() < javaVersion) { - toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion) - } -} - processResources { def props = [version: version] inputs.properties props 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 20bab37..a9d284f 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,9 +12,13 @@ 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.spigot.api.Api; +import com.github.kaspiandev.antipopup.spigot.hook.HookManager; +import com.github.kaspiandev.antipopup.spigot.hook.viaversion.ViaVersionHook; +import com.github.kaspiandev.antipopup.spigot.hook.viaversion.Via_1_20_4_to_1_20_5; import com.github.kaspiandev.antipopup.spigot.listeners.ChatListener; import com.github.kaspiandev.antipopup.spigot.nms.PlayerListener; import com.github.kaspiandev.antipopup.spigot.platform.SpigotPlatform; +import com.github.kaspiandev.antipopup.spigot.hook.viaversion.Via_1_19_to_1_19_1; import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.manager.server.ServerManager; import com.github.retrooper.packetevents.manager.server.ServerVersion; @@ -30,7 +34,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; import java.util.Objects; import java.util.Properties; import java.util.concurrent.TimeUnit; @@ -39,6 +42,7 @@ public final class AntiPopup extends JavaPlugin { + private static AntiPopup instance; private static File propertiesFile; private static APConfig config; private static FoliaLib foliaLib; @@ -51,8 +55,13 @@ public static File getPropertiesFile() { return propertiesFile; } + public static AntiPopup getInstance() { + return instance; + } + @Override public void onLoad() { + instance = this; PacketEvents.setAPI(SpigotPacketEventsBuilder.build(this)); PacketEvents.getAPI().getSettings().debug(false).bStats(false).checkForUpdates(false); PacketEvents.getAPI().load(); @@ -79,17 +88,15 @@ public void onEnable() { getLogger().info("Loaded optional metrics."); } - if (getPluginManager().getPlugin("ViaVersion") != null - && PacketEvents.getAPI().getServerManager().getVersion().equals(ServerVersion.V_1_19)) { - try { - var hookClass = ViaHook.class; - hookClass.getConstructor().newInstance(); - getLogger().info("Enabled 1.19 ViaVersion Hook."); - } catch (InvocationTargetException | InstantiationException | IllegalAccessException | - NoSuchMethodException ex) { - throw new RuntimeException(ex); - } - } + ServerManager serverManager = PacketEvents.getAPI().getServerManager(); + PluginManager pluginManager = getPluginManager(); + + HookManager hookManager = new HookManager(); + ViaVersionHook viaVersionHook = new ViaVersionHook(); + viaVersionHook.addModifier(Via_1_19_to_1_19_1.class); + viaVersionHook.addModifier(Via_1_20_4_to_1_20_5.class); + hookManager.addHook(viaVersionHook); + hookManager.load(); if (config.isClickableUrls()) { getServer().getPluginManager().registerEvents(new ChatListener(), this); @@ -100,9 +107,6 @@ public void onEnable() { PacketEvents.getAPI().init(); getLogger().info("Initiated PacketEvents."); - PluginManager pluginManager = getServer().getPluginManager(); - ServerManager serverManager = PacketEvents.getAPI().getServerManager(); - if (config.isBlockChatReports() && serverManager.getVersion().isOlderThan(ServerVersion.V_1_19_1)) { config.setBlockChatReports(false); diff --git a/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/ViaHook.java b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/ViaHook.java deleted file mode 100644 index a18170b..0000000 --- a/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/ViaHook.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.github.kaspiandev.antipopup.spigot; - -import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.protocol.Protocol; -import com.viaversion.viaversion.api.protocol.packet.State; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; -import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; -import com.viaversion.viaversion.api.type.Type; - -// Temp -@SuppressWarnings("deprecation") -public class ViaHook { - - public ViaHook() { - var viaProtocolManager = Via.getManager().getProtocolManager(); - Protocol protocol = viaProtocolManager.getProtocol( - ProtocolVersion.v1_19_1, ProtocolVersion.v1_19); - - if (protocol == null) return; - - // 0x3F - 1.19 SERVER_DATA packet. - // 0x42 - 1.19.1+ SERVER_DATA packet. - protocol.registerClientbound(State.PLAY, 0x3F, 0x42, - new ServerDataRemapper(), true); - } - - private static class ServerDataRemapper extends PacketRemapper { - - @Override - public void registerMap() { - map(Type.OPTIONAL_COMPONENT); - map(Type.OPTIONAL_STRING); - map(Type.BOOLEAN); - create(Type.BOOLEAN, true); - } - - } - -} diff --git a/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/Hook.java b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/Hook.java new file mode 100644 index 0000000..2f4bbbb --- /dev/null +++ b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/Hook.java @@ -0,0 +1,9 @@ +package com.github.kaspiandev.antipopup.spigot.hook; + +public interface Hook { + + String getPluginName(); + + void register(); + +} diff --git a/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/HookManager.java b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/HookManager.java new file mode 100644 index 0000000..8bfc99d --- /dev/null +++ b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/HookManager.java @@ -0,0 +1,32 @@ +package com.github.kaspiandev.antipopup.spigot.hook; + +import com.github.kaspiandev.antipopup.spigot.AntiPopup; +import org.bukkit.Bukkit; +import org.bukkit.plugin.PluginManager; + +import java.util.ArrayList; +import java.util.List; + +public class HookManager { + + private final List hooks; + + public HookManager() { + this.hooks = new ArrayList<>(); + } + + public void addHook(Hook hook) { + hooks.add(hook); + } + + public void load() { + PluginManager pluginManager = Bukkit.getServer().getPluginManager(); + for (Hook hook : hooks) { + if (pluginManager.isPluginEnabled(hook.getPluginName())) { + AntiPopup.getInstance().getLogger().info("Loaded a hook for " + hook.getPluginName() + "."); + hook.register(); + } + } + } + +} diff --git a/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/ViaProtocolModifier.java b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/ViaProtocolModifier.java new file mode 100644 index 0000000..faaac4e --- /dev/null +++ b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/ViaProtocolModifier.java @@ -0,0 +1,34 @@ +package com.github.kaspiandev.antipopup.spigot.hook.viaversion; + +import com.github.retrooper.packetevents.manager.server.ServerVersion; +import com.github.retrooper.packetevents.manager.server.VersionComparison; +import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.protocol.Protocol; + +public abstract class ViaProtocolModifier> { + + protected final T protocol; + protected final VersionComparison comparison; + protected final ServerVersion version; + + protected ViaProtocolModifier(VersionComparison comparison, ServerVersion version) throws NullPointerException { + T protocol = Via.getManager().getProtocolManager().getProtocol(getProtocolClass()); + if (protocol == null) throw new NullPointerException(); + this.protocol = protocol; + this.comparison = comparison; + this.version = version; + } + + protected abstract void modify(); + + protected abstract Class getProtocolClass(); + + public VersionComparison getComparison() { + return comparison; + } + + public ServerVersion getVersion() { + return version; + } + +} diff --git a/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/ViaVersionHook.java b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/ViaVersionHook.java new file mode 100644 index 0000000..b8fc7cf --- /dev/null +++ b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/ViaVersionHook.java @@ -0,0 +1,69 @@ +package com.github.kaspiandev.antipopup.spigot.hook.viaversion; + +import com.github.kaspiandev.antipopup.spigot.AntiPopup; +import com.github.kaspiandev.antipopup.spigot.hook.Hook; +import com.github.retrooper.packetevents.PacketEvents; +import com.github.retrooper.packetevents.manager.server.ServerVersion; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; +import java.util.StringJoiner; + +public class ViaVersionHook implements Hook { + + private final List>> modifiers; + private final List> registeredModifiers; + + public ViaVersionHook() { + this.modifiers = new ArrayList<>(); + this.registeredModifiers = new ArrayList<>(); + } + + public void addModifier(Class> modifier) { + modifiers.add(modifier); + } + + @Override + public String getPluginName() { + return "ViaVersion"; + } + + @Override + public void register() { + ServerVersion serverVersion = PacketEvents.getAPI().getServerManager().getVersion(); + for (Class> modifier : modifiers) { + try { + ViaProtocolModifier modifierInstance = modifier.getDeclaredConstructor().newInstance(); + + if (serverVersion.is(modifierInstance.getComparison(), modifierInstance.getVersion())) { + modifierInstance.modify(); + registeredModifiers.add(modifierInstance); + modifiers.remove(modifier); + AntiPopup.getInstance().getLogger().info(() -> getRegisteredMessage(modifierInstance)); + } + } catch (IllegalAccessException | InvocationTargetException + | NoSuchMethodException | InstantiationException ignored) {} + } + } + + private String getRegisteredMessage(ViaProtocolModifier modifier) { + String comparison = switch (modifier.getComparison()) { + case EQUALS -> "equal to"; + case OLDER_THAN -> "older than"; + case NEWER_THAN -> "newer than"; + case NEWER_THAN_OR_EQUALS -> "newer than or equal to"; + case OLDER_THAN_OR_EQUALS -> "older than or equal to"; + }; + return "Registered a modifier targeted for versions " + + comparison + + " " + + modifier.getVersion().getReleaseName() + + "."; + } + + public List> getRegisteredModifiers() { + return registeredModifiers; + } + +} diff --git a/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/Via_1_19_to_1_19_1.java b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/Via_1_19_to_1_19_1.java new file mode 100644 index 0000000..b91d3f8 --- /dev/null +++ b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/Via_1_19_to_1_19_1.java @@ -0,0 +1,30 @@ +package com.github.kaspiandev.antipopup.spigot.hook.viaversion; + +import com.github.retrooper.packetevents.manager.server.ServerVersion; +import com.github.retrooper.packetevents.manager.server.VersionComparison; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.Protocol1_19_1To1_19; +import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ClientboundPackets1_19; + +public class Via_1_19_to_1_19_1 extends ViaProtocolModifier { + + public Via_1_19_to_1_19_1() { + super(VersionComparison.EQUALS, ServerVersion.V_1_19); + } + + @Override + public void modify() { + protocol.appendClientbound(ClientboundPackets1_19.SERVER_DATA, (wrapper) -> { + wrapper.passthrough(Type.OPTIONAL_COMPONENT); + wrapper.passthrough(Type.OPTIONAL_STRING); + wrapper.passthrough(Type.BOOLEAN); + wrapper.write(Type.BOOLEAN, true); + }); + } + + @Override + public Class getProtocolClass() { + return Protocol1_19_1To1_19.class; + } + +} diff --git a/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/Via_1_20_4_to_1_20_5.java b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/Via_1_20_4_to_1_20_5.java new file mode 100644 index 0000000..869f151 --- /dev/null +++ b/spigot/src/main/java/com/github/kaspiandev/antipopup/spigot/hook/viaversion/Via_1_20_4_to_1_20_5.java @@ -0,0 +1,45 @@ +package com.github.kaspiandev.antipopup.spigot.hook.viaversion; + +import com.github.retrooper.packetevents.manager.server.ServerVersion; +import com.github.retrooper.packetevents.manager.server.VersionComparison; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPackets1_20_3; +import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.Protocol1_20_5To1_20_3; + +public class Via_1_20_4_to_1_20_5 extends ViaProtocolModifier { + + public Via_1_20_4_to_1_20_5() { + super(VersionComparison.OLDER_THAN, ServerVersion.V_1_20_5); + } + + @Override + public void modify() { + protocol.appendClientbound(ClientboundPackets1_20_3.JOIN_GAME, (wrapper) -> { + wrapper.passthrough(Type.INT); // Entity ID + wrapper.passthrough(Type.BOOLEAN); // Hardcore + wrapper.passthrough(Type.STRING_ARRAY); // World List + wrapper.passthrough(Type.VAR_INT); // Max players + wrapper.passthrough(Type.VAR_INT); // View distance + wrapper.passthrough(Type.VAR_INT); // Simulation distance + wrapper.passthrough(Type.BOOLEAN); // Reduced debug info + wrapper.passthrough(Type.BOOLEAN); // Show death screen + wrapper.passthrough(Type.BOOLEAN); // Limited crafting + wrapper.passthrough(Type.VAR_INT); // Dimension + wrapper.passthrough(Type.STRING); // World + wrapper.passthrough(Type.LONG); // Seed + wrapper.passthrough(Type.BYTE); // Gamemode + wrapper.passthrough(Type.BYTE); // Previous gamemode + wrapper.passthrough(Type.BOOLEAN); // Debug + wrapper.passthrough(Type.BOOLEAN); // Flat + wrapper.passthrough(Type.OPTIONAL_GLOBAL_POSITION); // Last death location + wrapper.passthrough(Type.VAR_INT); // Portal cooldown + wrapper.write(Type.BOOLEAN, true); // Enforces secure chat + }); + } + + @Override + public Class getProtocolClass() { + return Protocol1_20_5To1_20_3.class; + } + +}