diff --git a/src/main/java/net/tfminecraft/vehicleframework/projectiles/Fragment.java b/src/main/java/net/tfminecraft/vehicleframework/projectiles/Fragment.java index f2ff69b..1381ce7 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,13 @@ 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 those chunks on the main thread every tick. + if (i >= MAX_TICKS || !armorStand.isValid() || !impactChunksLoaded(world, armorStand.getLocation())) { + armorStand.remove(); + cancel(); + return; + } List hits = checker.getHitEntities(armorStand, projectiles, 0.5); if(hits.size() > 0) { for(Entity e : hits) { @@ -62,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 7a412ed..8561a06 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)); @@ -231,6 +232,47 @@ void fragmentsBurnVehiclesTrailAndStopOnImpactOrDeath() { verify(another).remove(); } + @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<>()); + 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()); + + // 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); + 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));