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
+2 -3
View File
@@ -5,7 +5,7 @@
// a block picks one with "model" and fills in its texture variables with "textures". baking turns a model
// into quads in block space (0 to 1) that the chunk mesher and the item renderers draw
import { type BlockRegistry, type BlockTextures, fluid_textures } from "./everything_registry.ts";
import { tint_without_biome } from "./worldgen/biome_colors.ts";
import { color_rgb, tint_without_biome } from "./worldgen/biome_colors.ts";
import { get_state_value } from "./utils.ts";
import { type DisplayContext, MODEL_FACES, type ModelFace } from "./mod_data.ts";
@@ -382,8 +382,7 @@ export function cube_face_tint(block: BlockRegistry, face: "top" | "bottom" | "f
// the color to draw a part of a block's item in that got its texture through these variables, see cube_face_tint
export function item_tint(block: BlockRegistry, variables: string[]): number[] {
if (!is_tinted(block, variables)) return [1, 1, 1];
const color = tint_without_biome(block.tint!.color);
return [color >> 16, (color >> 8) & 0xff, color & 0xff].map((c) => c / 255);
return color_rgb(tint_without_biome(block.tint!.color));
}
// how much darker each side of an item's model is drawn, like the mesher's: top, bottom, south, north, west, east
+20
View File
@@ -247,6 +247,23 @@ export interface FoodProperties {
returns?: string;
}
// an item that holds one fluid, full or empty, like a bucket. a full one's stack data is { fluid: FluidVariant }, see
// docs/modding/resources/fluids.md. what it looks like full is a layer tinted "fluid"
export interface FluidContainerProperties {
// how much a full one holds, in mB
capacity: number;
// how many full ones fit in a slot, the item's max_stack when not set
full_max_stack?: number;
}
// a texture drawn over a flat item's own, like the fluid in a bucket, drawn in its tint's color: a block tint's color
// outside any biome, or "fluid" for the fluid a fluid container holds. a layer whose tint has no color for a stack, like
// "fluid" on an empty container, isn't drawn
export interface ItemLayer {
texture: string;
tint?: TintColor | "fluid";
}
export interface ItemRegistry<T = unknown | undefined> {
texture_id: string | ((item: ItemStack<T>) => string);
// shown in place of texture_id while the stack's data has that key set, like a cast fishing rod. the first wins
@@ -267,6 +284,9 @@ export interface ItemRegistry<T = unknown | undefined> {
// whether players holding it aim at fluids, like buckets and fishing rods. others look through them, so they can
// break and place blocks under water
targets_fluids?: boolean;
fluid_container?: FluidContainerProperties;
// drawn over its texture in order, see ItemLayer
layers?: ItemLayer[];
components?: Record<string, unknown>;
on_create?(item: ItemStack<T>): void;
+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;
}
+8 -2
View File
@@ -421,6 +421,9 @@ export interface Player {
readonly held_item: ItemStack | undefined;
// what doesn't fit in the inventory drops at their feet
give_item(id: Id, count?: number, data?: unknown): void;
// one of the held item becomes result, like an empty bucket filling: straight into the hand when it was the last
// one, into the inventory (or at their feet) when there are more
exchange_held(result: ItemStack): void;
send_message(text: string, options?: MessageOptions): void;
// items drawn on them, under a key so each mod keeps its own list, like "bworld:curios". replaces what that key
// had, [] clears it. only items whose json has worn show. not saved: set it again when they join
@@ -525,8 +528,11 @@ export interface ItemApi {
exists(id: Id): boolean;
// every item, in load order
ids(): Id[];
// how many fit in one slot
max_stack(id: Id): number;
// how many fit in one slot, with this data: a full fluid container's can be fewer
max_stack(id: Id, data?: unknown): number;
// how much a fluid container holds when full, in mB, or undefined for an item that isn't one. see
// docs/modding/resources/fluids.md
fluid_capacity(id: Id): number | undefined;
// the tool type it is, like "pickaxe" or "shovel", from the item's "tool"
tool(id: Id): string | undefined;
// what one of it sells for, from the item's "base_sell_value", or undefined if it can't be sold
+6
View File
@@ -323,6 +323,12 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
if (json.model) uses(file, json.model, "model");
if (json.places) uses(file, json.places, "block");
if (json.food?.returns) uses(file, json.food.returns, "item");
for (const layer of json.layers ?? []) uses(file, layer.texture, "texture");
if (json.layers && json.model) {
errors.push(
`${file}: item ${json.id} has a model, so it isn't drawn flat and its layers wouldn't show`,
);
}
if (json.worn) {
const { model, texture, slim_model, slim_texture } = json.worn;
for (const id of [model, slim_model]) {
+25 -2
View File
@@ -10,8 +10,10 @@ import {
type BlockStateDefinition,
FLUID_DEFAULTS,
FLUID_LEVEL,
type FluidContainerProperties,
type FluidProperties,
type FoodProperties,
type ItemLayer,
type ItemRegistry,
MAX_FLUID_RANGE,
RENDER_LAYERS,
@@ -58,6 +60,10 @@ type BlockTextures = BlockRegistry["textures"];
// how far a model turns
const right_angle = z.literal([0, 90, 180, 270]);
// what a block's tint draws its textures in, see TintColor
const tint_color = z.custom<TintColor>(is_tint_color, 'must be "grass", "foliage", "water" or a color like "#80a755"');
// a texture id, or texture variables mapped to ids
const textures = z.union([id, z.record(z.string(), id)], "must be a texture id or { variable: texture id }");
const stack = z.object({ id, count: z.int().min(1) });
@@ -168,7 +174,7 @@ const block_fields = z.object({
render_layer: z.enum(RENDER_LAYERS).optional(),
cull_same: z.boolean().optional(),
tint: z.object({
color: z.custom<TintColor>(is_tint_color, 'must be "grass", "foliage", "water" or a color like "#80a755"'),
color: tint_color,
// texture variables, like ["top"]
textures: z.array(z.string()).optional(),
}).optional(),
@@ -368,6 +374,8 @@ export interface ItemJson {
worn?: WornProperties;
// see ItemRegistry.targets_fluids
targets_fluids?: boolean;
fluid_container?: FluidContainerProperties;
layers?: ItemLayer[];
name?: string;
lore?: string;
components?: Record<string, unknown>;
@@ -401,6 +409,17 @@ const item_schema: z.ZodType<ItemJson> = z.object({
slim_texture: id.optional(),
}, "must be { model, texture }, an entity model and a texture").optional(),
targets_fluids: z.boolean().optional(),
fluid_container: z.strictObject({
capacity: z.int().min(1),
full_max_stack: z.int().min(1).optional(),
}).optional(),
layers: z.array(z.strictObject({
texture: id,
tint: z.custom<ItemLayer["tint"]>(
(value) => value === "fluid" || is_tint_color(value),
'must be "fluid", "grass", "foliage", "water" or a color like "#80a755"',
).optional(),
})).optional(),
name: text.optional(),
lore: text.optional(),
components: json_object.optional(),
@@ -463,7 +482,7 @@ const fluid_schema = z.object({
id,
// what players see, from the id when not set like an item's
name: text.optional(),
// for whatever shows it, like a container's tinted overlay
// for whatever shows it, like a layer tinted "fluid" on a full container
color,
});
@@ -746,6 +765,8 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson {
if (item.food !== undefined) json.food = item.food;
if (item.worn !== undefined) json.worn = item.worn;
if (item.targets_fluids !== undefined) json.targets_fluids = item.targets_fluids;
if (item.fluid_container !== undefined) json.fluid_container = item.fluid_container;
if (item.layers !== undefined) json.layers = item.layers;
if (item.name !== undefined) json.name = item.name;
if (item.lore !== undefined) json.lore = item.lore;
if (item.components) json.components = item.components;
@@ -774,6 +795,8 @@ function item_fields(json: ItemJson): Partial<ItemRegistry> {
if (json.food !== undefined) item.food = json.food;
if (json.worn !== undefined) item.worn = json.worn;
if (json.targets_fluids !== undefined) item.targets_fluids = json.targets_fluids;
if (json.fluid_container !== undefined) item.fluid_container = json.fluid_container;
if (json.layers !== undefined) item.layers = json.layers;
if (json.name !== undefined) item.name = json.name;
if (json.lore !== undefined) item.lore = json.lore;
if (json.components) item.components = json.components;
+5
View File
@@ -30,6 +30,11 @@ export function is_tint_color(value: unknown): value is TintColor {
(typeof value === "string" && /^#[0-9a-fA-F]{6}$/.test(value));
}
// a color like 0x80a755 as red, green and blue from 0 to 1, for drawing
export function color_rgb(color: number): [number, number, number] {
return [(color >> 16) / 255, ((color >> 8) & 0xff) / 255, (color & 0xff) / 255];
}
// the color outside any biome, for items: the default grass, foliage or water color, or the fixed one
export function tint_without_biome(color: TintColor) {
if (color === "grass") return DEFAULT_GRASS_COLOR;