From ef6f41b12c34e5cec584360ac3ccb8283664b930 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 9 Oct 2026 12:42:27 +0200 Subject: [PATCH] Add scoreboards and teams Scoreboards now work like vanilla: objectives with every criteria kind (dummy, trigger, health, food, xp, level, armor, deathCount, kill counts, per-colour team kills and any statistic such as minecraft.mined:minecraft.stone), the full /scoreboard command (objectives add, remove, list, modify and setdisplay; players set, add, remove, reset, get, list, enable, operation and display name or number format), /trigger, and /team with colour, prefix, suffix, friendly fire, invisibility, nametag, death message and collision options. Scores, teams and display slots are saved as vanilla NBT in data/minecraft/scoreboard.dat. The sidebar draws with the objective title, team colour slots, number formats and the vanilla layout, and target selectors accept scores={...}, team= and tag=. Co-Authored-By: Claude Sonnet 5.5 --- crates/horizon-client/src/command/args.rs | 272 +++- crates/horizon-client/src/command/mod.rs | 1 + .../src/command/tree_scoreboard.rs | 214 ++++ crates/horizon-client/src/command/vanilla.rs | 1 + crates/horizon-client/src/game.rs | 8 + crates/horizon-client/src/game/commands.rs | 21 +- crates/horizon-client/src/game/creatures.rs | 2 + crates/horizon-client/src/game/persist.rs | 6 +- crates/horizon-client/src/game/scoreboard.rs | 510 ++++++++ crates/horizon-client/src/game/stats.rs | 7 + crates/horizon-client/src/lib.rs | 2 + crates/horizon-client/src/save.rs | 4 +- crates/horizon-client/src/scoreboard.rs | 1115 ++++++++++++++++ crates/horizon-client/src/scoreboard_cmd.rs | 1124 +++++++++++++++++ .../src/scoreboard_cmd_tests.rs | 295 +++++ 15 files changed, 3568 insertions(+), 14 deletions(-) create mode 100644 crates/horizon-client/src/command/tree_scoreboard.rs create mode 100644 crates/horizon-client/src/game/scoreboard.rs create mode 100644 crates/horizon-client/src/scoreboard.rs create mode 100644 crates/horizon-client/src/scoreboard_cmd.rs create mode 100644 crates/horizon-client/src/scoreboard_cmd_tests.rs diff --git a/crates/horizon-client/src/command/args.rs b/crates/horizon-client/src/command/args.rs index bfe79244d..a98c30816 100644 --- a/crates/horizon-client/src/command/args.rs +++ b/crates/horizon-client/src/command/args.rs @@ -29,6 +29,13 @@ pub struct Registries { /// Game rule name -> integer-valued. pub game_rules: Vec<(String, bool)>, pub players: Vec, + /// Scoreboard objective names, team names and tracked score holders. + pub objectives: Vec, + pub teams: Vec, + pub tracked: Vec, + /// Loaded datapack functions (`ns:path`) and function tags (`ns:path`). + pub functions: Vec, + pub function_tags: Vec, /// The block the source is looking at (relevant coordinates). pub target_block: Option<[i32; 3]>, } @@ -116,6 +123,24 @@ pub enum ArgType { Component, /// CompoundTagArgument (kept as SNBT text). Nbt, + /// ScoreHolderArgument. + ScoreHolder { + multiple: bool, + }, + /// ObjectiveArgument (checked when the command runs). + Objective, + /// TeamArgument (checked when the command runs). + Team, + /// ObjectiveCriteriaArgument. + Criteria, + /// ScoreboardSlotArgument. + ScoreboardSlot, + /// OperationArgument. + Operation, + /// TeamColorArgument. + TeamColor, + /// RangeArgument.Ints (`1`, `1..`, `..5`, `1..5`). + IntRange, } pub fn integer(min: i32) -> ArgType { ArgType::Integer { min, max: i32::MAX } @@ -227,6 +252,29 @@ pub struct Selector { pub y_rotation: Option, pub pos: [Option; 3], pub delta: [Option; 3], + /// `scores={objective=range,...}`. + pub scores: Vec<(String, IntRange)>, + /// `team=` (an empty name matches entities without a team). + pub teams: Vec<(String, bool)>, + /// `nbt=` compounds (SNBT text). + pub nbt: Vec<(String, bool)>, + /// `advancements={id=true|{criterion=true}}`. + pub advancements: Vec<(String, AdvCondition)>, + /// `predicate=` ids. + pub predicates: Vec<(String, bool)>, +} +/// An `advancements=` entry: all done or not, or per-criterion states. +#[derive(Clone, Debug, PartialEq)] +pub enum AdvCondition { + Done(bool), + Criteria(Vec<(String, bool)>), +} +/// A `ScoreHolderArgument` value. +#[derive(Clone, Debug, PartialEq)] +pub enum ScoreHolderArg { + Wildcard, + Name(String), + Selector(Selector), } impl Selector { pub fn is_self(&self) -> bool { @@ -293,6 +341,8 @@ pub enum Arg { GameMode(GameMode), /// `eyes` (true) or `feet`. Anchor(bool), + ScoreHolder(ScoreHolderArg), + IntRange(IntRange), } fn is_id_char(c: char) -> bool { @@ -563,6 +613,7 @@ fn suggest_coordinates(builder: &mut SuggestionsBuilder, reg: &Registries) { } } +pub const OPERATIONS: [&str; 9] = ["=", "+=", "-=", "*=", "/=", "%=", "<", ">", "><"]; const SELECTORS: [(&str, &str); 6] = [ ("@a", "argument.entity.selector.allPlayers"), ("@e", "argument.entity.selector.allEntities"), @@ -571,7 +622,7 @@ const SELECTORS: [(&str, &str); 6] = [ ("@r", "argument.entity.selector.randomPlayer"), ("@s", "argument.entity.selector.self"), ]; -const OPTIONS: [&str; 16] = [ +const OPTIONS: [&str; 21] = [ "name", "distance", "level", @@ -588,7 +639,22 @@ const OPTIONS: [&str; 16] = [ "gamemode", "tag", "type", + "scores", + "team", + "nbt", + "advancements", + "predicate", ]; +/// StringReader.expect. +fn expect_symbol(reader: &mut Reader, c: char) -> CmdResult<()> { + reader.skip_whitespace(); + if reader.can_read() && reader.peek() == c { + reader.skip(); + Ok(()) + } else { + Err(CommandError::tr_at("parsing.expected", &[&c], reader)) + } +} fn parse_selector(reader: &mut Reader, reg: &Registries) -> CmdResult { let start = reader.cursor; if !(reader.can_read() && reader.peek() == '@') { @@ -791,6 +857,81 @@ fn parse_selector(reader: &mut Reader, reg: &Registries) -> CmdResult let v = reader.read_unquoted_string().to_owned(); s.tags.push((v, neg)); } + "team" => { + let neg = negated(reader); + let v = reader.read_unquoted_string().to_owned(); + s.teams.push((v, neg)); + } + "nbt" => { + let neg = negated(reader); + if !reader.can_read() || reader.peek() != '{' { + return Err(CommandError::tr_at( + "argument.nbt.expected.compound", + &[], + reader, + )); + } + s.nbt.push((read_braced(reader, '{', '}')?, neg)); + } + "predicate" => { + let neg = negated(reader); + s.predicates.push((read_id(reader)?, neg)); + } + "scores" => { + expect_symbol(reader, '{')?; + reader.skip_whitespace(); + while reader.can_read() && reader.peek() != '}' { + reader.skip_whitespace(); + let name = reader.read_unquoted_string().to_owned(); + expect_symbol(reader, '=')?; + let range = range_int(reader)?; + s.scores.push((name, range)); + reader.skip_whitespace(); + if reader.can_read() && reader.peek() == ',' { + reader.skip(); + } + reader.skip_whitespace(); + } + expect_symbol(reader, '}')?; + } + "advancements" => { + expect_symbol(reader, '{')?; + reader.skip_whitespace(); + while reader.can_read() && reader.peek() != '}' { + reader.skip_whitespace(); + let id = read_id(reader)?; + expect_symbol(reader, '=')?; + reader.skip_whitespace(); + if reader.can_read() && reader.peek() == '{' { + reader.skip(); + reader.skip_whitespace(); + let mut criteria = Vec::new(); + while reader.can_read() && reader.peek() != '}' { + reader.skip_whitespace(); + let c = reader.read_unquoted_string().to_owned(); + expect_symbol(reader, '=')?; + criteria.push((c, reader.read_boolean()?)); + reader.skip_whitespace(); + if reader.can_read() && reader.peek() == ',' { + reader.skip(); + } + reader.skip_whitespace(); + } + expect_symbol(reader, '}')?; + s.advancements.push((id, AdvCondition::Criteria(criteria))); + } else { + s.advancements + .push((id, AdvCondition::Done(reader.read_boolean()?))); + } + reader.skip_whitespace(); + if reader.can_read() && reader.peek() == ',' { + reader.skip(); + } + reader.skip_whitespace(); + } + expect_symbol(reader, '}')?; + s.include_entities = false; + } "type" => { let neg = negated(reader); let at = reader.cursor; @@ -1436,6 +1577,91 @@ impl ArgType { } Arg::Str(axes) } + ArgType::ScoreHolder { multiple } => { + if reader.can_read() && reader.peek() == '@' { + let sel = parse_selector(reader, reg)?; + if !*multiple && sel.limit > 1 { + reader.cursor = start; + return Err(CommandError::tr_at("argument.entity.toomany", &[], reader)); + } + Arg::ScoreHolder(ScoreHolderArg::Selector(sel)) + } else { + while reader.can_read() && reader.peek() != ' ' { + reader.skip(); + } + let text = &reader.string[start..reader.cursor]; + if text == "*" { + Arg::ScoreHolder(ScoreHolderArg::Wildcard) + } else { + Arg::ScoreHolder(ScoreHolderArg::Name(text.to_owned())) + } + } + } + ArgType::Objective | ArgType::Team => { + Arg::Str(reader.read_unquoted_string().to_owned()) + } + ArgType::Criteria => { + while reader.can_read() && reader.peek() != ' ' { + reader.skip(); + } + let id = &reader.string[start..reader.cursor]; + if !crate::scoreboard::criteria_exists(id, reg) { + reader.cursor = start; + return Err(CommandError::tr_at( + "argument.criteria.invalid", + &[&id], + reader, + )); + } + Arg::Str(id.to_owned()) + } + ArgType::ScoreboardSlot => { + let name = reader.read_unquoted_string(); + if !crate::scoreboard::display_slot_valid(name) { + reader.cursor = start; + return Err(CommandError::tr_at( + "argument.scoreboardDisplaySlot.invalid", + &[&name], + reader, + )); + } + Arg::Str(name.to_owned()) + } + ArgType::Operation => { + if !reader.can_read() { + return Err(CommandError::tr_at( + "arguments.operation.invalid", + &[], + reader, + )); + } + while reader.can_read() && reader.peek() != ' ' { + reader.skip(); + } + let op = &reader.string[start..reader.cursor]; + if !OPERATIONS.contains(&op) { + reader.cursor = start; + return Err(CommandError::tr_at( + "arguments.operation.invalid", + &[], + reader, + )); + } + Arg::Str(op.to_owned()) + } + ArgType::TeamColor => { + let name = reader.read_unquoted_string(); + if crate::scoreboard::team_color_rgb(name).is_none() { + reader.cursor = start; + return Err(CommandError::tr_at( + "argument.color.invalid", + &[&name], + reader, + )); + } + Arg::Str(name.to_owned()) + } + ArgType::IntRange => Arg::IntRange(range_int(reader)?), ArgType::Anchor => { let name = reader.read_unquoted_string(); match name { @@ -1511,6 +1737,50 @@ impl ArgType { } } ArgType::GameMode => builder.suggest_matching(GameMode::ALL.map(GameMode::name)), + ArgType::ScoreHolder { .. } => { + suggest_selector(builder, reg); + let names: Vec = reg + .players + .iter() + .chain(reg.tracked.iter()) + .cloned() + .collect(); + builder.suggest_matching(names.iter().map(String::as_str)); + } + ArgType::Objective => { + builder.suggest_matching(reg.objectives.iter().map(String::as_str)) + } + ArgType::Team => builder.suggest_matching(reg.teams.iter().map(String::as_str)), + ArgType::Criteria => { + let mut ids: Vec = crate::scoreboard::CUSTOM_CRITERIA + .iter() + .map(|s| (*s).to_owned()) + .collect(); + for c in crate::scoreboard::team_colors() { + ids.push(format!("teamkill.{c}")); + ids.push(format!("killedByTeam.{c}")); + } + let stat = |ty: &str, key: &str| crate::scoreboard::stat_criteria(ty, key); + ids.extend(reg.blocks.keys().map(|b| stat("mined", b))); + for ty in ["crafted", "used", "broken", "picked_up", "dropped"] { + ids.extend(reg.items.iter().map(|i| stat(ty, i))); + } + for ty in ["killed", "killed_by"] { + ids.extend(reg.entity_types.iter().map(|e| stat(ty, e))); + } + ids.extend( + crate::scoreboard::CUSTOM_STATS + .iter() + .map(|c| stat("custom", &format!("minecraft:{c}"))), + ); + builder.suggest_matching(ids.iter().map(String::as_str)); + } + ArgType::ScoreboardSlot => { + let slots = crate::scoreboard::display_slots(); + builder.suggest_matching(slots.iter().map(String::as_str)); + } + ArgType::Operation => builder.suggest_matching(OPERATIONS), + ArgType::TeamColor => builder.suggest_matching(crate::scoreboard::team_colors()), ArgType::Anchor => builder.suggest_matching(["eyes", "feet"]), ArgType::Swizzle | ArgType::Component => {} ArgType::Message => { diff --git a/crates/horizon-client/src/command/mod.rs b/crates/horizon-client/src/command/mod.rs index 7e8090d6b..d1ca22799 100644 --- a/crates/horizon-client/src/command/mod.rs +++ b/crates/horizon-client/src/command/mod.rs @@ -4,6 +4,7 @@ //! usage text and the vanilla error messages. Argument types live in //! [`args`]; the vanilla command tree in [`vanilla`]. pub mod args; +pub mod tree_scoreboard; pub mod vanilla; use crate::text::Text; diff --git a/crates/horizon-client/src/command/tree_scoreboard.rs b/crates/horizon-client/src/command/tree_scoreboard.rs new file mode 100644 index 000000000..4493ca8f1 --- /dev/null +++ b/crates/horizon-client/src/command/tree_scoreboard.rs @@ -0,0 +1,214 @@ +//! The `/scoreboard`, `/team` and `/trigger` command trees +//! (ScoreboardCommand, TeamCommand, TriggerCommand). +use super::{Dispatcher, args::ArgType, vanilla::GAMEMASTERS}; + +const HOLDERS: ArgType = ArgType::ScoreHolder { multiple: true }; +const HOLDER: ArgType = ArgType::ScoreHolder { multiple: false }; + +/// `addNumberFormats`: blank | fixed | styled