Skip to content

Commit

Permalink
reloading rework, allows you to have per player configurations & othe…
Browse files Browse the repository at this point in the history
…r things, doesn't cover every option yet
  • Loading branch information
AoElite committed Oct 10, 2024
1 parent 3cab3fe commit de0c28b
Show file tree
Hide file tree
Showing 20 changed files with 273 additions and 134 deletions.
9 changes: 5 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ plugins {
group = "ac.grim.grimac"
version = "2.3.68"
description = "Libre simulation anticheat designed for 1.21 with 1.8-1.21 support, powered by PacketEvents 2.0."
java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8
java.sourceCompatibility = JavaVersion.VERSION_17
java.targetCompatibility = JavaVersion.VERSION_17

// Set to false for debug builds
// You cannot live reload classes if the jar relocates dependencies
Expand Down Expand Up @@ -44,8 +44,9 @@ dependencies {
implementation("it.unimi.dsi:fastutil:8.5.13")
implementation("github.scarsz:configuralize:1.4.0")

implementation("com.github.grimanticheat:grimapi:1193c4fa41")
// Used for local testing: implementation("ac.grim.grimac:grimapi:1.0")
//implementation("com.github.grimanticheat:grimapi:1193c4fa41")
// Used for local testing: implementation("ac.grim.grimac:GRIMAPI:1.0")
implementation("com.github.grimanticheat:grimapi:fc5634e444")

implementation("org.jetbrains:annotations:24.1.0")
compileOnly("org.geysermc.floodgate:api:2.0-SNAPSHOT")
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ac/grim/grimac/GrimAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public enum GrimAPI {
private final TickManager tickManager = new TickManager();
private final GrimExternalAPI externalAPI = new GrimExternalAPI(this);
private InitManager initManager;
private ConfigManager configManager;
private ConfigManagerImpl configManager;
private JavaPlugin plugin;

public void load(final JavaPlugin plugin) {
this.plugin = plugin;
this.configManager = new ConfigManager();
this.configManager = new ConfigManagerImpl();
initManager = new InitManager();
initManager.load();
}
Expand Down
117 changes: 92 additions & 25 deletions src/main/java/ac/grim/grimac/GrimExternalAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@
import ac.grim.grimac.api.GrimAbstractAPI;
import ac.grim.grimac.api.GrimUser;
import ac.grim.grimac.api.alerts.AlertManager;
import ac.grim.grimac.api.config.ConfigManager;
import ac.grim.grimac.api.events.GrimReloadEvent;
import ac.grim.grimac.manager.init.Initable;
import ac.grim.grimac.player.GrimPlayer;
import ac.grim.grimac.utils.common.ConfigReloadObserver;
import com.github.retrooper.packetevents.netty.channel.ChannelHelper;
import io.github.retrooper.packetevents.util.SpigotReflectionUtil;
import io.github.retrooper.packetevents.util.folia.FoliaScheduler;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

//This is used for grim's external API. It has its own class just for organization.
public class GrimExternalAPI implements GrimAbstractAPI, Initable {

public class GrimExternalAPI implements GrimAbstractAPI, ConfigReloadObserver, Initable {

private final GrimAPI api;

Expand All @@ -41,7 +48,8 @@ public void setServerName(String name) {
@Getter
private final Map<String, Function<GrimUser, String>> variableReplacements = new ConcurrentHashMap<>();

@Getter private final Map<String, String> staticReplacements = new ConcurrentHashMap<>();
@Getter
private final Map<String, String> staticReplacements = new ConcurrentHashMap<>();

public String replaceVariables(GrimUser user, String content, boolean colors) {
if (colors) content = ChatColor.translateAlternateColorCodes('&', content);
Expand All @@ -56,12 +64,20 @@ public String replaceVariables(GrimUser user, String content, boolean colors) {

@Override
public void registerVariable(String string, Function<GrimUser, String> replacement) {
variableReplacements.put(string, replacement);
if (replacement == null) {
variableReplacements.remove(string);
} else {
variableReplacements.put(string, replacement);
}
}

@Override
public void registerVariable(String variable, String replacement) {
staticReplacements.put(variable, replacement);
if (replacement == null) {
staticReplacements.remove(variable);
} else {
staticReplacements.put(variable, replacement);
}
}

@Override
Expand All @@ -70,43 +86,37 @@ public String getGrimVersion() {
return description.getVersion();
}

private final Map<String, Function<Object, Object>> functions = new ConcurrentHashMap<>();

@Override
public void registerFunction(String key, Function<Object, Object> function) {

if (function == null) {
functions.remove(key);
} else {
functions.put(key, function);
}
}

@Override
public Function<Object, Object> getFunction(String key) {
return null;
return functions.get(key);
}

@Override
public void reload() {
GrimAPI.INSTANCE.getConfigManager().reload();
//Reload checks for all players
for (GrimPlayer grimPlayer : GrimAPI.INSTANCE.getPlayerDataManager().getEntries()) {
ChannelHelper.runInEventLoop(grimPlayer.user.getChannel(), () -> {
grimPlayer.onReload();
grimPlayer.updatePermissions();
grimPlayer.punishmentManager.reload();
for (AbstractCheck value : grimPlayer.checkManager.allChecks.values()) {
value.reload();
}
});
}
//Restart
GrimAPI.INSTANCE.getDiscordManager().start();
GrimAPI.INSTANCE.getSpectateManager().start();
GrimAPI.INSTANCE.getExternalAPI().start();
public AlertManager getAlertManager() {
return GrimAPI.INSTANCE.getAlertManager();
}

@Override
public AlertManager getAlertManager() {
return GrimAPI.INSTANCE.getAlertManager();
public ConfigManager getConfigManager() {
return configManager;
}

private ConfigManager configManager = null;

@Override
public void start() {
if (configManager == null) configManager = GrimAPI.INSTANCE.getConfigManager();
variableReplacements.put("%player%", GrimUser::getName);
variableReplacements.put("%uuid%", user -> user.getUniqueId().toString());
variableReplacements.put("%ping%", user -> user.getTransactionPing() + "");
Expand All @@ -118,4 +128,61 @@ public void start() {
variableReplacements.put("%version%", GrimUser::getVersionName);
variableReplacements.put("%prefix%", user -> ChatColor.translateAlternateColorCodes('&', GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("prefix", "&bGrim &8»")));
}

@Override
public void reload(ConfigManager config) {
if (config.isLoadedAsync()) {
FoliaScheduler.getAsyncScheduler().runNow(GrimAPI.INSTANCE.getPlugin(),
o -> successfulReload(config));
} else {
successfulReload(config);
}
}

@Override
public CompletableFuture<Boolean> reloadAsync(ConfigManager config) {
if (config.isLoadedAsync()) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
FoliaScheduler.getAsyncScheduler().runNow(GrimAPI.INSTANCE.getPlugin(),
o -> future.complete(successfulReload(config)));
return future;
}
return CompletableFuture.completedFuture(successfulReload(config));
}

private boolean successfulReload(ConfigManager config) {
try {
config.reload();
onReload(config);
FoliaScheduler.getAsyncScheduler().runNow(GrimAPI.INSTANCE.getPlugin(),
o -> Bukkit.getPluginManager().callEvent(new GrimReloadEvent(true)));
return true;
} catch (Exception e) {
e.printStackTrace();
}
FoliaScheduler.getAsyncScheduler().runNow(GrimAPI.INSTANCE.getPlugin(),
o -> Bukkit.getPluginManager().callEvent(new GrimReloadEvent(false)));
return false;
}

@Override
public void onReload(ConfigManager newConfig) {
configManager = newConfig != null ? newConfig : configManager;
//Reload checks for all players
for (GrimPlayer grimPlayer : GrimAPI.INSTANCE.getPlayerDataManager().getEntries()) {
ChannelHelper.runInEventLoop(grimPlayer.user.getChannel(), () -> {
grimPlayer.reload(configManager);
grimPlayer.updatePermissions();
grimPlayer.punishmentManager.reload(configManager);
for (AbstractCheck value : grimPlayer.checkManager.allChecks.values()) {
value.reload(configManager);
}
});
}
//Restart
GrimAPI.INSTANCE.getDiscordManager().start();
GrimAPI.INSTANCE.getSpectateManager().start();
GrimAPI.INSTANCE.getExternalAPI().start();
}

}
33 changes: 20 additions & 13 deletions src/main/java/ac/grim/grimac/checks/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import ac.grim.grimac.GrimAPI;
import ac.grim.grimac.api.AbstractCheck;
import ac.grim.grimac.api.config.ConfigManager;
import ac.grim.grimac.api.events.FlagEvent;
import ac.grim.grimac.player.GrimPlayer;
import ac.grim.grimac.utils.common.ConfigReloadObserver;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import github.scarsz.configuralize.DynamicConfig;
import io.github.retrooper.packetevents.util.folia.FoliaScheduler;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;

// Class from https://github.com/Tecnio/AntiCheatBase/blob/master/src/main/java/me/tecnio/anticheat/check/Check.java
@Getter
public class Check implements AbstractCheck {
public class Check implements AbstractCheck, ConfigReloadObserver {
protected final GrimPlayer player;

public double violations;
Expand Down Expand Up @@ -53,8 +54,8 @@ public Check(final GrimPlayer player) {
this.experimental = checkData.experimental();
this.description = checkData.description();
}

reload();
//
reload(GrimAPI.INSTANCE.getConfigManager().getConfig());
}

public boolean shouldModifyPackets() {
Expand Down Expand Up @@ -107,21 +108,22 @@ public final void reward() {
violations = Math.max(0, violations - decay);
}

public void reload() {
decay = getConfig().getDoubleElse(configName + ".decay", decay);
setbackVL = getConfig().getDoubleElse(configName + ".setbackvl", setbackVL);

@Override
public void reload(ConfigManager configuration) {
decay = configuration.getDoubleElse(configName + ".decay", decay);
setbackVL = configuration.getDoubleElse(configName + ".setbackvl", setbackVL);
if (setbackVL == -1) setbackVL = Double.MAX_VALUE;

updateExempted();
onReload(configuration);
}

public boolean alert(String verbose) {
return player.punishmentManager.handleAlert(player, verbose, this);
@Override
public void onReload(ConfigManager config) {

}

public DynamicConfig getConfig() {
return GrimAPI.INSTANCE.getConfigManager().getConfig();
public boolean alert(String verbose) {
return player.punishmentManager.handleAlert(player, verbose, this);
}

public boolean setbackIfAboveSetbackVL() {
Expand All @@ -144,5 +146,10 @@ public boolean isTransaction(PacketTypeCommon packetType) {
packetType == PacketType.Play.Client.WINDOW_CONFIRMATION;
}

@Override
public void reload() {
reload(GrimAPI.INSTANCE.getConfigManager().getConfig());
}

}

8 changes: 4 additions & 4 deletions src/main/java/ac/grim/grimac/checks/impl/combat/Reach.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package ac.grim.grimac.checks.impl.combat;

import ac.grim.grimac.api.config.ConfigManager;
import ac.grim.grimac.checks.Check;
import ac.grim.grimac.checks.CheckData;
import ac.grim.grimac.checks.type.PacketCheck;
Expand Down Expand Up @@ -236,9 +237,8 @@ private String checkReach(PacketEntity reachEntity, Vector3d from, boolean isPre
}

@Override
public void reload() {
super.reload();
this.cancelImpossibleHits = getConfig().getBooleanElse("Reach.block-impossible-hits", true);
this.threshold = getConfig().getDoubleElse("Reach.threshold", 0.0005);
public void onReload(ConfigManager config) {
this.cancelImpossibleHits = config.getBooleanElse("Reach.block-impossible-hits", true);
this.threshold = config.getDoubleElse("Reach.threshold", 0.0005);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ac.grim.grimac.checks.impl.misc;

import ac.grim.grimac.api.config.ConfigManager;
import ac.grim.grimac.checks.type.BlockPlaceCheck;
import ac.grim.grimac.player.GrimPlayer;
import ac.grim.grimac.utils.anticheat.update.BlockPlace;
Expand Down Expand Up @@ -63,10 +64,9 @@ public void onBlockPlace(final BlockPlace place) {
}

@Override
public void reload() {
super.reload();
allow = getConfig().getBooleanElse("exploit.allow-building-on-ghostblocks", true);
distance = getConfig().getIntElse("exploit.distance-to-check-for-ghostblocks", 2);
public void onReload(ConfigManager config) {
allow = config.getBooleanElse("exploit.allow-building-on-ghostblocks", true);
distance = config.getIntElse("exploit.distance-to-check-for-ghostblocks", 2);

if (distance < 2 || distance > 4) distance = 2;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ac.grim.grimac.checks.impl.movement;

import ac.grim.grimac.api.config.ConfigManager;
import ac.grim.grimac.checks.CheckData;
import ac.grim.grimac.checks.type.PostPredictionCheck;
import ac.grim.grimac.player.GrimPlayer;
Expand Down Expand Up @@ -35,8 +36,7 @@ public void doCheck(final PacketReceiveEvent event) {
}

@Override
public void reload() {
super.reload();
clockDrift = (long) (getConfig().getDoubleElse(getConfigName() + ".drift", 1200.0) * 1e6);
public void onReload(ConfigManager config) {
clockDrift = (long) (config.getDoubleElse(getConfigName() + ".drift", 1200.0) * 1e6);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ac.grim.grimac.checks.impl.movement;

import ac.grim.grimac.api.config.ConfigManager;
import ac.grim.grimac.checks.Check;
import ac.grim.grimac.checks.CheckData;
import ac.grim.grimac.checks.type.PostPredictionCheck;
Expand Down Expand Up @@ -51,8 +52,7 @@ public void handlePredictionAnalysis(double offset) {
}

@Override
public void reload() {
super.reload();
offsetToFlag = getConfig().getDoubleElse("NoSlowA.threshold", 0.001);
public void onReload(ConfigManager config) {
offsetToFlag = config.getDoubleElse("NoSlowA.threshold", 0.001);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ac.grim.grimac.checks.impl.movement;

import ac.grim.grimac.api.config.ConfigManager;
import ac.grim.grimac.checks.Check;
import ac.grim.grimac.checks.CheckData;
import ac.grim.grimac.checks.type.PacketCheck;
Expand Down Expand Up @@ -115,9 +116,8 @@ public boolean shouldCountPacketForTimer(PacketTypeCommon packetType) {
}

@Override
public void reload() {
super.reload();
clockDrift = (long) (getConfig().getDoubleElse(getConfigName() + ".drift", 120.0) * 1e6);
limitAbuseOverPing = (long) (getConfig().getDoubleElse(getConfigName() + ".ping-abuse-limit-threshold", 1000));
public void onReload(ConfigManager config) {
clockDrift = (long) (config.getDoubleElse(getConfigName() + ".drift", 120.0) * 1e6);
limitAbuseOverPing = (long) (config.getDoubleElse(getConfigName() + ".ping-abuse-limit-threshold", 1000));
}
}
Loading

0 comments on commit de0c28b

Please sign in to comment.