use crate::hands::{Interact, InteractEffect as Fx};
#[cfg(any(feature = "v26_1", feature = "v26_2", feature = "v26_3"))]
pub(crate) mod modern;
use ghast_entity::Entities;
#[cfg(any(feature = "v1_8", feature = "v1_12_2", feature = "v1_16_5"))]
use ghast_entity::{Entity, EntityType};
pub(crate) fn kept_types(protocol: i32) -> &'static [u16] {
#[cfg(any(feature = "v26_1", feature = "v26_2", feature = "v26_3"))]
return modern::kept_types(protocol);
#[cfg(not(any(feature = "v26_1", feature = "v26_2", feature = "v26_3")))]
{
let _ = protocol;
&[]
}
}
#[allow(clippy::too_many_arguments)]
#[cold]
#[inline(never)]
pub(crate) fn outcome(
ents: &Entities,
protocol: i32,
id: i32,
name: &str,
spectator: bool,
sneaking: bool,
creative: bool,
items: [Option<(u16, i16)>; 2],
named: [bool; 2],
) -> [(Interact, Fx); 2] {
let Some(e) = ents.get(id) else {
return [(Interact::NotHandled, Fx::None); 2];
};
#[cfg(any(feature = "v26_1", feature = "v26_2", feature = "v26_3"))]
if let Some(t) = modern::table(protocol) {
let held = |k: usize| {
items[k].map(|(id, _)| modern::Held {
id,
named: named[k],
})
};
return modern::outcome(
t,
ents,
e,
name,
spectator,
sneaking,
creative,
[held(0), held(1)],
);
}
let _ = (name, named);
let one = |held: Option<(u16, i16)>| {
let item = held.map(|h| h.0);
let (kind, fx) = match protocol {
#[cfg(feature = "v1_8")]
47 => mob_1_8(e, spectator, held).unwrap_or((legacy_1_8(ents, e, spectator), Fx::None)),
#[cfg(feature = "v1_12_2")]
335..=340 => mob_1_12(e, spectator, held)
.unwrap_or((legacy_1_12(e, spectator, sneaking, item), Fx::None)),
#[cfg(feature = "v1_16_5")]
735..=754 => mob_1_16(e, spectator, item)
.unwrap_or((v1_16_5(e, spectator, sneaking, item), Fx::None)),
_ => {
let _ = (e, spectator, sneaking, item, held);
(Interact::NotHandled, Fx::None)
}
};
(kind, if creative { Fx::None } else { fx })
};
[one(items[0]), one(items[1])]
}
#[cfg(any(feature = "v1_8", feature = "v1_12_2"))]
#[allow(dead_code)]
mod legacy_ids {
pub(super) const PIG: u8 = 90;
pub(super) const SHEEP: u8 = 91;
pub(super) const COW: u8 = 92;
pub(super) const WOLF: u8 = 95;
pub(super) const VILLAGER: u8 = 120;
pub(super) const LEAD: u16 = 420;
pub(super) const SADDLE: u16 = 329;
pub(super) const SHEARS: u16 = 359;
pub(super) const DYE: u16 = 351;
pub(super) const WHEAT: u16 = 296;
pub(super) const BUCKET: u16 = 325;
pub(super) const NAME_TAG: u16 = 421;
pub(super) const COOKED_BEEF: u16 = 364;
pub(super) const BONE: u16 = 352;
pub(super) const SPAWN_EGG: u16 = 383;
}
#[cfg(any(feature = "v1_8", feature = "v1_12_2", feature = "v1_16_5"))]
#[allow(dead_code)]
#[derive(Clone, Copy, Default)]
struct MobState {
baby: bool,
sheared: bool,
color: u8,
saddled: bool,
tamed: bool,
}
#[cfg(any(feature = "v1_8", feature = "v1_12_2", feature = "v1_16_5"))]
fn mob_state(e: &Entity, baby: u8, baby_is_age: bool, mob: u8) -> MobState {
use ghast_entity::MetaValue;
let m = e.metadata();
let flag = |i: u8| match m.get(i) {
Some(MetaValue::Bool(b)) => Some(*b),
Some(MetaValue::Byte(b)) => Some(*b != 0),
_ => None,
};
let byte = m.byte(mob).unwrap_or(0) as u8;
MobState {
baby: if baby_is_age {
m.byte(baby).is_some_and(|a| a < 0)
} else {
flag(baby).unwrap_or(false)
},
sheared: byte & 0x10 != 0,
color: byte & 0x0F,
saddled: flag(mob).unwrap_or(false),
tamed: byte & 0x04 != 0,
}
}
#[cfg(feature = "v1_8")]
fn mob_1_8(e: &Entity, spectator: bool, held: Option<(u16, i16)>) -> Option<(Interact, Fx)> {
use legacy_ids::*;
let EntityType::LegacyMob(m) = e.entity_type() else {
return None;
};
if spectator {
return None;
}
let st = mob_state(e, 12, true, 16);
let (item, damage) = held.unwrap_or((0, 0));
Some(match (m, item) {
(PIG, LEAD) if !st.baby => (Interact::Handled, Fx::Take),
(PIG, SADDLE) if st.saddled => (Interact::Handled, Fx::None),
(PIG, SADDLE) if !st.baby => (Interact::Handled, Fx::Take),
(SHEEP, SHEARS) if !st.sheared && !st.baby => (Interact::NotHandled, Fx::Damage),
(SHEEP, SHEARS) => (Interact::NotHandled, Fx::None),
(SHEEP, DYE) if i32::from(st.color) != 15 - i32::from(damage) => {
(Interact::Handled, Fx::Take)
}
(SHEEP, DYE) => (Interact::Handled, Fx::None),
(COW | SHEEP, WHEAT) | (WOLF, COOKED_BEEF) => (Interact::Handled, Fx::Take),
(COW, BUCKET) if !st.baby => (Interact::Handled, Fx::IntoMilk),
(WOLF, BONE) if !st.tamed => (Interact::Handled, Fx::Take),
(WOLF, BONE) => (Interact::NotHandled, Fx::None),
(VILLAGER, i) if i != SPAWN_EGG => (Interact::Handled, Fx::None),
_ => return None,
})
}
#[cfg(feature = "v1_12_2")]
fn mob_1_12(e: &Entity, spectator: bool, held: Option<(u16, i16)>) -> Option<(Interact, Fx)> {
use legacy_ids::*;
let EntityType::LegacyMob(m) = e.entity_type() else {
return None;
};
if spectator {
return None;
}
let st = mob_state(e, 12, false, 13);
let (item, damage) = held.unwrap_or((0, 0));
Some(match (m, item) {
(PIG, LEAD) if !st.baby => (Interact::Handled, Fx::Take),
(PIG, SADDLE) if st.saddled => (Interact::Handled, Fx::None),
(PIG, SADDLE) if !st.baby => (Interact::Handled, Fx::Take),
(PIG, NAME_TAG) => (Interact::Handled, Fx::None),
(SHEEP, SHEARS) if !st.sheared && !st.baby => (Interact::NotHandled, Fx::Damage),
(SHEEP, SHEARS) => (Interact::NotHandled, Fx::None),
(SHEEP, DYE) if i32::from(st.color) != 15 - i32::from(damage) => {
(Interact::Handled, Fx::Take)
}
(SHEEP, DYE) => (Interact::Handled, Fx::None),
(COW | SHEEP, WHEAT) if st.baby => (Interact::Handled, Fx::Take),
(COW, BUCKET) if !st.baby => (Interact::Handled, Fx::IntoMilk),
(WOLF, BONE) if !st.tamed => (Interact::Handled, Fx::Take),
(VILLAGER, i) if i != SPAWN_EGG => (Interact::Handled, Fx::None),
_ => return None,
})
}
#[cfg(feature = "v1_16_5")]
fn mob_1_16(e: &Entity, spectator: bool, item: Option<u16>) -> Option<(Interact, Fx)> {
if spectator || e.class() != ghast_entity::EntityClass::Mob {
return None;
}
let mob = ghast_entity::v1_16_5::info(e.entity_type())?.name;
let name = item
.and_then(ghast_data::v1_16_5::Item::from_id)
.map_or("", |i| i.info().name);
let st = mob_state(e, 15, false, 16);
Some(match (mob, name) {
("pig", "lead") if !st.baby => (Interact::HandledSwing, Fx::Take),
("pig", "saddle") => (Interact::HandledSwing, Fx::None),
("sheep", "red_dye") if st.color == 14 => return None,
("sheep", "red_dye") => (Interact::HandledSwing, Fx::None),
("cow" | "sheep", "wheat") if st.baby => (Interact::HandledSwing, Fx::Take),
("sheep", "shears") | ("cow" | "sheep", "wheat") | ("wolf", "bone") => {
(Interact::Handled, Fx::None)
}
("cow", "bucket") if !st.baby => (Interact::HandledSwing, Fx::IntoMilk),
("villager", n) if !n.ends_with("spawn_egg") => (Interact::HandledSwing, Fx::None),
_ => return None,
})
}
#[cfg(feature = "v1_8")]
fn legacy_1_8(ents: &Entities, e: &Entity, spectator: bool) -> Interact {
let EntityType::LegacyObject(t) = e.entity_type() else {
return Interact::NotHandled;
};
match t {
78 => Interact::AtHandled,
_ if spectator => Interact::NotHandled,
1 | 71 | 77 => Interact::Handled,
10 => match e.data() {
0 => {
let other = e
.passengers()
.first()
.copied()
.filter(|r| Some(*r) != ents.self_id());
if other.is_some_and(|r| !ents.get(r).is_some_and(Entity::is_player)) {
Interact::NotHandled
} else {
Interact::Handled
}
}
1 | 2 | 5 => Interact::Handled,
_ => Interact::NotHandled,
},
_ => Interact::NotHandled,
}
}
#[cfg(feature = "v1_12_2")]
const NAME_TAG_1_12: u16 = 421;
#[cfg(feature = "v1_12_2")]
fn legacy_1_12(e: &Entity, spectator: bool, sneaking: bool, item: Option<u16>) -> Interact {
let EntityType::LegacyObject(t) = e.entity_type() else {
return Interact::NotHandled;
};
if spectator {
return Interact::NotHandled;
}
match t {
78 if item == Some(NAME_TAG_1_12) => Interact::NotHandled,
78 => Interact::AtHandled,
1 => handled_unless(sneaking),
10 => match e.data() {
0 => handled_unless(sneaking),
1 | 2 | 5 => Interact::Handled,
_ => Interact::NotHandled,
},
71 | 77 => Interact::Handled,
_ => Interact::NotHandled,
}
}
#[cfg(feature = "v1_12_2")]
fn handled_unless(sneaking: bool) -> Interact {
if sneaking {
Interact::NotHandled
} else {
Interact::Handled
}
}
#[cfg(feature = "v1_16_5")]
fn v1_16_5(e: &Entity, spectator: bool, sneaking: bool, item: Option<u16>) -> Interact {
if spectator || !matches!(e.entity_type(), EntityType::Registry(_)) {
return Interact::NotHandled;
}
let Some(info) = ghast_entity::v1_16_5::info(e.entity_type()) else {
return Interact::NotHandled;
};
let name_of = |id: u16| ghast_data::v1_16_5::Item::from_id(id).map(|i| i.info().name);
match info.name {
"armor_stand" if item.and_then(name_of) == Some("name_tag") => Interact::NotHandled,
"armor_stand" => Interact::AtHandled,
"boat" if sneaking => Interact::NotHandled,
"boat" => Interact::HandledSwing,
"minecart" if sneaking || !e.passengers().is_empty() => Interact::NotHandled,
"minecart" | "chest_minecart" | "hopper_minecart" | "furnace_minecart" | "leash_knot" => {
Interact::HandledSwing
}
"item_frame" if item.is_none() => Interact::NotHandled,
"item_frame" => Interact::HandledSwing,
_ => Interact::NotHandled,
}
}