use ghast_data::v1_8::BlockState;
use ghast_data::v1_8::place::{Click, Held, Mode, Outcome, 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()?,
damage: s.damage(),
count: s.count(),
nbt: s.nbt().is_some(),
})
}
#[allow(clippy::too_many_arguments)]
fn outcome(
world: &ghast_world::v1_8::World,
player: &ghast_physics::math::Aabb,
pos: ghast_protocol::BlockPos,
face: crate::common::Face,
cursor: [f32; 3],
stack: Option<&ghast_inventory::Stack>,
sneaking: bool,
game_mode: GameMode,
yaw: f32,
) -> Outcome {
if !super::predict::border_contains(&ghast_physics::BlockView::world_border(world), pos) {
return Outcome {
block_packet: Some(false),
used: Some(false),
placed: Some(Vec::new()),
count_after: stack.map(ghast_inventory::Stack::count),
};
}
let click = Click {
pos: [pos.x, pos.y, pos.z],
face: face.id() as u8,
cursor,
held: held(stack),
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,
},
yaw,
};
right_click(&click, &|[x, y, z]| {
world.block_state(x, y, z).map_or(0, BlockState::raw)
})
}
pub(crate) fn rules(
world: &ghast_world::v1_8::World,
player: &ghast_physics::math::Aabb,
q: &PlaceQuery<'_>,
) -> PlaceRules {
let o = outcome(
world,
player,
q.pos,
q.face,
q.cursor,
q.held,
q.sneaking,
q.game_mode,
q.yaw,
);
PlaceRules {
block_packet: o.block_packet,
used: o.used,
air_use_unknown: false,
no_swing: false,
ends: false,
}
}
pub(crate) fn sent(
world: &mut ghast_world::v1_8::World,
player: &ghast_physics::math::Aabb,
p: &PlaceSent<'_>,
) -> bool {
let o = outcome(
world,
player,
p.pos,
p.face,
p.cursor,
p.held,
p.sneaking,
p.game_mode,
p.yaw,
);
let Some(placed) = o.placed else {
return false;
};
for ([x, y, z], raw) in placed {
world.set_block_state(x, y, z, BlockState::from_raw_vanilla(raw));
}
let before = p.held.map_or(0, ghast_inventory::Stack::count);
o.count_after.is_some_and(|n| n < before)
}