Slim skins
This commit is contained in:
@@ -8,6 +8,8 @@ export abstract class Player extends Entity {
|
||||
name: string;
|
||||
// undefined draws the default skin
|
||||
skin: Texture | undefined;
|
||||
// 3 pixel wide arms, for skins made for them
|
||||
slim = false;
|
||||
#skin_version = 0;
|
||||
|
||||
// how far the limbs have swung and how hard they swing (0 to 1), like minecraft's WalkAnimationState
|
||||
@@ -21,7 +23,7 @@ export abstract class Player extends Entity {
|
||||
}
|
||||
|
||||
// a base64 png from the server, or undefined for the default. the old skin stays until the new one is decoded
|
||||
async set_skin(skin: string | undefined) {
|
||||
async set_skin(skin: string | undefined, slim = false) {
|
||||
const version = ++this.#skin_version;
|
||||
const texture = skin ? await decode_skin(skin) : undefined;
|
||||
if (version !== this.#skin_version) {
|
||||
@@ -31,6 +33,7 @@ export abstract class Player extends Entity {
|
||||
}
|
||||
this.skin?.tex.destroy();
|
||||
this.skin = texture;
|
||||
this.slim = slim;
|
||||
}
|
||||
|
||||
// after moving each tick. full speed is 5 blocks per second, a bit faster than walking
|
||||
|
||||
@@ -21,7 +21,7 @@ export class RemotePlayer extends Player {
|
||||
this.yaw = info.yaw;
|
||||
this.pitch = info.pitch;
|
||||
if (info.skin) {
|
||||
this.set_skin(info.skin);
|
||||
this.set_skin(info.skin, info.slim);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ export class ClientPacketListener {
|
||||
case "player_skin": {
|
||||
const player = level.entities.get(message.id);
|
||||
if (player instanceof Player) {
|
||||
player.set_skin(message.skin);
|
||||
player.set_skin(message.skin, message.slim);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -79,6 +79,29 @@ const PARTS: ModelPart[] = [
|
||||
},
|
||||
];
|
||||
|
||||
// slim (alex) skins have 3 pixel wide arms, half a pixel lower, like minecraft's PlayerModel with slim set
|
||||
const SLIM_ARMS: ModelPart[] = [
|
||||
{
|
||||
name: "right_arm",
|
||||
size: [3, 12, 4],
|
||||
pivot: [5, 21.5, 0],
|
||||
from: [-1, -10, -2],
|
||||
texture: [40, 16],
|
||||
overlay: [40, 32],
|
||||
inflate: 0.25,
|
||||
},
|
||||
{
|
||||
name: "left_arm",
|
||||
size: [3, 12, 4],
|
||||
pivot: [-5, 21.5, 0],
|
||||
from: [-2, -10, -2],
|
||||
texture: [32, 48],
|
||||
overlay: [48, 48],
|
||||
inflate: 0.25,
|
||||
},
|
||||
];
|
||||
const SLIM_PARTS = PARTS.map((part) => SLIM_ARMS.find((arm) => arm.name === part.name) ?? part);
|
||||
|
||||
// a box's faces, like minecraft's ModelPart.Cube. `rect` is where the face is in the box's texture, which is laid
|
||||
// out as in a skin: the top and bottom above the sides (right, front, left, back). corners go counter clockwise from
|
||||
// outside as [x, y, z, u, v], 0 for the low end and 1 for the high one
|
||||
@@ -139,7 +162,7 @@ export function render_player(player: Player, skin: Texture, partial_tick: numbe
|
||||
left_leg: -swing * 1.4,
|
||||
};
|
||||
|
||||
for (const part of PARTS) {
|
||||
for (const part of player.slim ? SLIM_PARTS : PARTS) {
|
||||
const sin = Math.sin(angles[part.name]);
|
||||
const cos = Math.cos(angles[part.name]);
|
||||
const [pivot_x, pivot_y, pivot_z] = part.pivot;
|
||||
|
||||
+3
-1
@@ -19,6 +19,8 @@ export interface PlayerInfo {
|
||||
pitch: number;
|
||||
// a 64x64 png as base64, see /skin. the default skin when there isn't one
|
||||
skin?: string;
|
||||
// the skin is for the model with 3 pixel wide arms
|
||||
slim?: boolean;
|
||||
}
|
||||
|
||||
// an entity that isn't a player, as the server first sends it. players have their own messages
|
||||
@@ -111,7 +113,7 @@ export type ServerMessage =
|
||||
| { type: "player_leave"; id: string }
|
||||
| { type: "player_move"; id: string; x: number; y: number; z: number; yaw: number; pitch: number }
|
||||
// a player changed their skin, including the one receiving it. without skin it's back to the default
|
||||
| { type: "player_skin"; id: string; skin?: string }
|
||||
| { type: "player_skin"; id: string; skin?: string; slim?: boolean }
|
||||
| { type: "add_entity"; entity: EntityInfo }
|
||||
| { type: "move_entity"; id: string; x: number; y: number; z: number }
|
||||
// a dropped item's stack changed, like when two stacks merge
|
||||
|
||||
@@ -1022,15 +1022,28 @@ 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")
|
||||
.describe(
|
||||
"Changes your skin to a 64x64 png from the web, slim for 3 pixel wide arms. " +
|
||||
"Back to the default without a url",
|
||||
)
|
||||
.executes((context) => {
|
||||
this.set_skin(context.source.server_player, undefined);
|
||||
this.set_skin(context.source.server_player, undefined, false);
|
||||
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"))
|
||||
this.#download_skin(context.source.server_player, context.get<string>("url"), false)
|
||||
)
|
||||
.then(
|
||||
argument<string, ServerCommandSource>("model", choice(["slim", "normal"]))
|
||||
.executes((context) =>
|
||||
this.#download_skin(
|
||||
context.source.server_player,
|
||||
context.get<string>("url"),
|
||||
context.get<string>("model") === "slim",
|
||||
)
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -1063,15 +1076,17 @@ 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) {
|
||||
// a base64 png, or undefined for the default skin, and whether it's for the slim model. everyone is told, the
|
||||
// player too
|
||||
set_skin(player: ServerPlayer, skin: string | undefined, slim: boolean) {
|
||||
player.skin = skin;
|
||||
player.slim = slim;
|
||||
this.world.dirty = true;
|
||||
this.#broadcast({ type: "player_skin", id: player.id, skin });
|
||||
this.#broadcast({ type: "player_skin", id: player.id, skin, slim: slim || undefined });
|
||||
}
|
||||
|
||||
// the host downloads it, which takes a while, so the player hears back later
|
||||
#download_skin(player: ServerPlayer, url: string) {
|
||||
#download_skin(player: ServerPlayer, url: string, slim: boolean) {
|
||||
if (player.downloading_skin) {
|
||||
throw new CommandError("Your last skin is still downloading");
|
||||
}
|
||||
@@ -1086,7 +1101,7 @@ export class GameServer {
|
||||
throw new Error(problem);
|
||||
}
|
||||
if (online()) {
|
||||
this.set_skin(player, encode_skin(bytes));
|
||||
this.set_skin(player, encode_skin(bytes), slim);
|
||||
this.send_chat(player, "Skin changed");
|
||||
}
|
||||
})
|
||||
|
||||
@@ -31,6 +31,7 @@ export interface SavedPlayer {
|
||||
vitals?: SavedVitals;
|
||||
// base64 png, see /skin
|
||||
skin?: string;
|
||||
slim?: boolean;
|
||||
}
|
||||
|
||||
export class ServerPlayer {
|
||||
@@ -56,6 +57,8 @@ export class ServerPlayer {
|
||||
screen: OpenScreen | undefined;
|
||||
// base64 png, see /skin. undefined is the default skin
|
||||
skin: string | undefined;
|
||||
// the skin is for the model with 3 pixel wide arms
|
||||
slim = false;
|
||||
// a /skin download is on its way, one at a time
|
||||
downloading_skin = false;
|
||||
|
||||
@@ -81,6 +84,7 @@ export class ServerPlayer {
|
||||
yaw: this.yaw,
|
||||
pitch: this.pitch,
|
||||
skin: this.skin,
|
||||
slim: this.slim || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -95,6 +99,7 @@ export class ServerPlayer {
|
||||
inventory: this.inventory.to_data(),
|
||||
vitals: this.vitals.save(),
|
||||
skin: this.skin,
|
||||
slim: this.slim || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -108,5 +113,6 @@ export class ServerPlayer {
|
||||
this.inventory.load(saved.inventory);
|
||||
this.vitals.load(saved.vitals);
|
||||
this.skin = saved.skin;
|
||||
this.slim = saved.slim ?? false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,33 @@ Deno.test("/skin downloads a png, tells everyone and is saved with the player",
|
||||
assertEquals(join.players.find((p: { name: string }) => p.name === "alice").skin, skin);
|
||||
});
|
||||
|
||||
Deno.test("/skin <url> slim is for the model with thin arms, normal is the default", async () => {
|
||||
const t = await test_game("mods");
|
||||
t.downloads.set("https://example.com/alex.png", SKIN);
|
||||
t.join(1, "alice");
|
||||
t.take(1);
|
||||
const skin_message = async (text: string) => {
|
||||
t.send(1, { type: "chat", text });
|
||||
await settle();
|
||||
return t.take(1).find((m) => m.type === "player_skin");
|
||||
};
|
||||
|
||||
assertEquals((await skin_message("/skin https://example.com/alex.png slim")).slim, true);
|
||||
const loaded = await test_game("mods", t.game.save());
|
||||
loaded.join(1, "alice");
|
||||
assertEquals(loaded.join(2, "bob").players.find((p: { name: string }) => p.name === "alice").slim, true);
|
||||
|
||||
assertEquals((await skin_message("/skin https://example.com/alex.png normal")).slim, undefined);
|
||||
assertEquals((await skin_message("/skin https://example.com/alex.png slim")).slim, true);
|
||||
assertEquals((await skin_message("/skin https://example.com/alex.png")).slim, undefined);
|
||||
assertEquals((await skin_message("/skin https://example.com/alex.png slim")).slim, true);
|
||||
// back to the default skin is back to the normal model too
|
||||
assertEquals((await skin_message("/skin")).slim, undefined);
|
||||
|
||||
t.send(1, { type: "chat", text: "/skin https://example.com/alex.png wide" });
|
||||
assertEquals(t.take(1).some((m) => m.type === "player_skin"), false);
|
||||
});
|
||||
|
||||
Deno.test("/skin says why a download can't be a skin, and /skin alone goes back to the default", async () => {
|
||||
const t = await test_game("mods");
|
||||
const wide = SKIN.slice();
|
||||
|
||||
Reference in New Issue
Block a user