Improve crops system

This commit is contained in:
2026-09-26 18:32:38 -03:00
parent e22f12c0b6
commit 0fa6ec4ed3
15 changed files with 394 additions and 31 deletions
+13 -1
View File
@@ -18,6 +18,18 @@
"bits": 1,
"default": 0
}
]
],
"variants": {
"watered=1": {
"textures": {
"side": "bworld:dirt",
"top": "bworld:hoed_watered_dirt",
"bottom": "bworld:dirt"
}
}
},
"components": {
"bworld:farmland": {}
}
}
}
+4 -1
View File
@@ -8,6 +8,9 @@
"alpha": 0.8,
"collision": false,
"item": false,
"replaceable": true
"replaceable": true,
"components": {
"bworld:water_source": {}
}
}
}
+8 -3
View File
@@ -64,9 +64,14 @@
],
"components": {
"bworld:crop": {
"fertilizer": "bworld:bone_meal",
"max_age": 7
"max_age": 7,
"produce": "bworld:wheat",
"seeds": "bworld:wheat_seeds",
"fertilizer": "bworld:bone_meal"
}
}
},
"place_on": [
"bworld:hoed_dirt"
]
}
}
+105 -14
View File
@@ -1,13 +1,12 @@
// the base game's block and item behavior, written against the same api as any other mod
import type { BlockRef, Container, ScreenHandle, ServerContext } from "bworld/server";
import type { BlockRef, Container, Player, ScreenHandle, ServerContext } from "bworld/server";
export function setup(ctx: ServerContext) {
register_hoeable(ctx);
register_storage(ctx);
register_furnace(ctx);
register_smithing_table(ctx);
register_crop(ctx);
register_watering_can(ctx);
register_farming(ctx);
}
// right clicking with a tool turns the block into another, like a hoe on grass
@@ -272,12 +271,88 @@ function register_smithing_table(ctx: ServerContext) {
});
}
// grows one stage each time it's right clicked with the fertilizer, like bone meal on wheat
function register_crop(ctx: ServerContext) {
ctx.components.register_block<{ fertilizer: string; max_age: number }>("bworld:crop", {
// farming: java's crop rules on stardew valley's clock. every morning at 06:00 a crop grows one stage if its farmland
// was watered the day before, then all farmland dries. crops only stand on farmland (their place_on), and pop off when
// it's dug up
const FARMLAND = "bworld:hoed_dirt";
const WATERING_CAN = "bworld:watering_can";
interface WateringCanData {
water: number;
max_water: number;
}
interface CropParams {
max_age: number;
// what a fully grown crop drops besides its seeds, and the seeds for the extra ones
produce?: string;
seeds?: string;
// grows it one stage right away, like bone meal
fertilizer?: string;
}
function register_farming(ctx: ServerContext) {
const is_watered = (x: number, y: number, z: number) =>
ctx.world.get_block(x, y, z) === FARMLAND && ctx.world.get_state(x, y, z, "watered") === 1;
// with the watering can the player holds. true when the click was about watering, so nothing gets placed
const water = (x: number, y: number, z: number, player: Player) => {
const can = player.held_item;
if (can?.id !== WATERING_CAN || ctx.world.get_block(x, y, z) !== FARMLAND) {
return false;
}
const data = can.data as WateringCanData;
if (is_watered(x, y, z)) {
return true;
}
if (data.water <= 0) {
player.send_message("The watering can is empty, fill it at some water");
return true;
}
can.data = { ...data, water: data.water - 1 };
ctx.world.set_state(x, y, z, "watered", 1);
return true;
};
ctx.components.register_block("bworld:farmland", {
// block data so on_new_day runs for it
on_create(block) {
block.data = {};
},
on_interact(block, _params, player) {
block.data ??= {};
return water(block.x, block.y, block.z, player);
},
on_new_day(block) {
if (!is_watered(block.x, block.y, block.z)) return;
// after every crop's on_new_day this morning, which look at it
ctx.system.run_timeout(() => {
if (ctx.world.get_block(block.x, block.y, block.z) === FARMLAND) {
ctx.world.set_state(block.x, block.y, block.z, "watered", 0);
}
}, 0);
},
});
ctx.components.register_block<CropParams>("bworld:crop", {
on_create(block) {
block.data = {};
},
on_new_day(block, params) {
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
if (age < params.max_age && is_watered(block.x, block.y - 1, block.z)) {
ctx.world.set_state(block.x, block.y, block.z, "age", age + 1);
}
},
on_interact(block, params, player) {
block.data ??= {};
const held = player.held_item;
if (held?.id !== params.fertilizer) {
// watering a crop waters the farmland under it
if (held?.id === WATERING_CAN) {
return water(block.x, block.y - 1, block.z, player);
}
if (!params.fertilizer || held?.id !== params.fertilizer) {
return false;
}
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
@@ -289,16 +364,32 @@ function register_crop(ctx: ServerContext) {
held.count -= 1;
return true;
},
// the block's own drops are one seed. fully grown it also drops its produce and 0 to 3 more seeds, like
// minecraft's wheat
on_break(block, params) {
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
if (age < params.max_age) return;
if (params.produce) ctx.world.drop_item(block.x, block.y, block.z, { id: params.produce, count: 1 });
let extra = 0;
for (let i = 0; i < 3; i++) if (Math.random() < 0.5714) extra++;
if (params.seeds && extra > 0) {
ctx.world.drop_item(block.x, block.y, block.z, { id: params.seeds, count: extra });
}
},
});
}
interface WateringCanData {
water: number;
max_water: number;
}
// right clicking water with the watering can fills it
ctx.components.register_block("bworld:water_source", {
on_interact(_block, _params, player) {
const can = player.held_item;
if (can?.id !== WATERING_CAN) return false;
const data = can.data as WateringCanData;
can.data = { ...data, water: data.max_water };
return true;
},
});
function register_watering_can(ctx: ServerContext) {
ctx.components.register_item<{ max_water: number }>("bworld:watering_can", {
ctx.components.register_item<{ max_water: number }>(WATERING_CAN, {
on_create(item, params) {
item.data = { water: 0, max_water: params.max_water } satisfies WateringCanData;
},