361 lines
9.7 KiB
TypeScript
361 lines
9.7 KiB
TypeScript
import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
|
|
|
// how item stacks are saved and sent over the network
|
|
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> {
|
|
type_id: string;
|
|
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);
|
|
this.type_id = type_id;
|
|
this.amount = amount;
|
|
this.max_amount = max_amount ?? item_info?.max_stack ?? (item_info?.tool ? 1 : 64);
|
|
this.data = undefined;
|
|
if (item_info?.on_create) {
|
|
item_info.on_create(this);
|
|
}
|
|
}
|
|
|
|
// what's left of a tool's durability, its max minus the damage kept in its data. undefined for non tools
|
|
durability(): number | undefined {
|
|
const tool = EverythingRegistry.get<ItemRegistry>("items", this.type_id)?.tool;
|
|
if (!tool) return undefined;
|
|
const damage = (this.data as { damage?: number } | undefined)?.damage ?? 0;
|
|
return tool.durability - damage;
|
|
}
|
|
|
|
// takes durability off a tool, true when that used it up
|
|
damage(amount: number): boolean {
|
|
const left = this.durability();
|
|
if (left === undefined) return false;
|
|
const data = (this.data ?? {}) as { damage?: number };
|
|
data.damage = (data.damage ?? 0) + amount;
|
|
this.data = data as T;
|
|
return left - amount <= 0;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
to_data(): ItemData {
|
|
return this.data === undefined
|
|
? { id: this.type_id, count: this.amount }
|
|
: { 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;
|
|
}
|
|
}
|
|
|
|
export class ContainerSlot {
|
|
#item_stack: ItemStack | undefined;
|
|
|
|
has_item() {
|
|
return this.get_item() !== undefined;
|
|
}
|
|
|
|
set_item(item_stack: ItemStack | undefined) {
|
|
if ((item_stack?.amount ?? 0) <= 0) {
|
|
item_stack = undefined;
|
|
}
|
|
this.#item_stack = item_stack;
|
|
}
|
|
|
|
get_item() {
|
|
// scripts can count a stack down to nothing, it's gone then
|
|
if (this.#item_stack && this.#item_stack.amount <= 0) {
|
|
this.#item_stack = undefined;
|
|
}
|
|
return this.#item_stack;
|
|
}
|
|
|
|
get type_id() {
|
|
return this.get_item()?.type_id;
|
|
}
|
|
|
|
set amount(new_amount: number) {
|
|
if (this.#item_stack) {
|
|
this.#item_stack.amount = new_amount;
|
|
if (this.#item_stack.amount <= 0) {
|
|
this.#item_stack = undefined;
|
|
}
|
|
}
|
|
}
|
|
|
|
get amount(): number | undefined {
|
|
return this.get_item()?.amount;
|
|
}
|
|
|
|
get max_amount() {
|
|
return this.get_item()?.max_amount;
|
|
}
|
|
}
|
|
|
|
export class Container {
|
|
#slots: ContainerSlot[] = [];
|
|
readonly size: number;
|
|
|
|
constructor(size: number) {
|
|
this.size = size;
|
|
for (let i = 0; i < size; i += 1) {
|
|
this.#slots.push(new ContainerSlot());
|
|
}
|
|
}
|
|
|
|
// returns how many items didn't fit
|
|
add_item(item_stack: ItemStack): number {
|
|
for (const slot of this.#slots.filter((slot) => slot.has_item())) {
|
|
const slot_item = slot.get_item()!;
|
|
if (slot_item.type_id === item_stack.type_id) {
|
|
const missing = slot_item.max_amount - slot_item.amount;
|
|
const adding = Math.min(missing, item_stack.amount);
|
|
slot_item.amount += adding;
|
|
item_stack.amount -= adding;
|
|
if (item_stack.amount === 0) {
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
for (const slot of this.#slots) {
|
|
if (!slot.has_item()) {
|
|
slot.set_item(item_stack);
|
|
return 0;
|
|
}
|
|
}
|
|
// TODO: drop item on ground
|
|
return item_stack.amount;
|
|
}
|
|
|
|
get_item(slot: number): ItemStack | undefined {
|
|
return this.#slots[slot]?.get_item();
|
|
}
|
|
|
|
get_slot(slot: number): ContainerSlot {
|
|
return this.#slots[slot];
|
|
}
|
|
|
|
set_item(slot: number, item: ItemStack | undefined) {
|
|
this.#slots[slot].set_item(item);
|
|
}
|
|
|
|
to_data(): (ItemData | null)[] {
|
|
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];
|
|
this.#slots[i].set_item(item ? ItemStack.from_data(item) : undefined);
|
|
}
|
|
}
|
|
|
|
clear() {
|
|
for (const slot of this.#slots) {
|
|
slot.set_item(undefined);
|
|
}
|
|
}
|
|
}
|
|
|
|
// the item a player is carrying around with the mouse in an inventory screen
|
|
export interface Cursor {
|
|
item: ItemStack | undefined;
|
|
}
|
|
|
|
export const LEFT_CLICK = 0;
|
|
export const RIGHT_CLICK = 2;
|
|
|
|
// what clicking a normal slot does, the server runs this for real and clients run it to predict
|
|
export function click_slot(container: Container, index: number, cursor: Cursor, button: number) {
|
|
if (button === LEFT_CLICK) {
|
|
left_click(container, index, cursor);
|
|
} else if (button === RIGHT_CLICK) {
|
|
right_click(container, index, cursor);
|
|
}
|
|
if (cursor.item && cursor.item.amount <= 0) {
|
|
cursor.item = undefined;
|
|
}
|
|
}
|
|
|
|
function swap_with_cursor(container: Container, index: number, cursor: Cursor) {
|
|
const slot = container.get_slot(index);
|
|
const original = cursor.item;
|
|
cursor.item = slot.get_item();
|
|
slot.set_item(original);
|
|
}
|
|
|
|
function left_click(container: Container, index: number, cursor: Cursor) {
|
|
const holding = cursor.item;
|
|
const slot = container.get_slot(index);
|
|
const slot_item = slot.get_item();
|
|
|
|
// if you aren't holding anything
|
|
// "swap" with nothing on your hand (pick it up)
|
|
if (!holding) {
|
|
swap_with_cursor(container, index, cursor);
|
|
return;
|
|
}
|
|
|
|
// if you are holding something and slot type equals holding type
|
|
// try to add to stack
|
|
if (slot_item && slot.type_id === holding.type_id) {
|
|
const space_left = slot.max_amount! - slot_item.amount;
|
|
const amount_to_add = Math.min(space_left, holding.amount);
|
|
|
|
slot_item.amount += amount_to_add;
|
|
holding.amount -= amount_to_add;
|
|
|
|
return;
|
|
}
|
|
|
|
// if something on hand but not the same
|
|
// swap
|
|
swap_with_cursor(container, index, cursor);
|
|
}
|
|
|
|
function right_click(container: Container, index: number, cursor: Cursor) {
|
|
const holding = cursor.item;
|
|
const slot = container.get_slot(index);
|
|
const slot_item = slot.get_item();
|
|
|
|
// if holding something
|
|
if (holding) {
|
|
// and slot type equals holding type
|
|
// add 1 to matching stack
|
|
if (slot_item && slot.type_id === holding.type_id) {
|
|
if (slot_item.amount < slot_item.max_amount) {
|
|
slot_item.amount += 1;
|
|
holding.amount -= 1;
|
|
}
|
|
return;
|
|
}
|
|
|
|
// place 1 into empty slot
|
|
if (!slot_item) {
|
|
const new_item = holding.clone();
|
|
new_item.amount = 1;
|
|
slot.set_item(new_item);
|
|
holding.amount -= 1;
|
|
return;
|
|
}
|
|
|
|
// if something on hand but not the same
|
|
// swap
|
|
swap_with_cursor(container, index, cursor);
|
|
return;
|
|
}
|
|
|
|
// if player is holding nothing and clicks nothing, nothing happens
|
|
if (!slot_item) {
|
|
return;
|
|
}
|
|
|
|
// pick up half of the stack
|
|
const original_amount = slot_item.amount;
|
|
const half = Math.floor(original_amount / 2);
|
|
|
|
const picked_up = slot_item.clone();
|
|
picked_up.amount = original_amount - half;
|
|
cursor.item = picked_up;
|
|
|
|
slot.amount = half;
|
|
}
|
|
|
|
// a slot of some container, menus' slots can come from several
|
|
export interface SlotRef {
|
|
container: Container;
|
|
index: number;
|
|
}
|
|
|
|
// shift clicking, like minecraft's moveItemStackTo: tops up stacks of the same item in these slots, in order, then
|
|
// fills empty ones. what doesn't fit stays in the item
|
|
export function move_into(item: ItemStack, slots: SlotRef[], accepts: (slot: SlotRef) => boolean = () => true) {
|
|
for (const slot of slots) {
|
|
const there = slot.container.get_item(slot.index);
|
|
if (item.amount <= 0) return;
|
|
if (there && there !== item && there.type_id === item.type_id && accepts(slot)) {
|
|
const moving = Math.min(there.max_amount - there.amount, item.amount);
|
|
there.amount += moving;
|
|
item.amount -= moving;
|
|
}
|
|
}
|
|
for (const slot of slots) {
|
|
if (item.amount <= 0) return;
|
|
if (!slot.container.get_item(slot.index) && accepts(slot)) {
|
|
slot.container.set_item(slot.index, item.clone());
|
|
item.amount = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
// how many of the item move_into could put in these slots
|
|
export function room_for(item: ItemStack, slots: SlotRef[], accepts: (slot: SlotRef) => boolean = () => true): number {
|
|
let room = 0;
|
|
for (const slot of slots) {
|
|
const there = slot.container.get_item(slot.index);
|
|
if (!accepts(slot)) continue;
|
|
if (!there) room += item.max_amount;
|
|
else if (there.type_id === item.type_id) room += there.max_amount - there.amount;
|
|
}
|
|
return room;
|
|
}
|
|
|
|
// whether take_output would take it
|
|
export function can_take_output(item: ItemStack, cursor: Cursor): boolean {
|
|
const holding = cursor.item;
|
|
return !holding || (holding.type_id === item.type_id && holding.max_amount - holding.amount >= item.amount);
|
|
}
|
|
|
|
// output slots (furnace result, crafting result) can only be taken from, all at once
|
|
// returns whether it was taken
|
|
export function take_output(item: ItemStack, cursor: Cursor): boolean {
|
|
const holding = cursor.item;
|
|
if (!holding) {
|
|
cursor.item = item.clone();
|
|
return true;
|
|
}
|
|
if (holding.type_id === item.type_id && holding.max_amount - holding.amount >= item.amount) {
|
|
holding.amount += item.amount;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|