// curios: accessory slots, shown with the player's inventory from the curios button. each player's curios are a // container of their own, and fall out when they die like the rest of what they carry. items with the // curios:attribute_modifiers component change the wearer's attributes, like curios' attribute modifiers import type { AttributeModifier, Container, Player, ServerContext } from "bworld/server"; import { active_slots, fits, SLOTS } from "./slots.ts"; // curios:attribute_modifiers' params, a list of these interface WornModifier { attribute: string; amount: number; operation: AttributeModifier["operation"]; } export function setup(ctx: ServerContext) { const active = active_slots(ctx.tags); ctx.components.register_item("curios:attribute_modifiers", { get_lore: (_item, modifiers) => ["When worn:", ...modifiers.map(describe)].join("\n"), }); const modifiers_of = (id: string) => (ctx.items.components(id)["curios:attribute_modifiers"] ?? []) as WornModifier[]; // the modifiers each online player's curios gave them, by player id, to take them off again const applied = new Map(); const stop_watching = new Map void>(); // by player name, like the players themselves are saved. undefined until they first open curios function saved_curios(player: Player): Container | undefined { const id = ctx.storage.get>("containers")?.[player.name]; return id ? ctx.containers.get(id) : undefined; } function curios_of(player: Player): Container { const existing = saved_curios(player); if (existing) return existing; const container = ctx.containers.create(SLOTS.length); const ids = ctx.storage.get>("containers") ?? {}; ctx.storage.set("containers", { ...ids, [player.name]: container.id }); watch(player, container); return container; } // keeps what their curios give them up to date function watch(player: Player, curios: Container) { stop_watching.get(player.id)?.(); stop_watching.set(player.id, curios.on_change(() => update_modifiers(player, curios))); update_modifiers(player, curios); } function update_modifiers(player: Player, curios: Container) { for (const { attribute, id } of applied.get(player.id) ?? []) player.remove_modifier(attribute, id); const now: { attribute: string; id: string }[] = []; for (const [index, slot] of SLOTS.entries()) { const item = curios.get(index); if (!item || !active.includes(slot)) continue; for (const [i, { attribute, amount, operation }] of modifiers_of(item.id).entries()) { const id = `curios:${slot}/${i}`; player.add_modifier(attribute, { id, amount, operation }); now.push({ attribute, id }); } } applied.set(player.id, now); } ctx.events.after.player_join.subscribe(({ player }) => { const curios = saved_curios(player); if (curios) watch(player, curios); }); ctx.events.after.player_leave.subscribe(({ player }) => { stop_watching.get(player.id)?.(); stop_watching.delete(player.id); applied.delete(player.id); }); // like curios' CuriosContainer: the slot types in rows of 9, each row centered, over the inventory. the screen is // in scripts/client.ts const rows = Math.ceil(active.length / 9); const menu_type = ctx.menus.register("curios:curios", (menu, _args, player) => { const curios = curios_of(player); menu.title = "Curios"; menu.open_data = { rows }; for (const [i, slot] of active.entries()) { const row = Math.floor(i / 9); const in_row = Math.min(9, active.length - row * 9); menu.add_slot(curios, SLOTS.indexOf(slot), { x: 7 + ((9 - in_row) / 2 + (i % 9)) * 18, y: 17 + row * 18, filter: (item) => fits(ctx.tags, slot, item.id), icon: `curios:empty_${slot}_slot`, }); } menu.add_player_inventory(7, 31 + rows * 18); }); // the curios button, curios' client asks for its menu like the java mod's packet does ctx.net.on("curios:open", (player) => { if (active.length === 0) return; const curios = curios_of(player); // what's in slot types no item goes in anymore, since the mod that added them was removed, goes back to them for (const [index, slot] of SLOTS.entries()) { const item = curios.get(index); if (item && !active.includes(slot)) { player.give_item(item.id, item.count, item.data); curios.set(index, undefined); } } menu_type.open(player, undefined); }); ctx.events.after.player_death.subscribe(({ player }) => { const curios = saved_curios(player); if (!curios) return; const { x, y, z } = player.position; for (let i = 0; i < curios.size; i++) { const item = curios.get(i); if (!item) continue; const { id, count, data } = item; curios.set(i, undefined); ctx.world.drop_item(Math.floor(x), Math.floor(Math.max(y, 0)), Math.floor(z), { id, count, data }); } }); } // like minecraft's attribute tooltips: "+4 Attack Damage", "+30% Attack Speed" function describe({ attribute, amount, operation }: WornModifier): string { const name = attribute.split(":").at(-1)!.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" "); const sign = amount < 0 ? "-" : "+"; const value = operation === "add_value" ? `${Math.abs(amount)}` : `${Math.round(Math.abs(amount) * 100)}%`; return `${sign}${value} ${name}`; }