Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed bossbar title to TextComponent instead of String #435

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Clippy and fmt
  • Loading branch information
leobeg committed Dec 30, 2024
commit 384dd6756f7567ffb077d17a307dd77b90532c53
27 changes: 14 additions & 13 deletions pumpkin/src/command/args/arg_textcomponent.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use async_trait::async_trait;
use pumpkin_core::text::TextComponent;
use pumpkin_protocol::client::play::{CommandSuggestion, ProtoCmdArgParser, ProtoCmdArgSuggestionType};
use crate::command::args::{Arg, ArgumentConsumer, FindArg, GetClientSideArgParser};
use crate::command::CommandSender;
use crate::command::dispatcher::CommandError;
use crate::command::tree::RawArgs;
use crate::command::CommandSender;
use crate::server::Server;
use async_trait::async_trait;
use pumpkin_core::text::TextComponent;
use pumpkin_protocol::client::play::{
CommandSuggestion, ProtoCmdArgParser, ProtoCmdArgSuggestionType,
};

pub(crate) struct TextComponentArgConsumer;

Expand All @@ -32,8 +34,8 @@ impl ArgumentConsumer for TextComponentArgConsumer {
let text_component = parse_text_component(s);

let Some(text_component) = text_component else {
if s.starts_with("\"") && s.ends_with("\"") {
let s = s.replace("\"", "");
if s.starts_with('"') && s.ends_with('"') {
let s = s.replace('"', "");
return Some(Arg::TextComponent(TextComponent::text(s)));
}
return None;
Expand All @@ -52,7 +54,7 @@ impl ArgumentConsumer for TextComponentArgConsumer {
}
}

impl<'a> FindArg<'a> for TextComponentArgConsumer {
impl FindArg<'_> for TextComponentArgConsumer {
type Data = TextComponent;

fn find_arg(args: &super::ConsumedArgs, name: &str) -> Result<Self::Data, CommandError> {
Expand All @@ -64,11 +66,10 @@ impl<'a> FindArg<'a> for TextComponentArgConsumer {
}

fn parse_text_component(input: &str) -> Option<TextComponent> {
if input.starts_with("[") && input.ends_with("]") {
let text_component_array: Option<Vec<TextComponent>> = serde_json::from_str(input).unwrap_or(None);
let Some(mut text_component_array) = text_component_array else {
return None;
};
if input.starts_with('[') && input.ends_with(']') {
let text_component_array: Option<Vec<TextComponent>> =
serde_json::from_str(input).unwrap_or(None);
let mut text_component_array = text_component_array?;
let mut constructed_text_component = text_component_array[0].clone();
text_component_array.remove(0);
constructed_text_component.extra = text_component_array;
Expand All @@ -77,4 +78,4 @@ fn parse_text_component(input: &str) -> Option<TextComponent> {
} else {
serde_json::from_str(input).unwrap_or(None)
}
}
}
4 changes: 2 additions & 2 deletions pumpkin/src/command/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::{collections::HashMap, hash::Hash, sync::Arc};

use arg_bounded_num::{NotInBounds, Number};
use async_trait::async_trait;
use pumpkin_core::text::TextComponent;
use pumpkin_core::{
math::{position::WorldPosition, vector2::Vector2, vector3::Vector3},
GameMode,
};
use pumpkin_core::text::TextComponent;
use pumpkin_protocol::client::play::{
CommandSuggestion, ProtoCmdArgParser, ProtoCmdArgSuggestionType,
};
Expand Down Expand Up @@ -37,8 +37,8 @@ pub(crate) mod arg_position_block;
pub(crate) mod arg_resource_location;
pub(crate) mod arg_rotation;
pub(crate) mod arg_simple;
mod coordinate;
pub(crate) mod arg_textcomponent;
mod coordinate;

/// see [`crate::commands::tree_builder::argument`]
#[async_trait]
Expand Down
34 changes: 18 additions & 16 deletions pumpkin/src/command/commands/cmd_bossbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use crate::command::args::arg_bounded_num::BoundedNumArgumentConsumer;
use crate::command::args::arg_players::PlayersArgumentConsumer;
use crate::command::args::arg_resource_location::ResourceLocationArgumentConsumer;

use crate::command::args::{
ConsumedArgs, DefaultNameArgConsumer, FindArg, FindArgDefaultName,
};
use crate::command::args::{ConsumedArgs, DefaultNameArgConsumer, FindArg, FindArgDefaultName};
use crate::command::dispatcher::CommandError;

use crate::command::args::arg_textcomponent::TextComponentArgConsumer;
use crate::command::tree::CommandTree;
use crate::command::tree_builder::{argument, argument_default_name, literal};
use crate::command::{CommandExecutor, CommandSender};
Expand All @@ -20,7 +19,6 @@ use async_trait::async_trait;
use pumpkin_core::text::color::{Color, NamedColor};
use pumpkin_core::text::TextComponent;
use uuid::Uuid;
use crate::command::args::arg_textcomponent::TextComponentArgConsumer;

const NAMES: [&str; 1] = ["bossbar"];
const DESCRIPTION: &str = "Display bossbar";
Expand Down Expand Up @@ -56,7 +54,6 @@ enum CommandValueSet {
struct BossbarAddExecuter;

#[async_trait]
#[expect(clippy::inefficient_to_string)]
impl CommandExecutor for BossbarAddExecuter {
async fn execute<'a>(
&self,
Expand Down Expand Up @@ -308,7 +305,7 @@ impl CommandExecutor for BossbarSetExecuter {
send_prefix_success_message(
sender,
bossbar.bossbar_data.title,
format!("has changed maximum to {}", max_value),
format!("has changed maximum to {max_value}"),
)
.await;
Ok(())
Expand All @@ -329,8 +326,12 @@ impl CommandExecutor for BossbarSetExecuter {
}
}

send_prefix_success_message(sender, text_component, String::from("has been renamed"))
.await;
send_prefix_success_message(
sender,
text_component,
String::from("has been renamed"),
)
.await;
Ok(())
}
CommandValueSet::Players(has_players) => {
Expand Down Expand Up @@ -440,7 +441,7 @@ impl CommandExecutor for BossbarSetExecuter {
send_prefix_success_message(
sender,
bossbar.bossbar_data.title,
format!("changed value to {}", value),
format!("changed value to {value}"),
)
.await;
Ok(())
Expand Down Expand Up @@ -485,12 +486,11 @@ fn value_consumer() -> BoundedNumArgumentConsumer<i32> {

pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION)
.with_child(
literal("add").with_child(
argument_default_name(non_autocomplete_consumer())
.with_child(argument(ARG_NAME, TextComponentArgConsumer).execute(BossbarAddExecuter)),
.with_child(literal("add").with_child(
argument_default_name(non_autocomplete_consumer()).with_child(
argument(ARG_NAME, TextComponentArgConsumer).execute(BossbarAddExecuter),
),
)
))
.with_child(
literal("get").with_child(
argument_default_name(autocomplete_consumer())
Expand Down Expand Up @@ -561,7 +561,7 @@ pub fn init_command_tree() -> CommandTree {
)
}

fn bossbar_prefix(title: TextComponent, trailing: String) -> TextComponent {
fn bossbar_prefix(title: TextComponent, trailing: &str) -> TextComponent {
TextComponent::text("Custom bossbar [")
.add_child(title)
.add_child(TextComponent::text(format!("] {trailing}")))
Expand All @@ -572,7 +572,9 @@ async fn send_prefix_success_message(
title: TextComponent,
message: String,
) {
sender.send_message(bossbar_prefix(title, message)).await;
sender
.send_message(bossbar_prefix(title, message.as_str()))
.await;
}
async fn send_success_message(sender: &CommandSender<'_>, message: String) {
sender.send_message(TextComponent::text(message)).await;
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/world/custom_bossbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use crate::command::args::GetCloned;
use crate::entity::player::Player;
use crate::server::Server;
use crate::world::bossbar::{Bossbar, BossbarColor, BossbarDivisions};
use pumpkin_core::text::TextComponent;
use std::collections::HashMap;
use std::sync::Arc;
use thiserror::Error;
use uuid::Uuid;
use pumpkin_core::text::TextComponent;

#[derive(Debug, Error)]
pub enum BossbarUpdateError {
Expand Down
Loading