Skip to content

Commit

Permalink
Drop support for Matrix AntiCheat versions before 2.3.59
Browse files Browse the repository at this point in the history
  • Loading branch information
2008Choco committed Aug 27, 2024
1 parent b1e8f2a commit 816c525
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 59 deletions.
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ junit = "5.10.2"

# (bukkit)
anticheat-aac = "5.0.0"
# anticheat-grim = "9f5aaef74b" # https://github.com/GrimAnticheat/GrimAPI/commit/9f5aaef74bf94bb3df5924a9c3f48d1ba9e2576d (REMOVED BECAUSE GRIM UPDATED IT'S API FORCING USE OF REFLECTION)
anticheat-grim = "1193c4fa41" # https://github.com/GrimAnticheat/GrimAPI/commit/1193c4fa41c7eb622645a8beafcc17034fc494f8
anticheat-matrix = "317d4635fd" # This repository is dead now, but we're going to keep using it because it exists on JitPack :)
anticheat-nocheatplus = "3.16.1-SNAPSHOT"
anticheat-spartan = "1.0"
Expand Down Expand Up @@ -39,7 +39,7 @@ junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", vers

# (bukkit)
anticheat-aac = { module = "de.janmm14:aac-api", version.ref = "anticheat-aac" }
# anticheat-grim = { module = "com.github.GrimAnticheat:GrimAPI", version.ref = "anticheat-grim" } (REMOVED BECAUSE GRIM UPDATED IT'S API FORCING USE OF REFLECTION)
anticheat-grim = { module = "com.github.GrimAnticheat:GrimAPI", version.ref = "anticheat-grim" }
anticheat-matrix = { module = "com.github.jiangdashao:matrix-api-repo", version.ref = "anticheat-matrix" }
anticheat-nocheatplus = { module = "fr.neatmonster:nocheatplus", version.ref = "anticheat-nocheatplus" }
anticheat-spartan = { module = "me.vagdedes:SpartanAPI", version.ref = "anticheat-spartan" }
Expand Down
2 changes: 1 addition & 1 deletion veinminer-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {

// Anti-cheats
compileOnly(libs.anticheat.aac)
// compileOnly(libs.anticheat.grim) // (REMOVED BECAUSE GRIM UPDATED IT'S API FORCING USE OF REFLECTION)
compileOnly(libs.anticheat.grim)
compileOnly(libs.anticheat.matrix)
compileOnly(libs.anticheat.nocheatplus)
compileOnly(libs.anticheat.spartan)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,38 @@
package wtf.choco.veinminer.anticheat;

import java.lang.reflect.Method;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.EventPriority;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;

import wtf.choco.veinminer.VeinMinerPlugin;

import ac.grim.grimac.api.events.FlagEvent;

/**
* The default Grim AntiCheat hook implementation.
*/
public final class AntiCheatHookGrim implements AntiCheatHook {

/*
* Unfortunately, in Grim 2.3.59 they decided to move the package declaration to "api". This is fine,
* it's correct. It should have been under an API package to begin with. But this now fricks up the way
* I write VeinMiner's Grim hook and forces me to use reflection unless I want to modularize VeinMiner's
* Bukkit code JUST for different Grim implementations... which I really do not want to do :(
*
* TODO: Once usage of GrimAC exceeds 75%, use of reflection should be dropped in favour of Grim's API
* again, thus dropping support for 2.3.58 and below.
*/
public final class AntiCheatHookGrim implements AntiCheatHook, Listener {

// FlagEvent is called asynchronously, so we need a ConcurrentHashMap to be certain
private final Set<UUID> exempt = ConcurrentHashMap.newKeySet();

private Method methodFlagEventGetPlayer;
private Method methodGrimUserGetUniqueId;

private boolean supported;

public AntiCheatHookGrim(@NotNull VeinMinerPlugin plugin) {
try {
Class<? extends Event> eventClass = findClass(Event.class, "ac.grim.grimac.events.FlagEvent", "ac.grim.grimac.api.events.FlagEvent");
Class<?> grimUserClass = findClass("ac.grim.grimac.GrimUser", "ac.grim.grimac.api.GrimUser");

this.methodFlagEventGetPlayer = eventClass.getMethod("getPlayer");
this.methodGrimUserGetUniqueId = grimUserClass.getMethod("getUniqueId");
this.supported = (methodFlagEventGetPlayer != null && methodGrimUserGetUniqueId != null);

if (supported) {
Bukkit.getPluginManager().registerEvent(eventClass, new Listener() {}, EventPriority.NORMAL, (listener, event) -> handleFlagEvent((Cancellable) event), plugin);
}
Class.forName("ac.grim.grimac.api.events.FlagEvent");
this.supported = true;
} catch (ReflectiveOperationException e) {
plugin.getLogger().severe("The version of GrimAC on this server is incompatible with Veinminer. Please post information on the spigot resource discussion page.");
this.supported = false;
}
}

private <T> Class<? extends T> findClass(Class<T> subclass, String... classNames) throws ClassNotFoundException {
for (String string : classNames) {
try {
Class<? extends T> clazz = Class.forName(string).asSubclass(subclass);
return clazz;
} catch (ReflectiveOperationException ignore) { }
}

throw new ClassNotFoundException();
}

private Class<?> findClass(String... classNames) throws ClassNotFoundException {
return findClass(Object.class, classNames);
}

@Override
public void exempt(@NotNull Player player) {
this.exempt.add(player.getUniqueId());
Expand All @@ -91,18 +53,9 @@ public boolean isSupported() {
return supported;
}

private void handleFlagEvent(Cancellable event) {
if (event.isCancelled()) {
return;
}

try {
Object player = methodFlagEventGetPlayer.invoke(event);
UUID playerUUID = (UUID) methodGrimUserGetUniqueId.invoke(player);
if (!exempt.contains(playerUUID)) {
return;
}
} catch (ReflectiveOperationException e) {
@EventHandler(ignoreCancelled = true)
private void onFlag(FlagEvent event) {
if (!exempt.contains(event.getPlayer().getUniqueId())) {
return;
}

Expand Down

0 comments on commit 816c525

Please sign in to comment.