feat: implement Bedrock cross-edition scoreboard support and remove_objective
This commit is contained in:
1 parent
f49e29aa3f
commit
2edc79f7cc
4 files changed
+105
-17
No files matched your search
@@ -98,3 +98,11 @@ impl ScoreEntry {
|
||||
pub struct CRemoveObjective {
|
||||
pub objective_name: String,
|
||||
}
|
||||
|
||||
impl CRemoveObjective {
|
||||
pub fn new(objective_name: impl Into<String>) -> Self {
|
||||
Self {
|
||||
objective_name: objective_name.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
|
||||
|
||||
@@ -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!(
|
||||
|
||||
Reference in new issue
Block a user