Send block data to clients

This commit is contained in:
2026-10-13 23:26:10 -03:00
parent a10b15a3fd
commit 55ad08266e
18 changed files with 283 additions and 90 deletions
+15 -5
View File
@@ -7,7 +7,7 @@ import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everyt
import { AssetManager } from "$/client/assets.ts";
import { get_sprite_region } from "$/client/sprites.ts";
import type { ItemEntity } from "$/client/entity/item_entity.ts";
import type { ItemStack } from "$/common/inventory.ts";
import { type ItemData, ItemStack } from "$/common/inventory.ts";
import { light_color, push_vertex, Texture } from "$/client/renderer/mod.ts";
import {
bake_model,
@@ -105,17 +105,27 @@ export function render_item_entity(entity: ItemEntity, partial_tick: number) {
}
}
// every item blocks with item_display show on top of them, see BlockRegistry.item_display. the atlas has to be the
// current texture
// the stacks shown items are drawn as, by their "item" client data, which is replaced whenever it changes
const shown_stacks = new WeakMap<ItemData, ItemStack>();
// every item blocks with item_display show on top of them, their "item" client data, see BlockRegistry.item_display.
// the atlas has to be the current texture
export function render_block_items(level: ClientLevel) {
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
for (const items of level.block_items.values()) {
for (const { x, y, z, item } of items.values()) {
for (const in_chunk of level.block_data.values()) {
for (const { x, y, z, data } of in_chunk.values()) {
const shown = data.item as ItemData | undefined;
if (!shown) continue;
// gone already when this client predicted breaking it
const value = level.get_block_value(x, y, z);
const block = blocks[value & ID_MASK];
if (!block?.item_display) continue;
let item = shown_stacks.get(shown);
if (!item) {
item = ItemStack.from_data(shown);
shown_stacks.set(shown, item);
}
// the block's own light, or the one above for blocks light doesn't get into, since the items sit on top
const here = level.get_light(x, y, z);
const above = level.get_light(x, y + 1, z);