Files
purpur-sentinel/src/main/java/games/dmg/purpursentinel/GroundMovementContext.java
T
dmg 0db3f32bd3
CI / build (push) Successful in 1m12s
CI / build (pull_request) Successful in 1m19s
feat(sentinel): detect contextual ground speed
2026-10-29 16:44:57 -04:00

59 lines
2.6 KiB
Java

package games.dmg.purpursentinel;
public record GroundMovementContext(
double movementSpeedAttribute,
double sneakingMultiplier,
boolean sprinting,
boolean sneaking,
boolean grounded,
int speedAmplifier,
int slownessAmplifier) {
private static final double ATTRIBUTE_TO_BLOCKS_PER_TICK = 2.2;
private static final double SPRINT_MULTIPLIER = 1.3;
private static final double AIRBORNE_MOMENTUM_MULTIPLIER = 1.3;
public GroundMovementContext {
if (!Double.isFinite(movementSpeedAttribute) || movementSpeedAttribute <= 0) {
throw new IllegalArgumentException("movement speed attribute must be positive and finite");
}
if (!Double.isFinite(sneakingMultiplier) || sneakingMultiplier <= 0) {
throw new IllegalArgumentException("sneaking multiplier must be positive and finite");
}
if (speedAmplifier < -1 || slownessAmplifier < -1) {
throw new IllegalArgumentException("effect amplifiers must be -1 or greater");
}
}
public static GroundMovementContext walking(double movementSpeedAttribute) {
return withEffects(movementSpeedAttribute, false, false, true, -1, -1);
}
public static GroundMovementContext sprinting(double baseMovementSpeedAttribute, boolean grounded) {
return withEffects(baseMovementSpeedAttribute, true, false, grounded, -1, -1);
}
public static GroundMovementContext sneaking(double movementSpeedAttribute) {
return new GroundMovementContext(movementSpeedAttribute, 0.3, false, true, true, -1, -1);
}
public static GroundMovementContext withEffects(double baseMovementSpeedAttribute, boolean sprinting,
boolean sneaking, boolean grounded, int speedAmplifier, int slownessAmplifier) {
double effectiveAttribute = baseMovementSpeedAttribute;
if (sprinting) effectiveAttribute *= SPRINT_MULTIPLIER;
if (speedAmplifier >= 0) effectiveAttribute *= 1.0 + 0.20 * (speedAmplifier + 1);
if (slownessAmplifier >= 0) {
effectiveAttribute *= Math.max(0.10, 1.0 - 0.15 * (slownessAmplifier + 1));
}
return new GroundMovementContext(effectiveAttribute, 0.3, sprinting, sneaking, grounded,
speedAmplifier, slownessAmplifier);
}
public double expectedSpeed() {
// Bukkit's effective MOVEMENT_SPEED value already includes sprint and potion modifiers.
double expected = movementSpeedAttribute * ATTRIBUTE_TO_BLOCKS_PER_TICK;
if (sneaking) expected *= sneakingMultiplier;
if (!grounded) expected *= AIRBORNE_MOMENTUM_MULTIPLIER;
return expected;
}
}