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.

57 lines
1.6 KiB

use crate::loot_tables::EARLY_GAME;
use valence::{block::BlockEntityKind, client::misc::InteractBlock, ecs as bevy_ecs, prelude::*};
#[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<InteractBlock>,
) {
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 inventory = EARLY_GAME.get_random_inventory(5, 7);
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);
}
}
}