Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity> 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);

Expand All @@ -40,6 +43,13 @@ public Fragment(ActiveVehicle vehicle, World world, Location loc, List<Entity> 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<Entity> hits = checker.getHitEntities(armorStand, projectiles, 0.5);
if(hits.size() > 0) {
for(Entity e : hits) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down