mod metadata;
mod methods;
mod motion;
mod spawn;
#[cfg(test)]
mod tests;
use metadata::*;
use motion::*;
use spawn::*;
use super::{ERA, PATHS, proto};
use std::ops::Deref;
use super::data::entities::{self as data, EntityTypeData, type_flags};
use super::proto::play::clientbound::{Login, Respawn, id};
use super::read_stack;
use crate::modern_state::st;
use bytes::Bytes;
use ghast_protocol::nbt::AnonNbt;
use ghast_protocol::types::LpVec3;
use ghast_protocol::types::slot::v775::ItemStack;
use ghast_protocol::{Decode, DecodeError, RawPacket, Reader};
use crate::Applied;
use crate::common::{IdMask, R, angle, for_each_varint, own_str, skip_string};
use crate::common::{remove_effect, set_effect};
use crate::entity::{Effect, Entity, EntityClass, EntityType, Item, MetaValue};
use crate::store::{Config, Entities, Event, Fast, PlayerInfo};
pub const CORE_IDS: &[i32] = &[
id::LOGIN,
id::RESPAWN,
id::SPAWN_ENTITY,
id::ENTITY_VELOCITY,
id::ENTITY_DESTROY,
id::REL_ENTITY_MOVE,
id::ENTITY_LOOK,
id::ENTITY_MOVE_LOOK,
id::SYNC_ENTITY_POSITION,
id::ENTITY_TELEPORT,
id::ENTITY_HEAD_ROTATION,
id::ENTITY_STATUS,
id::DAMAGE_EVENT,
id::ATTACH_ENTITY,
id::SET_PASSENGERS,
id::COLLECT,
id::ENTITY_METADATA,
id::ENTITY_UPDATE_ATTRIBUTES,
id::MAP_CHUNK,
id::UNLOAD_CHUNK,
];
pub const METADATA_IDS: &[i32] = &[id::ENTITY_METADATA];
pub const EQUIPMENT_IDS: &[i32] = &[id::ENTITY_EQUIPMENT];
pub const EFFECT_IDS: &[i32] = &[id::ENTITY_EFFECT, id::REMOVE_ENTITY_EFFECT];
pub const PLAYER_LIST_IDS: &[i32] = &[id::PLAYER_INFO, id::PLAYER_REMOVE];
const FOX_FLAGS: u8 = 19;
pub const SCALE_ATTRIBUTE: i32 = super::SCALE_ATTRIBUTE_ID;
pub mod meta {
pub const FLAGS: u8 = 0;
pub const AIR: u8 = 1;
pub const CUSTOM_NAME: u8 = 2;
pub const NO_GRAVITY: u8 = 5;
pub const POSE: u8 = 6;
pub const LIVING_FLAGS: u8 = 8;
pub const HEALTH: u8 = 9;
pub const SLEEPING_POS: u8 = 14;
pub const ITEM: u8 = 8;
}
#[derive(Clone, Debug)]
pub struct Tracker {
store: Entities,
dimension: String,
wanted: IdMask,
chunks: Fast<u64, ()>,
ghosts: Fast<i32, ()>,
order: Fast<i32, u32>,
next_seq: u32,
order_cap: usize,
early: Vec<i32>,
order_dirty: bool,
}
impl Deref for Tracker {
type Target = Entities;
fn deref(&self) -> &Entities {
&self.store
}
}
#[must_use]
pub fn server_position(e: &Entity) -> [f64; 3] {
base(e)
}
#[must_use]
pub fn interpolation_target(e: &Entity) -> Option<([f64; 3], [f32; 2])> {
(e.m.steps > 0).then_some((e.m.target, e.m.target_rot))
}
#[must_use]
pub fn entity_type(e: &Entity) -> Option<&'static EntityTypeData> {
e.m.td
}
#[must_use]
pub fn pick_radius(e: &Entity) -> f32 {
e.m.td
.and_then(|t| t.size)
.map_or(0.0, |s| s.targeting_margin)
}
#[must_use]
pub fn body_yaw(e: &Entity) -> f32 {
e.m.body_yaw
}
#[must_use]
pub fn pose(e: &Entity) -> u8 {
e.m.pose
}
#[must_use]
pub fn scale(e: &Entity) -> f32 {
e.m.scale
}
#[must_use]
pub fn item_count(e: &Entity) -> i32 {
e.m.item_count
}
#[must_use]
pub fn item_stack(e: &Entity) -> Option<&ItemStack> {
match e.meta.get(meta::ITEM)? {
MetaValue::Stack(s) if e.ty == EntityType::Registry(data::ITEM) => s.as_ref(),
_ => None,
}
}
#[must_use]
pub fn health(e: &Entity) -> Option<f32> {
e.metadata().float(meta::HEALTH)
}
#[must_use]
pub fn alive(e: &Entity) -> bool {
if !living(e) {
return true;
}
match health(e) {
Some(h) => h > 0.0,
None => !e.m.flag(st::DEAD),
}
}
#[must_use]
pub fn flags(e: &Entity) -> u8 {
e.metadata().byte(meta::FLAGS).unwrap_or(0) as u8
}
#[must_use]
pub fn custom_name(e: &Entity) -> Option<&Bytes> {
match e.metadata().get(meta::CUSTOM_NAME)? {
MetaValue::OptComponent(Some(b)) => Some(b),
_ => None,
}
}
#[must_use]
pub fn info(t: EntityType) -> Option<&'static ghast_data::EntityInfo> {
match t {
EntityType::Registry(id) => super::data::entity(id),
EntityType::Player => super::data::entity(data::PLAYER),
_ => None,
}
}
pub(crate) fn class(e: &Entity) -> EntityClass {
match e.ty {
EntityType::Player => EntityClass::Player,
EntityType::Registry(data::ITEM) => EntityClass::Item,
EntityType::Registry(data::EXPERIENCE_ORB) => EntityClass::ExperienceOrb,
EntityType::Registry(data::PAINTING) => EntityClass::Painting,
_ if e.living => EntityClass::Mob,
_ => EntityClass::Object,
}
}
pub(crate) fn vanilla_size(e: &Entity) -> Option<&'static ghast_data::VanillaEntitySize> {
e.m.td.and_then(|t| t.size)
}
pub(crate) fn dimensions(e: &Entity) -> Option<ghast_data::EntityDimensions> {
e.m.dims()
}
fn compute_dims(e: &Entity) -> Option<ghast_data::EntityDimensions> {
let t = e.m.td?;
let m = &e.m;
let (variant, slime) = if t.size_meta.is_some() {
("default", Some(m.slime_size))
} else if t.flags_meta.is_some() {
let f = m.stand_flags;
let v = match (f & 0x01 != 0, f & 0x10 != 0) {
(true, true) => "small_marker",
(false, true) => "marker",
(true, false) => "small",
_ => "default",
};
(v, None)
} else if t.baby_meta.is_some() && m.flag(st::BABY) {
("baby", None)
} else {
("default", None)
};
let d = t.dimensions(variant, slime, i32::from(m.pose))?;
if t.is_living() && m.pose != 2 {
let fixed = t.flags & type_flags::FIXED != 0
|| variant.ends_with("marker")
|| t.fixed_poses & (1 << m.pose) != 0;
return Some(EntityTypeData::scaled(d, m.scale, fixed));
}
Some(d)
}