feat(03-04): ajoute FallDamageGuard tracker thread-safe (Task 2)

- ConcurrentHashMap<UUID, region> current + exit + regionAtExit
- shouldSuppressFallDamage: in-region FallDamage=false OU grace window
- Pas de static mutable, injecté via constructeur
- 7 tests pure-data couvrant entry/in-region/exit/grace/re-entry/override
This commit is contained in:
2026-04-23 14:00:56 +02:00
parent a834c59b66
commit f9d5595837
2 changed files with 182 additions and 0 deletions
@@ -0,0 +1,97 @@
package com.mythlane.gravityflip.physics;
import com.hypixel.hytale.math.shape.Box;
import com.hypixel.hytale.math.vector.Vector3d;
import com.mythlane.gravityflip.region.GravityFlipRegion;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Pure-data tests for {@link FallDamageGuard} — no Hytale runtime dependency.
* Covers entry / in-region / exit / grace-window / re-entry / FallDamage=true override.
*/
class FallDamageGuardTest {
@Test
void notInRegionReturnsFalse() {
FallDamageGuard guard = new FallDamageGuard();
assertFalse(guard.shouldSuppressFallDamage(UUID.randomUUID(), 1000L));
}
@Test
void inRegionFallDamageTrueReturnsFalse() {
FallDamageGuard guard = new FallDamageGuard();
UUID uuid = UUID.randomUUID();
GravityFlipRegion region = region(true, 2500);
guard.markInRegion(uuid, region);
assertFalse(guard.shouldSuppressFallDamage(uuid, 1000L));
}
@Test
void inRegionFallDamageFalseReturnsTrue() {
FallDamageGuard guard = new FallDamageGuard();
UUID uuid = UUID.randomUUID();
GravityFlipRegion region = region(false, 2500);
guard.markInRegion(uuid, region);
assertTrue(guard.shouldSuppressFallDamage(uuid, 1000L));
}
@Test
void afterExitWithinGraceReturnsTrue() {
FallDamageGuard guard = new FallDamageGuard();
UUID uuid = UUID.randomUUID();
GravityFlipRegion region = region(false, 2500);
guard.markInRegion(uuid, region);
long t0 = 1000L;
guard.markExit(uuid, region, t0);
assertTrue(guard.shouldSuppressFallDamage(uuid, t0 + 2499));
}
@Test
void afterExitBeyondGraceReturnsFalse() {
FallDamageGuard guard = new FallDamageGuard();
UUID uuid = UUID.randomUUID();
GravityFlipRegion region = region(false, 2500);
guard.markInRegion(uuid, region);
long t0 = 1000L;
guard.markExit(uuid, region, t0);
assertFalse(guard.shouldSuppressFallDamage(uuid, t0 + 2501));
}
@Test
void reEntryAfterExitResetsTracking() {
FallDamageGuard guard = new FallDamageGuard();
UUID uuid = UUID.randomUUID();
GravityFlipRegion region = region(false, 2500);
guard.markInRegion(uuid, region);
guard.markExit(uuid, region, 1000L);
guard.markInRegion(uuid, region); // re-enter
// In-region again with FallDamage=false → immediate suppression, grace reset.
assertTrue(guard.shouldSuppressFallDamage(uuid, 1500L));
}
@Test
void fallDamageTrueRegionNeverSuppressesEvenDuringGrace() {
FallDamageGuard guard = new FallDamageGuard();
UUID uuid = UUID.randomUUID();
GravityFlipRegion suppressed = region(false, 2500);
GravityFlipRegion allowed = region(true, 2500);
guard.markInRegion(uuid, suppressed);
guard.markExit(uuid, suppressed, 1000L);
// New region has FallDamage=true → override immediately.
guard.markInRegion(uuid, allowed);
assertFalse(guard.shouldSuppressFallDamage(uuid, 1500L));
}
private static GravityFlipRegion region(boolean fallDamage, int graceMs) {
GravityFlipRegion r = new GravityFlipRegion(
"t", new Box(new Vector3d(0, 0, 0), new Vector3d(1, 1, 1)), true);
r.setFallDamage(fallDamage);
r.setGracePeriodMs(graceMs);
return r;
}
}