ghast-client 0.5.6

Headless Minecraft Java Edition client for the ghast library: connection, login, fixed 50 ms tick loop and a version-agnostic bot API (1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3)
Documentation
//! Modern versions: item uses that move the player (use effects, the riptide release), the
//! inventory-derived inputs of gliding and rockets, and the rain that wets the player.

use super::*;

impl ModernSim {
    /// The `use_effects` of the stack in use (`LivingEntity.getUseItem`), set by the engine
    /// each tick from `super::inv::item_use`; the component default without one.
    pub(super) fn use_effects(&self) -> modern::UseEffects {
        self.use_effects.map_or_else(
            modern::UseEffects::default,
            |(can_sprint, speed_multiplier)| modern::UseEffects {
                can_sprint,
                speed_multiplier,
            },
        )
    }

    #[cfg(all(test, feature = "v1_8", feature = "v1_16_5"))]
    pub(crate) fn use_effects_now(&self) -> Option<(bool, f32)> {
        self.use_effects
    }

    /// The inventory-derived inputs of `ext` (see [`Sim::pre_tick`]): kept out of line, they
    /// matter only on jump presses and while rockets are attached.
    #[cold]
    #[inline(never)]
    pub(super) fn ext_inputs(&mut self, inv: &ghast_inventory::Inventory, controls: Controls) {
        // The glider is read only when a glide starts (a jump press in the air).
        if controls.jump {
            self.ext.glider = glider_equipped(inv);
            self.ext_active |= self.ext.glider;
        }
        // Entity.getHandHoldingItemAngle(FIREWORK_ROCKET): the main arm, or the other one when
        // only the off hand holds a rocket. Read only while rockets are attached.
        if self.ext.rocket_count() > 0 {
            let rocket = |s: Option<&ghast_inventory::Stack>| {
                s.is_some_and(|s| super::inv::item_name(s.id()) == Some("firework_rocket"))
            };
            let off_only = rocket(inv.offhand()) && !rocket(inv.held());
            self.ext.rocket_arm_left = self.own.main_arm_left != off_only;
        }
    }

    /// `isInWaterOrRain` with the level's rain (biome precipitation is not known: every biome
    /// counts as rainy).
    /// `Entity.isInWaterOrRain`. `Level.isRainingAt` per position (feet and head): rain level
    /// above 0.2, the biome rains there (`Biome.getPrecipitationAt`; an unknown biome counts as
    /// raining: biomes are kept only once a riptide trident was held, or with
    /// `WorldConfig::biomes`), and nothing above blocks motion or holds fluid (the sky check
    /// of the physics; sky light itself is not modelled).
    pub(super) fn wet(&self) -> bool {
        let p = &self.player;
        let (bx, bz) = (p.pos[0].floor() as i32, p.pos[2].floor() as i32);
        let view = self.world.view(bx, bz);
        let raining = self.rain_level > 0.2;
        if p.touching_water || !raining {
            return modern::in_water_or_rain::<Rules, _>(p, &view, raining);
        }
        let rains = |y: f64| {
            self.world
                .precipitation_at(bx, y.floor() as i32, bz)
                .is_none_or(|r| r == ghast_world::climate::Precipitation::Rain)
        };
        // in_water_or_rain looks at the feet and the head block: keep only the raining ones.
        let mut q = *p;
        match (rains(p.pos[1]), rains(p.bb.max_y)) {
            (false, false) => return false,
            (true, true) => {}
            (true, false) => q.bb.max_y = q.pos[1],
            (false, true) => q.pos[1] = q.bb.max_y,
        }
        modern::in_water_or_rain::<Rules, _>(&q, &view, true)
    }

    /// `releaseUsingItem` → `TridentItem.releaseUsing` with riptide: the launch, before the tick,
    /// along the tick's look (the mouse turned before the key handling).
    pub(super) fn release_use(
        &mut self,
        inv: &ghast_inventory::Inventory,
        (off, id, ticks): (bool, u16, u32),
        look: (f32, f32),
    ) {
        let stack = if off { inv.offhand() } else { inv.held() };
        let Some(s) = stack.filter(|s| i64::from(s.id()) == i64::from(id)) else {
            return;
        };
        let strength = self.riptide[usize::from(off)];
        if ticks < 10 || strength <= 0.0 || super::inv::next_damage_will_break(s) {
            return;
        }
        let (yaw, pitch) = (self.player.yaw, self.player.pitch);
        self.player.yaw = look.0;
        self.player.pitch = look.1;
        if !self.wet() {
            self.player.yaw = yaw;
            self.player.pitch = pitch;
            return;
        }
        let p = &mut self.player;
        let view = self
            .world
            .view(p.pos[0].floor() as i32, p.pos[2].floor() as i32);
        modern::riptide::<Rules, _>(p, &mut self.ext, &view, strength);
        self.ext_active = true;
    }
}