Player skins !

This commit is contained in:
2026-09-27 12:01:52 -03:00
parent edf4b72dc4
commit 60188ff735
14 changed files with 325 additions and 8 deletions
+53
View File
@@ -43,6 +43,7 @@ import {
import type { GameLoop } from "./game_loop.ts";
import { consume_recipe_items, update_crafting_result } from "./crafting.ts";
import { OpenScreen, SavedPlayer, ServerPlayer } from "./player.ts";
import { encode_skin, MAX_SKIN_BYTES, skin_problem } from "./skins.ts";
import { ServerWorld, Tile } from "./world.ts";
import {
BLOCK_DROP_PICKUP_DELAY,
@@ -86,6 +87,8 @@ const READY_TIMEOUT_TICKS = 60 * TICKS_PER_SECOND;
export interface GameHost {
send(conn: number, data: string): void;
close(conn: number): void;
// fetches a url, failing with a message for players when it can't or it's more than max_bytes
download(url: string, max_bytes: number): Promise<Uint8Array>;
}
export interface SavedWorld {
@@ -1017,6 +1020,21 @@ export class GameServer {
}),
);
register(
literal<ServerCommandSource>("skin")
.describe("Changes your skin to a 64x64 png from the web, or back to the default without a url")
.executes((context) => {
this.set_skin(context.source.server_player, undefined);
context.source.send_message("Your skin is back to the default");
})
.then(
argument<string, ServerCommandSource>("url", word())
.executes((context) =>
this.#download_skin(context.source.server_player, context.get<string>("url"))
),
),
);
// every command's usage, or one's. aliases like /mod:name aren't listed twice
const commands = this.mods.commands;
const help = (context: CommandContext<ServerCommandSource>, only?: string) => {
@@ -1045,6 +1063,41 @@ export class GameServer {
);
}
// a base64 png, or undefined for the default skin. everyone is told, the player too
set_skin(player: ServerPlayer, skin: string | undefined) {
player.skin = skin;
this.world.dirty = true;
this.#broadcast({ type: "player_skin", id: player.id, skin });
}
// the host downloads it, which takes a while, so the player hears back later
#download_skin(player: ServerPlayer, url: string) {
if (player.downloading_skin) {
throw new CommandError("Your last skin is still downloading");
}
player.downloading_skin = true;
this.send_chat(player, "Downloading your skin...");
const online = () => this.#players.get(player.conn) === player;
this.#host.download(url, MAX_SKIN_BYTES)
.then((bytes) => {
const problem = skin_problem(bytes);
if (problem) {
throw new Error(problem);
}
if (online()) {
this.set_skin(player, encode_skin(bytes));
this.send_chat(player, "Skin changed");
}
})
.catch((e) => {
if (online()) {
this.send_chat(player, `Couldn't change your skin: ${e instanceof Error ? e.message : e}`);
}
})
.finally(() => player.downloading_skin = false);
}
#close_screen(player: ServerPlayer) {
const screen = player.screen;
player.screen = undefined;