This commit is contained in:
2026-09-26 18:06:05 -03:00
parent 01a81b926e
commit e22f12c0b6
12 changed files with 215 additions and 35 deletions
+23
View File
@@ -5,6 +5,8 @@ export interface ItemData {
id: string;
count: number;
data?: unknown;
// what server scripts say about it (get_lore), only sent to players, never saved
lore?: string;
}
export class ItemStack<T = unknown | undefined> {
@@ -12,6 +14,8 @@ export class ItemStack<T = unknown | undefined> {
amount: number;
max_amount: number;
data?: T;
// on clients, the lore the server's scripts gave it when it was last synced
lore?: string;
constructor(type_id: string | string, amount: number = 1, max_amount?: number) {
const item_info = EverythingRegistry.get<ItemRegistry>("items", type_id);
@@ -27,6 +31,7 @@ export class ItemStack<T = unknown | undefined> {
clone(): ItemStack {
const item = new ItemStack(this.type_id, this.amount, this.max_amount);
item.data = structuredClone(this.data);
item.lore = this.lore;
return item;
}
@@ -36,11 +41,24 @@ export class ItemStack<T = unknown | undefined> {
: { id: this.type_id, count: this.amount, data: this.data };
}
// what players are sent: to_data with the lore from the item's get_lore, which only runs on the server
to_synced_data(): ItemData {
const data = this.to_data();
const lore = EverythingRegistry.get<ItemRegistry>("items", this.type_id)?.get_lore?.(this);
if (lore) {
data.lore = lore;
}
return data;
}
static from_data(data: ItemData): ItemStack {
const item = new ItemStack(data.id, data.count);
if (data.data !== undefined) {
item.data = data.data;
}
if (data.lore !== undefined) {
item.lore = data.lore;
}
return item;
}
}
@@ -140,6 +158,11 @@ export class Container {
return this.#slots.map((slot) => slot.get_item()?.to_data() ?? null);
}
// what players are sent, with lore, see ItemStack.to_synced_data
to_synced_data(): (ItemData | null)[] {
return this.#slots.map((slot) => slot.get_item()?.to_synced_data() ?? null);
}
load(data: (ItemData | null)[]) {
for (let i = 0; i < this.size; i += 1) {
const item = data[i];