Basic crops system
This commit is contained in:
+32
-1
@@ -1,6 +1,7 @@
|
||||
import { Container, ItemStack } from "$/common/inventory.ts";
|
||||
import { ScreenLayout } from "$/common/protocol.ts";
|
||||
import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
import type { RecipeBook } from "$/common/mod_loader.ts";
|
||||
import type { GameServer } from "./game_server.ts";
|
||||
import type { ServerPlayer } from "./player.ts";
|
||||
@@ -51,6 +52,36 @@ function hoe_into_hoed_dirt(
|
||||
BLOCK_BEHAVIORS["bworld:grass"] = { on_interact: hoe_into_hoed_dirt };
|
||||
BLOCK_BEHAVIORS["bworld:dirt"] = { on_interact: hoe_into_hoed_dirt };
|
||||
|
||||
// crops
|
||||
|
||||
// minecraft's bone meal: a crop grows 2 to 5 stages at once, and the bone meal is used up
|
||||
function grow_with_bone_meal(
|
||||
game: GameServer,
|
||||
block: { x: number; y: number; z: number; id: string },
|
||||
player: ServerPlayer,
|
||||
): boolean {
|
||||
const held = player.held_item;
|
||||
if (held?.type_id !== "bworld:bone_meal") {
|
||||
return false;
|
||||
}
|
||||
const info = EverythingRegistry.get<BlockRegistry>("blocks", block.id)!;
|
||||
const value = game.world.get_block_value(block.x, block.y, block.z);
|
||||
const age = get_state_value(value, info, "age")!;
|
||||
const max_age = 2 ** info.states!.find((s) => s.name === "age")!.bits - 1;
|
||||
// fully grown, keep the bone meal
|
||||
if (age >= max_age) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const new_age = Math.min(age + 1, max_age);
|
||||
game.set_block_state(block.x, block.y, block.z, set_state_value(value, info, "age", new_age)! >>> 16);
|
||||
const slot = player.inventory.get_slot(player.selected_slot);
|
||||
slot.amount = slot.amount! - 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
BLOCK_BEHAVIORS["bworld:wheat"] = { on_interact: grow_with_bone_meal };
|
||||
|
||||
// chest
|
||||
|
||||
const CHEST_LAYOUT: ScreenLayout = {
|
||||
|
||||
Reference in New Issue
Block a user