Skip to content

Commit

Permalink
Prevent players from spawning above the logical height of the map
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Jan 5, 2024
1 parent 9be6c56 commit 961e131
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.github.haykam821.deathswap.game.map;

import net.minecraft.entity.EntityType;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.Heightmap;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.dimension.DimensionType;

public final class DeathSwapMap {
Expand All @@ -30,6 +34,31 @@ public BlockBox getBox() {
}

public int getSurfaceY(ServerWorld world, int x, int z) {
return this.chunkGenerator.getHeight(x, z, Heightmap.Type.WORLD_SURFACE, world, world.getChunkManager().getNoiseConfig());
int bottomY = world.getBottomY();
int maxY = Math.min(world.getTopY(), bottomY + world.getLogicalHeight()) - 1;

BlockPos.Mutable pos = new BlockPos.Mutable(x, maxY, z);
Chunk chunk = world.getChunk(pos);

int air = 0;

while (pos.getY() > bottomY) {
if (chunk.getBlockState(pos).isAir()) {
air += 1;
} else if (air > EntityType.PLAYER.getHeight()) {
air = 0;
break;
} else {
air = 0;
}

pos.move(Direction.DOWN);
}

if (pos.getY() == bottomY) {
pos.setY(chunk.sampleHeightmap(Heightmap.Type.WORLD_SURFACE, x, z));
}

return pos.getY() + 1;
}
}

0 comments on commit 961e131

Please sign in to comment.