Skip to main content

steel_core/player/advancement/
rewards.rs

1//! use to manage rewards
2
3use crate::entity::{Entity, entity_loot_ref, living_loot_equipment};
4use crate::player::Player;
5use steel_registry::advancement::AdvancementRewards;
6use steel_registry::loot_table::LootContext;
7
8/// Grants experience and loot rewards but recipes and functions are not handled yet
9pub fn grant_reward(player: &Player, reward: &AdvancementRewards) {
10    player.give_experience_points(reward.experience);
11
12    let position = player.position();
13    let equipment = living_loot_equipment(player);
14    let mut rng = rand::rng();
15    let mut ctx = LootContext::new(&mut rng)
16        .with_this_entity(entity_loot_ref(player, Some(&equipment)))
17        .with_origin(position.x, position.y, position.z);
18
19    let mut changes = false;
20    for loot_table in &reward.loots {
21        for mut item in loot_table.get_random_items(&mut ctx) {
22            if player.add_item_with_sound(&mut item) {
23                changes = true;
24                continue;
25            }
26            if let Some(drop) = player.spawn_dropped_item(item, false, false) {
27                drop.set_no_pickup_delay();
28                drop.set_owner(Some(player.gameprofile.id()));
29            }
30        }
31    }
32
33    if changes {
34        player.broadcast_inventory_changes();
35    }
36
37    //TODO grant recipes and functions when implemented
38}