Fluid containers

This commit is contained in:
2026-10-25 15:02:31 -03:00
parent fc27771a92
commit 26b59a1899
24 changed files with 486 additions and 82 deletions
+42 -11
View File
@@ -2,6 +2,9 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import type { FluidVariant } from "./mod_api/shared.ts";
import { is_object } from "./schema.ts";
import { same_data } from "./utils.ts";
// how item stacks are saved and sent over the network
export interface ItemData {
@@ -12,25 +15,53 @@ export interface ItemData {
lore?: string;
}
// whether two stacks are of one thing and can be one stack, like minecraft's isSameItemSameComponents: the same item,
// and the same data whatever order its keys are in
export function same_stack(a: ItemStack, b: ItemStack): boolean {
return a.type_id === b.type_id && same_data(a.data, b.data);
}
// how many fit in one slot: the item's max_stack, 1 for tools and 64 for everything else when it has none, and a full
// fluid container's full_max_stack
export function max_stack(id: string, data?: unknown): number {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
const max = item?.max_stack ?? (item?.tool ? 1 : 64);
return contained_fluid(id, data) ? item!.fluid_container!.full_max_stack ?? max : max;
}
// the fluid a fluid container's stack holds, undefined for an empty one or an item that isn't a container. one of a
// fluid no mod defines anymore is empty
export function contained_fluid(id: string, data?: unknown): FluidVariant | undefined {
if (!EverythingRegistry.get<ItemRegistry>("items", id)?.fluid_container || !is_object(data)) return undefined;
const fluid = data.fluid;
if (!is_object(fluid) || typeof fluid.id !== "string" || !EverythingRegistry.get("fluids", fluid.id)) {
return undefined;
}
return fluid as unknown as FluidVariant;
}
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) {
constructor(type_id: string | string, amount: number = 1) {
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);
}
}
// from its data each time, since scripts change that, like filling a bucket
get max_amount(): number {
return max_stack(this.type_id, this.data);
}
// 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;
@@ -50,7 +81,7 @@ export class ItemStack<T = unknown | undefined> {
}
clone(): ItemStack {
const item = new ItemStack(this.type_id, this.amount, this.max_amount);
const item = new ItemStack(this.type_id, this.amount);
item.data = structuredClone(this.data);
item.lore = this.lore;
return item;
@@ -143,7 +174,7 @@ export class Container {
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) {
if (same_stack(slot_item, item_stack)) {
const missing = slot_item.max_amount - slot_item.amount;
const adding = Math.min(missing, item_stack.amount);
slot_item.amount += adding;
@@ -240,7 +271,7 @@ function left_click(container: Container, index: number, cursor: Cursor, limit:
// 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) {
if (slot_item && same_stack(slot_item, holding)) {
const space_left = Math.min(slot.max_amount!, limit) - slot_item.amount;
const amount_to_add = Math.max(0, Math.min(space_left, holding.amount));
@@ -275,7 +306,7 @@ function right_click(container: Container, index: number, cursor: Cursor, limit:
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 && same_stack(slot_item, holding)) {
if (slot_item.amount < Math.min(slot_item.max_amount, limit)) {
slot_item.amount += 1;
holding.amount -= 1;
@@ -333,7 +364,7 @@ export function move_into(item: ItemStack, slots: SlotRef[], accepts: (slot: Slo
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)) {
if (there && there !== item && same_stack(there, item) && accepts(slot)) {
const moving = Math.max(0, Math.min(slot_cap(slot, there) - there.amount, item.amount));
there.amount += moving;
item.amount -= moving;
@@ -357,7 +388,7 @@ export function room_for(item: ItemStack, slots: SlotRef[], accepts: (slot: Slot
const there = slot.container.get_item(slot.index);
if (!accepts(slot)) continue;
if (!there) room += slot_cap(slot, item);
else if (there.type_id === item.type_id) room += Math.max(0, slot_cap(slot, there) - there.amount);
else if (same_stack(there, item)) room += Math.max(0, slot_cap(slot, there) - there.amount);
}
return room;
}
@@ -365,7 +396,7 @@ export function room_for(item: ItemStack, slots: SlotRef[], accepts: (slot: Slot
// 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);
return !holding || (same_stack(holding, item) && holding.max_amount - holding.amount >= item.amount);
}
// output slots (furnace result, crafting result) can only be taken from, all at once
@@ -376,7 +407,7 @@ export function take_output(item: ItemStack, cursor: Cursor): boolean {
cursor.item = item.clone();
return true;
}
if (holding.type_id === item.type_id && holding.max_amount - holding.amount >= item.amount) {
if (same_stack(holding, item) && holding.max_amount - holding.amount >= item.amount) {
holding.amount += item.amount;
return true;
}