Merge feat/mod-recipe-pages into feat/mod-recipe-pages-game
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -557,6 +557,8 @@ interface browser {
|
||||
page(u32),
|
||||
/// The back button of `set-recipes`.
|
||||
back,
|
||||
/// The player picked the tab at this index of `set-recipes`' tabs.
|
||||
tab(u32),
|
||||
}
|
||||
/// How the game lays out a recipe of `set-recipes`.
|
||||
enum layout {
|
||||
@@ -590,9 +592,11 @@ interface browser {
|
||||
/// The panel's items, at most 8192 (an empty list hides the panel).
|
||||
set-items: func(items: list<stack>);
|
||||
/// Shows these recipes in the panel instead of its items, as many as
|
||||
/// fit a page, under `title` and a back button; an empty list shows the
|
||||
/// items again. At most 256 recipes.
|
||||
set-recipes: func(title: string, recipes: list<recipe-view>);
|
||||
/// fit a page, under `title` and a back button, with a row of `tabs`
|
||||
/// (each an icon; `tab` is the selected one) between them; an empty
|
||||
/// list of recipes shows the items again. At most 256 recipes and 16
|
||||
/// tabs.
|
||||
set-recipes: func(title: string, tabs: list<stack>, tab: u32, recipes: list<recipe-view>);
|
||||
/// What the player did on the panel since the last poll, at most 256
|
||||
/// kept.
|
||||
poll: func() -> list<event>;
|
||||
|
||||
@@ -323,15 +323,25 @@ struct BrowserState {
|
||||
/// across mods and reloads, so the game converts them once.
|
||||
version: u64,
|
||||
/// `set-recipes`' title and recipes, shown instead of the items.
|
||||
recipes: Option<(String, Vec<BrowserRecipe>)>,
|
||||
recipes: Option<RecipePage>,
|
||||
events: std::collections::VecDeque<BrowserEvent>,
|
||||
}
|
||||
/// What `browser.set-recipes` shows: a title, tabs (`tab` the selected
|
||||
/// one) and the recipes.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RecipePage {
|
||||
pub title: String,
|
||||
pub tabs: Vec<BrowserStack>,
|
||||
pub tab: u32,
|
||||
pub recipes: Vec<BrowserRecipe>,
|
||||
}
|
||||
static BROWSER_VERSION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
||||
/// Items a panel may hold, and events kept for `browser.poll`.
|
||||
const BROWSER_ITEMS: usize = 8192;
|
||||
/// Recipes `set-recipes` may show, slots in one, and stacks a slot cycles.
|
||||
const BROWSER_RECIPES: usize = 256;
|
||||
const RECIPE_SLOTS: usize = 9;
|
||||
const RECIPE_TABS: usize = 16;
|
||||
const SLOT_STACKS: usize = 64;
|
||||
const BROWSER_EVENTS: usize = 256;
|
||||
impl BrowserState {
|
||||
@@ -848,13 +858,11 @@ impl Mods {
|
||||
))
|
||||
})
|
||||
}
|
||||
/// The recipes mod `owner`'s panel shows instead of its items
|
||||
/// (`browser.set-recipes`): the title and the recipes.
|
||||
pub fn browser_recipes(&self, owner: usize) -> Option<(&str, &[BrowserRecipe])> {
|
||||
/// What mod `owner`'s panel shows instead of its items
|
||||
/// (`browser.set-recipes`).
|
||||
pub fn browser_recipes(&self, owner: usize) -> Option<&RecipePage> {
|
||||
let b = &self.entries.get(owner)?.run.as_ref()?.store.data().browser;
|
||||
b.recipes
|
||||
.as_ref()
|
||||
.map(|(title, list)| (title.as_str(), list.as_slice()))
|
||||
b.recipes.as_ref()
|
||||
}
|
||||
/// The player did `event` on mod `owner`'s panel.
|
||||
pub fn browser_event(&mut self, owner: usize, event: BrowserEvent) {
|
||||
@@ -1261,7 +1269,13 @@ impl browser::Host for State {
|
||||
.collect();
|
||||
self.browser.version = BROWSER_VERSION.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
fn set_recipes(&mut self, title: String, recipes: Vec<BrowserRecipe>) {
|
||||
fn set_recipes(
|
||||
&mut self,
|
||||
title: String,
|
||||
tabs: Vec<BrowserStack>,
|
||||
tab: u32,
|
||||
recipes: Vec<BrowserRecipe>,
|
||||
) {
|
||||
let stack = |s: BrowserStack| BrowserStack {
|
||||
item: clip(s.item),
|
||||
components: clip_to(s.components, 4096),
|
||||
@@ -1283,7 +1297,12 @@ impl browser::Host for State {
|
||||
..r
|
||||
})
|
||||
.collect();
|
||||
(clip(title), recipes)
|
||||
RecipePage {
|
||||
title: clip(title),
|
||||
tabs: tabs.into_iter().take(RECIPE_TABS).map(stack).collect(),
|
||||
tab,
|
||||
recipes,
|
||||
}
|
||||
});
|
||||
self.browser.version = BROWSER_VERSION.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
|
||||
@@ -1196,9 +1196,13 @@ fn browser_lists_items_and_hears_the_player() {
|
||||
said(&mut mods, "browser_recipes minecraft:oak_planks"),
|
||||
["recipes shown: 1"]
|
||||
);
|
||||
let (title, recipes) = mods.browser_recipes(owner).unwrap();
|
||||
assert_eq!(title, "Recipes for minecraft:oak_planks");
|
||||
let r = &recipes[0];
|
||||
let page = mods.browser_recipes(owner).unwrap();
|
||||
assert_eq!(page.title, "Recipes for minecraft:oak_planks");
|
||||
assert_eq!(
|
||||
(page.tabs[0].item.as_str(), page.tab),
|
||||
("minecraft:crafting_table", 0)
|
||||
);
|
||||
let r = &page.recipes[0];
|
||||
assert_eq!(
|
||||
(r.layout, r.width, r.height, r.action),
|
||||
(BrowserLayout::Grid, 1, 1, Some(false))
|
||||
|
||||
@@ -364,7 +364,25 @@ impl Mod for Guest {
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
browser::set_recipes(&format!("Recipes for {args}"), &views);
|
||||
// A tab per layout shown, the first selected.
|
||||
let mut tabs: Vec<browser::Stack> = Vec::new();
|
||||
for v in &views {
|
||||
let icon = match v.layout {
|
||||
browser::Layout::Grid => "minecraft:crafting_table",
|
||||
browser::Layout::Cooking => "minecraft:furnace",
|
||||
browser::Layout::Single => "minecraft:stonecutter",
|
||||
browser::Layout::Smithing => "minecraft:smithing_table",
|
||||
browser::Layout::Brewing => "minecraft:brewing_stand",
|
||||
};
|
||||
if !tabs.iter().any(|t| t.item == icon) {
|
||||
tabs.push(browser::Stack {
|
||||
item: icon.into(),
|
||||
count: 1,
|
||||
components: String::new(),
|
||||
});
|
||||
}
|
||||
}
|
||||
browser::set_recipes(&format!("Recipes for {args}"), &tabs, 0, &views);
|
||||
chat::show(&format!("recipes shown: {}", views.len()));
|
||||
}
|
||||
"browser_poll" => {
|
||||
@@ -389,6 +407,7 @@ impl Mod for Guest {
|
||||
browser::Event::Action(i) => format!("action {i}"),
|
||||
browser::Event::Page(first) => format!("page {first}"),
|
||||
browser::Event::Back => "back".into(),
|
||||
browser::Event::Tab(i) => format!("tab {i}"),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -587,6 +587,7 @@ A click on a slot of a recipe the panel shows (`set-recipes`).
|
||||
| `action` | `u32` | The action button of the recipe at this index in `set-recipes`'s list. |
|
||||
| `page` | `u32` | The player turned the recipes' page (`set-recipes`): the index of the first recipe now shown. |
|
||||
| `back` | | The back button of `set-recipes`. |
|
||||
| `tab` | `u32` | The player picked the tab at this index of `set-recipes`' tabs. |
|
||||
|
||||
### enum `layout`
|
||||
|
||||
@@ -617,7 +618,7 @@ A recipe to show. Each slot, and the result, cycles through its stacks, one a se
|
||||
| Function | Signature | What it does |
|
||||
| --- | --- | --- |
|
||||
| `set-items` | `func(items: list<stack>)` | The panel's items, at most 8192 (an empty list hides the panel). |
|
||||
| `set-recipes` | `func(title: string, recipes: list<recipe-view>)` | Shows these recipes in the panel instead of its items, as many as fit a page, under `title` and a back button; an empty list shows the items again. At most 256 recipes. |
|
||||
| `set-recipes` | `func(title: string, tabs: list<stack>, tab: u32, recipes: list<recipe-view>)` | Shows these recipes in the panel instead of its items, as many as fit a page, under `title` and a back button, with a row of `tabs` (each an icon; `tab` is the selected one) between them; an empty list of recipes shows the items again. At most 256 recipes and 16 tabs. |
|
||||
| `poll` | `func() -> list<event>` | What the player did on the panel since the last poll, at most 256 kept. |
|
||||
|
||||
## `players`
|
||||
|
||||
Reference in New Issue
Block a user