Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Start implementing TryAsEntityRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubelotix committed Nov 10, 2023
1 parent 7bbebc5 commit 9681c01
Show file tree
Hide file tree
Showing 14 changed files with 593 additions and 10 deletions.
25 changes: 25 additions & 0 deletions minecraft-entities-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ pub fn MinecraftEntity(attr: TokenStream, item: TokenStream) -> TokenStream {
}
let code: TokenStream = code.into_iter().collect();
codes.push(code);
} else {
// Implement TryAsEntityRef for this struct
let code: TokenStream = r#"
impl TryAsEntityRef<This> for AnyEntity {
fn try_as_entity_ref(&self) -> Option<&This> {
match self {
AnyEntity::This(ref val) => Some(val),
_ => None,
}
}
fn try_as_entity_mut(&mut self) -> Option<&mut This> {
match self {
AnyEntity::This(ref mut val) => Some(val),
_ => None,
}
}
}
"#.parse().unwrap();
let mut code = code.clone().into_iter().collect::<Vec<_>>();
for element in &mut code {
replace_idents(element, &to_replace);
}
let code: TokenStream = code.into_iter().collect();
codes.push(code);
}

// Generate ext trait
Expand Down
22 changes: 22 additions & 0 deletions minecraft-entities/src/animals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ pub struct Animal {
pub ageable_mob: AgeableMob,
}

impl TryAsEntityRef<Animal> for AnyEntity {
fn try_as_entity_ref(&self) -> Option<&Animal> {
match self {
AnyEntity::Animal(animal) => return Some(&animal),
_ => (),
}
if let Some(tameable_animal) = self.try_as_entity_ref::<TameableAnimal>() {
return tameable_animal.animal.try_as_entity_ref();
}
}

fn try_as_entity_mut(&mut self) -> Option<&mut Animal> {
match self {
AnyEntity::Animal(animal) => return Some(animal),
_ => (),
}
if let Some(tameable_animal) = self.try_as_entity_mut::<TameableAnimal>() {
return tameable_animal.animal.try_as_entity_mut();
}
}
}

#[derive(Default)]
#[MinecraftEntity(
inheritable, parents { Animal, AgeableMob, PathfinderMob, Mob, LivingEntity, Entity },
Expand Down
22 changes: 22 additions & 0 deletions minecraft-entities/src/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ impl Default for AbstractArrow {
}
}

impl TryAsEntityRef<AbstractArrow> for AnyEntity {
fn try_as_entity_ref(&self) -> Option<&AbstractArrow> {
match self {
AnyEntity::AbstractArrow(abstract_arrow) => Some(abstract_arrow),
AnyEntity::Arrow(arrow) => Some(&arrow.abstract_arrow),
AnyEntity::SpectralArrow(spectral_arrow) => Some(&spectral_arrow.abstract_arrow),
AnyEntity::ThrownTrident(thrown_trident) => Some(&thrown_trident.abstract_arrow),
_ => None,
}
}

fn try_as_entity_mut(&mut self) -> Option<&mut AbstractArrow> {
match self {
AnyEntity::AbstractArrow(abstract_arrow) => Some(abstract_arrow),
AnyEntity::Arrow(arrow) => Some(&mut arrow.abstract_arrow),
AnyEntity::SpectralArrow(spectral_arrow) => Some(&mut spectral_arrow.abstract_arrow),
AnyEntity::ThrownTrident(thrown_trident) => Some(&mut thrown_trident.abstract_arrow),
_ => None,
}
}
}

#[MinecraftEntity(
parents { AbstractArrow, Entity },
)]
Expand Down
18 changes: 18 additions & 0 deletions minecraft-entities/src/boat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ impl Default for Boat {
}
}

impl TryAsEntityRef<Boat> for AnyEntity {
fn try_as_entity_ref(&self) -> Option<&Entity> {
match self {
AnyEntity::Boat(boat) => Some(&boat.entity),
AnyEntity::ChestBoat(chest_boat) => Some(&chest_boat.boat.entity),
_ => None,
}
}

fn try_as_entity_mut(&mut self) -> Option<&mut Boat> {
match self {
AnyEntity::Boat(boat) => Some(boat),
AnyEntity::ChestBoat(chest_boat) => Some(&mut chest_boat.boat),
_ => None,
}
}
}

#[derive(Default)]
#[MinecraftEntity(
parents { Boat, Entity },
Expand Down
22 changes: 22 additions & 0 deletions minecraft-entities/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ impl Default for Display {
}
}

impl TryAsEntityRef<Display> for AnyEntity {
fn try_as_entity_ref(&self) -> Option<&Entity> {
match self {
AnyEntity::Display(display) => Some(&display.entity),
AnyEntity::BlockDisplay(block_display) => Some(&block_display.display.entity),
AnyEntity::ItemDisplay(item_display) => Some(&item_display.display.entity),
AnyEntity::TextDisplay(text_display) => Some(&text_display.display.entity),
_ => None,
}
}

fn try_as_entity_mut(&mut self) -> Option<&mut Display> {
match self {
AnyEntity::Display(display) => Some(display),
AnyEntity::BlockDisplay(block_display) => Some(&mut block_display.display),
AnyEntity::ItemDisplay(item_display) => Some(&mut item_display.display),
AnyEntity::TextDisplay(text_display) => Some(&mut text_display.display),
_ => None,
}
}
}

#[MinecraftEntity(
parents { Display, Entity },
)]
Expand Down
Loading

0 comments on commit 9681c01

Please sign in to comment.