Cactus destroys dropped items that touch it

This commit is contained in:
Kacpherek
2026-10-26 14:28:06 +01:00
parent b78daab11c
commit 54fe73854e
3 changed files with 62 additions and 0 deletions
+9
View File
@@ -95,6 +95,15 @@ impl ItemEntity {
self.health -= fire_damage as i32;
}
}
// CactusBlock.entityInside: `entity.hurt(cactus, 1.0)`. An item has no
// hurt cooldown, so it loses 1 health every tick it touches a cactus
// (5 health: gone in 5 ticks). Fire resistance does not help;
// `ItemStack.canBeHurtBy` only skips it for a damage_resistant stack.
if crate::motion::environment::cactus_contact(world, registry, self.bounds())
&& !items.resists_cactus(&self.stack)
{
self.health -= 1;
}
ItemEnvironmentTick {
alive: self.health > 0,
lava_hurt,
+30
View File
@@ -392,6 +392,36 @@ impl Items {
self.get(&stack.item).is_some_and(|i| i.fire_resistant)
}
/// `ItemStack.canBeHurtBy` for the cactus damage type: a `damage_resistant`
/// component naming `minecraft:cactus`, or a damage type tag that holds it.
/// (`#minecraft:is_fire`, as on netherite gear, does not.)
pub fn resists_cactus(&self, stack: &ItemStack) -> bool {
const CACTUS_TAGS: [&str; 5] = [
"#minecraft:bypasses_shield",
"#minecraft:no_knockback",
"#minecraft:panic_environmental_causes",
"#minecraft:panic_causes",
"#minecraft:sulfur_cube_with_block_immune_to",
];
let Some((_, v)) = stack.data().and_then(|data| {
data.other
.iter()
.find(|(key, _)| key == "minecraft:damage_resistant")
}) else {
return false;
};
match v.get("types") {
Some(horizon_world::nbt::Tag::String(id)) => id
.to_string()
.is_ok_and(|id| id == "minecraft:cactus" || CACTUS_TAGS.contains(&id.as_str())),
Some(horizon_world::nbt::Tag::List(ids)) => ids.iter().any(|t| {
matches!(t, horizon_world::nbt::Tag::String(id)
if id.to_string().is_ok_and(|id| id == "minecraft:cactus"))
}),
_ => false,
}
}
pub fn load(root: &Path, registry: &BlockRegistry, facts: &BlockFacts) -> Result<Self> {
let mut block_for_item: HashMap<&str, BlockStateId> = HashMap::new();
for (i, s) in facts.states.iter().enumerate() {
@@ -161,6 +161,29 @@ pub(crate) fn fire_contact(
}
damage
}
/// `CactusBlock.entityInside`: the box (deflated by `1.0E-5`, as in
/// `Entity.checkInsideBlocks`) touches a cactus; its inside shape is the
/// whole block.
pub(crate) fn cactus_contact(
world: &World,
registry: &horizon_world::registry::BlockRegistry,
bounds: Aabb,
) -> bool {
let b = bounds.deflate(Vec3::new(1e-5, 1e-5, 1e-5));
for x in b.min_position().x.floor() as i32..=b.max_position().x.floor() as i32 {
for y in b.min_position().y.floor() as i32..=b.max_position().y.floor() as i32 {
for z in b.min_position().z.floor() as i32..=b.max_position().z.floor() as i32 {
if registry
.state(world.block(x, y, z))
.is_some_and(|s| s.name == "minecraft:cactus")
{
return true;
}
}
}
}
false
}
impl LivingFluidWorld for MotionWorld<'_, '_, '_> {
fn in_water(&mut self) -> Result<bool> {
Ok(self.player.fluids.was_touching_water)