diff --git a/crates/pumpkin-protocol/src/bedrock/client/scoreboard.rs b/crates/pumpkin-protocol/src/bedrock/client/scoreboard.rs index 0cecb9e75..24ec10639 100644 --- a/crates/pumpkin-protocol/src/bedrock/client/scoreboard.rs +++ b/crates/pumpkin-protocol/src/bedrock/client/scoreboard.rs @@ -98,3 +98,11 @@ impl ScoreEntry { pub struct CRemoveObjective { pub objective_name: String, } + +impl CRemoveObjective { + pub fn new(objective_name: impl Into) -> Self { + Self { + objective_name: objective_name.into(), + } + } +} diff --git a/crates/pumpkin/src/command/commands/scoreboard.rs b/crates/pumpkin/src/command/commands/scoreboard.rs index e9ef2f261..de5b91ae9 100644 --- a/crates/pumpkin/src/command/commands/scoreboard.rs +++ b/crates/pumpkin/src/command/commands/scoreboard.rs @@ -172,6 +172,42 @@ impl CommandExecutor for PlayersEnableExecutor { } } +struct ObjectivesRemoveExecutor; + +impl CommandExecutor for ObjectivesRemoveExecutor { + fn execute<'a>(&'a self, context: &'a CommandContext) -> CommandExecutorResult<'a> { + Box::pin(async move { + let objective_name = ObjectiveArgumentType::get(context, ARG_OBJECTIVE)?; + + let world = context.world(); + let mut scoreboard = world.scoreboard.lock().await; + + let objective = scoreboard + .get_objectives() + .get(objective_name) + .ok_or_else(|| INVALID_ENABLE_ERROR.create_without_context())?; + + let display_name = objective.display_name.clone(); + + scoreboard.remove_objective(world, objective_name).await; + + context + .source + .send_feedback( + TextComponent::translate_cross( + translation::java::COMMANDS_SCOREBOARD_OBJECTIVES_REMOVE_SUCCESS, + translation::bedrock::COMMANDS_SCOREBOARD_OBJECTIVES_REMOVE_SUCCESS, + [display_name], + ), + true, + ) + .await; + + Ok(1) + }) + } +} + pub fn register(dispatcher: &mut CommandDispatcher, registry: &mut PermissionRegistry) { registry.register_permission_or_panic(Permission::new( PERMISSION, @@ -183,22 +219,34 @@ pub fn register(dispatcher: &mut CommandDispatcher, registry: &mut PermissionReg command("scoreboard", DESCRIPTION) .requires(PERMISSION) .then( - literal("objectives").then( - literal("add").then( - argument(ARG_OBJECTIVE, StringArgumentType::SingleWord).then( - argument(ARG_CRITERION, StringArgumentType::SingleWord) - .executes(ObjectivesAddExecutor { - has_display_name: false, - }) - .then( - argument(ARG_DISPLAY_NAME, StringArgumentType::GreedyPhrase) - .executes(ObjectivesAddExecutor { - has_display_name: true, - }), - ), + literal("objectives") + .then( + literal("add").then( + argument(ARG_OBJECTIVE, StringArgumentType::SingleWord).then( + argument(ARG_CRITERION, StringArgumentType::SingleWord) + .executes(ObjectivesAddExecutor { + has_display_name: false, + }) + .then( + argument( + ARG_DISPLAY_NAME, + StringArgumentType::GreedyPhrase, + ) + .executes( + ObjectivesAddExecutor { + has_display_name: true, + }, + ), + ), + ), + ), + ) + .then( + literal("remove").then( + argument(ARG_OBJECTIVE, ObjectiveArgumentType) + .executes(ObjectivesRemoveExecutor), ), ), - ), ) .then(literal("players").then(literal("enable").then( argument(ARG_TARGETS, EntityArgumentType::Players).then( diff --git a/crates/pumpkin/src/plugin/loader/wasm/wasm_host/wit/v0_1/scoreboard.rs b/crates/pumpkin/src/plugin/loader/wasm/wasm_host/wit/v0_1/scoreboard.rs index 1350be907..0f1eaa243 100644 --- a/crates/pumpkin/src/plugin/loader/wasm/wasm_host/wit/v0_1/scoreboard.rs +++ b/crates/pumpkin/src/plugin/loader/wasm/wasm_host/wit/v0_1/scoreboard.rs @@ -131,9 +131,12 @@ impl scoreboard::HostScoreboard for PluginHostState { } }; - world.broadcast_packet_all( - &pumpkin_protocol::java::client::play::CDisplayObjective::new(slot, objective_name), - ); + world + .scoreboard + .lock() + .await + .set_display_objective(&world, slot, &objective_name) + .await; Ok(()) } diff --git a/crates/pumpkin/src/world/scoreboard.rs b/crates/pumpkin/src/world/scoreboard.rs index e88251fac..a276fdff2 100644 --- a/crates/pumpkin/src/world/scoreboard.rs +++ b/crates/pumpkin/src/world/scoreboard.rs @@ -86,6 +86,35 @@ impl Scoreboard { .insert(objective.name.to_string(), objective); } + pub async fn set_display_objective( + &mut self, + world: &World, + slot: ScoreboardDisplaySlot, + objective_name: &str, + ) { + let slot_str = match slot { + ScoreboardDisplaySlot::List => "list", + ScoreboardDisplaySlot::BelowName => "belowname", + _ => "sidebar", + }; + + let display_name = self.objectives.get(objective_name).map_or_else( + || objective_name.to_string(), + |o| o.display_name.clone().get_text(), + ); + + let je_display = CDisplayObjective::new(slot, objective_name.to_string()); + let be_display = BSetDisplayObjective { + display_slot: slot_str.to_string(), + objective_name: objective_name.to_string(), + display_name, + criteria_name: "dummy".to_string(), + sort_order: VarInt(0), + }; + + Self::broadcast_editioned(world, &je_display, &be_display).await; + } + pub async fn remove_objective(&mut self, world: &World, name: &str) { if !self.objectives.contains_key(name) { warn!(