Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/check reloading #10

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
108 changes: 107 additions & 1 deletion src/main/java/ac/grim/grimac/api/AbstractCheck.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,127 @@
package ac.grim.grimac.api;

import ac.grim.grimac.api.common.BasicStatus;
import ac.grim.grimac.api.dynamic.DefaultUnloadedBehavior;
import ac.grim.grimac.api.dynamic.UnloadedBehavior;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Base interface for all anti-cheat checks. Checks are modular components that detect specific
* types of cheating behavior. This interface defines the very core functionality that all checks
* must implement.
*/
public interface AbstractCheck extends AbstractProcessor, BasicStatus {

/**
* Gets the name of this check, used for identification and permissions.
* @return The check's name
*/
String getCheckName();

/**
* Gets an alternative name for this check, defaults to the check name.
* Useful for checks that may have multiple names or variants (ie AntiKB and AntiKnockback)
* @return The alternative check name
*/
default String getAlternativeName() {
return getCheckName();
}

/**
* Gets the current violation level for this check.
* @return Number of violations accumulated
*/
double getViolations();

/**
* Gets the rate at which violations decay over time.
* @return The violation decay rate
*/
double getDecay();

/**
* Gets the violation level at which a setback/punishment should occur.
* @return The setback threshold
*/
double getSetbackVL();

/**
* Whether this check is experimental/in testing.
* @return true if experimental, false if stable
*/
boolean isExperimental();

/**
* Defines how this check should behave when unloaded.
* This allows checks to specify custom behavior when they are disabled
* rather than using the default no-op behavior.
*
* @return The UnloadedBehavior implementation for this check
*/
default UnloadedBehavior getUnloadedBehavior() {
return DefaultUnloadedBehavior.INSTANCE;
}

/**
* @return Set of check classes that must be loaded along with this check to run
*/
default Set<Class<? extends AbstractCheck>> getDependencies() {
return Stream.concat(getLoadAfter().stream(), getLoadBefore().stream())
.collect(Collectors.toSet());
}

/**
* @return Set of check classes that must run after this check
*/
default Set<Class<? extends AbstractCheck>> getLoadAfter() {
return Collections.emptySet();
}

/**
* @return Set of check classes that must run before this check
*/
default Set<Class<? extends AbstractCheck>> getLoadBefore() {
return Collections.emptySet();
}

/**
* Called when check is being loaded
* @return true if loaded successfully
*/
default boolean onLoad() {
return true;
}

/**
* Called when check is being unloaded
*/
default void onUnload() {}

/**
* @return Bit mask representing all check types this check implements
*/
int getMask();

/**
* Test if this check is of a specific type
* @param type The check type to test for
* @return true if this check handles the given type
*/
default boolean is(CheckType type) {
return (getMask() & type.getMask()) != 0;
}
Axionize marked this conversation as resolved.
Show resolved Hide resolved

/**
* Determines if this check supports/is compatible with the given player.
* This can be used to disable checks for players on certain versions,
* with specific conditions, or other compatibility requirements.
*
* @param player The {@link GrimUser} to check compatibility for
* @return true if the check supports this player, false otherwise
*/
default boolean supportsPlayer(GrimUser player) {
return true;
}
}
67 changes: 67 additions & 0 deletions src/main/java/ac/grim/grimac/api/CheckType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ac.grim.grimac.api;

/**
* Represents the types of events a check can listen to.
* Checks can listen to multiple event types simultaneously via bitmasks.
* For example, a check could listen to both POSITION and ROTATION events
* by combining their respective masks.
*/
public enum CheckType {
/**
* For checks without any event listeners
*/
NONE(0),

/**
* Packet analysis events
*/
PACKET(1),

/**
* Player movement events
*/
POSITION(2),

/**
* Player rotation events
*/
ROTATION(3),

/**
* Vehicle-related events
*/
VEHICLE(4),

/**
* Pre-prediction calculation events
*/
PRE_PREDICTION(5),

/**
* Block breaking events
*/
BLOCK_BREAK(6),

/**
* Block placement events
*/
BLOCK_PLACE(7),

/**
* Post-prediction calculation events
*/
POST_PREDICTION(8);

private final int mask;

CheckType(int bitPosition) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could get bitPosition from ordinal() instead of it being a constructor parameter

if (bitPosition > 0 && bitPosition >= 32) {
throw new IllegalArgumentException("Cannot have more than 32 check types");
}
this.mask = bitPosition == 0 ? 0 : 1 << (bitPosition - 1);
}

public int getMask() {
return mask;
}
}
Axionize marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

Axionize marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ac.grim.grimac.api.dynamic;

import java.lang.reflect.Method;

/**
* Default implementation that returns safe "no-op" values for unloaded checks.
* - boolean methods return false
* - numeric methods return 0
* - void methods do nothing
* - object methods return null
*/
public class DefaultUnloadedBehavior implements UnloadedBehavior {
Axionize marked this conversation as resolved.
Show resolved Hide resolved
public static final UnloadedBehavior INSTANCE = new DefaultUnloadedBehavior();

@Override
public Object handleUnloadedCall(Method method, Object[] args) {
Class<?> returnType = method.getReturnType();
if (returnType == boolean.class) return false;
if (returnType == int.class) return 0;
if (returnType == void.class) return null;
return null;
}
}
18 changes: 18 additions & 0 deletions src/main/java/ac/grim/grimac/api/dynamic/UnloadedBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ac.grim.grimac.api.dynamic;

import java.lang.reflect.Method;

/**
* Defines how an unloaded check should behave when its methods are called.
* This allows for graceful degradation when checks are disabled/unloaded.
*/
public interface UnloadedBehavior {
/**
* Handle a method call on an unloaded check
*
* @param method The method being called
* @param args The arguments passed to the method
* @return The value to return from the method call
*/
Object handleUnloadedCall(Method method, Object[] args);
}