diff --git a/client/entity/local_player.ts b/client/entity/local_player.ts index ea2fcac..857c2b7 100644 --- a/client/entity/local_player.ts +++ b/client/entity/local_player.ts @@ -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; diff --git a/client/game_mode.ts b/client/game_mode.ts index 623ab81..b7163d8 100644 --- a/client/game_mode.ts +++ b/client/game_mode.ts @@ -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; + } } } diff --git a/client/packet_listener.ts b/client/packet_listener.ts index a5f660c..e8779e6 100644 --- a/client/packet_listener.ts +++ b/client/packet_listener.ts @@ -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; diff --git a/common/protocol.ts b/common/protocol.ts index 3bcd5d7..1cca446 100644 --- a/common/protocol.ts +++ b/common/protocol.ts @@ -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 }[] } diff --git a/docs/modding/misc/commands.md b/docs/modding/misc/commands.md index 902a80d..a54e99d 100644 --- a/docs/modding/misc/commands.md +++ b/docs/modding/misc/commands.md @@ -119,6 +119,7 @@ Commands can only be registered while the server phase runs. | `/tp ` | Teleports you there | | `/fill ` | 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 | diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 03d0104..ac3be73 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -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("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("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 })); diff --git a/server/game/player.ts b/server/game/player.ts index 947cac6..a12c48e 100644 --- a/server/game/player.ts +++ b/server/game/player.ts @@ -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; } } diff --git a/tests/player_actions_test.ts b/tests/player_actions_test.ts index 91747b1..7dd1352 100644 --- a/tests/player_actions_test.ts +++ b/tests/player_actions_test.ts @@ -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(); +});