Skip to content

Commit

Permalink
8.1 (#88)
Browse files Browse the repository at this point in the history
* Create a hook system and add a new viaversion hook

* Fixes for via hook
  • Loading branch information
KaspianDev authored May 10, 2024
1 parent a4872f3 commit 78b7730
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 67 deletions.
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tasks {

allprojects {
group = "com.github.kaspiandev.antipopup"
version = "8"
version = "8.1"

repositories {
mavenCentral()
Expand All @@ -42,6 +42,11 @@ allprojects {
url = "https://repo.codemc.io/repository/maven-releases/"
}
}

java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
}

tasks.register("buildAndMove", Copy) {
Expand Down
13 changes: 1 addition & 12 deletions spigot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.kaspiandev.antipopup.spigot.hook;

public interface Hook {

String getPluginName();

void register();

}
Original file line number Diff line number Diff line change
@@ -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<Hook> 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();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -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<T extends Protocol<?, ?, ?, ?>> {

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<T> getProtocolClass();

public VersionComparison getComparison() {
return comparison;
}

public ServerVersion getVersion() {
return version;
}

}
Original file line number Diff line number Diff line change
@@ -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.Iterator;
import java.util.List;

public class ViaVersionHook implements Hook {

private final List<Class<? extends ViaProtocolModifier<?>>> modifiers;
private final List<ViaProtocolModifier<?>> registeredModifiers;

public ViaVersionHook() {
this.modifiers = new ArrayList<>();
this.registeredModifiers = new ArrayList<>();
}

public void addModifier(Class<? extends ViaProtocolModifier<?>> modifier) {
modifiers.add(modifier);
}

@Override
public String getPluginName() {
return "ViaVersion";
}

@Override
public void register() {
ServerVersion serverVersion = PacketEvents.getAPI().getServerManager().getVersion();
Iterator<Class<? extends ViaProtocolModifier<?>>> iterator = modifiers.iterator();
while (iterator.hasNext())
try {
Class<? extends ViaProtocolModifier<?>> modifier = iterator.next();
ViaProtocolModifier<?> modifierInstance = modifier.getDeclaredConstructor().newInstance();

if (serverVersion.is(modifierInstance.getComparison(), modifierInstance.getVersion())) {
modifierInstance.modify();
registeredModifiers.add(modifierInstance);
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<ViaProtocolModifier<?>> getRegisteredModifiers() {
return registeredModifiers;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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<Protocol1_19_1To1_19> {

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.set(Type.BOOLEAN, 1, true);
});
}

@Override
public Class<Protocol1_19_1To1_19> getProtocolClass() {
return Protocol1_19_1To1_19.class;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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<Protocol1_20_5To1_20_3> {

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.set(Type.BOOLEAN, 6, true); // Enforces secure chat
});
}

@Override
public Class<Protocol1_20_5To1_20_3> getProtocolClass() {
return Protocol1_20_5To1_20_3.class;
}

}

0 comments on commit 78b7730

Please sign in to comment.