Port getStateForPlacement, BlockItem.place and useWithoutItem
PlaceContext resolves the replaced position as BlockPlaceContext does (canBeReplaced per class, nearest looking directions), place_block runs BlockItem.place with standing/wall and double-high items and setPlacedBy, and use_without_item covers doors, trapdoors, gates, buttons, levers, diodes, note blocks, cakes, composters, berries, pots, beds and menus. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -179,6 +179,7 @@ classes! {
|
||||
HorizontalDirectional = "HorizontalDirectionalBlock",
|
||||
HugeMushroom = "HugeMushroomBlock",
|
||||
Infested = "InfestedBlock",
|
||||
InfestedRotatedPillar = "InfestedRotatedPillarBlock",
|
||||
IronBars = "IronBarsBlock",
|
||||
StainedGlassPane = "StainedGlassPaneBlock",
|
||||
Jukebox = "JukeboxBlock",
|
||||
|
||||
@@ -228,10 +228,8 @@ pub enum ItemKind {
|
||||
Block,
|
||||
DoubleHigh,
|
||||
StandingAndWall,
|
||||
Bed,
|
||||
Scaffolding,
|
||||
PlaceOnWater,
|
||||
Sign,
|
||||
HangingSign,
|
||||
SolidBucket,
|
||||
Other,
|
||||
@@ -896,17 +894,23 @@ pub(super) fn load(registry: &BlockRegistry, root: &Path) -> io::Result<Behaviou
|
||||
continue;
|
||||
};
|
||||
let classes: Vec<&str> = binding["class"].as_array().into_iter().flatten().filter_map(Value::as_str).collect();
|
||||
let kind = match classes.first().copied().unwrap_or("BlockItem") {
|
||||
"BlockItem" | "GameMasterBlockItem" => ItemKind::Block,
|
||||
"DoubleHighBlockItem" => ItemKind::DoubleHigh,
|
||||
"StandingAndWallBlockItem" => ItemKind::StandingAndWall,
|
||||
"SignItem" => ItemKind::Sign,
|
||||
"HangingSignItem" => ItemKind::HangingSign,
|
||||
"BedItem" => ItemKind::Bed,
|
||||
"ScaffoldingBlockItem" => ItemKind::Scaffolding,
|
||||
"PlaceOnWaterBlockItem" => ItemKind::PlaceOnWater,
|
||||
"SolidBucketItem" => ItemKind::SolidBucket,
|
||||
_ => ItemKind::Other,
|
||||
let has = |name: &str| classes.contains(&name);
|
||||
let kind = if has("HangingSignItem") {
|
||||
ItemKind::HangingSign
|
||||
} else if has("StandingAndWallBlockItem") {
|
||||
ItemKind::StandingAndWall
|
||||
} else if has("DoubleHighBlockItem") {
|
||||
ItemKind::DoubleHigh
|
||||
} else if has("ScaffoldingBlockItem") {
|
||||
ItemKind::Scaffolding
|
||||
} else if has("PlaceOnWaterBlockItem") {
|
||||
ItemKind::PlaceOnWater
|
||||
} else if has("SolidBucketItem") {
|
||||
ItemKind::SolidBucket
|
||||
} else if has("BlockItem") {
|
||||
ItemKind::Block
|
||||
} else {
|
||||
ItemKind::Other
|
||||
};
|
||||
items.insert(
|
||||
item.clone(),
|
||||
|
||||
@@ -4,7 +4,7 @@ use super::{
|
||||
LevelAccess, Sim,
|
||||
classes::Class,
|
||||
states::BlockId,
|
||||
update::{HORIZONTAL, below, rel},
|
||||
update::{below, rel},
|
||||
};
|
||||
use crate::registry::BlockStateId;
|
||||
use horizon_core::{geometry::Direction, position::BlockPos, random::RandomSource};
|
||||
@@ -449,9 +449,6 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
|
||||
});
|
||||
}
|
||||
}
|
||||
pub(crate) fn horizontal_neighbours(pos: BlockPos) -> [BlockPos; 4] {
|
||||
HORIZONTAL.map(|d| rel(pos, d))
|
||||
}
|
||||
}
|
||||
|
||||
impl super::Behaviours {
|
||||
|
||||
@@ -1,12 +1,420 @@
|
||||
//! `useWithoutItem` interactions.
|
||||
//! `BlockBehaviour.useWithoutItem` (right-click with an empty hand or an item
|
||||
//! that does not handle the block itself).
|
||||
use super::{Behaviours, LevelAccess, LevelEvent, Sim, Ticks, classes::Class, query::axis_of, update::below};
|
||||
use crate::registry::BlockStateId;
|
||||
use horizon_core::{
|
||||
geometry::{Axis, Direction, Vec3},
|
||||
position::BlockPos,
|
||||
random::RandomSource,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// `InteractionResult` as seen by the server.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum InteractionResult {
|
||||
Success,
|
||||
Consume,
|
||||
Pass,
|
||||
}
|
||||
impl InteractionResult {
|
||||
pub fn consumes_action(self) -> bool {
|
||||
!matches!(self, Self::Pass)
|
||||
}
|
||||
}
|
||||
|
||||
/// The interacting player, as far as block behaviour needs it.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct PlayerContext {
|
||||
/// View rotation in degrees.
|
||||
pub yaw: f32,
|
||||
pub pitch: f32,
|
||||
/// `Abilities.mayBuild` (false in adventure mode).
|
||||
pub may_build: bool,
|
||||
/// `Player.canEat(false)` (hunger below max, or creative).
|
||||
pub can_eat: bool,
|
||||
/// `Player.canUseGameMasterBlocks` (creative operator).
|
||||
pub game_master: bool,
|
||||
}
|
||||
|
||||
/// Player-side effects of an interaction, reported through
|
||||
/// [`LevelEvent::Player`]; block changes are already applied.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum PlayerEffect {
|
||||
/// Open the block's menu (crafting table, furnace, chest, sign editor, ...).
|
||||
OpenMenu,
|
||||
/// `CakeBlock.eat`: the player eats a slice (food 2, saturation 0.1).
|
||||
EatCake,
|
||||
/// Give the item to the player (dropping it when the inventory is full).
|
||||
GiveItem(Arc<str>),
|
||||
/// Pop the item at the block (composter bone meal).
|
||||
DropItem(Arc<str>),
|
||||
/// Evaluate the block-interaction loot table and pop the results.
|
||||
InteractLoot(&'static str),
|
||||
/// Try to sleep in the bed whose head is at the event position.
|
||||
Sleep,
|
||||
/// Respawn anchor: set the respawn point (or explode outside the Nether).
|
||||
RespawnAnchor,
|
||||
}
|
||||
|
||||
impl<L: LevelAccess + ?Sized> Sim<'_, L> {
|
||||
fn player_effect(&mut self, pos: BlockPos, state: BlockStateId, effect: PlayerEffect) {
|
||||
self.emit(LevelEvent::Player { pos, state, effect });
|
||||
}
|
||||
|
||||
pub(crate) fn use_without_item(&mut self, pos: BlockPos, face: Direction, hit: Vec3, player: PlayerContext) -> InteractionResult {
|
||||
let b = self.b;
|
||||
let p = b.props;
|
||||
let table = &b.table;
|
||||
let state = self.state(pos);
|
||||
let data = b.data(state);
|
||||
let block = b.block(state);
|
||||
use InteractionResult::*;
|
||||
for class in data.chain.clone() {
|
||||
return match class {
|
||||
Class::Door => {
|
||||
if !data.set_type.as_ref().is_some_and(|s| s.can_open_by_hand) {
|
||||
return Pass;
|
||||
}
|
||||
let next = table.cycle(state, p.open);
|
||||
self.set_block(pos, next, 10);
|
||||
let open = table.bool(next, p.open);
|
||||
self.door_sound(pos, next, open);
|
||||
self.game_event(pos, if open { "minecraft:block_open" } else { "minecraft:block_close" });
|
||||
Success
|
||||
}
|
||||
Class::TrapDoor => {
|
||||
if !data.set_type.as_ref().is_some_and(|s| s.can_open_by_hand) {
|
||||
return Pass;
|
||||
}
|
||||
let next = table.cycle(state, p.open);
|
||||
self.set_block(pos, next, 2);
|
||||
if b.is_waterlogged(next) {
|
||||
self.schedule_water_tick(pos);
|
||||
}
|
||||
self.trapdoor_sound(pos, next, table.bool(next, p.open));
|
||||
Success
|
||||
}
|
||||
Class::FenceGate => {
|
||||
let next = if table.bool(state, p.open) {
|
||||
table.set_bool(state, p.open, false)
|
||||
} else {
|
||||
let direction = super::place::from_y_rot(f64::from(player.yaw));
|
||||
let mut next = state;
|
||||
if b.facing(state) == direction.opposite() {
|
||||
next = table.set_dir(next, p.facing, direction);
|
||||
}
|
||||
table.set_bool(next, p.open, true)
|
||||
};
|
||||
self.set_block(pos, next, 10);
|
||||
let opens = table.bool(next, p.open);
|
||||
let sound = if opens { data.gate_open.clone() } else { data.gate_close.clone() };
|
||||
let pitch = self.random().next_float() * 0.1 + 0.9;
|
||||
if let Some(sound) = sound {
|
||||
self.sound(pos, &sound, 1.0, pitch);
|
||||
}
|
||||
self.game_event(pos, if opens { "minecraft:block_open" } else { "minecraft:block_close" });
|
||||
Success
|
||||
}
|
||||
Class::Button => {
|
||||
if table.bool(state, p.powered) {
|
||||
return Consume;
|
||||
}
|
||||
self.button_press(pos, state);
|
||||
Success
|
||||
}
|
||||
Class::Lever => {
|
||||
self.lever_pull(pos, state);
|
||||
Success
|
||||
}
|
||||
Class::Repeater => {
|
||||
if !player.may_build {
|
||||
return Pass;
|
||||
}
|
||||
self.set_block_and_update(pos, table.cycle(state, p.delay));
|
||||
Success
|
||||
}
|
||||
Class::Comparator => {
|
||||
if !player.may_build {
|
||||
return Pass;
|
||||
}
|
||||
let next = table.cycle(state, p.mode);
|
||||
let pitch = if table.text(next, p.mode) == "subtract" { 0.55 } else { 0.5 };
|
||||
self.emit(LevelEvent::Sound {
|
||||
pos,
|
||||
sound: "minecraft:block.comparator.click".into(),
|
||||
volume: 0.3,
|
||||
pitch,
|
||||
});
|
||||
self.set_block(pos, next, 2);
|
||||
if b.is_block(self.state(pos), block) {
|
||||
self.comparator_refresh(pos, next);
|
||||
}
|
||||
Success
|
||||
}
|
||||
Class::Note => {
|
||||
let next = table.cycle(state, p.note);
|
||||
self.set_block_and_update(pos, next);
|
||||
self.note_play(pos, next);
|
||||
Success
|
||||
}
|
||||
Class::RedstoneWire => self.wire_use(pos, state, player),
|
||||
Class::DaylightDetector => {
|
||||
if !player.may_build {
|
||||
return Pass;
|
||||
}
|
||||
let next = table.cycle(state, p.inverted);
|
||||
self.set_block(pos, next, 2);
|
||||
self.game_event(pos, "minecraft:block_change");
|
||||
Success
|
||||
}
|
||||
Class::Cake => {
|
||||
if !player.can_eat {
|
||||
return Pass;
|
||||
}
|
||||
self.player_effect(pos, state, PlayerEffect::EatCake);
|
||||
self.game_event(pos, "minecraft:eat");
|
||||
let bites = table.int(state, p.bites);
|
||||
if bites < 6 {
|
||||
self.set_block_and_update(pos, table.set_int(state, p.bites, bites + 1));
|
||||
} else {
|
||||
self.remove_block(pos, false);
|
||||
self.game_event(pos, "minecraft:block_destroy");
|
||||
}
|
||||
Success
|
||||
}
|
||||
Class::CandleCake => {
|
||||
if !player.can_eat {
|
||||
return Pass;
|
||||
}
|
||||
self.player_effect(pos, state, PlayerEffect::EatCake);
|
||||
self.game_event(pos, "minecraft:eat");
|
||||
let cake = b.default_of(b.named.cake);
|
||||
self.set_block_and_update(pos, table.set_int(cake, p.bites, 1));
|
||||
self.drop_resources(state, pos);
|
||||
Success
|
||||
}
|
||||
Class::Composter => {
|
||||
if table.int(state, p.level) != 8 {
|
||||
return Pass;
|
||||
}
|
||||
self.player_effect(pos, state, PlayerEffect::DropItem("minecraft:bone_meal".into()));
|
||||
self.set_block_and_update(pos, table.set_int(state, p.level, 0));
|
||||
self.game_event(pos, "minecraft:block_change");
|
||||
self.emit(LevelEvent::Sound {
|
||||
pos,
|
||||
sound: "minecraft:block.composter.empty".into(),
|
||||
volume: 1.0,
|
||||
pitch: 1.0,
|
||||
});
|
||||
Success
|
||||
}
|
||||
Class::CaveVines | Class::CaveVinesPlant => {
|
||||
if !table.bool(state, p.berries) {
|
||||
return Pass;
|
||||
}
|
||||
self.player_effect(pos, state, PlayerEffect::InteractLoot("minecraft:harvest/cave_vine"));
|
||||
let pitch = self.random().next_float() * 0.4 + 0.8;
|
||||
self.emit(LevelEvent::Sound {
|
||||
pos,
|
||||
sound: "minecraft:block.cave_vines.pick_berries".into(),
|
||||
volume: 1.0,
|
||||
pitch,
|
||||
});
|
||||
let next = table.set_bool(state, p.berries, false);
|
||||
self.set_block(pos, next, 2);
|
||||
self.game_event(pos, "minecraft:block_change");
|
||||
Success
|
||||
}
|
||||
Class::SweetBerryBush => {
|
||||
if table.int(state, p.age) <= 1 {
|
||||
return Pass;
|
||||
}
|
||||
self.player_effect(pos, state, PlayerEffect::InteractLoot("minecraft:harvest/sweet_berry_bush"));
|
||||
let pitch = 0.8 + self.random().next_float() * 0.4;
|
||||
self.emit(LevelEvent::Sound {
|
||||
pos,
|
||||
sound: "minecraft:block.sweet_berry_bush.pick_berries".into(),
|
||||
volume: 1.0,
|
||||
pitch,
|
||||
});
|
||||
self.set_block(pos, table.set_int(state, p.age, 1), 2);
|
||||
self.game_event(pos, "minecraft:block_change");
|
||||
Success
|
||||
}
|
||||
Class::FlowerPot => {
|
||||
let Some(potted) = data.link else {
|
||||
return Consume;
|
||||
};
|
||||
if b.is_air(b.default_of(potted)) {
|
||||
return Consume;
|
||||
}
|
||||
let item = b.block_data(potted).item.clone();
|
||||
self.player_effect(pos, state, PlayerEffect::GiveItem(item));
|
||||
self.set_block_and_update(pos, b.default_of(b.named.flower_pot));
|
||||
self.game_event(pos, "minecraft:block_change");
|
||||
Success
|
||||
}
|
||||
Class::DragonEgg => {
|
||||
self.dragon_egg_teleport(pos, state);
|
||||
Success
|
||||
}
|
||||
Class::AbstractBed => {
|
||||
let part = table.text(state, p.part).to_owned();
|
||||
let facing = b.facing(state);
|
||||
let other_dir = if part == "foot" { facing } else { facing.opposite() };
|
||||
let other = self.state(super::update::rel(pos, other_dir));
|
||||
if b.is_block(other, block) && table.text(other, p.part) != part {
|
||||
let head = if part == "head" { pos } else { super::update::rel(pos, facing) };
|
||||
let head_state = self.state(head);
|
||||
self.player_effect(head, head_state, PlayerEffect::Sleep);
|
||||
Success
|
||||
} else {
|
||||
Consume
|
||||
}
|
||||
}
|
||||
Class::RespawnAnchor => {
|
||||
if table.int(state, p.charges) == 0 {
|
||||
return Pass;
|
||||
}
|
||||
self.player_effect(pos, state, PlayerEffect::RespawnAnchor);
|
||||
Success
|
||||
}
|
||||
Class::Light => {
|
||||
if player.game_master {
|
||||
self.set_block(pos, table.cycle(state, p.level), 2);
|
||||
Success
|
||||
} else {
|
||||
Consume
|
||||
}
|
||||
}
|
||||
Class::Bell => {
|
||||
let click_y = hit.y - f64::from(pos.y);
|
||||
let proper = axis_of(face) != Axis::Y && click_y <= 0.8124 && {
|
||||
let facing_axis = axis_of(b.facing(state));
|
||||
match table.text(state, p.attachment) {
|
||||
"floor" => facing_axis == axis_of(face),
|
||||
"single_wall" | "double_wall" => facing_axis != axis_of(face),
|
||||
"ceiling" => true,
|
||||
_ => false,
|
||||
}
|
||||
};
|
||||
if proper {
|
||||
self.emit(LevelEvent::Sound {
|
||||
pos,
|
||||
sound: "minecraft:block.bell.use".into(),
|
||||
volume: 2.0,
|
||||
pitch: 1.0,
|
||||
});
|
||||
self.game_event(pos, "minecraft:block_change");
|
||||
Success
|
||||
} else {
|
||||
Pass
|
||||
}
|
||||
}
|
||||
Class::AbstractFurnace
|
||||
| Class::Anvil
|
||||
| Class::Barrel
|
||||
| Class::Beacon
|
||||
| Class::BrewingStand
|
||||
| Class::CartographyTable
|
||||
| Class::Chest
|
||||
| Class::Crafter
|
||||
| Class::CraftingTable
|
||||
| Class::Dispenser
|
||||
| Class::EnchantingTable
|
||||
| Class::EnderChest
|
||||
| Class::Grindstone
|
||||
| Class::Hopper
|
||||
| Class::Loom
|
||||
| Class::ShulkerBox
|
||||
| Class::Sign
|
||||
| Class::SmithingTable
|
||||
| Class::Stonecutter
|
||||
| Class::Lectern => {
|
||||
self.player_effect(pos, state, PlayerEffect::OpenMenu);
|
||||
Success
|
||||
}
|
||||
Class::Command | Class::Structure | Class::Jigsaw => {
|
||||
if player.game_master {
|
||||
self.player_effect(pos, state, PlayerEffect::OpenMenu);
|
||||
Success
|
||||
} else {
|
||||
Pass
|
||||
}
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
}
|
||||
let _ = below;
|
||||
Pass
|
||||
}
|
||||
|
||||
/// `RedstoneWireBlock.useWithoutItem`: toggle between dot and cross.
|
||||
fn wire_use(&mut self, pos: BlockPos, state: BlockStateId, player: PlayerContext) -> InteractionResult {
|
||||
let b = self.b;
|
||||
if !player.may_build {
|
||||
return InteractionResult::Pass;
|
||||
}
|
||||
let connected = |s: BlockStateId, d: Direction| b.table.text(s, b.dir_prop(d)) != "none";
|
||||
let horizontal = super::update::HORIZONTAL;
|
||||
let cross = horizontal.into_iter().all(|d| connected(state, d));
|
||||
let dot = !horizontal.into_iter().any(|d| connected(state, d));
|
||||
if !(cross || dot) {
|
||||
return InteractionResult::Pass;
|
||||
}
|
||||
let power = b.table.int(state, b.props.power);
|
||||
let base = if cross {
|
||||
b.table.set_int(b.default_of(b.named.redstone_wire), b.props.power, power)
|
||||
} else {
|
||||
self.wire_cross_state(power)
|
||||
};
|
||||
let next = self.wire_connection_state(base, pos);
|
||||
if next == state {
|
||||
return InteractionResult::Pass;
|
||||
}
|
||||
self.set_block_and_update(pos, next);
|
||||
for direction in horizontal {
|
||||
let relative = super::update::rel(pos, direction);
|
||||
if connected(state, direction) != connected(next, direction) && self.is_redstone_conductor(self.state(relative), relative) {
|
||||
let block = b.block(next);
|
||||
self.update_neighbors_at_except(relative, block, direction.opposite());
|
||||
}
|
||||
}
|
||||
InteractionResult::Success
|
||||
}
|
||||
|
||||
/// `DragonEggBlock.teleport`.
|
||||
fn dragon_egg_teleport(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
for _ in 0..1000 {
|
||||
let dx = self.random().next_int_bound(16).unwrap_or(0) - self.random().next_int_bound(16).unwrap_or(0);
|
||||
let dy = self.random().next_int_bound(8).unwrap_or(0) - self.random().next_int_bound(8).unwrap_or(0);
|
||||
let dz = self.random().next_int_bound(16).unwrap_or(0) - self.random().next_int_bound(16).unwrap_or(0);
|
||||
let test = pos.offset(dx, dy, dz);
|
||||
if b.is_air(self.state(test)) && !b.is_air(self.below_state(test)) && self.is_inside_build_height(test.y) {
|
||||
// BlockUtil.packDifferenceInPosition(pos, test, 16, 8, 16)
|
||||
let packed = ((dx + 16) << 16) | ((dy + 8) << 8) | (dz + 16);
|
||||
self.level_event(2015, pos, packed);
|
||||
self.set_block(test, state, 2);
|
||||
self.remove_block(pos, false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Behaviours {
|
||||
/// `BlockState.useWithoutItem` for a right-click on `pos` (hit face and
|
||||
/// absolute hit location as in `BlockHitResult`).
|
||||
pub fn use_without_item<L: LevelAccess + ?Sized>(
|
||||
&self,
|
||||
level: &mut L,
|
||||
ticks: &mut Ticks,
|
||||
pos: BlockPos,
|
||||
face: Direction,
|
||||
hit: Vec3,
|
||||
player: PlayerContext,
|
||||
) -> InteractionResult {
|
||||
self.sim(level, ticks).use_without_item(pos, face, hit, player)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ mod tests;
|
||||
pub use data::{BlockData, ItemBinding, ItemKind, Named, Props, PushReaction, StateFacts, Tags};
|
||||
pub use falling::Landing;
|
||||
pub use place::{PlaceContext, PlaceOutcome};
|
||||
pub use interact::{InteractionResult, PlayerContext};
|
||||
pub use interact::{InteractionResult, PlayerContext, PlayerEffect};
|
||||
pub use states::{BlockId, PropKey, StateTable};
|
||||
pub use ticks::Ticks;
|
||||
|
||||
@@ -183,6 +183,12 @@ pub enum LevelEvent {
|
||||
state: BlockStateId,
|
||||
kind: &'static str,
|
||||
},
|
||||
/// A player-side effect of [`Behaviours::use_without_item`].
|
||||
Player {
|
||||
pos: BlockPos,
|
||||
state: BlockStateId,
|
||||
effect: PlayerEffect,
|
||||
},
|
||||
/// `Level.gameEvent` (vibration system), by vanilla identifier.
|
||||
GameEvent { pos: BlockPos, event: &'static str },
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -39,12 +39,6 @@ pub(crate) fn axis_of(direction: Direction) -> horizon_core::geometry::Axis {
|
||||
Direction::North | Direction::South => Axis::Z,
|
||||
}
|
||||
}
|
||||
pub(crate) fn step_x(direction: Direction) -> i32 {
|
||||
direction.normal()[0]
|
||||
}
|
||||
pub(crate) fn step_z(direction: Direction) -> i32 {
|
||||
direction.normal()[2]
|
||||
}
|
||||
/// `Direction.from2DDataValue` (S, W, N, E).
|
||||
pub(crate) fn from_2d(value: i32) -> Direction {
|
||||
[Direction::South, Direction::West, Direction::North, Direction::East][value.rem_euclid(4) as usize]
|
||||
@@ -90,12 +84,6 @@ impl Behaviours {
|
||||
pub(crate) fn full_collision(&self, state: BlockStateId) -> bool {
|
||||
self.has_flag(state, state_flags::FULL_COLLISION)
|
||||
}
|
||||
pub(crate) fn collision_empty(&self, state: BlockStateId) -> bool {
|
||||
self.has_flag(state, state_flags::COLLISION_EMPTY)
|
||||
}
|
||||
pub(crate) fn has_block_entity(&self, state: BlockStateId) -> bool {
|
||||
self.has_flag(state, state_flags::BLOCK_ENTITY)
|
||||
}
|
||||
/// `Block.isFaceFull(state.getCollisionShape(level, pos), direction)`.
|
||||
pub(crate) fn collision_face_full(&self, state: BlockStateId, direction: Direction) -> bool {
|
||||
self.shape_info(state).full_faces & (1 << direction as u8) != 0
|
||||
@@ -117,15 +105,24 @@ impl Behaviours {
|
||||
|| block == n.pumpkin
|
||||
|| self.tags.shulker_boxes.contains(block)
|
||||
}
|
||||
pub(crate) fn bool_prop(&self, state: BlockStateId, key: PropKey) -> bool {
|
||||
self.table.bool(state, key)
|
||||
}
|
||||
pub(crate) fn is_waterlogged(&self, state: BlockStateId) -> bool {
|
||||
self.table.bool(state, self.props.waterlogged)
|
||||
}
|
||||
pub(crate) fn facing(&self, state: BlockStateId) -> Direction {
|
||||
self.table.dir(state, self.props.facing).unwrap_or(Direction::North)
|
||||
}
|
||||
/// The boolean/enum property named after `direction` (`north`, `up`, ...).
|
||||
pub(crate) fn dir_prop(&self, direction: Direction) -> PropKey {
|
||||
let p = &self.props;
|
||||
match direction {
|
||||
Direction::Down => p.down,
|
||||
Direction::Up => p.up,
|
||||
Direction::North => p.north,
|
||||
Direction::South => p.south,
|
||||
Direction::West => p.west,
|
||||
Direction::East => p.east,
|
||||
}
|
||||
}
|
||||
pub(crate) fn default_of(&self, block: BlockId) -> BlockStateId {
|
||||
self.table.default_state(block)
|
||||
}
|
||||
|
||||
@@ -418,7 +418,7 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
|
||||
}
|
||||
result
|
||||
}
|
||||
fn diode_should_turn_on(&self, pos: BlockPos, state: BlockStateId) -> bool {
|
||||
pub(crate) fn diode_should_turn_on(&self, pos: BlockPos, state: BlockStateId) -> bool {
|
||||
if self.b.instance_of(state, Class::Comparator) {
|
||||
let input = self.diode_input_signal(pos, state);
|
||||
if input == 0 {
|
||||
|
||||
@@ -434,3 +434,125 @@ fn crops_grow_and_leaves_decay() {
|
||||
w.tick();
|
||||
assert_eq!(w.prop(BlockPos::new(9, 5, 5), "distance"), "1");
|
||||
}
|
||||
|
||||
impl World<'_> {
|
||||
/// Right-click `item` on the `face` of `clicked`, hitting the face centre
|
||||
/// (offset vertically by `dy`), looking with `yaw`/`pitch`.
|
||||
fn place(&mut self, item: &str, clicked: BlockPos, face: Direction, yaw: f32, pitch: f32, dy: f64) -> PlaceOutcome {
|
||||
let [nx, ny, nz] = face.normal();
|
||||
let hit = horizon_core::geometry::Vec3::new(
|
||||
f64::from(clicked.x) + 0.5 + f64::from(nx) * 0.5,
|
||||
f64::from(clicked.y) + 0.5 + f64::from(ny) * 0.5 + dy,
|
||||
f64::from(clicked.z) + 0.5 + f64::from(nz) * 0.5,
|
||||
);
|
||||
let ctx = PlaceContext {
|
||||
clicked_pos: clicked,
|
||||
face,
|
||||
hit,
|
||||
inside: false,
|
||||
yaw,
|
||||
pitch,
|
||||
sneaking: false,
|
||||
item: format!("minecraft:{item}"),
|
||||
};
|
||||
self.f.behaviours.place_block(&mut self.level, &mut self.ticks, &ctx)
|
||||
}
|
||||
fn use_block(&mut self, pos: BlockPos) -> InteractionResult {
|
||||
let player = PlayerContext {
|
||||
yaw: 0.0,
|
||||
pitch: 0.0,
|
||||
may_build: true,
|
||||
can_eat: true,
|
||||
game_master: false,
|
||||
};
|
||||
let hit = horizon_core::geometry::Vec3::new(f64::from(pos.x) + 0.5, f64::from(pos.y) + 0.5, f64::from(pos.z));
|
||||
self.f.behaviours.use_without_item(&mut self.level, &mut self.ticks, pos, Direction::North, hit, player)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placement_orients_blocks() {
|
||||
let mut w = world!();
|
||||
w.floor(0, 6, "stone");
|
||||
let ground = BlockPos::new(0, 0, 0);
|
||||
// Looking north (yaw 180) at the top of the floor.
|
||||
w.place("oak_stairs", ground, Direction::Up, 180.0, 30.0, 0.0);
|
||||
assert_eq!(w.name(BlockPos::new(0, 1, 0)), "oak_stairs[facing=north,half=bottom,shape=straight,waterlogged=false]");
|
||||
w.place("oak_log", BlockPos::new(1, 0, 0), Direction::Up, 0.0, 0.0, 0.0);
|
||||
w.place("oak_log", BlockPos::new(1, 1, 0), Direction::East, 0.0, 0.0, 0.0);
|
||||
assert_eq!(w.prop(BlockPos::new(2, 1, 0), "axis"), "x");
|
||||
// Torch against the log's east face becomes a wall torch facing east.
|
||||
w.place("torch", BlockPos::new(2, 1, 0), Direction::East, 270.0, 0.0, 0.0);
|
||||
assert_eq!(w.name(BlockPos::new(3, 1, 0)), "wall_torch[facing=east]");
|
||||
// Furnaces face the player; observers face where the player looks.
|
||||
w.place("furnace", BlockPos::new(-2, 0, 0), Direction::Up, 0.0, 0.0, 0.0);
|
||||
assert_eq!(w.prop(BlockPos::new(-2, 1, 0), "facing"), "north");
|
||||
w.place("observer", BlockPos::new(-3, 0, 0), Direction::Up, 0.0, 0.0, 0.0);
|
||||
assert_eq!(w.prop(BlockPos::new(-3, 1, 0), "facing"), "south");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placement_merges_slabs_and_stacks() {
|
||||
let mut w = world!();
|
||||
w.floor(0, 4, "stone");
|
||||
let ground = BlockPos::new(0, 0, 0);
|
||||
w.place("oak_slab", ground, Direction::Up, 0.0, 60.0, 0.0);
|
||||
assert_eq!(w.prop(BlockPos::new(0, 1, 0), "type"), "bottom");
|
||||
w.place("oak_slab", BlockPos::new(0, 1, 0), Direction::Up, 0.0, 60.0, -0.5);
|
||||
assert_eq!(w.prop(BlockPos::new(0, 1, 0), "type"), "double");
|
||||
// Clicking the upper half of a side face places a top slab.
|
||||
w.place("oak_slab", BlockPos::new(0, 1, 0), Direction::East, 0.0, 0.0, 0.25);
|
||||
assert_eq!(w.prop(BlockPos::new(1, 1, 0), "type"), "top");
|
||||
w.place("snow", BlockPos::new(3, 0, 0), Direction::Up, 0.0, 60.0, 0.0);
|
||||
w.place("snow", BlockPos::new(3, 1, 0), Direction::Up, 0.0, 60.0, -0.375);
|
||||
assert_eq!(w.prop(BlockPos::new(3, 1, 0), "layers"), "2");
|
||||
w.place("candle", BlockPos::new(-3, 0, 0), Direction::Up, 0.0, 60.0, 0.0);
|
||||
w.place("candle", BlockPos::new(-3, 1, 0), Direction::Up, 0.0, 60.0, -0.4);
|
||||
assert_eq!(w.prop(BlockPos::new(-3, 1, 0), "candles"), "2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doors_beds_and_plants_place_both_halves() {
|
||||
let mut w = world!();
|
||||
w.floor(0, 6, "grass_block");
|
||||
let ground = BlockPos::new(0, 0, 0);
|
||||
w.place("oak_door", ground, Direction::Up, 180.0, 30.0, 0.0);
|
||||
assert_eq!(w.prop(BlockPos::new(0, 1, 0), "half"), "lower");
|
||||
assert_eq!(w.prop(BlockPos::new(0, 2, 0), "half"), "upper");
|
||||
assert_eq!(w.use_block(BlockPos::new(0, 1, 0)), InteractionResult::Success);
|
||||
assert_eq!(w.prop(BlockPos::new(0, 1, 0), "open"), "true");
|
||||
assert_eq!(w.prop(BlockPos::new(0, 2, 0), "open"), "true", "the upper half follows");
|
||||
w.place("red_bed", BlockPos::new(3, 0, 0), Direction::Up, 180.0, 30.0, 0.0);
|
||||
assert_eq!(w.prop(BlockPos::new(3, 1, 0), "part"), "foot");
|
||||
assert_eq!(w.prop(BlockPos::new(3, 1, -1), "part"), "head");
|
||||
w.place("sunflower", BlockPos::new(-3, 0, 0), Direction::Up, 0.0, 30.0, 0.0);
|
||||
assert_eq!(w.prop(BlockPos::new(-3, 2, 0), "half"), "upper");
|
||||
// Iron doors cannot be opened by hand.
|
||||
w.place("iron_door", BlockPos::new(5, 0, 0), Direction::Up, 180.0, 30.0, 0.0);
|
||||
assert_eq!(w.use_block(BlockPos::new(5, 1, 0)), InteractionResult::Pass);
|
||||
// A placement into a solid block fails.
|
||||
assert_eq!(w.place("stone", BlockPos::new(0, 0, 0), Direction::West, 0.0, 0.0, 0.0), PlaceOutcome::Failed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn levers_power_wire_through_interaction() {
|
||||
let mut w = world!();
|
||||
w.floor(0, 8, "stone");
|
||||
w.place("lever", BlockPos::new(0, 0, 0), Direction::Up, 0.0, 60.0, 0.0);
|
||||
for x in 1..=3 {
|
||||
w.place("redstone", BlockPos::new(x, 0, 0), Direction::Up, 0.0, 60.0, 0.0);
|
||||
}
|
||||
assert_eq!(w.use_block(BlockPos::new(0, 1, 0)), InteractionResult::Success);
|
||||
assert_eq!(w.prop(BlockPos::new(0, 1, 0), "powered"), "true");
|
||||
assert_eq!(w.prop(BlockPos::new(3, 1, 0), "power"), "13");
|
||||
w.use_block(BlockPos::new(0, 1, 0));
|
||||
assert_eq!(w.prop(BlockPos::new(1, 1, 0), "power"), "0");
|
||||
// Buttons release after their pressed time.
|
||||
w.place("stone_button", BlockPos::new(5, 0, 0), Direction::Up, 0.0, 60.0, 0.0);
|
||||
w.use_block(BlockPos::new(5, 1, 0));
|
||||
assert_eq!(w.prop(BlockPos::new(5, 1, 0), "powered"), "true");
|
||||
for _ in 0..20 {
|
||||
w.tick();
|
||||
}
|
||||
assert_eq!(w.prop(BlockPos::new(5, 1, 0), "powered"), "false");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user