Skip to content

Commit

Permalink
Fix HoverEvent::show_entity calls (#539)
Browse files Browse the repository at this point in the history
* expose entity resource name in EntityType

* Use correct resource name in HoverEvent::show_entity

* fix lint
  • Loading branch information
yarml authored Feb 10, 2025
1 parent 8755b6c commit 45f5c27
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
26 changes: 16 additions & 10 deletions pumpkin-data/build/entity_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@ pub struct EntityType {
pub eye_height: f32,
}

impl ToTokens for EntityType {
pub struct NamedEntityType<'a>(&'a str, &'a EntityType);

impl ToTokens for NamedEntityType<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let id = LitInt::new(&self.id.to_string(), proc_macro2::Span::call_site());
let name = self.0;
let entity = self.1;
let id = LitInt::new(&entity.id.to_string(), proc_macro2::Span::call_site());

let max_health = match self.max_health {
let max_health = match entity.max_health {
Some(mh) => quote! { Some(#mh) },
None => quote! { None },
};

let attackable = match self.attackable {
let attackable = match entity.attackable {
Some(a) => quote! { Some(#a) },
None => quote! { None },
};

let summonable = self.summonable;
let fire_immune = self.fire_immune;
let eye_height = self.eye_height;
let summonable = entity.summonable;
let fire_immune = entity.fire_immune;
let eye_height = entity.eye_height;

let dimension0 = self.dimension[0];
let dimension1 = self.dimension[1];
let dimension0 = entity.dimension[0];
let dimension1 = entity.dimension[1];

tokens.extend(quote! {
EntityType {
Expand All @@ -46,6 +50,7 @@ impl ToTokens for EntityType {
fire_immune: #fire_immune,
dimension: [#dimension0, #dimension1], // Correctly construct the array
eye_height: #eye_height,
resource_name: #name,
}
});
}
Expand All @@ -67,7 +72,7 @@ pub(crate) fn build() -> TokenStream {
let id_lit = LitInt::new(&id.to_string(), proc_macro2::Span::call_site());
let upper_name = format_ident!("{}", name.to_uppercase());

let entity_tokens = entity.to_token_stream();
let entity_tokens = NamedEntityType(name, entity).to_token_stream();

consts.extend(quote! {
pub const #upper_name: EntityType = #entity_tokens;
Expand All @@ -91,6 +96,7 @@ pub(crate) fn build() -> TokenStream {
pub fire_immune: bool,
pub dimension: [f32; 2],
pub eye_height: f32,
pub resource_name: &'static str,
}

impl EntityType {
Expand Down
4 changes: 2 additions & 2 deletions pumpkin-util/src/text/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ impl HoverEvent {
pub fn show_text(text: TextComponent) -> Self {
Self::ShowText(vec![text.0])
}
pub fn show_entity<P>(id: P, kind: Option<P>, name: Option<TextComponent>) -> Self
pub fn show_entity<P>(id: P, kind: P, name: Option<TextComponent>) -> Self
where
P: Into<Cow<'static, str>>,
{
Self::ShowEntity {
id: id.into(),
r#type: kind.map(|kind| kind.into()),
r#type: Some(kind.into()),
name: match name {
Some(name) => Some(vec![name.0]),
None => None,
Expand Down
4 changes: 1 addition & 3 deletions pumpkin/src/command/commands/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ fn clear_command_text_output(item_count: usize, targets: &[Arc<Player>]) -> Text
))
.hover_event(HoverEvent::show_entity(
target.living_entity.entity.entity_uuid.to_string(),
Some(
format!("{:?}", target.living_entity.entity.entity_type).to_lowercase(),
),
target.living_entity.entity.entity_type.resource_name.into(),
Some(TextComponent::text(target.gameprofile.name.clone())),
)),
],
Expand Down
10 changes: 6 additions & 4 deletions pumpkin/src/command/commands/give.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ impl CommandExecutor for GiveExecutor {
TextComponent::text(targets[0].gameprofile.name.to_string())
.hover_event(HoverEvent::show_entity(
targets[0].living_entity.entity.entity_uuid.to_string(),
Some(
format!("{:?}", targets[0].living_entity.entity.entity_type)
.to_lowercase(),
),
targets[0]
.living_entity
.entity
.entity_type
.resource_name
.into(),
Some(TextComponent::text(targets[0].gameprofile.name.clone())),
))
.click_event(ClickEvent::SuggestCommand(
Expand Down
4 changes: 2 additions & 2 deletions pumpkin/src/command/commands/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl CommandExecutor for KillExecutor {
let mut entity_display =
TextComponent::text(name.clone()).hover_event(HoverEvent::show_entity(
entity.entity_uuid.to_string(),
Some(format!("{:?}", entity.entity_type).to_lowercase()),
entity.entity_type.resource_name.into(),
Some(TextComponent::text(name.clone())),
));

Expand Down Expand Up @@ -88,7 +88,7 @@ impl CommandExecutor for KillSelfExecutor {
[TextComponent::text(name.clone())
.hover_event(HoverEvent::show_entity(
entity.entity_uuid.to_string(),
Some(format!("{:?}", entity.entity_type).to_lowercase()),
entity.entity_type.resource_name.into(),
Some(TextComponent::text(name.clone())),
))
.click_event(ClickEvent::SuggestCommand(
Expand Down
10 changes: 2 additions & 8 deletions pumpkin/src/command/commands/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ impl CommandExecutor for MsgExecutor {
&TextComponent::text(target.gameprofile.name.clone())
.hover_event(HoverEvent::show_entity(
target.living_entity.entity.entity_uuid.to_string(),
Some(
format!("{:?}", target.living_entity.entity.entity_type)
.to_lowercase(),
),
target.living_entity.entity.entity_type.resource_name.into(),
Some(TextComponent::text(target.gameprofile.name.clone())),
))
.click_event(ClickEvent::SuggestCommand(
Expand All @@ -66,10 +63,7 @@ impl CommandExecutor for MsgExecutor {
&TextComponent::text(player.gameprofile.name.clone())
.hover_event(HoverEvent::show_entity(
player.living_entity.entity.entity_uuid.to_string(),
Some(
format!("{:?}", player.living_entity.entity.entity_type)
.to_lowercase(),
),
player.living_entity.entity.entity_type.resource_name.into(),
Some(TextComponent::text(player.gameprofile.name.clone())),
))
.click_event(ClickEvent::SuggestCommand(
Expand Down

0 comments on commit 45f5c27

Please sign in to comment.