240 lines
8.1 KiB
Rust
240 lines
8.1 KiB
Rust
use std::sync::{
|
|
Arc, Weak,
|
|
atomic::{AtomicBool, AtomicI32, Ordering},
|
|
};
|
|
|
|
use pumpkin_data::Block;
|
|
use pumpkin_data::dimension::Dimension;
|
|
use pumpkin_data::entity::EntityType;
|
|
use pumpkin_data::sound::{Sound, SoundCategory};
|
|
use pumpkin_data::tracked_data;
|
|
use pumpkin_nbt::compound::NbtCompound;
|
|
use pumpkin_protocol::java::client::play::Metadata;
|
|
use pumpkin_util::math::position::BlockPos;
|
|
|
|
use crate::entity::{
|
|
Entity, EntityBase, EntityBaseFuture, NbtFuture,
|
|
ai::goal::{
|
|
active_target::ActiveTargetGoal, look_around::RandomLookAroundGoal,
|
|
look_at_entity::LookAtEntityGoal, melee_attack::MeleeAttackGoal, open_door::OpenDoorGoal,
|
|
revenge::RevengeGoal, swim::SwimGoal, wander_around::WanderAroundGoal,
|
|
},
|
|
mob::{Mob, MobEntity},
|
|
};
|
|
use crate::world::World;
|
|
|
|
pub struct PiglinBruteEntity {
|
|
pub mob_entity: MobEntity,
|
|
pub immune_to_zombification: AtomicBool,
|
|
pub time_in_overworld: AtomicI32,
|
|
}
|
|
|
|
impl PiglinBruteEntity {
|
|
pub const CONVERSION_TIME: i32 = 300;
|
|
pub const XP_REWARD: u32 = 20;
|
|
|
|
pub fn new(entity: Entity) -> Arc<Self> {
|
|
let mob_entity = MobEntity::new(entity);
|
|
let piglin = Self {
|
|
mob_entity,
|
|
immune_to_zombification: AtomicBool::new(false),
|
|
time_in_overworld: AtomicI32::new(0),
|
|
};
|
|
let mob_arc = Arc::new(piglin);
|
|
let mob_weak: Weak<dyn Mob> = {
|
|
let mob_arc: Arc<dyn Mob> = mob_arc.clone();
|
|
Arc::downgrade(&mob_arc)
|
|
};
|
|
|
|
{
|
|
let mut goal_selector = mob_arc
|
|
.mob_entity
|
|
.goals_selector
|
|
.lock()
|
|
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
|
|
|
goal_selector.add_goal(0, Box::new(SwimGoal::default()));
|
|
goal_selector.add_goal(1, Box::new(OpenDoorGoal::new(true)));
|
|
goal_selector.add_goal(2, Box::new(MeleeAttackGoal::new(1.0, true)));
|
|
goal_selector.add_goal(5, Box::new(WanderAroundGoal::new(1.0)));
|
|
goal_selector.add_goal(
|
|
6,
|
|
LookAtEntityGoal::with_default(mob_weak.clone(), &EntityType::PLAYER, 8.0),
|
|
);
|
|
goal_selector.add_goal(7, Box::new(RandomLookAroundGoal::default()));
|
|
|
|
let mut target_selector = mob_arc
|
|
.mob_entity
|
|
.target_selector
|
|
.lock()
|
|
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
|
target_selector.add_goal(1, Box::new(RevengeGoal::new(true)));
|
|
// Piglin brutes are always hostile to players (even with gold armor)
|
|
target_selector.add_goal(
|
|
2,
|
|
ActiveTargetGoal::with_default(&mob_arc.mob_entity, &EntityType::PLAYER, true),
|
|
);
|
|
target_selector.add_goal(
|
|
3,
|
|
ActiveTargetGoal::with_default(
|
|
&mob_arc.mob_entity,
|
|
&EntityType::WITHER_SKELETON,
|
|
true,
|
|
),
|
|
);
|
|
target_selector.add_goal(
|
|
3,
|
|
ActiveTargetGoal::with_default(&mob_arc.mob_entity, &EntityType::WITHER, true),
|
|
);
|
|
};
|
|
|
|
mob_arc
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn is_immune_to_zombification(&self) -> bool {
|
|
self.immune_to_zombification.load(Ordering::Relaxed)
|
|
}
|
|
|
|
pub fn set_immune_to_zombification(&self, immune: bool) {
|
|
self.immune_to_zombification
|
|
.store(immune, Ordering::Relaxed);
|
|
self.mob_entity.living_entity.entity.send_meta_data(
|
|
&[Metadata::new(
|
|
tracked_data::piglin_brute::DATA_IMMUNE_TO_ZOMBIFICATION,
|
|
immune,
|
|
)],
|
|
None,
|
|
);
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn is_converting(&self, world: &World) -> bool {
|
|
!self.is_immune_to_zombification()
|
|
&& !self.mob_entity.is_no_ai()
|
|
&& world.dimension.minecraft_name != Dimension::THE_NETHER.minecraft_name
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn check_piglin_brute_spawn_rules(world: &World, pos: &BlockPos) -> bool {
|
|
let below = BlockPos::new(pos.0.x, pos.0.y - 1, pos.0.z);
|
|
let state = world.get_block_state(&below);
|
|
state.id != Block::NETHER_WART_BLOCK.default_state.id
|
|
}
|
|
|
|
async fn convert_to_zombified(&self) {
|
|
let entity = &self.mob_entity.living_entity.entity;
|
|
let world = entity.world.load();
|
|
let pos = entity.pos.load();
|
|
|
|
if world.level_info.load().difficulty != pumpkin_util::Difficulty::Peaceful {
|
|
world.play_sound(
|
|
Sound::EntityPiglinBruteConvertedToZombified,
|
|
SoundCategory::Hostile,
|
|
&pos,
|
|
);
|
|
}
|
|
|
|
let zombified = crate::entity::r#type::from_type(
|
|
&EntityType::ZOMBIFIED_PIGLIN,
|
|
pos,
|
|
&world,
|
|
uuid::Uuid::new_v4(),
|
|
);
|
|
|
|
let zombified_base = zombified.get_entity();
|
|
zombified_base.set_rotation(entity.yaw.load(), entity.pitch.load());
|
|
zombified_base.head_yaw.store(entity.head_yaw.load());
|
|
zombified_base.velocity.store(entity.velocity.load());
|
|
|
|
if let Some(living) = zombified.get_living_entity() {
|
|
living.set_health(self.mob_entity.living_entity.health.load());
|
|
}
|
|
|
|
if let Some(custom_name) = &**entity.custom_name.load() {
|
|
zombified_base.set_custom_name(custom_name.clone());
|
|
}
|
|
|
|
{
|
|
let src_equip = self.mob_entity.living_entity.entity_equipment.lock().await;
|
|
if let Some(living) = zombified.get_living_entity() {
|
|
let mut dst_equip = living.entity_equipment.lock().await;
|
|
for (slot, item) in &src_equip.equipment {
|
|
dst_equip.put(slot, item.clone());
|
|
}
|
|
}
|
|
}
|
|
|
|
world.spawn_entity(zombified).await;
|
|
entity.remove().await;
|
|
}
|
|
}
|
|
|
|
impl Mob for PiglinBruteEntity {
|
|
fn get_mob_entity(&self) -> &MobEntity {
|
|
&self.mob_entity
|
|
}
|
|
|
|
fn mob_init_data_tracker(&self) -> EntityBaseFuture<'_, ()> {
|
|
Box::pin(async move {
|
|
let entity = self.get_entity();
|
|
if self.is_immune_to_zombification() {
|
|
entity.send_meta_data(
|
|
&[Metadata::new(
|
|
tracked_data::piglin_brute::DATA_IMMUNE_TO_ZOMBIFICATION,
|
|
true,
|
|
)],
|
|
None,
|
|
);
|
|
}
|
|
})
|
|
}
|
|
|
|
fn mob_write_nbt<'a>(&'a self, nbt: &'a mut NbtCompound) -> NbtFuture<'a, ()> {
|
|
Box::pin(async move {
|
|
if self.is_immune_to_zombification() {
|
|
nbt.put_bool("IsImmuneToZombification", true);
|
|
}
|
|
let time_in_overworld = self.time_in_overworld.load(Ordering::Relaxed);
|
|
if time_in_overworld > 0 {
|
|
nbt.put_int("TimeInOverworld", time_in_overworld);
|
|
}
|
|
nbt.put_bool("CanPickUpLoot", true);
|
|
})
|
|
}
|
|
|
|
fn mob_read_nbt<'a>(&'a self, nbt: &'a NbtCompound) -> NbtFuture<'a, ()> {
|
|
Box::pin(async move {
|
|
if let Some(immune) = nbt.get_bool("IsImmuneToZombification") {
|
|
self.set_immune_to_zombification(immune);
|
|
}
|
|
if let Some(time) = nbt.get_int("TimeInOverworld") {
|
|
self.time_in_overworld.store(time, Ordering::Relaxed);
|
|
}
|
|
})
|
|
}
|
|
|
|
fn mob_tick<'a>(&'a self, _caller: &'a Arc<dyn EntityBase>) -> EntityBaseFuture<'a, ()> {
|
|
Box::pin(async move {
|
|
let entity = &self.mob_entity.living_entity.entity;
|
|
if !entity.is_alive() {
|
|
return;
|
|
}
|
|
|
|
let world = entity.world.load();
|
|
if self.is_converting(&world) {
|
|
let time = self.time_in_overworld.fetch_add(1, Ordering::Relaxed) + 1;
|
|
if time > Self::CONVERSION_TIME {
|
|
self.convert_to_zombified().await;
|
|
}
|
|
} else {
|
|
self.time_in_overworld.store(0, Ordering::Relaxed);
|
|
}
|
|
})
|
|
}
|
|
|
|
fn get_base_experience_reward(&self) -> u32 {
|
|
Self::XP_REWARD
|
|
}
|
|
}
|