ctx.items.damage and player_death event moved to bworld
This commit is contained in:
@@ -576,6 +576,9 @@ export interface ItemApi {
|
||||
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;
|
||||
// takes durability off a tool, kept in its data's damage, like ItemStack.hurtAndBreak. true when that used it up.
|
||||
// does nothing to items that aren't tools
|
||||
damage(item: ItemStack, amount: number): boolean;
|
||||
// what one of it sells for with this data, or undefined if it can't be sold: the item's "base_sell_value", and a full
|
||||
// fluid container's fluid on top, rounded up. see docs/modding/resources/fluids.md
|
||||
sell_value(id: Id, data?: unknown): number | undefined;
|
||||
|
||||
@@ -212,6 +212,6 @@ blowing them along does it every tick. Their client moves them, so it's sent the
|
||||
|
||||
## Death
|
||||
|
||||
When a player dies, everything they carried falls out where they died, and their tools lose durability (see
|
||||
[Tools](../items/tools.md#durability)). They respawn at spawn right away with full health and hunger, and `player_death`
|
||||
fires.
|
||||
When a player dies, `player_death` fires while they're still where they died, then they respawn at spawn right away with
|
||||
full health and hunger. The engine leaves what they carried alone: the base game's `player_death` handler drops it all
|
||||
where they died, and their tools lose durability (see [Tools](../items/tools.md#durability)).
|
||||
|
||||
@@ -249,6 +249,7 @@ ctx.items.max_stack("bworld:wood_pickaxe"); // 1
|
||||
ctx.items.max_stack(id, data); // the same for a stack with this data, fewer for a full fluid container
|
||||
ctx.items.fluid_capacity("bworld:bucket"); // how much a fluid container holds full, or undefined
|
||||
ctx.items.tool("bworld:wood_pickaxe"); // "pickaxe", or undefined for items that aren't tools
|
||||
ctx.items.damage(item, 1); // takes durability off a tool stack, true when that used it up
|
||||
ctx.items.sell_value("bworld:copper_coin"); // 1, or undefined for items without a base_sell_value
|
||||
ctx.items.sell_value(id, data); // the same for a stack with this data, with a full fluid container's fluid on top
|
||||
|
||||
|
||||
@@ -51,3 +51,6 @@ dropping.
|
||||
|
||||
The damage a tool has taken is kept in its stack's `data.damage`, and a worn tool shows a bar under it in its slot,
|
||||
going from green to red as it wears down. Upgrading a tool at the smithing table keeps its data, damage included.
|
||||
|
||||
A server script wears a tool down with `ctx.items.damage(item, amount)`, which adds to the stack's `data.damage` and
|
||||
returns true when that used it up. It does nothing to items that aren't tools. The base game's death drops use it.
|
||||
|
||||
@@ -39,6 +39,22 @@ export function setup(ctx: ServerContext) {
|
||||
register_stove(ctx);
|
||||
register_cooking_pot(ctx);
|
||||
register_cutting_board(ctx);
|
||||
register_death_drops(ctx);
|
||||
}
|
||||
|
||||
// everything a player carried falls out where they died. tools lose 1 durability first, and ones that run out break
|
||||
// instead of dropping
|
||||
function register_death_drops(ctx: ServerContext) {
|
||||
ctx.events.after.player_death.subscribe(({ player }) => {
|
||||
const { x, y, z } = player.position;
|
||||
for (let i = 0; i < player.inventory.size; i++) {
|
||||
const item = player.inventory.get(i);
|
||||
if (!item) continue;
|
||||
player.inventory.set(i, undefined);
|
||||
if (ctx.items.damage(item, 1)) continue;
|
||||
ctx.world.drop_item(Math.floor(x), Math.floor(Math.max(y, 0)), Math.floor(z), item);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// right clicking with a type of tool turns the block into another, like any hoe on grass
|
||||
|
||||
@@ -1969,20 +1969,11 @@ export class GameServer {
|
||||
player.heal(amount);
|
||||
}
|
||||
|
||||
// like minecraft: everything they carried falls out where they died, and they start over at spawn. tools lose 1
|
||||
// durability first, and ones that run out break instead of dropping
|
||||
// they start over at spawn. what happens to what they carried is up to mods' player_death, which runs while
|
||||
// they're still where they died
|
||||
#kill(player: ServerPlayer, source: DamageSource) {
|
||||
const cause = source.cause;
|
||||
const { x, y, z } = player;
|
||||
this.close_menu(player);
|
||||
for (let i = 0; i < player.inventory.size; i++) {
|
||||
const item = player.inventory.get_item(i);
|
||||
if (item) {
|
||||
player.inventory.set_item(i, undefined);
|
||||
if (item.damage(1)) continue;
|
||||
this.pop_item(Math.floor(x), Math.floor(Math.max(y, 0)), Math.floor(z), item);
|
||||
}
|
||||
}
|
||||
this.mods.after.player_death.emit({ player: this.mods.player(player), cause });
|
||||
this.#broadcast({ type: "chat", text: death_message(player.name, source) });
|
||||
player.vitals.reset();
|
||||
|
||||
@@ -1002,6 +1002,12 @@ export class ModRuntime {
|
||||
if (!item) throw new Error(`unknown item ${id}`);
|
||||
return item.tool?.type;
|
||||
},
|
||||
damage: (item, amount) => {
|
||||
const stack = EngineItemStack.from_data(item);
|
||||
const used_up = stack.damage(amount);
|
||||
item.data = stack.data;
|
||||
return used_up;
|
||||
},
|
||||
sell_value: shared_items_api(mod, this.fluid_rules).sell_value,
|
||||
components: (id) => {
|
||||
const item = EverythingRegistry.get<ItemRegistry>("items", id);
|
||||
|
||||
Reference in New Issue
Block a user