Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 92 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,112 @@
package ac.grim.grimac.api;

import ac.grim.grimac.api.common.BasicStatus;
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
*/
UnloadedBehavior getUnloadedBehavior();

/**
* @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 getCheckMask();

/**
* 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 isCheckType(CheckType type) {
return (getCheckMask() & type.getMask()) != 0;
}
}
30 changes: 30 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,30 @@
package ac.grim.grimac.api;

/**
* Represents different types of checks that can be performed
*/
public enum CheckType {
PACKET(0),
POSITION(1),
ROTATION(2),
VEHICLE(3),
PRE_PREDICTION(4),
BLOCK_BREAK(5),
BLOCK_PLACE(6),
POST_PREDICTION(7);

private final int bitPosition;
private final int mask;

CheckType(int bitPosition) {
Copy link
Contributor

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 >= 32) {
throw new IllegalArgumentException("Cannot have more than 32 check types");
}
this.bitPosition = bitPosition;
this.mask = 1 << bitPosition;
}

public int getMask() {
return mask;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
}

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 {
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;
}
}
19 changes: 19 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,19 @@
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);
}