This commit is contained in:
2026-11-16 13:39:34 -03:00
parent be0770f3cf
commit 5301e0307c
8 changed files with 74 additions and 9 deletions
+2
View File
@@ -126,6 +126,8 @@ export class LocalPlayer extends Player {
sprinting = false;
// what the server says, see /fly
flying = false;
// what the server says, see /creative
creative = false;
#fov_scale = 1;
#prev_fov_scale = 1;
#prev_eye_height = PLAYER_EYE_HEIGHT;
+6 -3
View File
@@ -45,7 +45,8 @@ export class MultiPlayerGameMode {
this.destroy_progress = 0;
}
this.destroy_pos = { x: hit.x, y: hit.y, z: hit.z };
this.destroy_time = block_info.toughness ?? 9999;
// blocks without a toughness, like water, can only be broken in creative
this.destroy_time = this.client.player.creative ? 0 : block_info.toughness ?? 9999;
this.destroy_progress += TICK_DELTA * (tool && tool.type === block_info.tool_to_break ? tool.speed : 1);
if (this.destroy_progress >= this.destroy_time) {
@@ -134,8 +135,10 @@ export class MultiPlayerGameMode {
if ((!block_info.interactive || skips_block) && place_id && placeable) {
level.add_block({ ...target, id: place_id }, place_value! >>> 16);
predict_neighbors(target.x, target.y, target.z);
const slot = this.#held_slot();
slot.amount = slot.amount! - 1;
if (!this.client.player.creative) {
const slot = this.#held_slot();
slot.amount = slot.amount! - 1;
}
}
}
+3
View File
@@ -95,6 +95,9 @@ export class ClientPacketListener {
case "flying":
client.player.flying = message.flying;
break;
case "creative":
client.player.creative = message.creative;
break;
case "effects":
client.player.effects = message.effects;
break;
+3 -1
View File
@@ -12,7 +12,7 @@ export const AIR_ID = "cubi:air";
// bump when a client and server of different versions can't play together.
// the server rejects a different version before the client downloads anything
export const PROTOCOL_VERSION = 28;
export const PROTOCOL_VERSION = 29;
export interface PlayerInfo {
id: string;
@@ -301,6 +301,8 @@ export type ServerMessage =
| { type: "using_item"; using: boolean }
// whether the player flies, see /fly, when it changes
| { type: "flying"; flying: boolean }
// whether the player is in creative, see /creative, when it changes
| { type: "creative"; creative: boolean }
// the player's own effects that show an icon, when they change. duration is in ticks, -1 for forever, and the
// client counts it down itself, so it isn't sent every tick
| { type: "effects"; effects: { id: string; amplifier: number; duration: number }[] }
+1
View File
@@ -119,6 +119,7 @@ Commands can only be registered while the server phase runs.
| `/tp <x> <y> <z>` | Teleports you there |
| `/fill <x1> <y1> <z1> <x2> <y2> <z2> <block>` | Fills the box between two corners with a block, `air` to clear it |
| `/fly` | Toggles flying: jump goes up, sneak goes down, sprint goes faster |
| `/creative` | Toggles placing blocks without using them up and instant breaking |
| `/tps` | Shows how the game loop keeps up |
| `/viewdistance [chunks]` | Shows or changes how far the server sends chunks, until it restarts |
| `/skin [url] [slim\|normal]` | Changes your skin to a 64×64 PNG, or back to the default |
+19 -5
View File
@@ -1273,8 +1273,8 @@ export class GameServer {
#break_block(player: ServerPlayer, x: number, y: number, z: number) {
const info = this.world.get_block_info(x, y, z);
// air, or something like water that can't be broken
if (!info || info.toughness === undefined || !this.#in_reach(player, x, y, z)) {
// air, or something like water that can't be broken outside creative
if (!info || (info.toughness === undefined && !player.creative) || !this.#in_reach(player, x, y, z)) {
this.#correct(player, x, y, z);
return;
}
@@ -1291,7 +1291,8 @@ export class GameServer {
return;
}
this.destroy_block(x, y, z, player);
// creative breaks drop nothing, since placing doesn't use anything up
this.destroy_block(x, y, z, player, { collect: player.creative });
player.vitals.add_exhaustion(EXHAUSTION.break_block);
this.mods.after.block_break.emit(event);
}
@@ -1537,7 +1538,7 @@ export class GameServer {
if (!place) return;
this.set_block(tx, ty, tz, held.block_id, player, state);
held_slot.amount = held_slot.amount! - 1;
if (!player.creative) held_slot.amount = held_slot.amount! - 1;
this.mods.after.block_place.emit(place);
}
@@ -1559,7 +1560,7 @@ export class GameServer {
z,
set_state_value(this.world.get_block_value(x, y, z), info, "waterlogged", 1)! >>> 16,
);
held_slot.amount = held_slot.amount! - 1;
if (!player.creative) held_slot.amount = held_slot.amount! - 1;
this.mods.after.block_place.emit(place);
}
@@ -1924,6 +1925,17 @@ export class GameServer {
}),
);
// TODO: only let operators do this
register(
literal<ServerCommandSource>("creative")
.describe("Toggles placing blocks without using them up and breaking any block instantly")
.executes((context) => {
const player = context.source.server_player;
player.creative = !player.creative;
context.source.send_message(player.creative ? "You're in creative" : "You're out of creative");
}),
);
// TODO: only let operators do this
register(
literal<ServerCommandSource>("viewdistance").describe("Shows or changes the server's view distance")
@@ -2337,6 +2349,8 @@ export class GameServer {
sync("using_item", using, () => ({ type: "using_item", using }));
const flying = player.flying;
sync("flying", flying, () => ({ type: "flying", flying }));
const creative = player.creative;
sync("creative", creative, () => ({ type: "creative", creative }));
const effects = [...player.effects.values()]
.filter((effect) => effect.show_icon)
.map(({ type, amplifier, duration }) => ({ id: type.id, amplifier, duration }));
+7
View File
@@ -38,6 +38,8 @@ export interface SavedPlayer {
slim?: boolean;
// see /fly
flying?: boolean;
// see /creative
creative?: boolean;
}
// the connection of the player machines use items as, see GameServer.use_item_on. nothing is ever sent to it
@@ -68,6 +70,9 @@ export class ServerPlayer extends LivingEntity {
using: { slot: number; type_id: string; ticks: number; duration: number } | undefined;
// flying, from /fly. their client moves them without gravity, and they don't fall or get hungry from moving
flying = false;
// from /creative: placing doesn't use up what they hold, and breaking is instant and drops nothing. their client
// times breaking, so it only needs to know
creative = false;
inventory = new Container(INVENTORY_SIZE);
// hunger and saturation, health is the entity's
@@ -240,6 +245,7 @@ export class ServerPlayer extends LivingEntity {
skin: this.skin,
slim: this.slim || undefined,
flying: this.flying || undefined,
creative: this.creative || undefined,
};
}
@@ -256,5 +262,6 @@ export class ServerPlayer extends LivingEntity {
this.skin = saved.skin;
this.slim = saved.slim ?? false;
this.flying = saved.flying === true;
this.creative = saved.creative === true;
}
}
+33
View File
@@ -202,3 +202,36 @@ Deno.test("how far players reach is an attribute, which their client is sent", a
assertEquals(game.world.get_block_id(0, 99, 9), "cubi:air");
cleanup();
});
Deno.test("/creative places blocks without using them up and breaks blocks without drops", async () => {
const { game, send, take, hold, cleanup } = await setup();
const [alice] = game.players();
hold("testmod:pillar");
send(1, { type: "chat", text: "/creative" });
game.tick();
assert(take(1).some((m) => m.type === "creative" && m.creative), "the player hears they're in creative");
send(1, { type: "use_block", x: 1, y: 99, z: 1, face: "top" });
send(1, { type: "use_block", x: 1, y: 100, z: 1, face: "top" });
assertEquals(game.world.get_block_id(1, 101, 1), "testmod:pillar");
assertEquals(alice.inventory.get_slot(alice.selected_slot).amount, 1);
game.set_block(2, 100, 1, "bworld:dirt");
send(1, { type: "break_block", x: 2, y: 100, z: 1 });
assertEquals(game.world.get_block_id(2, 100, 1), "cubi:air");
assertEquals(game.item_entities(), []);
// even ones that can't be broken otherwise
game.set_block(2, 100, 1, "bworld:water");
send(1, { type: "break_block", x: 2, y: 100, z: 1 });
assertEquals(game.world.get_block_id(2, 100, 1), "cubi:air");
// out of it, placing uses the block up again and breaking drops
send(1, { type: "chat", text: "/creative" });
send(1, { type: "use_block", x: 1, y: 101, z: 1, face: "top" });
assertEquals(game.world.get_block_id(1, 102, 1), "testmod:pillar");
assert(!alice.inventory.get_slot(alice.selected_slot).has_item());
game.set_block(2, 100, 1, "bworld:dirt");
send(1, { type: "break_block", x: 2, y: 100, z: 1 });
assertEquals(game.item_entities().length, 1);
cleanup();
});