Skip to content

Commit

Permalink
Changed bossbar title to TextComponent instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
leobeg committed Dec 30, 2024
1 parent f772d97 commit f0cd552
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 84 deletions.
80 changes: 80 additions & 0 deletions pumpkin/src/command/args/arg_textcomponent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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::server::Server;

pub(crate) struct TextComponentArgConsumer;

impl GetClientSideArgParser for TextComponentArgConsumer {
fn get_client_side_parser(&self) -> ProtoCmdArgParser {
ProtoCmdArgParser::Component
}

fn get_client_side_suggestion_type_override(&self) -> Option<ProtoCmdArgSuggestionType> {
None
}
}

#[async_trait]
impl ArgumentConsumer for TextComponentArgConsumer {
async fn consume<'a>(
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
args: &mut RawArgs<'a>,
) -> Option<Arg<'a>> {
let s = args.pop()?;

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("\"", "");
return Some(Arg::TextComponent(TextComponent::text(s)));
}
return None;
};

Some(Arg::TextComponent(text_component))
}

async fn suggest<'a>(
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
_input: &'a str,
) -> Result<Option<Vec<CommandSuggestion>>, CommandError> {
Ok(None)
}
}

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

fn find_arg(args: &super::ConsumedArgs, name: &str) -> Result<Self::Data, CommandError> {
match args.get(name) {
Some(Arg::TextComponent(data)) => Ok(data.clone()),
_ => Err(CommandError::InvalidConsumption(Some(name.to_string()))),
}
}
}

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;
};
let mut constructed_text_component = text_component_array[0].clone();
text_component_array.remove(0);
constructed_text_component.extra = text_component_array;

Some(constructed_text_component)
} else {
serde_json::from_str(input).unwrap_or(None)
}
}
3 changes: 3 additions & 0 deletions pumpkin/src/command/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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,6 +38,7 @@ pub(crate) mod arg_resource_location;
pub(crate) mod arg_rotation;
pub(crate) mod arg_simple;
mod coordinate;
pub(crate) mod arg_textcomponent;

/// see [`crate::commands::tree_builder::argument`]
#[async_trait]
Expand Down Expand Up @@ -87,6 +89,7 @@ pub(crate) enum Arg<'a> {
BossbarColor(BossbarColor),
BossbarStyle(BossbarDivisions),
Msg(String),
TextComponent(TextComponent),
Num(Result<Number, NotInBounds>),
Bool(bool),
#[allow(unused)]
Expand Down
Loading

0 comments on commit f0cd552

Please sign in to comment.