use bytes::Bytes;
use uuid::Uuid;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum EntityType {
Player,
LegacyMob(u8),
LegacyObject(u8),
ExperienceOrb,
Painting,
Registry(u16),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum EntityClass {
Player,
Item,
ExperienceOrb,
Mob,
Object,
Painting,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Item {
pub id: i32,
pub count: i8,
pub damage: i16,
pub nbt: Option<Bytes>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Effect {
pub id: u8,
pub amplifier: i8,
pub duration: i32,
pub flags: u8,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum MetaValue {
Byte(i8),
Short(i16),
Int(i32),
VarInt(i32),
Float(f32),
String(Box<str>),
Chat(Box<str>),
OptChat(Option<Box<str>>),
Item(Option<Item>),
Bool(bool),
Rotation([f32; 3]),
BlockPos([i32; 3]),
OptBlockPos(Option<[i32; 3]>),
Direction(i32),
OptUuid(Option<Uuid>),
BlockState(i32),
Nbt(Bytes),
Particle(i32),
VillagerData([i32; 3]),
OptVarInt(i32),
Pose(i32),
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Metadata {
entries: Vec<(u8, MetaValue)>,
}
impl Metadata {
#[must_use]
pub fn get(&self, index: u8) -> Option<&MetaValue> {
self.entries
.iter()
.find(|(i, _)| *i == index)
.map(|(_, v)| v)
}
pub fn set(&mut self, index: u8, value: MetaValue) {
match self.entries.iter_mut().find(|(i, _)| *i == index) {
Some(slot) => slot.1 = value,
None => self.entries.push((index, value)),
}
}
pub fn iter(&self) -> impl Iterator<Item = (u8, &MetaValue)> {
self.entries.iter().map(|(i, v)| (*i, v))
}
#[must_use]
pub fn len(&self) -> usize {
self.entries.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
#[must_use]
pub fn byte(&self, index: u8) -> Option<i8> {
match self.get(index)? {
MetaValue::Byte(b) => Some(*b),
_ => None,
}
}
#[must_use]
pub fn float(&self, index: u8) -> Option<f32> {
match self.get(index)? {
MetaValue::Float(f) => Some(*f),
_ => None,
}
}
#[must_use]
pub fn item(&self, index: u8) -> Option<Option<&Item>> {
match self.get(index)? {
MetaValue::Item(i) => Some(i.as_ref()),
_ => None,
}
}
pub(crate) fn clear(&mut self) {
self.entries.clear();
}
pub(crate) fn entries_mut(&mut self) -> impl Iterator<Item = &mut (u8, MetaValue)> {
self.entries.iter_mut()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub(crate) enum Era {
#[default]
V1_8,
V1_12,
V1_16,
}
const ITEM_1_16: u16 = 37;
const XP_ORB_1_16: u16 = 24;
const PAINTING_1_16: u16 = 55;
pub const EQUIPMENT_SLOTS: usize = 6;
#[derive(Clone, Debug)]
pub struct Entity {
pub(crate) id: i32,
pub(crate) uuid: Uuid,
pub(crate) ty: EntityType,
pub(crate) living: bool,
pub(crate) era: Era,
pub(crate) sizer: Option<&'static ghast_data::VanillaEntitySize>,
pub(crate) pos: [f64; 3],
pub(crate) fixed: [i32; 3],
pub(crate) traced: [i64; 3],
pub(crate) yaw: f32,
pub(crate) pitch: f32,
pub(crate) head_yaw: f32,
pub(crate) velocity: [f64; 3],
pub(crate) on_ground: bool,
pub(crate) data: i32,
pub(crate) vehicle: Option<i32>,
pub(crate) passengers: Vec<i32>,
pub(crate) leash_holder: Option<i32>,
pub(crate) meta: Metadata,
pub(crate) equipment: Option<Box<[Option<Item>; EQUIPMENT_SLOTS]>>,
pub(crate) effects: Vec<Effect>,
}
impl Entity {
pub(crate) fn new(id: i32, ty: EntityType) -> Self {
Entity {
id,
uuid: Uuid::nil(),
ty,
living: matches!(ty, EntityType::Player | EntityType::LegacyMob(_)),
era: Era::V1_8,
sizer: None,
pos: [0.0; 3],
fixed: [0; 3],
traced: [0; 3],
yaw: 0.0,
pitch: 0.0,
head_yaw: 0.0,
velocity: [0.0; 3],
on_ground: false,
data: 0,
vehicle: None,
passengers: Vec::new(),
leash_holder: None,
meta: Metadata::default(),
equipment: None,
effects: Vec::new(),
}
}
#[must_use]
pub fn id(&self) -> i32 {
self.id
}
#[must_use]
pub fn uuid(&self) -> Uuid {
self.uuid
}
#[must_use]
pub fn entity_type(&self) -> EntityType {
self.ty
}
#[must_use]
pub fn class(&self) -> EntityClass {
match self.ty {
EntityType::Player => EntityClass::Player,
EntityType::LegacyObject(2) | EntityType::Registry(ITEM_1_16) => EntityClass::Item,
EntityType::ExperienceOrb | EntityType::Registry(XP_ORB_1_16) => {
EntityClass::ExperienceOrb
}
EntityType::Painting | EntityType::Registry(PAINTING_1_16) => EntityClass::Painting,
_ if self.living => EntityClass::Mob,
_ => EntityClass::Object,
}
}
#[must_use]
pub fn item(&self) -> Option<&Item> {
let index = match self.ty {
EntityType::LegacyObject(2) if self.era == Era::V1_12 => 6,
EntityType::LegacyObject(2) => 10,
EntityType::Registry(ITEM_1_16) => 7,
_ => return None,
};
self.meta.item(index).flatten()
}
#[must_use]
pub fn dimensions(&self) -> Option<ghast_data::EntityDimensions> {
let vs = self.sizer?;
match self.era {
Era::V1_8 => crate::v1_8::dimensions(self, vs),
Era::V1_12 => crate::v1_12::dimensions(self, vs),
Era::V1_16 => crate::v1_16_5::dimensions(self, vs),
}
}
#[must_use]
pub fn size(&self) -> Option<(f32, f32)> {
self.dimensions().map(|d| (d.width, d.height))
}
#[must_use]
pub fn bounding_box(&self) -> Option<[f64; 6]> {
let [x, y, z] = self.pos;
let b = self.dimensions()?.bounding_box(x, y, z);
Some([b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z])
}
#[must_use]
pub fn is_player(&self) -> bool {
self.ty == EntityType::Player
}
#[must_use]
pub fn position(&self) -> [f64; 3] {
self.pos
}
#[must_use]
pub fn fixed_position(&self) -> [i32; 3] {
self.fixed
}
#[must_use]
pub fn yaw(&self) -> f32 {
self.yaw
}
#[must_use]
pub fn pitch(&self) -> f32 {
self.pitch
}
#[must_use]
pub fn head_yaw(&self) -> f32 {
self.head_yaw
}
#[must_use]
pub fn velocity(&self) -> [f64; 3] {
self.velocity
}
#[must_use]
pub fn on_ground(&self) -> bool {
self.on_ground
}
#[must_use]
pub fn data(&self) -> i32 {
self.data
}
#[must_use]
pub fn vehicle(&self) -> Option<i32> {
self.vehicle
}
#[must_use]
pub fn passengers(&self) -> &[i32] {
&self.passengers
}
#[must_use]
pub fn leash_holder(&self) -> Option<i32> {
self.leash_holder
}
#[must_use]
pub fn metadata(&self) -> &Metadata {
&self.meta
}
#[must_use]
pub fn equipment(&self, slot: usize) -> Option<&Item> {
self.equipment.as_ref()?.get(slot)?.as_ref()
}
#[must_use]
pub fn effects(&self) -> &[Effect] {
&self.effects
}
#[must_use]
pub fn effect(&self, id: u8) -> Option<&Effect> {
self.effects.iter().find(|e| e.id == id)
}
}