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 -2
View File
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { type ItemData, ItemStack } from "$/common/inventory.ts";
import { type ItemData, ItemStack, same_stack } from "$/common/inventory.ts";
import type { EntityInfo } from "$/common/protocol.ts";
import { TICK_DELTA } from "$/common/constants.ts";
import { physics } from "$/common/physics.ts";
@@ -126,7 +126,7 @@ export class ItemEntity extends Entity {
can_merge_with(other: ItemEntity) {
return other !== this && !other.removed && other.item.type_id === this.item.type_id &&
this.item.amount < this.item.max_amount && other.item.amount < other.item.max_amount &&
(other.item.data === this.item.data || JSON.stringify(other.item.data) === JSON.stringify(this.item.data));
same_stack(other.item, this.item);
}
// the smaller stack goes into the bigger one, like minecraft's ItemEntity.tryToMerge
+2 -1
View File
@@ -502,8 +502,9 @@ export class GameServer {
let left = count;
while (left > 0) {
const stack = new ItemStack(id);
stack.amount = Math.min(left, stack.max_amount);
// first, a full fluid container stacks to fewer
if (data !== undefined) stack.data = structuredClone(data);
stack.amount = Math.min(left, stack.max_amount);
left -= stack.amount;
this.give_stack(player, stack);
}
+15 -3
View File
@@ -4,7 +4,7 @@
// runs mods' server scripts: builds each one's ServerContext and keeps what they register.
// see docs/modding/. parts not built yet throw when used, saying so
import { BlockRegistry, EverythingRegistry, fluid_height, ItemRegistry } from "$/common/everything_registry.ts";
import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/common/inventory.ts";
import { Container as EngineContainer, ItemStack as EngineItemStack, max_stack } from "$/common/inventory.ts";
import {
block_states,
fluid_block,
@@ -903,10 +903,14 @@ export class ModRuntime {
items: {
exists: (id) => EverythingRegistry.get("items", id) !== undefined,
ids: () => EverythingRegistry.entries("items").map(([id]) => id),
max_stack: (id) => {
max_stack: (id, data) => {
if (!EverythingRegistry.get("items", id)) throw new Error(`unknown item ${id}`);
return max_stack(id, data);
},
fluid_capacity: (id) => {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
if (!item) throw new Error(`unknown item ${id}`);
return item.max_stack ?? (item.tool ? 1 : 64);
return item.fluid_container?.capacity;
},
tool: (id) => {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
@@ -1401,6 +1405,14 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
if (!EverythingRegistry.get("items", id)) throw new Error(`unknown item ${id}`);
game.give(player, id, count, data);
},
exchange_held(result) {
const held = player.held_item;
if (!held) throw new Error("exchange_held needs something in the hand");
const stack = new_stack(result);
held.amount -= 1;
if (held.amount <= 0) player.inventory.set_item(player.selected_slot, stack);
else game.give_stack(player, stack);
},
send_message(text, options) {
game.send_chat(player, String(text), options?.overlay === true);
},