Add a bunch of crops and farmer's delight crops

This commit is contained in:
2026-09-26 21:35:17 -03:00
parent 8d9c85d379
commit 7e5bcaa239
77 changed files with 980 additions and 81 deletions
+60
View File
@@ -0,0 +1,60 @@
{
"format_version": 1,
"block": {
"id": "bworld:carrots",
"textures": {
"crop": "bworld:carrot_stage_0"
},
"model": "engine:crop",
"variants": {
"age=0": {
"textures": {
"crop": "bworld:carrot_stage_0"
}
},
"age=1": {
"textures": {
"crop": "bworld:carrot_stage_1"
}
},
"age=2": {
"textures": {
"crop": "bworld:carrot_stage_2"
}
},
"age=3": {
"textures": {
"crop": "bworld:carrot_stage_3"
}
}
},
"render_layer": "cutout",
"collision": false,
"mining": {
"toughness": 0
},
"item": false,
"place_on": [
"bworld:hoed_dirt"
],
"states": [
{
"name": "age",
"bits": 2,
"default": 0
}
],
"components": {
"bworld:crop": {
"days": 3,
"stages": 4,
"fertilizer": "bworld:bone_meal",
"drops": {
"young": "bworld:carrot",
"mature": "bworld:carrot",
"bonus": "bworld:carrot"
}
}
}
}
}
+60
View File
@@ -0,0 +1,60 @@
{
"format_version": 1,
"block": {
"id": "bworld:potatoes",
"textures": {
"crop": "bworld:potatoes_stage_0"
},
"model": "engine:crop",
"variants": {
"age=0": {
"textures": {
"crop": "bworld:potatoes_stage_0"
}
},
"age=1": {
"textures": {
"crop": "bworld:potatoes_stage_1"
}
},
"age=2": {
"textures": {
"crop": "bworld:potatoes_stage_2"
}
},
"age=3": {
"textures": {
"crop": "bworld:potatoes_stage_3"
}
}
},
"render_layer": "cutout",
"collision": false,
"mining": {
"toughness": 0
},
"item": false,
"place_on": [
"bworld:hoed_dirt"
],
"states": [
{
"name": "age",
"bits": 2,
"default": 0
}
],
"components": {
"bworld:crop": {
"days": 6,
"stages": 4,
"fertilizer": "bworld:bone_meal",
"drops": {
"young": "bworld:potato",
"mature": "bworld:potato",
"bonus": "bworld:potato"
}
}
}
}
}
+12 -9
View File
@@ -53,8 +53,10 @@
"mining": {
"toughness": 0
},
"drops": "bworld:wheat_seeds",
"item": false,
"place_on": [
"bworld:hoed_dirt"
],
"states": [
{
"name": "age",
@@ -64,14 +66,15 @@
],
"components": {
"bworld:crop": {
"max_age": 7,
"produce": "bworld:wheat",
"seeds": "bworld:wheat_seeds",
"fertilizer": "bworld:bone_meal"
"days": 7,
"stages": 8,
"fertilizer": "bworld:bone_meal",
"drops": {
"young": "bworld:wheat_seeds",
"mature": "bworld:wheat",
"bonus": "bworld:wheat_seeds"
}
}
},
"place_on": [
"bworld:hoed_dirt"
]
}
}
}
+2 -1
View File
@@ -6,6 +6,7 @@
"food": {
"nutrition": 3,
"saturation_modifier": 0.6
}
},
"places": "bworld:carrots"
}
}
+2 -1
View File
@@ -6,6 +6,7 @@
"food": {
"nutrition": 1,
"saturation_modifier": 0.3
}
},
"places": "bworld:potatoes"
}
}
+129 -29
View File
@@ -283,13 +283,44 @@ interface WateringCanData {
max_water: number;
}
// items as they appear in crop params: one id, or several
type Items = string | string[];
// what a crop drops when broken, like the loot tables of minecraft's and farmer's delight's crops
interface CropDrops {
// before it's fully grown, like wheat's seeds
young?: Items;
// fully grown, like wheat's wheat
mature?: Items;
// fully grown, 0 to 3 more of this, like the extra seeds from wheat (minecraft's binomial with 3 tries at 57%)
bonus?: string;
}
// a crop, like a fabric CropBlock: its "age" state is its stage, and it grows one day every morning its farmland was
// watered the day before. see "Crops" in MODS.md
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
// watered mornings from planting to fully grown
days: number;
// how many stages it shows, ages 0 to stages - 1. its "age" state must fit them
stages: number;
drops?: CropDrops;
// grows back after it's harvested by right clicking, like tomatoes
regrow_days?: number;
harvest?: { item: string; count: [number, number] };
// grows it a day right away, like bone meal
fertilizer?: string;
// "farmland" (the default) needs watered farmland under it, "always" grows every morning, like rice in water
water?: "farmland" | "always";
// the last stages grow a second block on top, like rice's panicles. the crop stays at supporting_age meanwhile
upper?: { block: string; stages: number; supporting_age: number };
// what the spot becomes when it's broken, like water where rice was
leaves?: string;
}
// the top of a two block crop. it grows with the crop under it, and breaking it breaks the whole plant
interface CropTopParams {
stages: number;
drops?: CropDrops;
}
function register_farming(ctx: ServerContext) {
@@ -335,46 +366,115 @@ function register_farming(ctx: ServerContext) {
},
});
const list = (items: Items | undefined) => items === undefined ? [] : typeof items === "string" ? [items] : items;
const drop = (x: number, y: number, z: number, drops: CropDrops | undefined, mature: boolean) => {
for (const id of list(mature ? drops?.mature : drops?.young)) ctx.world.drop_item(x, y, z, { id, count: 1 });
if (mature && drops?.bonus) {
let count = 0;
for (let i = 0; i < 3; i++) if (Math.random() < 0.5714) count++;
if (count > 0) ctx.world.drop_item(x, y, z, { id: drops.bonus, count });
}
};
// the stage for how many days it has grown: evenly spread, fully grown on the last day
const stage_for = (params: CropParams, days: number) => {
const stages = params.stages + (params.upper?.stages ?? 0);
return Math.min(stages - 1, Math.floor(days * (stages - 1) / params.days));
};
// days grown, kept in the block's data. crops from before this start at the first day of the stage they show
const days_of = (block: BlockRef, params: CropParams) => {
if (typeof block.data?.days === "number") return block.data.days as number;
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
let days = 0;
while (days < params.days && stage_for(params, days) < age) days++;
return days;
};
// shows the stage for days, growing or taking away the top block for two block crops
const set_days = (block: BlockRef, params: CropParams, days: number) => {
block.data = { ...block.data, days };
const stage = stage_for(params, days);
const upper = params.upper;
const above = ctx.world.get_block(block.x, block.y + 1, block.z);
if (!upper || stage < params.stages) {
ctx.world.set_state(block.x, block.y, block.z, "age", stage);
if (upper && above === upper.block) ctx.world.set_block(block.x, block.y + 1, block.z, "bworld:air");
return;
}
// the top needs room, like farmer's delight's rice
if (above !== upper.block && above !== "bworld:air") {
ctx.world.set_state(block.x, block.y, block.z, "age", params.stages - 1);
return;
}
ctx.world.set_state(block.x, block.y, block.z, "age", upper.supporting_age);
if (above !== upper.block) ctx.world.set_block(block.x, block.y + 1, block.z, upper.block);
ctx.world.set_state(block.x, block.y + 1, block.z, "age", stage - params.stages);
};
const is_grown = (block: BlockRef, params: CropParams) => days_of(block, params) >= params.days;
ctx.components.register_block<CropParams>("bworld:crop", {
on_create(block) {
// block data so on_new_day runs for it. placed at a later stage (by a script), it has grown that long
on_create(block, params) {
block.data = {};
block.data = { days: days_of(block, params) };
},
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);
}
const days = days_of(block, params);
if (days >= params.days) return;
const watered = params.water === "always" || is_watered(block.x, block.y - 1, block.z);
if (watered) set_days(block, params, days + 1);
},
on_interact(block, params, player) {
block.data ??= {};
const held = player.held_item;
// watering a crop waters the farmland under it
if (held?.id === WATERING_CAN) {
block.data ??= { days: days_of(block, params) };
return water(block.x, block.y - 1, block.z, player);
}
if (!params.fertilizer || held?.id !== params.fertilizer) {
return false;
const days = days_of(block, params);
if (params.fertilizer && held?.id === params.fertilizer) {
// fully grown, keep the fertilizer
if (days >= params.days) return false;
set_days(block, params, days + 1);
held.count -= 1;
return true;
}
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
// fully grown, keep the fertilizer
if (age >= params.max_age) {
return false;
// regrowing crops are picked by hand and grow back, like farmer's delight's tomatoes
if (params.regrow_days && params.harvest && days >= params.days) {
const [min, max] = params.harvest.count;
const count = min + Math.floor(Math.random() * (max - min + 1));
ctx.world.drop_item(block.x, block.y, block.z, { id: params.harvest.item, count });
set_days(block, params, Math.max(0, params.days - params.regrow_days));
return true;
}
ctx.world.set_state(block.x, block.y, block.z, "age", age + 1);
held.count -= 1;
return true;
return false;
},
// 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) {
drop(block.x, block.y, block.z, params.drops, is_grown(block, params));
if (params.leaves) {
const { x, y, z } = block;
const leaves = params.leaves;
// after it's gone, it's still there while on_break runs
ctx.system.run_timeout(() => {
if (ctx.world.get_block(x, y, z) === "bworld:air") ctx.world.set_block(x, y, z, leaves);
}, 0);
}
},
});
ctx.components.register_block<CropTopParams>("bworld:crop_top", {
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 });
}
drop(block.x, block.y, block.z, params.drops, age >= params.stages - 1);
// the plant under it goes too, once this is gone
const { x, y, z } = block;
const lower = ctx.world.get_block(x, y - 1, z);
ctx.system.run_timeout(() => {
if (lower && ctx.world.get_block(x, y - 1, z) === lower) ctx.world.break_block(x, y - 1, z);
}, 0);
},
});
Binary file not shown.

After

Width:  |  Height:  |  Size: 192 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 202 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 B