add killed_by_player for loot table

This commit is contained in:
Alexander Medvedev 2025-07-28 19:59:07 +02:00
parent 0e375434c5
commit a66fbc9671
6 changed files with 18 additions and 16 deletions

View File

@ -171,8 +171,7 @@ impl UniformLootNumberProvider {
}
pub fn get(&self, random: &mut impl RandomImpl) -> f32 {
// TODO
random.next_f32()
random.next_f32() * (self.max - self.min) + self.min
}
pub fn get_max(&self) -> f32 {

View File

@ -66,7 +66,6 @@ use blocks::{
};
use fluid::lava::FlowingLava;
use fluid::water::FlowingWater;
use loot::LootTableExt;
use pumpkin_data::block_properties::Integer0To15;
use pumpkin_data::{Block, BlockState};
@ -101,16 +100,15 @@ use crate::block::blocks::lectern::LecternBlock;
use crate::block::blocks::shulker_box::ShulkerBoxBlock;
use crate::block::blocks::skull_block::SkullBlock;
use crate::block::blocks::smoker::SmokerBlock;
use crate::block::loot::LootContextParameters;
use crate::block::registry::BlockRegistry;
use crate::world::World;
use crate::world::loot::{LootContextParameters, LootTableExt};
use crate::{block::blocks::crafting_table::CraftingTableBlock, entity::player::Player};
use crate::{block::blocks::jukebox::JukeboxBlock, entity::experience_orb::ExperienceOrbEntity};
use std::sync::Arc;
pub mod blocks;
mod fluid;
pub mod loot;
pub mod pumpkin_block;
pub mod pumpkin_fluid;
pub mod registry;

View File

@ -6,8 +6,8 @@ use std::{collections::HashMap, sync::atomic::AtomicI32};
use super::EntityBase;
use super::{Entity, NBTStorage, effect::Effect};
use crate::block::loot::{LootContextParameters, LootTableExt};
use crate::server::Server;
use crate::world::loot::{LootContextParameters, LootTableExt};
use async_trait::async_trait;
use crossbeam::atomic::AtomicCell;
use pumpkin_config::advanced_config;
@ -255,7 +255,12 @@ impl LivingEntity {
EntityStatus::PlayDeathSoundOrAddProjectileHitParticles,
)
.await;
self.drop_loot().await;
let params = LootContextParameters {
killed_by_player: cause.map(|c| c.get_entity().entity_type == EntityType::PLAYER),
..Default::default()
};
self.drop_loot(params).await;
self.entity.pose.store(EntityPose::Dying);
let level_info = world.level_info.read().await;
@ -310,13 +315,10 @@ impl LivingEntity {
}
}
async fn drop_loot(&self) {
async fn drop_loot(&self, params: LootContextParameters) {
if let Some(loot_table) = &self.get_entity().entity_type.loot_table {
let world = self.entity.world.read().await;
let pos = self.entity.block_pos.load();
let params = LootContextParameters {
..Default::default()
};
for stack in loot_table.get_loot(params) {
world.drop_stack(&pos, stack).await;
}

View File

@ -5,8 +5,9 @@ use pumpkin_data::{Block, BlockState};
use pumpkin_util::math::{position::BlockPos, vector3::Vector3};
use crate::{
block::{drop_loot, loot::LootContextParameters, pumpkin_block::ExplodeArgs},
block::{drop_loot, pumpkin_block::ExplodeArgs},
server::Server,
world::loot::LootContextParameters,
};
use super::{BlockFlags, World};
@ -88,6 +89,7 @@ impl Explosion {
let params = LootContextParameters {
block_state: Some(state),
explosion_radius: Some(self.power),
..Default::default()
};
drop_loot(world, block, &pos, false, params).await;
}

View File

@ -13,6 +13,7 @@ use rand::Rng;
pub struct LootContextParameters {
pub explosion_radius: Option<f32>,
pub block_state: Option<&'static BlockState>,
pub killed_by_player: Option<bool>,
}
pub trait LootTableExt {
@ -164,6 +165,7 @@ impl LootConditionExt for LootCondition {
}
true
}
Self::KilledByPlayer => params.killed_by_player.unwrap_or(false),
Self::BlockStateProperty {
block: _,
properties,

View File

@ -6,9 +6,11 @@ use std::{
pub mod chunker;
pub mod explosion;
pub mod loot;
pub mod portal;
pub mod time;
use crate::world::loot::LootContextParameters;
use crate::{
PLUGIN_MANAGER,
block::{
@ -27,10 +29,7 @@ use crate::{
},
server::{CURRENT_BEDROCK_MC_VERSION, Server},
};
use crate::{
block::{BlockEvent, loot::LootContextParameters},
entity::item::ItemEntity,
};
use crate::{block::BlockEvent, entity::item::ItemEntity};
use async_trait::async_trait;
use border::Worldborder;
use bytes::BufMut;