Change default view distance to 16 and add /viewdistance command

This commit is contained in:
2026-10-02 19:01:08 -03:00
parent a4fec0681f
commit 3202c18ab5
7 changed files with 56 additions and 12 deletions
+38 -9
View File
@@ -95,8 +95,9 @@ const GROUND_EPSILON = 0.02;
const MAX_CATCH_UP_DAYS = 64;
// how long a client gets between welcome and ready to download and load everything
const READY_TIMEOUT_TICKS = 60 * TICKS_PER_SECOND;
// the furthest the server sends chunks around a player, and what players who don't say get. minecraft's default
const SERVER_VIEW_DISTANCE = 10;
// the furthest the server sends chunks around a player, and what players who don't say get, until /viewdistance
// changes it
const SERVER_VIEW_DISTANCE = 16;
// how long a tick can spend building chunks for players, the rest go out on the next ticks
const CHUNK_SEND_BUDGET_MS = 20;
@@ -171,6 +172,8 @@ export class GameServer {
do_mob_spawning = true;
// what runs tick(), for /tps. tests call tick() themselves
loop?: GameLoop;
// SERVER_VIEW_DISTANCE, or what /viewdistance set it to. not saved, it's for testing
view_distance = SERVER_VIEW_DISTANCE;
// how long a tick can spend building chunks for players. tests lift it so what gets sent doesn't depend on speed
chunk_send_budget_ms = CHUNK_SEND_BUDGET_MS;
@@ -392,6 +395,15 @@ export class GameServer {
return this.#tick;
}
// the furthest the server sends chunks to anyone, for now. players who asked for more get more, up to it
set_view_distance(distance: number) {
this.view_distance = distance;
for (const player of this.players()) {
player.chunks.view_distance = view_distance(player.requested_view_distance, distance);
this.#update_view(player, true);
}
}
players(): ServerPlayer[] {
return [...this.#players.values()];
}
@@ -695,7 +707,7 @@ export class GameServer {
return;
}
this.#pending.delete(conn);
this.#join(conn, pending.name, view_distance(message.view_distance));
this.#join(conn, pending.name, message.view_distance);
}
}
@@ -704,14 +716,15 @@ export class GameServer {
this.#host.close(conn);
}
#join(conn: number, raw_name: unknown, view_distance: number) {
#join(conn: number, raw_name: unknown, requested_view_distance: unknown) {
const player = new ServerPlayer(
this.entities,
conn,
this.#unique_name(raw_name),
view_distance,
view_distance(requested_view_distance, this.view_distance),
() => this.recipes.shaped,
);
if (is_int(requested_view_distance)) player.requested_view_distance = requested_view_distance;
const saved = this.#saved_players[player.name];
if (saved) {
player.load_player(saved);
@@ -778,7 +791,8 @@ export class GameServer {
}
case "view_distance":
if (is_int(message.distance)) {
player.chunks.view_distance = view_distance(message.distance);
player.requested_view_distance = message.distance;
player.chunks.view_distance = view_distance(message.distance, this.view_distance);
this.#update_view(player, true);
}
break;
@@ -1293,6 +1307,21 @@ export class GameServer {
}),
);
// TODO: only let operators do this
register(
literal<ServerCommandSource>("viewdistance").describe("Shows or changes the server's view distance")
.executes((context) => {
context.source.send_message(`The view distance is ${this.view_distance} chunks`);
})
.then(
argument<number, ServerCommandSource>("chunks", integer(MIN_VIEW_DISTANCE, MAX_VIEW_DISTANCE))
.executes((context) => {
this.set_view_distance(context.get<number>("chunks"));
context.source.send_message(`Set the view distance to ${this.view_distance} chunks`);
}),
),
);
register(
literal<ServerCommandSource>("tps").describe("Shows how the game loop keeps up").executes((context) => {
if (!this.loop) {
@@ -1815,11 +1844,11 @@ function is_int(value: unknown): value is number {
}
// what a player asked for, up to the server's
function view_distance(requested: unknown): number {
function view_distance(requested: unknown, server: number): number {
if (!is_int(requested)) {
return SERVER_VIEW_DISTANCE;
return server;
}
return Math.min(SERVER_VIEW_DISTANCE, Math.max(MIN_VIEW_DISTANCE, Math.min(MAX_VIEW_DISTANCE, requested)));
return Math.min(server, Math.max(MIN_VIEW_DISTANCE, Math.min(MAX_VIEW_DISTANCE, requested)));
}
// messages are parsed json, so the types in ClientMessage are only what a well behaved client sends
+2
View File
@@ -94,6 +94,8 @@ export class ServerPlayer extends LivingEntity {
sent = new Map<string, string>();
// the chunks their client has and still needs
chunks: PlayerChunks;
// the view distance their client asked for, which the server's might cap, undefined when it didn't say
requested_view_distance: number | undefined;
constructor(
level: EntityLevel,