You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

use valence::bevy_ecs;
use valence::prelude::{event::PlayerInteractBlock, *};
use valence_protocol::block::BlockEntityKind;
#[derive(Component, Debug)]
pub struct Chest {
pub inventory: Inventory,
pub block_pos: BlockPos,
}
#[derive(Component)]
pub struct Pos(BlockPos);
pub fn open_chests(
mut commands: Commands,
chests: Query<(Entity, &Inventory, &Pos), (With<Inventory>, With<Pos>)>,
instances: Query<&Instance>,
mut events: EventReader<PlayerInteractBlock>,
) {
let instance = instances.single();
for event in events.iter() {
if instance
.block(event.position)
.unwrap()
.state()
.block_entity_kind()
.or(None)
== Some(BlockEntityKind::Chest)
{
let chest = chests
.iter()
.filter(|chest| event.position == (*chest.2).0)
.collect::<Vec<(Entity, &Inventory, &Pos)>>();
let entity;
if chest.len() < 1 {
tracing::info!(
"Generating new chest at ({}, {}, {})",
event.position.x,
event.position.y,
event.position.z
);
let mut inventory = Inventory::new(InventoryKind::Generic9x3);
inventory.set_slot(0, ItemStack::new(ItemKind::IronSword, 1, None));
let chest_commands = commands.spawn((inventory, Pos(event.position)));
entity = Some(chest_commands.id());
} else {
entity = Some(chest[0].0);
}
let open_inventory = OpenInventory::new(entity.unwrap());
commands.entity(event.client).insert(open_inventory);
}
}
}