From b18d85bbed38accc6ef1483757e02520c61a2907 Mon Sep 17 00:00:00 2001 From: XxFran10xX <318299142+XxFran10xX@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:05:11 +0100 Subject: [PATCH 1/3] perf: fragments stop when stuck out of sight and are never saved Co-Authored-By: Claude Opus 5.5 --- .../projectiles/Fragment.java | 14 ++++++++- .../projectiles/ProjectilesCoverageTest.java | 31 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/tfminecraft/vehicleframework/projectiles/Fragment.java b/src/main/java/net/tfminecraft/vehicleframework/projectiles/Fragment.java index f2ff69b..3d76dc9 100644 --- a/src/main/java/net/tfminecraft/vehicleframework/projectiles/Fragment.java +++ b/src/main/java/net/tfminecraft/vehicleframework/projectiles/Fragment.java @@ -15,13 +15,16 @@ import net.tfminecraft.vehicleframework.vehicles.ActiveVehicle; public class Fragment { + /** A fragment lands within about 4 s; one still flying after 10 s is stuck where nobody sees it. */ + static final int MAX_TICKS = 200; private HitChecker checker = new HitChecker(); - + public Fragment(ActiveVehicle vehicle, World world, Location loc, List projectiles) { ArmorStand armorStand = world.spawn(loc, ArmorStand.class, stand -> { stand.setInvisible(true); stand.setGravity(true); // Make sure they don't fall down stand.setSmall(true); // Makes them smaller, looks more like particles in motion + stand.setPersistent(false); // Never saved with the chunk }); projectiles.add(armorStand); @@ -40,6 +43,15 @@ public Fragment(ActiveVehicle vehicle, World world, Location loc, List p int i = 0; @Override public void run() { + // A fragment that flew out of the loaded area never lands, and reading its blocks + // would load that chunk on the main thread every tick. + Location at = armorStand.getLocation(); + if (i >= MAX_TICKS || !armorStand.isValid() + || !world.isChunkLoaded(at.getBlockX() >> 4, at.getBlockZ() >> 4)) { + armorStand.remove(); + cancel(); + return; + } List hits = checker.getHitEntities(armorStand, projectiles, 0.5); if(hits.size() > 0) { for(Entity e : hits) { diff --git a/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java b/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java index 7a412ed..407fea7 100644 --- a/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java +++ b/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java @@ -231,6 +231,36 @@ void fragmentsBurnVehiclesTrailAndStopOnImpactOrDeath() { verify(another).remove(); } + @Test + void fragmentsAreNotSavedAndStopWhenStuckOutOfSight() { + ActiveVehicle source = r.vehicle(); + when(source.getNearbyPlayers()).thenReturn(List.of()); + new Fragment(source, r.world, new Location(r.world, 0, 200, 0), new ArrayList<>()); + ArmorStand flying = (ArmorStand) r.spawned.getLast(); + verify(flying).setPersistent(false); + r.ticks(Fragment.MAX_TICKS); + verify(flying, never()).remove(); + r.tick(); + verify(flying).remove(); + assertEquals(0, r.pending()); + + new Fragment(source, r.world, new Location(r.world, 40, 200, 0), new ArrayList<>()); + ArmorStand gone = (ArmorStand) r.spawned.getLast(); + when(r.world.isChunkLoaded(2, 0)).thenReturn(false); + Block below = r.block(40, 199, 0); + r.ticks(20); + verify(gone).remove(); + verify(below, never()).getType(); + assertEquals(0, r.pending()); + + new Fragment(source, r.world, new Location(r.world, 80, 200, 0), new ArrayList<>()); + ArmorStand unloaded = (ArmorStand) r.spawned.getLast(); + when(unloaded.isValid()).thenReturn(false); + r.tick(); + verify(unloaded).remove(); + assertEquals(0, r.pending()); + } + @Test void livingTargetsReceiveDirectDamageOrConfiguredExplosions() { LivingEntity target = r.entity(LivingEntity.class, new Location(r.world, .5, 64.5, 2)); @@ -370,6 +400,7 @@ public Rig() { oldProjectiles = new ArrayList<>(Cache.projectiles); Cache.projectiles.clear(); when(world.getName()).thenReturn("weapons"); + when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true); when(world.getUID()).thenReturn(UUID.randomUUID()); when(world.getBlockAt(anyInt(), anyInt(), anyInt())) .thenAnswer(c -> block(c.getArgument(0), c.getArgument(1), c.getArgument(2))); From 132e73afd67081d2cd8b04e7da968f9c86222639 Mon Sep 17 00:00:00 2001 From: XxFran10xX <318299142+XxFran10xX@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:23:06 +0100 Subject: [PATCH 2/3] test: fragment tests mark their chunks loaded instead of the shared rig Co-Authored-By: Claude Opus 5.5 --- .../vehicleframework/projectiles/ProjectilesCoverageTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java b/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java index 407fea7..b3e382c 100644 --- a/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java +++ b/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java @@ -198,6 +198,7 @@ void bulletsFilterShooterDeckDeadAndNonVehicleEntities() { @Test void fragmentsBurnVehiclesTrailAndStopOnImpactOrDeath() { + when(r.world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true); ActiveVehicle source = r.vehicle(); Player observer = r.entity(Player.class, new Location(r.world, 0, 64, 0)); when(source.getNearbyPlayers()).thenReturn(List.of(observer)); @@ -233,6 +234,7 @@ void fragmentsBurnVehiclesTrailAndStopOnImpactOrDeath() { @Test void fragmentsAreNotSavedAndStopWhenStuckOutOfSight() { + when(r.world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true); ActiveVehicle source = r.vehicle(); when(source.getNearbyPlayers()).thenReturn(List.of()); new Fragment(source, r.world, new Location(r.world, 0, 200, 0), new ArrayList<>()); @@ -400,7 +402,6 @@ public Rig() { oldProjectiles = new ArrayList<>(Cache.projectiles); Cache.projectiles.clear(); when(world.getName()).thenReturn("weapons"); - when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true); when(world.getUID()).thenReturn(UUID.randomUUID()); when(world.getBlockAt(anyInt(), anyInt(), anyInt())) .thenAnswer(c -> block(c.getArgument(0), c.getArgument(1), c.getArgument(2))); From 61a0bdc445d54a5405897420ae30ac81e802919d Mon Sep 17 00:00:00 2001 From: XxFran10xX <318299142+XxFran10xX@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:31:10 +0100 Subject: [PATCH 3/3] perf: fragment guard covers every chunk the impact check reads Co-Authored-By: Claude Opus 5.5 --- .../vehicleframework/projectiles/Fragment.java | 17 +++++++++++++---- .../projectiles/ProjectilesCoverageTest.java | 10 ++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/tfminecraft/vehicleframework/projectiles/Fragment.java b/src/main/java/net/tfminecraft/vehicleframework/projectiles/Fragment.java index 3d76dc9..1381ce7 100644 --- a/src/main/java/net/tfminecraft/vehicleframework/projectiles/Fragment.java +++ b/src/main/java/net/tfminecraft/vehicleframework/projectiles/Fragment.java @@ -44,10 +44,8 @@ public Fragment(ActiveVehicle vehicle, World world, Location loc, List p @Override public void run() { // A fragment that flew out of the loaded area never lands, and reading its blocks - // would load that chunk on the main thread every tick. - Location at = armorStand.getLocation(); - if (i >= MAX_TICKS || !armorStand.isValid() - || !world.isChunkLoaded(at.getBlockX() >> 4, at.getBlockZ() >> 4)) { + // would load those chunks on the main thread every tick. + if (i >= MAX_TICKS || !armorStand.isValid() || !impactChunksLoaded(world, armorStand.getLocation())) { armorStand.remove(); cancel(); return; @@ -74,4 +72,15 @@ public void run() { } }.runTaskTimer(VehicleFramework.plugin, 0L, 1L); // Run every tick to simulate the trail } + + /** The impact check reads the blocks one step around the fragment, so up to four chunks. */ + static boolean impactChunksLoaded(World world, Location at) { + int x = at.getBlockX(), z = at.getBlockZ(); + for (int cx = (x - 1) >> 4; cx <= (x + 1) >> 4; cx++) { + for (int cz = (z - 1) >> 4; cz <= (z + 1) >> 4; cz++) { + if (!world.isChunkLoaded(cx, cz)) return false; + } + } + return true; + } } diff --git a/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java b/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java index b3e382c..8561a06 100644 --- a/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java +++ b/src/test/java/net/tfminecraft/vehicleframework/projectiles/ProjectilesCoverageTest.java @@ -255,6 +255,16 @@ void fragmentsAreNotSavedAndStopWhenStuckOutOfSight() { verify(below, never()).getType(); assertEquals(0, r.pending()); + // Inside a loaded chunk, but the impact check would read x 112 in the unloaded chunk next to it. + new Fragment(source, r.world, new Location(r.world, 111.5, 200, 8.5), new ArrayList<>()); + ArmorStand border = (ArmorStand) r.spawned.getLast(); + when(r.world.isChunkLoaded(7, 0)).thenReturn(false); + Block across = r.block(112, 200, 8); + r.ticks(20); + verify(border).remove(); + verify(across, never()).getType(); + assertEquals(0, r.pending()); + new Fragment(source, r.world, new Location(r.world, 80, 200, 0), new ArrayList<>()); ArmorStand unloaded = (ArmorStand) r.spawned.getLast(); when(unloaded.isValid()).thenReturn(false);