Merge pull request #2507 from kacpherek/feat/create-world-rules

🤖 Agent: Merged by Ludovic's AI agent (Claude, contrib-gate-19). Thanks @kacpherek: Create New World: edit the game rules before creating the world. I checked it against vanilla 26.3, and it passed every harness it reaches in a union with your other PRs, plus lan_e2e in both modes.
This commit is contained in:
Ludovic Marie
2026-10-27 09:36:39 +01:00
committed by GitHub
9 changed files with 140 additions and 42 deletions
+2 -2
View File
@@ -63,7 +63,6 @@ mod frames;
mod friends;
mod functions;
mod game_mode;
mod game_rules_screen;
mod gamemode_switcher;
mod ghasts;
mod golems;
@@ -12195,7 +12194,8 @@ impl ApplicationHandler for Game<'_> {
let (action, sound) = self.title.as_mut().map_or((Action::None, false), |t| {
let clicked = t.click(self.cursor.0, self.cursor.1, (b.width, b.height));
t.press_field(atlas, self.cursor, (b.width, b.height), shift);
clicked
let rules = t.press_rules(atlas, self.cursor, (b.width, b.height));
(clicked.0, clicked.1 || rules)
});
self.save_title_options();
if sound {
+2 -6
View File
@@ -5,14 +5,10 @@
//! forcing the game mode. Its difficulty lock asks first (PopupScreen), and
//! Restrictions opens RestrictionsScreen.
//! Parity reference: https://mcsrc.dev/2/26.3/net/minecraft/client/gui/screens/WorldOptionsScreen
use super::{
Game, Screen,
game_rules_screen::{Exit, RulesScreen},
integrated::ServerOptions,
lan::LanSettings,
};
use super::{Game, Screen, integrated::ServerOptions, lan::LanSettings};
use horizon_client::{
font::Font,
game_rules_screen::{Exit, RulesScreen},
hud::{HudBatch, rgba},
text::{Text, named_color},
ui::{self, EditBox},
@@ -546,6 +546,11 @@ impl Loading {
(batch.width, batch.height),
self.menu.modifiers.shift_key(),
);
title.press_rules(
&gui.atlas,
self.menu.cursor,
(batch.width, batch.height),
);
self.menu_action(event_loop, action);
}
}
@@ -94,6 +94,8 @@ impl WorldRequest {
level.difficulty = horizon_client::title::DIFFICULTIES[o.difficulty()].into();
level.allow_commands = Some(o.commands(creative));
horizon_client::save::write_world_gen_settings(&save_root, level.seed, o.world_gen())?;
// WorldCreationUiState.getGameRules, written with the level.
level.game_rules = o.rules.by_name();
}
// Worlds made without the Create New World screen (--world, the
// control socket) take its default: commands on for Creative only.
@@ -1,8 +1,9 @@
//! InWorldGameRulesScreen (AbstractGameRulesScreen), opened from World
//! Options: the rules by category under a search box, a switch or a number
//! box each, and Done sending the changed ones (ServerboundSetGameRulePacket).
//! Leaving with changes asks first (ConfirmScreen).
use horizon_client::{
//! AbstractGameRulesScreen: the rules by category under a search box, a
//! switch or a number box each. InWorldGameRulesScreen, opened from World
//! Options, sends the changed ones on Done (ServerboundSetGameRulePacket) and
//! asks before leaving with changes (ConfirmScreen); WorldCreationGameRulesScreen,
//! opened from Create New World, hands them back and leaves without asking.
use crate::{
confirm_screen::Confirm,
font::Font,
gamerules::{self, GameRules, RULES, Rule, Value},
@@ -32,14 +33,14 @@ enum Row {
/// What a click or Escape asks of World Options.
#[derive(Debug, PartialEq)]
pub(super) enum Exit {
pub enum Exit {
/// Done: the rules that changed.
Apply(Vec<(&'static str, Value)>),
/// Cancel, or discarding the changes.
Discard,
}
pub(super) struct RulesScreen {
pub struct RulesScreen {
search: EditBox,
values: GameRules,
initial: GameRules,
@@ -49,6 +50,8 @@ pub(super) struct RulesScreen {
scroll: ui::Scroll,
/// ConfirmScreen "Game Rule Changes" in its place.
confirm: bool,
/// InWorldGameRulesScreen rather than WorldCreationGameRulesScreen.
in_world: bool,
}
fn name_key(r: &Rule) -> String {
@@ -102,7 +105,15 @@ fn confirm(font: &Font, size: (f32, f32)) -> Confirm {
}
impl RulesScreen {
pub(super) fn new(rules: &GameRules) -> Self {
/// InWorldGameRulesScreen over the world's rules.
pub fn new(rules: &GameRules) -> Self {
Self::build(rules, true)
}
/// WorldCreationGameRulesScreen over the new world's rules.
pub fn for_creation(rules: &GameRules) -> Self {
Self::build(rules, false)
}
fn build(rules: &GameRules, in_world: bool) -> Self {
// setInitialFocus: the search box.
let search = EditBox::new(0.0, 0.0, SEARCH_WIDTH, 15.0);
let numbers = RULES
@@ -123,6 +134,7 @@ impl RulesScreen {
numbers,
scroll: ui::Scroll::default(),
confirm: false,
in_world,
}
}
@@ -197,9 +209,10 @@ impl RulesScreen {
})
.collect()
}
/// InWorldGameRulesScreen.onClose: pending changes ask first.
/// InWorldGameRulesScreen.onClose: pending changes ask first;
/// WorldCreationGameRulesScreen.onClose discards them.
fn close(&mut self) -> Option<Exit> {
if self.changes().is_empty() {
if !self.in_world || self.changes().is_empty() {
Some(Exit::Discard)
} else {
self.confirm = true;
@@ -207,7 +220,7 @@ impl RulesScreen {
}
}
/// Escape: ConfirmScreen answers no; the rules close.
pub(super) fn escape(&mut self) -> Option<Exit> {
pub fn escape(&mut self) -> Option<Exit> {
if self.confirm {
self.confirm = false;
None
@@ -216,7 +229,7 @@ impl RulesScreen {
}
}
/// The mouse wheel (AbstractSelectionList: half an entry a step).
pub(super) fn wheel(&mut self, delta: f32, h: f32) {
pub fn wheel(&mut self, delta: f32, h: f32) {
if !self.confirm {
let content = self.content();
self.scroll
@@ -224,20 +237,20 @@ impl RulesScreen {
}
}
/// A press on the scrollbar holds it (AbstractSelectionList.updateScrolling).
pub(super) fn press_scrollbar(&mut self, cursor: (f32, f32), (w, h): (f32, f32)) -> bool {
pub fn press_scrollbar(&mut self, cursor: (f32, f32), (w, h): (f32, f32)) -> bool {
let content = self.content();
self.scroll
.press(cursor, Self::bar(w), HEADER, h - FOOTER, content)
}
/// The pointer moved while the scrollbar is held.
pub(super) fn drag(&mut self, y: f32, h: f32) {
pub fn drag(&mut self, y: f32, h: f32) {
if self.scroll.held() {
let content = self.content();
self.scroll.drag(y, HEADER, h - FOOTER, content);
}
}
/// The button came up: the scrollbar lets go.
pub(super) fn release(&mut self) {
pub fn release(&mut self) {
self.scroll.release();
}
fn focused(&mut self) -> Option<(Option<&'static str>, &mut EditBox)> {
@@ -272,7 +285,7 @@ impl RulesScreen {
}
}
/// Run an edit on the focused box, then its responder.
pub(super) fn edit(&mut self, f: impl FnOnce(&mut EditBox)) {
pub fn edit(&mut self, f: impl FnOnce(&mut EditBox)) {
let Some((rule, b)) = self.focused() else {
return;
};
@@ -282,12 +295,12 @@ impl RulesScreen {
self.edited(rule);
}
}
pub(super) fn typing(&self) -> bool {
pub fn typing(&self) -> bool {
!self.confirm && (self.search.focused || self.numbers.iter().any(|(_, b)| b.focused))
}
/// A left click; Some when the screen closes.
pub(super) fn click(
pub fn click(
&mut self,
atlas: &HudAtlas,
cursor: (f32, f32),
@@ -355,12 +368,7 @@ impl RulesScreen {
}
}
/// Whether a click lands on a button (for its sound).
pub(super) fn clicks_button(
&self,
atlas: &HudAtlas,
cursor: (f32, f32),
size: (f32, f32),
) -> bool {
pub fn clicks_button(&self, atlas: &HudAtlas, cursor: (f32, f32), size: (f32, f32)) -> bool {
let (w, h) = size;
let on = |(x, y): (f32, f32)| ui::hovered(cursor, x, y, 150.0, 20.0);
if self.confirm {
@@ -377,9 +385,9 @@ impl RulesScreen {
})
}
pub(super) fn draw(&mut self, batch: &mut HudBatch, atlas: &HudAtlas, cursor: (f32, f32)) {
pub fn draw(&mut self, batch: &mut HudBatch, atlas: &HudAtlas, cursor: (f32, f32)) {
let (w, h) = (batch.width, batch.height);
ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true);
ui::menu_background(batch, atlas, 0.0, 0.0, w, h, self.in_world);
if self.confirm {
let labels = [
Text::translate("gui.yes", &[]),
@@ -393,7 +401,7 @@ impl RulesScreen {
self.scroll.clamp(HEADER, h - FOOTER, content);
// RuleList: the in-world list background, its entries clipped to it,
// then the separators and scrollbar.
ui::list_background(batch, atlas, HEADER, h - FOOTER, true);
ui::list_background(batch, atlas, HEADER, h - FOOTER, self.in_world);
let first = batch.vertices.len();
let hovered = self.hovered(rows.len(), cursor, (w, h));
let (left, x) = (row_left(w), control_x(w));
@@ -450,7 +458,7 @@ impl RulesScreen {
}
}
batch.clip_since(first, 0.0, HEADER, w, h - FOOTER);
ui::separators(batch, atlas, HEADER, h - FOOTER, true);
ui::separators(batch, atlas, HEADER, h - FOOTER, self.in_world);
ui::scrollbar(
batch,
atlas,
@@ -526,8 +534,8 @@ mod tests {
#[test]
fn box_colours_are_vanillas() {
assert_eq!(VALID, horizon_client::hud::rgba(224, 224, 224, 255));
assert_eq!(INVALID, horizon_client::hud::rgba(255, 0, 0, 255));
assert_eq!(VALID, crate::hud::rgba(224, 224, 224, 255));
assert_eq!(INVALID, crate::hud::rgba(255, 0, 0, 255));
}
#[test]
+1
View File
@@ -74,6 +74,7 @@ pub mod fog;
pub mod font;
pub mod friends_screen;
pub mod game_mode_switcher;
pub mod game_rules_screen;
pub mod gamepad;
pub mod gfx;
pub mod gizmos;
+79
View File
@@ -1,6 +1,7 @@
//! Title, world selection and world creation screens (TitleScreen,
//! SelectWorldScreen, CreateWorldScreen layouts), drawn as HUD quads.
use crate::{
game_rules_screen::{Exit, RulesScreen},
hud::{HudAtlas, HudBatch, rgba},
lang,
save::Level,
@@ -144,6 +145,8 @@ pub struct Title {
splash: Option<String>,
/// The saves listed for Home, re-read every couple of seconds.
recent: std::cell::RefCell<Option<(std::time::Instant, Vec<WorldEntry>)>>,
/// WorldCreationGameRulesScreen, over Create New World while it is open.
rules: RefCell<Option<RulesScreen>>,
}
pub(crate) struct Button {
x: f32,
@@ -252,6 +255,7 @@ impl Title {
(today.month(), today.day()),
),
recent: std::cell::RefCell::new(None),
rules: RefCell::new(None),
}
}
/// Vanilla bypasses the selector when there are no saves.
@@ -360,6 +364,9 @@ impl Title {
}
/// The pointer moved with the button held (`mouseDragged`).
pub fn drag(&mut self, cursor: (f32, f32), size: (f32, f32)) {
if let Some(rules) = self.rules.get_mut() {
return rules.drag(cursor.1, size.1.ceil());
}
if let Mode::Multiplayer(screen) = &mut self.mode {
return screen.drag(cursor.1, size);
}
@@ -372,6 +379,9 @@ impl Title {
}
/// The button was released (`onRelease`).
pub fn release(&mut self) {
if let Some(rules) = self.rules.get_mut() {
rules.release();
}
if let Mode::Multiplayer(screen) = &mut self.mode {
screen.release();
}
@@ -379,6 +389,9 @@ impl Title {
}
/// Mouse wheel over the classic world and server lists.
pub fn scroll_worlds(&mut self, delta: f32, cursor: (f32, f32), size: (f32, f32)) {
if let Some(rules) = self.rules.get_mut() {
return rules.wheel(delta, size.1.ceil());
}
if let Mode::Multiplayer(screen) = &mut self.mode {
screen.wheel(delta, cursor, size);
return;
@@ -566,6 +579,10 @@ impl Title {
folder
}
fn buttons(&self, w: f32, h: f32) -> Vec<Button> {
// The game rules draw and press their own.
if self.rules.borrow().is_some() {
return Vec::new();
}
let (w, h) = (w.ceil(), h.ceil());
let b = |x: f32, y: f32, w: f32, label: &str, enabled: bool, id: u8| Button {
x,
@@ -814,6 +831,9 @@ impl Title {
hide_splash: bool,
) {
let (w, h) = (batch.width.ceil(), batch.height.ceil());
if let Some(rules) = self.rules.borrow_mut().as_mut() {
return rules.draw(batch, atlas, cursor);
}
match &self.mode {
Mode::Main => {
// LogoRenderer: the logo's top 44 of 64 rows, then the edition
@@ -1207,6 +1227,10 @@ impl Title {
/// Navigation buttons can return `Action::None` while still sounding.
pub fn click(&mut self, x: f32, y: f32, size: (f32, f32)) -> (Action, bool) {
self.remember_direct_address();
// The game rules take the press in `press_rules`, which has the atlas.
if self.rules.get_mut().is_some() {
return (Action::None, false);
}
let (w, h) = (size.0.ceil(), size.1.ceil());
// MenuTabBar: a new tab clears the focus and clicks.
if let Mode::Create { focus, options, .. } = &mut self.mode
@@ -1387,6 +1411,11 @@ impl Title {
self.mode = *return_to.clone();
Action::None
}
// CreateWorldScreen.openGameRulesScreen over a copy of the rules.
(Mode::Create { options, .. }, Some(29)) => {
*self.rules.get_mut() = Some(RulesScreen::for_creation(&options.rules));
Action::None
}
(
Mode::Create {
creative, options, ..
@@ -1410,6 +1439,9 @@ impl Title {
}
/// The focused text field, its id and maximum length (copy and paste).
pub fn focused_field(&mut self) -> Option<(crate::clipboard::FieldId, &mut String, usize)> {
if self.rules.get_mut().is_some() {
return None;
}
match &mut self.mode {
Mode::Multiplayer(screen) => screen.focused_text(),
mode => caret_field(mode, self.search_focused, &mut self.search),
@@ -1417,10 +1449,18 @@ impl Title {
}
/// Whether the focused field edits at its cursor.
pub fn has_caret_field(&mut self) -> bool {
// The game rules' boxes edit themselves (`edit_field`).
if let Some(rules) = self.rules.get_mut() {
return rules.typing();
}
caret_field(&mut self.mode, self.search_focused, &mut self.search).is_some()
}
/// Edit the focused field through its `EditBox`; false when there is none.
fn edit_field(&mut self, f: impl FnOnce(&mut crate::ui::EditBox)) -> bool {
if let Some(rules) = self.rules.get_mut() {
rules.edit(f);
return rules.typing();
}
let Some((id, value, max)) =
caret_field(&mut self.mode, self.search_focused, &mut self.search)
else {
@@ -1479,6 +1519,9 @@ impl Title {
shift: bool,
) {
let (w, h) = (size.0.ceil(), size.1.ceil());
if self.rules.get_mut().is_some() {
return;
}
let Some(rect @ [fx, fy, fw, fh]) = self.focused_rect((w, h)) else {
return;
};
@@ -1491,6 +1534,9 @@ impl Title {
}
/// The pointer moved with the button held (`EditBox.onDrag`).
pub fn drag_field(&mut self, atlas: &HudAtlas, x: f32) {
if self.rules.get_mut().is_some() {
return;
}
if let Some(field) = caret_field(&mut self.mode, self.search_focused, &mut self.search) {
self.caret.get_mut().drag(atlas, field, x);
}
@@ -1527,6 +1573,9 @@ impl Title {
}
}
pub fn tab(&mut self) {
if self.rules.get_mut().is_some() {
return;
}
if let Mode::Edit { focused, .. } = &mut self.mode {
*focused = !*focused;
}
@@ -1576,7 +1625,37 @@ impl Title {
}
outcome
}
/// A left press on the game rules (they need the atlas for text): whether
/// it lands on a button, for its sound.
pub fn press_rules(&mut self, atlas: &HudAtlas, cursor: (f32, f32), size: (f32, f32)) -> bool {
let size = (size.0.ceil(), size.1.ceil());
let Some(rules) = self.rules.get_mut() else {
return false;
};
let sound = rules.clicks_button(atlas, cursor, size);
let exit = rules.click(atlas, cursor, size);
if let Some(exit) = exit {
self.close_rules(exit);
}
sound
}
/// WorldCreationGameRulesScreen's exit callback: Done keeps the edited
/// rules (`setGameRules`), Cancel drops them.
fn close_rules(&mut self, exit: Exit) {
*self.rules.get_mut() = None;
if let (Exit::Apply(changes), Mode::Create { options, .. }) = (exit, &mut self.mode) {
for (name, value) in changes {
options.rules.set(name, value);
}
}
}
pub fn escape(&mut self) {
if let Some(rules) = self.rules.get_mut() {
if let Some(exit) = rules.escape() {
self.close_rules(exit);
}
return;
}
self.remember_direct_address();
if let Mode::Multiplayer(screen) = &mut self.mode
&& !screen.escape(&self.saves)
@@ -34,6 +34,8 @@ pub struct WorldOptions {
pub flat: bool,
pub structures: bool,
pub bonus_chest: bool,
/// `getGameRules`: the Game Rules screen's values, saved with the world.
pub rules: crate::gamerules::GameRules,
}
impl Default for WorldOptions {
fn default() -> Self {
@@ -45,6 +47,7 @@ impl Default for WorldOptions {
flat: false,
structures: true,
bonus_chest: false,
rules: crate::gamerules::GameRules::default(),
}
}
}
@@ -66,6 +69,7 @@ impl WorldOptions {
flat: generation.flat,
structures: generation.structures,
bonus_chest: generation.bonus_chest,
rules: crate::gamerules::GameRules::load(dir),
..Self::default()
}
}
@@ -263,7 +267,7 @@ pub(super) fn buttons(w: f32, h: f32, creative: bool, o: &WorldOptions) -> Vec<B
28,
),
],
// Game rules, experiments and data packs have no screens yet.
// Experiments and data packs have no screens yet.
_ => [
"selectWorld.gameRules",
"selectWorld.experiments",
@@ -277,7 +281,7 @@ pub(super) fn buttons(w: f32, h: f32, creative: bool, o: &WorldOptions) -> Vec<B
gy + 28.0 * i as f32,
210.0,
tr(key),
false,
i == 0,
29 + i as u8,
)
})
+5 -2
View File
@@ -770,9 +770,12 @@ fn create_world_tabs_follow_vanilla_and_their_choices_reach_the_action() {
bonus_chest: true
}
);
// The More tab's screens don't exist yet.
// The More tab's Experiments and Data Packs screens don't exist yet.
f.title.click(54.0 + 248.0 + 10.0, 10.0, size);
assert!(labels(&f)[..3].iter().all(|b| !b.3));
assert_eq!(
labels(&f)[..3].iter().map(|b| b.3).collect::<Vec<_>>(),
[true, false, false]
);
}
#[test]