Fluid data
This commit is contained in:
+48
-4
@@ -6,8 +6,10 @@ import { BlockRegistry, EverythingRegistry, fluid_height, ItemRegistry } from ".
|
||||
import { block_item_texture, cube_face_texture } from "./block_models.ts";
|
||||
import { FULL_SHAPE, type Shape } from "./block_shapes.ts";
|
||||
import type { ItemStack } from "./inventory.ts";
|
||||
import type { SharedContext } from "./mod_api/shared.ts";
|
||||
import type { FluidInCell } from "./physics.ts";
|
||||
import { list_matches } from "./tags.ts";
|
||||
import { is_object } from "./schema.ts";
|
||||
|
||||
export function point_inside_rec(
|
||||
point_x: number,
|
||||
@@ -80,14 +82,56 @@ export function item_texture(
|
||||
return typeof info.texture_id === "function" ? info.texture_id(item) : info.texture_id;
|
||||
}
|
||||
|
||||
// what players see an item called: its name, or one made from its id, so bworld:iron_ingot is "Iron Ingot"
|
||||
// what players see an item called: its name, or one made from its id
|
||||
export function display_name(item_id: string): string {
|
||||
const name = EverythingRegistry.get<ItemRegistry>("items", item_id)?.name;
|
||||
if (name) return name;
|
||||
const words = (item_id.split(":")[1] ?? item_id).split("_").filter((word) => word.length > 0);
|
||||
return EverythingRegistry.get<ItemRegistry>("items", item_id)?.name || name_from_id(item_id);
|
||||
}
|
||||
|
||||
// ctx.items for shared scripts, and the part of the client's that's the same
|
||||
export function shared_items_api(mod: string): SharedContext["items"] {
|
||||
const known = (id: string) => {
|
||||
const item = EverythingRegistry.get<ItemRegistry>("items", id);
|
||||
if (!item) throw new Error(`[${mod}] unknown item ${id}`);
|
||||
return item;
|
||||
};
|
||||
return {
|
||||
exists: (id) => EverythingRegistry.get("items", id) !== undefined,
|
||||
name: (id) => (known(id), display_name(id)),
|
||||
components: (id) => structuredClone(known(id).components ?? {}),
|
||||
};
|
||||
}
|
||||
|
||||
// a name for what didn't give one, so bworld:iron_ingot is "Iron Ingot"
|
||||
export function name_from_id(id: string): string {
|
||||
const words = (id.split(":")[1] ?? id).split("_").filter((word) => word.length > 0);
|
||||
return words.map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
|
||||
}
|
||||
|
||||
// whether two stacks' or fluids' data are the same, whatever order their keys are in. no data is the same as {}, and a
|
||||
// key set to undefined as one that isn't there, the way they'd be once saved or sent
|
||||
export function same_data(a: unknown, b: unknown): boolean {
|
||||
return (is_empty_data(a) && is_empty_data(b)) || same_json(a, b);
|
||||
}
|
||||
|
||||
function is_empty_data(value: unknown) {
|
||||
return value === undefined || (is_object(value) && defined_keys(value).length === 0);
|
||||
}
|
||||
|
||||
function same_json(a: unknown, b: unknown): boolean {
|
||||
if (a === b) return true;
|
||||
if (Array.isArray(a) || Array.isArray(b)) {
|
||||
return Array.isArray(a) && Array.isArray(b) && a.length === b.length &&
|
||||
a.every((value, i) => same_json(value, b[i]));
|
||||
}
|
||||
if (!is_object(a) || !is_object(b)) return false;
|
||||
const keys = defined_keys(a);
|
||||
return keys.length === defined_keys(b).length && keys.every((key) => same_json(a[key], b[key]));
|
||||
}
|
||||
|
||||
function defined_keys(value: Record<string, unknown>) {
|
||||
return Object.keys(value).filter((key) => value[key] !== undefined);
|
||||
}
|
||||
|
||||
export function get_state_value(value: number, block_info: BlockRegistry, name: string) {
|
||||
if (!block_info.states) {
|
||||
return undefined;
|
||||
|
||||
Reference in New Issue
Block a user