Compare commits
2
Commits
3c80938e91
...
fbef1d96d2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbef1d96d2 | ||
|
|
935c0df7af |
@@ -304,6 +304,10 @@ function client_context(listing: ModListing): ClientContext {
|
||||
const namespace = id.split(":")[0];
|
||||
return mod_names.get(namespace) ?? namespace;
|
||||
},
|
||||
components: (id) => (known_item(id),
|
||||
structuredClone(
|
||||
EverythingRegistry.get<ItemRegistry>("items", id)?.components ?? {},
|
||||
)),
|
||||
},
|
||||
recipes: {
|
||||
list: (type) =>
|
||||
|
||||
@@ -38,6 +38,9 @@ export interface ClientContext {
|
||||
name(id: Id): string;
|
||||
// the mod that registered it
|
||||
mod(id: Id): string;
|
||||
// the components its json lists and their params, a copy, like the server's ctx.items.components. for showing
|
||||
// what a component means, like when a fish can be caught
|
||||
components(id: Id): Record<Id, unknown>;
|
||||
};
|
||||
// every recipe as its json (see docs/modding/resources/recipes.md), including mods' own types, in load order. copies
|
||||
recipes: {
|
||||
|
||||
@@ -150,6 +150,7 @@ ctx.items.ids(); // every item, in load order
|
||||
ctx.items.exists(id);
|
||||
ctx.items.name(id); // "Cooked Rice"
|
||||
ctx.items.mod(id); // the name of the mod that added it
|
||||
ctx.items.components(id); // its components and their params, like the server's, to show what they mean
|
||||
ctx.recipes.list(type?); // every recipe as its JSON, including mods' own types
|
||||
ctx.options.key_inventory; // the key the player has bound, like Minecraft's Options key mappings
|
||||
```
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"block": {
|
||||
"id": "bworld:worm_bin",
|
||||
"name": "Worm Bin",
|
||||
"textures": {
|
||||
"front": "bworld:worm_bin_front",
|
||||
"side": "bworld:worm_bin_side",
|
||||
"top": "bworld:worm_bin_top"
|
||||
},
|
||||
"states": [
|
||||
{
|
||||
"name": "facing",
|
||||
"bits": 2,
|
||||
"default": 0
|
||||
}
|
||||
],
|
||||
"variants": {
|
||||
"facing=1": { "y": 90 },
|
||||
"facing=2": { "y": 180 },
|
||||
"facing=3": { "y": 270 }
|
||||
},
|
||||
"mining": {
|
||||
"toughness": 2
|
||||
},
|
||||
"drops": "bworld:worm_bin",
|
||||
"interactive": true,
|
||||
"components": {
|
||||
"bworld:worm_bin": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "shaped",
|
||||
"pattern": [
|
||||
"CWC",
|
||||
"CDC",
|
||||
"CCC"
|
||||
],
|
||||
"key": {
|
||||
"C": "#c:ingots/copper",
|
||||
"D": "bworld:dirt",
|
||||
"W": "bworld:worms"
|
||||
},
|
||||
"result": {
|
||||
"id": "bworld:worm_bin",
|
||||
"count": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ export function setup(ctx: ClientContext) {
|
||||
ctx.ui.register_menu_screen("bworld:storage", chest_screen);
|
||||
ctx.ui.register_menu_screen("bworld:furnace", furnace_screen);
|
||||
ctx.ui.register_menu_screen("bworld:crabpot", (menu) => container_screen(menu, HEIGHT));
|
||||
ctx.ui.register_menu_screen("bworld:worm_bin", (menu) => container_screen(menu, HEIGHT));
|
||||
ctx.ui.register_menu_screen("bworld:smithing_table", smithing_screen);
|
||||
ctx.ui.register_menu_screen("bworld:fishing_minigame", fishing_minigame_screen);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ export function setup(ctx: ServerContext) {
|
||||
register_shipping_bin(ctx, storage_menu);
|
||||
register_furnace(ctx);
|
||||
register_crabpot(ctx);
|
||||
register_worm_bin(ctx);
|
||||
register_smithing_table(ctx);
|
||||
register_farming(ctx);
|
||||
register_fishing(ctx);
|
||||
@@ -328,6 +329,40 @@ function register_crabpot(ctx: ServerContext) {
|
||||
});
|
||||
}
|
||||
|
||||
// the worm bin makes worms every morning, for the crab pot's bait, into its one slot, which players can only take from.
|
||||
// they pile up to a full stack
|
||||
const WORMS_A_DAY = 5;
|
||||
|
||||
function register_worm_bin(ctx: ServerContext) {
|
||||
const menu_type = ctx.menus.register<BlockRef>("bworld:worm_bin", (menu, block) => {
|
||||
menu.title = "Worm Bin";
|
||||
menu.add_slot(block_container(ctx, block, 1), 0, { x: 79, y: 34, output_only: true });
|
||||
menu.add_player_inventory(7, INVENTORY_Y);
|
||||
menu.still_valid_near(block);
|
||||
});
|
||||
|
||||
ctx.components.register_block("bworld:worm_bin", {
|
||||
// its container is its tile data, so its mornings come anywhere in the world, like a crop's
|
||||
on_create(block) {
|
||||
block_container(ctx, block, 1);
|
||||
},
|
||||
on_interact(block, _params, player) {
|
||||
menu_type.open(player, block);
|
||||
return true;
|
||||
},
|
||||
on_break(block) {
|
||||
drop_contents(ctx, block);
|
||||
},
|
||||
on_new_day(block) {
|
||||
const container = block_container(ctx, block, 1);
|
||||
const worms = container.get(0);
|
||||
const max = ctx.items.max_stack(CRABPOT_WORMS);
|
||||
if (!worms) container.set(0, { id: CRABPOT_WORMS, count: WORMS_A_DAY });
|
||||
else if (worms.id === CRABPOT_WORMS) worms.count = Math.min(max, worms.count + WORMS_A_DAY);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function register_smithing_table(ctx: ServerContext) {
|
||||
const menu_type = ctx.menus.register<BlockRef>("bworld:smithing_table", (menu, block, player) => {
|
||||
const container = ctx.containers.create(4);
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 300 B |
Binary file not shown.
|
After Width: | Height: | Size: 268 B |
Binary file not shown.
|
After Width: | Height: | Size: 347 B |
@@ -11,7 +11,9 @@ The recipes open in a screen of their own, laid out like JEI's: a tab for each c
|
||||
made down its left side, the category and the page between their arrows, then the recipes. Clicking items in it follows
|
||||
them, Backspace goes back, and Escape or the inventory key (E) returns to the inventory.
|
||||
|
||||
It reads every mod's recipes with `ctx.recipes.list()`.
|
||||
It reads every mod's recipes with `ctx.recipes.list()`. Fish, the items with the base game's `bworld:fish` component,
|
||||
get a Fishing category too: clicking one shows its seasons, biomes, hours and the liquid it's caught in, read with
|
||||
`ctx.items.components`, and the fishing rod's uses are every fish.
|
||||
|
||||
- `scripts/categories.ts` has a category for each recipe type, like JEI's `IRecipeCategory`: its title, size, where it's
|
||||
made (`MOD_CATALYSTS` for mods' types) and where its items go. Types without one are drawn from their JSON: the items
|
||||
|
||||
@@ -98,9 +98,65 @@ function base_categories(tags: TagApi): RecipeCategory[] {
|
||||
layout.slot({ ids: [recipe.result], count: 1 }, 90, 0);
|
||||
},
|
||||
},
|
||||
{
|
||||
// when and where a fish bites, see fishing_recipes
|
||||
type: FISHING,
|
||||
title: "Fishing",
|
||||
width: 180,
|
||||
height: 54,
|
||||
catalysts: ["bworld:fishing_rod"],
|
||||
draw(recipe, layout) {
|
||||
const fish = recipe.fish as FishParams;
|
||||
layout.slot({ ids: [recipe.result], count: 1 }, 0, 0);
|
||||
const lines = [
|
||||
// all four is "all", which also fits
|
||||
`Seasons: ${
|
||||
fish.seasons && fish.seasons.length < 4 ? fish.seasons.map(title_case).join(", ") : "all"
|
||||
}`,
|
||||
`Biomes: ${fish.biomes?.map(biome_name).join(", ") ?? "all"}`,
|
||||
`Time: ${fish.time ? `${clock(fish.time.from)} to ${clock(fish.time.to)}` : "all day"}`,
|
||||
`In: ${biome_name(fish.liquid ?? "bworld:water")}`,
|
||||
];
|
||||
lines.forEach((line, i) => layout.text(line, 24, 1 + i * 13));
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
// the base game's fish, the items with bworld:fish, as recipes of their own type that make them, so clicking one shows
|
||||
// when and where it bites. their params are under "fish", which reading a recipe's inputs skips
|
||||
const FISHING = "bworld:fishing";
|
||||
|
||||
interface FishParams {
|
||||
seasons?: string[];
|
||||
biomes?: string[];
|
||||
time?: { from: number; to: number };
|
||||
liquid?: string;
|
||||
}
|
||||
|
||||
export function fishing_recipes(ctx: ClientContext): Recipe[] {
|
||||
return ctx.items.ids().flatMap((id) => {
|
||||
const fish = ctx.items.components(id)["bworld:fish"];
|
||||
return fish ? [{ type: FISHING, result: id, fish }] : [];
|
||||
});
|
||||
}
|
||||
|
||||
// "autumn" is "Autumn", "dark_oak" is "Dark Oak"
|
||||
function title_case(name: string): string {
|
||||
return name.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
|
||||
}
|
||||
|
||||
// a biome, liquid or #biome tag by its name: "bworld:swamp" is "Swamp", "#bworld:is_ocean" is "Ocean"
|
||||
function biome_name(id: string): string {
|
||||
return title_case(id.replace(/^#/, "").split(":")[1].replace(/^is_/, ""));
|
||||
}
|
||||
|
||||
// hours on a 24 hour clock, fractions as minutes: 6.5 is "6:30"
|
||||
function clock(hours: number): string {
|
||||
const minutes = Math.round((hours % 1) * 60);
|
||||
return `${Math.floor(hours)}:${String(minutes).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
// a mod's own type, from its recipes' json: the items in its lists in a grid, a single item (like a bowl to serve in)
|
||||
// under the arrow, then what it makes, one under the other
|
||||
function mod_category(type: string, tags: TagApi): RecipeCategory {
|
||||
@@ -144,6 +200,7 @@ export function category_name(type: string): string {
|
||||
furnace: "Smelting",
|
||||
fuel: "Fuel",
|
||||
smithing: "Smithing",
|
||||
[FISHING]: "Fishing",
|
||||
};
|
||||
if (base[type]) return base[type];
|
||||
const name = type.split(":")[1] ?? type;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// many recipes as fit, one under the other. the item list stays on the right. escape or the inventory key go back to
|
||||
// the inventory, and backspace to what was shown before
|
||||
import type { ClientContext, Graphics, Screen } from "bworld/client";
|
||||
import { make_categories, type RecipeCategory, type RecipeLayout, SLOT } from "./categories.ts";
|
||||
import { fishing_recipes, make_categories, type RecipeCategory, type RecipeLayout, SLOT } from "./categories.ts";
|
||||
import { BUTTON, item_cell, ItemList, LABEL_COLOR, type Mode, SLOT_COLOR } from "./item_list.ts";
|
||||
import { type Ingredient, inputs, outputs, type Recipe } from "./recipes.ts";
|
||||
|
||||
@@ -32,7 +32,7 @@ export class RecipeIndex {
|
||||
|
||||
get categories(): RecipeCategory[] {
|
||||
if (!this.#categories) {
|
||||
this.#recipes = this.ctx.recipes.list();
|
||||
this.#recipes = [...this.ctx.recipes.list(), ...fishing_recipes(this.ctx)];
|
||||
this.#categories = make_categories(this.ctx, this.#recipes);
|
||||
}
|
||||
return this.#categories;
|
||||
|
||||
@@ -4,7 +4,7 @@ import { category_name } from "../mods/recipe_viewer/scripts/categories.ts";
|
||||
import { inputs as recipe_inputs, outputs } from "../mods/recipe_viewer/scripts/recipes.ts";
|
||||
import { RecipeIndex } from "../mods/recipe_viewer/scripts/recipes_screen.ts";
|
||||
import { tag_api } from "$/common/tags.ts";
|
||||
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ClientContext } from "bworld/client";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
@@ -12,6 +12,15 @@ const ids = (ingredients: { ids: string[] }[]) => ingredients.map((i) => i.ids.j
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const inputs = (recipe: any) => recipe_inputs(recipe, tag_api());
|
||||
|
||||
// what the recipe viewer reads of ctx.items, from the registry like the client's
|
||||
function client_items() {
|
||||
return {
|
||||
exists: (id: string) => EverythingRegistry.get("items", id) !== undefined,
|
||||
ids: () => EverythingRegistry.entries("items").map(([id]) => id),
|
||||
components: (id: string) => EverythingRegistry.get<ItemRegistry>("items", id)?.components ?? {},
|
||||
};
|
||||
}
|
||||
|
||||
Deno.test("the recipe viewer finds what makes and uses an item, in any recipe type", async () => {
|
||||
const { game } = await test_game("mods");
|
||||
const all = game.recipes.all;
|
||||
@@ -52,7 +61,7 @@ Deno.test("recipes are found by category in jei's order, and a catalyst's uses a
|
||||
const ctx = {
|
||||
recipes: { list: () => game.recipes.all },
|
||||
tags: tag_api(),
|
||||
items: { exists: (id: string) => EverythingRegistry.get("items", id) !== undefined },
|
||||
items: client_items(),
|
||||
} as unknown as ClientContext;
|
||||
const index = new RecipeIndex(ctx);
|
||||
|
||||
@@ -69,3 +78,35 @@ Deno.test("recipes are found by category in jei's order, and a catalyst's uses a
|
||||
assertEquals(furnace_uses.find(([type]) => type === "fuel")?.[1], all.filter((r) => r.type === "fuel"));
|
||||
assertEquals(index.find("bworld:air", "recipes"), []);
|
||||
});
|
||||
|
||||
Deno.test("clicking a fish shows when and where it bites, and the fishing rod's uses are every fish", async () => {
|
||||
await test_game("mods");
|
||||
const ctx = {
|
||||
recipes: { list: () => [] },
|
||||
tags: tag_api(),
|
||||
items: client_items(),
|
||||
} as unknown as ClientContext;
|
||||
const index = new RecipeIndex(ctx);
|
||||
const [fishing] = index.find("bworld:red_mullet", "recipes");
|
||||
assertEquals(fishing.category.title, "Fishing");
|
||||
assertEquals(fishing.category.catalysts, ["bworld:fishing_rod"]);
|
||||
const lines: string[] = [];
|
||||
const slots: string[] = [];
|
||||
fishing.category.draw(fishing.recipes[0], {
|
||||
slot: (ingredient) => slots.push(ingredient!.ids[0]),
|
||||
texture: () => {},
|
||||
text: (text) => lines.push(text),
|
||||
});
|
||||
assertEquals(slots, ["bworld:red_mullet"]);
|
||||
assertEquals(lines, ["Seasons: Summer, Winter", "Biomes: Ocean", "Time: 6:00 to 19:00", "In: Water"]);
|
||||
|
||||
// a lava fish without limits, and every fish under the rod
|
||||
const lava_lines: string[] = [];
|
||||
const [lava] = index.find("bworld:prismfish", "recipes");
|
||||
lava.category.draw(lava.recipes[0], { slot: () => {}, texture: () => {}, text: (text) => lava_lines.push(text) });
|
||||
assertEquals(lava_lines, ["Seasons: all", "Biomes: all", "Time: all day", "In: Lava"]);
|
||||
const fish = index.find("bworld:fishing_rod", "uses")[0].recipes.map((recipe) => recipe.result);
|
||||
assert(fish.includes("bworld:cod") && fish.includes("bworld:prismfish"));
|
||||
// a fish isn't a use of the water it lives in
|
||||
assertEquals(index.find("bworld:water", "uses"), []);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
// the worm bin: 5 worms every morning into its one slot, for the crab pot's bait
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { CYCLE_TICKS } from "$/common/time.ts";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
import { opened_menu, test_game } from "./helpers.ts";
|
||||
|
||||
async function setup() {
|
||||
const t = await test_game("mods");
|
||||
t.join(1, "alice");
|
||||
t.game.set_block(0, 99, 0, "bworld:stone");
|
||||
t.game.set_block(0, 100, 0, "bworld:worm_bin");
|
||||
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
|
||||
const data = t.game.world.get_tile(0, 100, 0)!.mod_data as { container: string };
|
||||
const container = t.game.containers.get(data.container)!;
|
||||
const next_morning = () => {
|
||||
t.game.set_time((t.game.world.day + 1) * CYCLE_TICKS - 1);
|
||||
t.game.tick();
|
||||
};
|
||||
return { t, container, next_morning };
|
||||
}
|
||||
|
||||
Deno.test("right clicking a worm bin opens its one output slot", async () => {
|
||||
const { t } = await setup();
|
||||
t.take(1);
|
||||
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] });
|
||||
const opened = opened_menu(t.take(1));
|
||||
assertEquals([opened.menu_type, opened.title], ["bworld:worm_bin", "Worm Bin"]);
|
||||
assertEquals(opened.slots.length, 1 + 36);
|
||||
assertEquals(opened.slots[0], { x: 79, y: 34, output: true });
|
||||
});
|
||||
|
||||
Deno.test("a worm bin makes 5 worms every morning, up to a full stack", async () => {
|
||||
const { container, next_morning } = await setup();
|
||||
assertEquals(container.get_item(0), undefined);
|
||||
next_morning();
|
||||
assertEquals(container.get_item(0)?.to_data(), { id: "bworld:worms", count: 5 });
|
||||
next_morning();
|
||||
assertEquals(container.get_item(0)?.amount, 10);
|
||||
container.set_item(0, new ItemStack("bworld:worms", 62));
|
||||
next_morning();
|
||||
assertEquals(container.get_item(0)?.amount, 64);
|
||||
});
|
||||
|
||||
Deno.test("breaking a worm bin drops it and its worms", async () => {
|
||||
const { t, next_morning } = await setup();
|
||||
next_morning();
|
||||
t.send(1, { type: "break_block", x: 0, y: 100, z: 0 });
|
||||
const dropped = t.game.item_entities().map((e) => `${e.item.type_id} ${e.item.amount}`).sort();
|
||||
assertEquals(dropped, ["bworld:worm_bin 1", "bworld:worms 5"]);
|
||||
assert(t.game.world.get_block_id(0, 100, 0) === "bworld:air");
|
||||
});
|
||||
Reference in New Issue
Block a user