use ghast_data::v1_16_5::BlockState;
use ghast_data::v1_16_5::place::{Click, Held, Mode, Outcome, Use, right_click};
use super::predict::{PlaceQuery, PlaceRules, PlaceSent};
use crate::common::GameMode;
fn mode(m: GameMode) -> Mode {
match m {
GameMode::Creative => Mode::Creative,
GameMode::Adventure => Mode::Adventure,
GameMode::Spectator => Mode::Spectator,
_ => Mode::Survival,
}
}
fn held(s: Option<&ghast_inventory::Stack>) -> Option<Held> {
let s = s?;
Some(Held {
id: u16::try_from(s.id()).ok()?,
count: s.count(),
nbt: s.nbt().is_some(),
})
}
#[allow(clippy::too_many_arguments)]
fn outcome(
world: &ghast_world::v1_16_5::World,
player: &ghast_physics::math::Aabb,
pos: ghast_protocol::BlockPos,
face: crate::common::Face,
cursor: [f32; 3],
stack: Option<&ghast_inventory::Stack>,
sneaking: bool,
any_item: bool,
game_mode: GameMode,
) -> Option<Outcome> {
if !super::predict::border_contains(&ghast_physics::BlockView::world_border(world), pos) {
return None;
}
let click = Click {
pos: [pos.x, pos.y, pos.z],
face: face.id() as u8,
cursor,
held: held(stack),
any_item,
sneaking,
mode: mode(game_mode),
player: ghast_data::Aabb {
min_x: player.min_x,
min_y: player.min_y,
min_z: player.min_z,
max_x: player.max_x,
max_y: player.max_y,
max_z: player.max_z,
},
};
Some(right_click(&click, &|[x, y, z]| {
world.block_state(x, y, z).map_or(0, BlockState::id)
}))
}
pub(crate) fn rules(
world: &ghast_world::v1_16_5::World,
player: &ghast_physics::math::Aabb,
q: &PlaceQuery<'_>,
) -> PlaceRules {
let air_use_unknown = q.held.is_some_and(|s| {
u16::try_from(s.id()).is_ok_and(ghast_data::v1_16_5::place::air_use_unknown)
});
let Some(o) = outcome(
world,
player,
q.pos,
q.face,
q.cursor,
q.held,
q.sneaking,
q.any_item,
q.game_mode,
) else {
return PlaceRules {
air_use_unknown,
block_packet: Some(false),
used: Some(false),
no_swing: false,
ends: true,
starts_use: false,
};
};
let id = q.held.and_then(|s| u16::try_from(s.id()).ok());
let eats = o.result == Some(Use::Fail)
&& id.is_some_and(|i| {
ghast_data::v1_16_5::place::block_item(i) != 0
&& ghast_data::v1_16_5::item_use::is_food(i)
&& q.food >= 0
&& (q.food < 20 || ghast_data::v1_16_5::item_use::always_edible(i))
});
let (used, no_swing, ends) = match o.result {
None => (None, false, false),
_ if eats => (Some(true), true, false),
Some(Use::Success) => (Some(true), false, false),
Some(Use::Consume) => (Some(true), true, false),
Some(Use::Fail) => (Some(false), false, true),
Some(Use::Pass) => (Some(false), false, false),
};
PlaceRules {
air_use_unknown,
block_packet: Some(true),
used,
no_swing,
ends,
starts_use: eats,
}
}
pub(crate) fn sent(
world: &mut ghast_world::v1_16_5::World,
player: &ghast_physics::math::Aabb,
p: &PlaceSent<'_>,
) -> Option<bool> {
let o = outcome(
world,
player,
p.pos,
p.face,
p.cursor,
p.held,
p.sneaking,
p.any_item,
p.game_mode,
)?;
let placed = o.placed?;
for ([x, y, z], id) in placed {
if let Some(s) = BlockState::from_id(id) {
world.set_block_state(x, y, z, s);
}
}
let before = p.held.map_or(0, ghast_inventory::Stack::count);
Some(o.count_after.is_some_and(|n| n < before))
}