Files
horizon-airdef/crates/horizon-client/src/items.rs
T
Ludovic Marie 3de8b358ec Dispense items like vanilla dispensers and droppers
Dispensers now follow DispenserBlock's behaviour table: arrows, eggs, snowballs, experience bottles, thrown potions, firework rockets (flying straight until they hit something) and fire charges are shot out with the vanilla power and spread; buckets place or pick up water, lava, powder snow and mobs through the new Behaviours bucket calls; flint and steel, bone meal, TNT, shears (sheep, mooshrooms, bogged, beehives), brushes on armadillos, honeycomb waxing, respawn anchors, glass bottles (water, honey), water potions on mud, boats, minecarts, armor stands, shulker boxes, carved pumpkins (which now build snow and iron golems for players too), wither skulls, spawn eggs and worn equipment on players, armor stands and mobs that may pick up loot. Every dispense plays the vanilla click, fail or launch sound and the smoke puff, and droppers keep the plain drop or container insert. Experience bottles can also be thrown by the player.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-09 12:56:20 +02:00

1031 lines
41 KiB
Rust

//! Items from vanilla's default-component reports, block<->item mapping,
//! tool mining rules and block loot tables (`LootTable`/`LootPool` semantics
//! for the condition, entry and modifier types used by block drops).
use crate::{Result, blocks::BlockFacts};
use horizon_core::random::{LegacyRandom, RandomSource};
use horizon_world::registry::{BlockRegistry, BlockStateId};
use serde_json::Value;
use std::{
collections::{BTreeMap, HashMap, HashSet},
fs,
path::Path,
sync::Arc,
};
#[derive(Clone, Debug)]
pub struct ToolRule {
pub blocks: Vec<String>,
pub speed: Option<f32>,
pub correct: Option<bool>,
}
#[derive(Clone, Debug)]
pub struct ItemInfo {
pub id: Arc<str>,
pub max_stack: u32,
pub max_damage: u32,
pub block: Option<BlockStateId>,
pub tool: Vec<ToolRule>,
pub tool_default_speed: f32,
pub food: Option<(u32, f32)>,
pub model: String,
/// Main-hand `attack_damage` / `attack_speed` modifier amounts.
pub attack_damage: f32,
pub attack_speed: f32,
/// `cooking_fuel`: burn-time and speed-multiplier provider references.
pub fuel: Option<(Value, Value)>,
/// `brewing_fuel`: uses and speed-multiplier provider references.
pub brewing_fuel: Option<(Value, Value)>,
/// Item left behind after crafting/fuel use (e.g. bucket).
pub remainder: Option<String>,
/// `equippable.slot` (head, chest, legs, feet, ...).
pub equip_slot: Option<String>,
/// `equippable.asset_id`: the equipment asset drawn when worn.
pub equip_asset: Option<String>,
/// All `attribute_modifiers`: (attribute, id, amount, operation, slot group).
pub modifiers: Vec<(String, String, f64, String, String)>,
/// `blocks_attacks` (shields).
pub blocks_attacks: Option<BlocksAttacks>,
/// `weapon.disable_blocking_for_seconds` (axes).
pub disable_blocking_seconds: f32,
/// `use_effects.speed_multiplier` (UseEffects.DEFAULT: 0.2).
pub use_speed: f32,
/// `glider` (elytra).
pub glider: bool,
/// `equippable.swappable` and `equip_sound`.
pub swappable: bool,
/// `equippable.dispensable` (dispensers may put it on an entity).
pub equip_dispensable: bool,
/// `equippable.allowed_entities` resolved to entity type ids; None allows all.
pub equip_allowed: Option<HashSet<String>>,
pub equip_sound: Option<String>,
/// `jukebox_playable`: the jukebox song it plays.
pub jukebox_song: Option<String>,
}
/// A reduction of `blocks_attacks.damage_reductions`.
#[derive(Clone, Debug)]
pub struct BlockReduction {
pub horizontal_angle: f32,
/// Damage type ids or `#tags`; None blocks every type.
pub types: Option<Vec<String>>,
pub base: f32,
pub factor: f32,
}
/// The `blocks_attacks` component.
#[derive(Clone, Debug)]
pub struct BlocksAttacks {
pub delay_seconds: f32,
pub disable_scale: f32,
pub reductions: Vec<BlockReduction>,
/// item_damage: (threshold, base, factor)
pub item_damage: (f32, f32, f32),
pub bypassed_by: Option<String>,
pub block_sound: Option<String>,
pub disabled_sound: Option<String>,
}
impl BlocksAttacks {
fn parse(v: &Value) -> Option<Self> {
if !v.is_object() {
return None;
}
let f = |v: &Value, d: f64| v.as_f64().unwrap_or(d) as f32;
let text = |v: &Value| v.as_str().map(str::to_owned);
let reductions = match v["damage_reductions"].as_array() {
Some(list) => list
.iter()
.map(|r| BlockReduction {
horizontal_angle: f(&r["horizontal_blocking_angle"], 90.0),
types: match &r["type"] {
Value::String(s) => Some(vec![s.clone()]),
Value::Array(a) => Some(
a.iter()
.filter_map(|t| t.as_str().map(str::to_owned))
.collect(),
),
_ => None,
},
base: f(&r["base"], 0.0),
factor: f(&r["factor"], 0.0),
})
.collect(),
// BlocksAttacks.CODEC default: block everything within 90 degrees.
None => vec![BlockReduction {
horizontal_angle: 90.0,
types: None,
base: 0.0,
factor: 1.0,
}],
};
let d = &v["item_damage"];
Some(Self {
delay_seconds: f(&v["block_delay_seconds"], 0.0),
disable_scale: f(&v["disable_cooldown_scale"], 1.0),
reductions,
item_damage: if d.is_object() {
(
f(&d["threshold"], 1.0),
f(&d["base"], 0.0),
f(&d["factor"], 1.0),
)
} else {
(1.0, 0.0, 1.0)
},
bypassed_by: text(&v["bypassed_by"]),
block_sound: text(&v["block_sound"]),
disabled_sound: text(&v["disabled_sound"]),
})
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ItemStack {
pub item: Arc<str>,
pub count: u32,
pub damage: u32,
/// Components beyond count and damage (see `item_data`); None when default.
pub extra: Option<Box<crate::item_data::StackData>>,
}
pub struct Items {
pub items: HashMap<Arc<str>, ItemInfo>,
pub order: Vec<Arc<str>>,
/// Block tag name (without '#') -> member block names.
pub block_tags: HashMap<String, HashSet<String>>,
pub item_tags: HashMap<String, HashSet<String>>,
loot: HashMap<String, Value>,
/// The `jukebox_song` registry.
pub songs: HashMap<String, Song>,
}
/// A `JukeboxSong`.
#[derive(Clone, Debug)]
pub struct Song {
pub sound: String,
pub length_ticks: i64,
pub comparator: i32,
pub description: Value,
}
impl Song {
/// `hasFinished`: the length plus one second.
pub fn finished(&self, ticks: i64) -> bool {
ticks >= self.length_ticks + 20
}
}
fn load_songs(root: &Path) -> HashMap<String, Song> {
let mut songs = HashMap::new();
let Ok(dir) = fs::read_dir(root.join("data/minecraft/jukebox_song")) else {
return songs;
};
for entry in dir.flatten() {
let path = entry.path();
let (Some(stem), Ok(bytes)) = (path.file_stem().and_then(|s| s.to_str()), fs::read(&path))
else {
continue;
};
let Ok(v) = serde_json::from_slice::<Value>(&bytes) else {
continue;
};
let sound = match &v["sound_event"] {
Value::String(s) => s.clone(),
other => other["sound_id"].as_str().unwrap_or("").to_owned(),
};
songs.insert(
format!("minecraft:{stem}"),
Song {
sound,
length_ticks: (v["length_in_seconds"].as_f64().unwrap_or(0.0) as f32 * 20.0).ceil()
as i64,
comparator: v["comparator_output"].as_i64().unwrap_or(0) as i32,
description: v["description"].clone(),
},
);
}
songs
}
pub(crate) fn load_tags(root: &Path, kind: &str) -> Result<HashMap<String, HashSet<String>>> {
let dir = root.join(format!("data/minecraft/tags/{kind}"));
let mut raw: HashMap<String, Vec<String>> = HashMap::new();
fn walk(dir: &Path, base: &Path, raw: &mut HashMap<String, Vec<String>>) -> Result<()> {
for entry in fs::read_dir(dir)?.flatten() {
let p = entry.path();
if p.is_dir() {
walk(&p, base, raw)?;
} else if p.extension().is_some_and(|e| e == "json") {
let name = format!(
"minecraft:{}",
p.strip_prefix(base)?
.with_extension("")
.to_string_lossy()
.replace('\\', "/")
);
let v: Value = serde_json::from_slice(&fs::read(&p)?)?;
let values = v["values"]
.as_array()
.into_iter()
.flatten()
.filter_map(|e| e.as_str().or_else(|| e["id"].as_str()).map(str::to_owned))
.collect();
raw.insert(name, values);
}
}
Ok(())
}
if dir.exists() {
walk(&dir, &dir, &mut raw)?;
}
fn resolve(
name: &str,
raw: &HashMap<String, Vec<String>>,
out: &mut HashSet<String>,
depth: u32,
) {
if depth > 32 {
return;
}
for v in raw.get(name).into_iter().flatten() {
if let Some(tag) = v.strip_prefix('#') {
resolve(tag, raw, out, depth + 1);
} else {
out.insert(v.clone());
}
}
}
Ok(raw
.keys()
.map(|k| {
let mut set = HashSet::new();
resolve(k, &raw, &mut set, 0);
(k.clone(), set)
})
.collect())
}
/// An entity type id or `#tag` as the set of entity type ids it names.
fn entity_spec(tags: &HashMap<String, HashSet<String>>, spec: &str) -> HashSet<String> {
match spec.strip_prefix('#') {
Some(tag) => tags.get(tag).cloned().unwrap_or_default(),
None => HashSet::from([spec.to_owned()]),
}
}
impl Items {
pub fn load(root: &Path, registry: &BlockRegistry, facts: &BlockFacts) -> Result<Self> {
let raw: BTreeMap<String, Value> =
serde_json::from_slice(&fs::read(root.join("items.json"))?)?;
let mut block_for_item: HashMap<&str, BlockStateId> = HashMap::new();
for (i, s) in facts.states.iter().enumerate() {
let default = registry.default_state(&s.name);
if default == Some(BlockStateId(i as u32)) && &*s.item != "minecraft:air" {
block_for_item
.entry(&s.item)
.or_insert(BlockStateId(i as u32));
}
}
let entity_tags = load_tags(root, "entity_type")?;
let mut items = HashMap::new();
let mut order = Vec::new();
for (name, c) in &raw {
let id: Arc<str> = Arc::from(name.as_str());
let tool = c["minecraft:tool"]["rules"]
.as_array()
.into_iter()
.flatten()
.map(|r| ToolRule {
blocks: match &r["blocks"] {
Value::String(s) => vec![s.clone()],
Value::Array(a) => a
.iter()
.filter_map(|v| v.as_str().map(str::to_owned))
.collect(),
_ => vec![],
},
speed: r["speed"].as_f64().map(|v| v as f32),
correct: r["correct_for_drops"].as_bool(),
})
.collect();
let modifier = |kind: &str| -> f32 {
c["minecraft:attribute_modifiers"]
.as_array()
.into_iter()
.flatten()
.filter(|m| {
m["type"].as_str() == Some(kind)
&& m["operation"].as_str() == Some("add_value")
})
.filter_map(|m| m["amount"].as_f64())
.sum::<f64>() as f32
};
let food = c.get("minecraft:food").map(|f| {
(
f["nutrition"].as_u64().unwrap_or(0) as u32,
f["saturation"].as_f64().unwrap_or(0.0) as f32,
)
});
items.insert(
id.clone(),
ItemInfo {
id: id.clone(),
max_stack: c["minecraft:max_stack_size"].as_u64().unwrap_or(64) as u32,
max_damage: c["minecraft:max_damage"].as_u64().unwrap_or(0) as u32,
block: block_for_item.get(name.as_str()).copied(),
tool,
tool_default_speed: c["minecraft:tool"]["default_mining_speed"]
.as_f64()
.unwrap_or(1.0) as f32,
food,
model: c["minecraft:item_model"]
.as_str()
.unwrap_or(name)
.to_owned(),
glider: c["minecraft:glider"].is_object(),
swappable: c["minecraft:equippable"]["swappable"]
.as_bool()
.unwrap_or(true),
equip_dispensable: c["minecraft:equippable"]["dispensable"]
.as_bool()
.unwrap_or(true),
equip_allowed: match &c["minecraft:equippable"]["allowed_entities"] {
Value::String(spec) => Some(entity_spec(&entity_tags, spec)),
Value::Array(list) => Some(
list.iter()
.filter_map(Value::as_str)
.flat_map(|spec| entity_spec(&entity_tags, spec))
.collect(),
),
_ => None,
},
equip_sound: c["minecraft:equippable"]["equip_sound"]
.as_str()
.map(str::to_owned),
jukebox_song: c["minecraft:jukebox_playable"].as_str().map(str::to_owned),
use_speed: c["minecraft:use_effects"]["speed_multiplier"]
.as_f64()
.unwrap_or(0.2) as f32,
blocks_attacks: BlocksAttacks::parse(&c["minecraft:blocks_attacks"]),
disable_blocking_seconds: c["minecraft:weapon"]["disable_blocking_for_seconds"]
.as_f64()
.unwrap_or(0.0) as f32,
equip_asset: c["minecraft:equippable"]["asset_id"]
.as_str()
.map(str::to_owned),
equip_slot: c["minecraft:equippable"]["slot"]
.as_str()
.map(str::to_owned),
modifiers: c["minecraft:attribute_modifiers"]
.as_array()
.into_iter()
.flatten()
.filter_map(|m| {
Some((
m["type"].as_str()?.to_owned(),
m["id"].as_str()?.to_owned(),
m["amount"].as_f64()?,
m["operation"].as_str().unwrap_or("add_value").to_owned(),
m["slot"].as_str().unwrap_or("any").to_owned(),
))
})
.collect(),
fuel: c
.get("minecraft:cooking_fuel")
.map(|f| (f["burn_time"].clone(), f["speed_multiplier"].clone())),
brewing_fuel: c
.get("minecraft:brewing_fuel")
.map(|f| (f["uses"].clone(), f["speed_multiplier"].clone())),
remainder: c["minecraft:use_remainder"]["id"]
.as_str()
.or(c["minecraft:crafting_remainder"]["id"].as_str())
.or(c["minecraft:crafting_remainder"].as_str())
.map(str::to_owned),
attack_damage: modifier("minecraft:attack_damage"),
attack_speed: modifier("minecraft:attack_speed"),
},
);
order.push(id);
}
let mut loot = HashMap::new();
// Every loot table, keyed by its id (blocks/stone, entities/sheep/white,
// gameplay/chicken_lay, shearing/sheep/red...).
fn walk_loot(dir: &Path, base: &Path, loot: &mut HashMap<String, Value>) -> Result<()> {
for entry in fs::read_dir(dir)?.flatten() {
let p = entry.path();
if p.is_dir() {
walk_loot(&p, base, loot)?;
} else if p.extension().is_some_and(|e| e == "json") {
let id = p
.strip_prefix(base)?
.with_extension("")
.to_string_lossy()
.replace('\\', "/");
loot.insert(
format!("minecraft:{id}"),
serde_json::from_slice(&fs::read(&p)?)?,
);
}
}
Ok(())
}
let loot_root = root.join("data/minecraft/loot_table");
walk_loot(&loot_root, &loot_root, &mut loot)?;
Ok(Self {
items,
order,
block_tags: load_tags(root, "block")?,
item_tags: load_tags(root, "item")?,
loot,
songs: load_songs(root),
})
}
pub fn get(&self, id: &str) -> Option<&ItemInfo> {
self.items.get(id)
}
/// Only the block and item tags, for tools that need tag membership.
pub fn tags_only(root: &Path) -> Result<Self> {
Ok(Self {
items: HashMap::new(),
order: Vec::new(),
block_tags: load_tags(root, "block")?,
item_tags: load_tags(root, "item")?,
loot: HashMap::new(),
songs: HashMap::new(),
})
}
/// An item table with only tags, for unit tests of item rules.
#[cfg(test)]
pub fn for_tests(item_tags: HashMap<String, HashSet<String>>) -> Self {
Self {
items: HashMap::new(),
order: Vec::new(),
block_tags: HashMap::new(),
item_tags,
loot: HashMap::new(),
songs: HashMap::new(),
}
}
fn block_matches(&self, spec: &str, block: &str) -> bool {
match spec.strip_prefix('#') {
Some(tag) => self.block_tags.get(tag).is_some_and(|s| s.contains(block)),
None => spec == block,
}
}
/// `Tool.getMiningSpeed` for the held item (1.0 without a tool component).
pub fn mining_speed(&self, held: Option<&ItemStack>, block: &str) -> f32 {
let Some(info) = held.and_then(|s| self.get(&s.item)) else {
return 1.0;
};
for rule in &info.tool {
if let Some(speed) = rule.speed
&& rule.blocks.iter().any(|b| self.block_matches(b, block))
{
return speed;
}
}
if info.tool.is_empty() {
1.0
} else {
info.tool_default_speed
}
}
/// `Tool.isCorrectForDrops`.
pub fn correct_for_drops(&self, held: Option<&ItemStack>, block: &str) -> bool {
let Some(info) = held.and_then(|s| self.get(&s.item)) else {
return false;
};
for rule in &info.tool {
if let Some(correct) = rule.correct
&& rule.blocks.iter().any(|b| self.block_matches(b, block))
{
return correct;
}
}
false
}
/// Evaluate the block's loot table for a player break without explosions.
pub fn block_drops(
&self,
registry: &BlockRegistry,
state: BlockStateId,
held: Option<&ItemStack>,
random: &mut LegacyRandom,
) -> Vec<ItemStack> {
let Some(block) = registry.state(state) else {
return vec![];
};
let name = block.name.trim_start_matches("minecraft:");
let Some(table) = self.loot.get(&format!("minecraft:blocks/{name}")) else {
return vec![];
};
let ctx = LootContext {
items: self,
block: &block.name,
properties: &block.properties,
held,
killed_by_player: false,
};
ctx.evaluate(table, random)
}
/// Evaluate an entity's loot table (`minecraft:entities/<name>`).
/// Evaluate any loot table by id (e.g. `minecraft:gameplay/chicken_lay`).
pub fn loot_table(
&self,
id: &str,
killed_by_player: bool,
held: Option<&ItemStack>,
random: &mut LegacyRandom,
) -> Vec<ItemStack> {
let Some(table) = self.loot.get(id) else {
return vec![];
};
let empty = BTreeMap::new();
let ctx = LootContext {
items: self,
block: "",
properties: &empty,
held,
killed_by_player,
};
ctx.evaluate(table, random)
}
pub fn entity_drops(
&self,
entity: &str,
killed_by_player: bool,
held: Option<&ItemStack>,
random: &mut LegacyRandom,
) -> Vec<ItemStack> {
let name = entity.trim_start_matches("minecraft:");
let Some(table) = self.loot.get(&format!("minecraft:entities/{name}")) else {
return vec![];
};
let empty = BTreeMap::new();
let ctx = LootContext {
items: self,
block: "",
properties: &empty,
held,
killed_by_player,
};
ctx.evaluate(table, random)
}
}
impl LootContext<'_> {
fn evaluate(&self, table: &Value, random: &mut LegacyRandom) -> Vec<ItemStack> {
let ctx = self;
let mut out = Vec::new();
for pool in table["pools"].as_array().into_iter().flatten() {
if !ctx.conditions(pool, random) {
continue;
}
let rolls = ctx.number(&pool["rolls"], random).max(0.0) as u32;
for _ in 0..rolls {
let mut produced = Vec::new();
ctx.choose(
pool["entries"].as_array().map_or(&[][..], Vec::as_slice),
random,
&mut produced,
);
for mut stack in produced {
ctx.modifiers(&pool["modifier"], &mut stack, random);
ctx.modifiers(&table["modifier"], &mut stack, random);
if stack.count > 0 {
out.push(stack);
}
}
}
}
out
}
}
struct LootContext<'a> {
items: &'a Items,
block: &'a str,
properties: &'a BTreeMap<String, String>,
held: Option<&'a ItemStack>,
killed_by_player: bool,
}
impl LootContext<'_> {
/// Level of an enchantment on the tool / killer's weapon.
fn enchantment_level(&self, id: &str) -> u32 {
self.held.map_or(0, |h| h.enchantment_level(id))
}
fn number(&self, v: &Value, random: &mut LegacyRandom) -> f32 {
match v {
Value::Number(n) => n.as_f64().unwrap_or(0.0) as f32,
Value::Object(o) => match o
.get("type")
.and_then(Value::as_str)
.unwrap_or("minecraft:uniform")
{
"minecraft:constant" => {
o.get("value").and_then(Value::as_f64).unwrap_or(0.0) as f32
}
"minecraft:uniform" => {
let min = self.number(o.get("min").unwrap_or(&Value::Null), random);
let max = self.number(o.get("max").unwrap_or(&Value::Null), random);
// UniformGenerator.getFloat / getInt (Mth.nextInt) differ; use int for counts.
if max <= min {
min
} else {
min + random.next_float() * (max - min)
}
}
"minecraft:binomial" => {
let n = self.number(o.get("n").unwrap_or(&Value::Null), random) as u32;
let p = self.number(o.get("p").unwrap_or(&Value::Null), random);
(0..n).filter(|_| random.next_float() < p).count() as f32
}
_ => 1.0,
},
_ => 1.0,
}
}
fn int_number(&self, v: &Value, random: &mut LegacyRandom) -> i32 {
if let Value::Object(o) = v
&& o.get("type")
.and_then(Value::as_str)
.unwrap_or("minecraft:uniform")
== "minecraft:uniform"
{
let min = self.int_number(o.get("min").unwrap_or(&Value::Null), random);
let max = self.int_number(o.get("max").unwrap_or(&Value::Null), random);
return if max <= min {
min
} else {
min + random.next_int_bound(max - min + 1).unwrap_or(0)
};
}
self.number(v, random).round() as i32
}
fn condition(&self, c: &Value, random: &mut LegacyRandom) -> bool {
let (kind, c) = match c {
Value::String(s) => (s.as_str(), &Value::Null),
Value::Object(o) => (o.get("type").and_then(Value::as_str).unwrap_or(""), c),
_ => return true,
};
match kind {
"minecraft:survives_explosion" => true,
"minecraft:tool/can_silk_touch" => self.enchantment_level("minecraft:silk_touch") > 0,
"minecraft:tool/can_shear" => self.held.is_some_and(|h| &*h.item == "minecraft:shears"),
"minecraft:inverted" => !self.condition(&c["term"], random),
"minecraft:any_of" => c["terms"]
.as_array()
.into_iter()
.flatten()
.any(|t| self.condition(t, random)),
"minecraft:all_of" => c["terms"]
.as_array()
.into_iter()
.flatten()
.all(|t| self.condition(t, random)),
"minecraft:random_chance" => random.next_float() < self.number(&c["chance"], random),
"minecraft:table_bonus" => {
// BonusLevelTableCondition: chances[min(level, len - 1)].
let level =
self.enchantment_level(c["enchantment"].as_str().unwrap_or("")) as usize;
let chances = c["chances"].as_array().map_or(&[][..], Vec::as_slice);
let chance = chances
.get(level.min(chances.len().saturating_sub(1)))
.and_then(Value::as_f64)
.unwrap_or(0.0) as f32;
random.next_float() < chance
}
"minecraft:match_block" | "minecraft:block_state_property" => {
let blocks_ok = match &c["blocks"].as_str().or(c["block"].as_str()) {
Some(spec) => self.items.block_matches(spec, self.block),
None => true,
};
blocks_ok
&& c["state"]
.as_object()
.into_iter()
.flatten()
.chain(c["properties"].as_object().into_iter().flatten())
.all(|(k, v)| {
let have = self.properties.get(k).map(String::as_str);
match v {
Value::String(s) => have == Some(s.as_str()),
Value::Object(r) => {
let have: i64 = have.and_then(|h| h.parse().ok()).unwrap_or(0);
r.get("min")
.and_then(Value::as_i64)
.is_none_or(|m| have >= m)
&& r.get("max")
.and_then(Value::as_i64)
.is_none_or(|m| have <= m)
}
_ => true,
}
})
}
"minecraft:match_tool" => {
let items = &c["predicate"]["items"];
let items_ok = match (self.held, items) {
(_, Value::Null) => self.held.is_some(),
(Some(h), Value::String(s)) => match s.strip_prefix('#') {
Some(tag) => self
.items
.item_tags
.get(tag)
.is_some_and(|t| t.contains(&*h.item)),
None => **s == *h.item,
},
_ => false,
};
// ItemPredicate "minecraft:enchantments": each entry needs a level in range.
let enchants_ok = c["predicate"]["predicates"]["minecraft:enchantments"]
.as_array()
.into_iter()
.flatten()
.all(|e| {
let level =
self.enchantment_level(e["enchantments"].as_str().unwrap_or(""));
let min = e["levels"]["min"].as_u64().unwrap_or(1) as u32;
let max = e["levels"]["max"].as_u64().map_or(u32::MAX, |m| m as u32);
level >= min.max(1) && level <= max
});
items_ok && enchants_ok
}
"minecraft:killed_by_player" => self.killed_by_player,
"minecraft:random_chance_with_enchanted_bonus" => {
let level = if self.killed_by_player && self.block.is_empty() {
self.enchantment_level(c["enchantment"].as_str().unwrap_or(""))
} else {
0
};
let chance = if level > 0 {
crate::enchant::level_value(&c["enchanted_chance"], level)
} else {
c["unenchanted_chance"].as_f64().unwrap_or(0.0) as f32
};
random.next_float() < chance
}
// Entity flags, equipment and damage-source predicates: none hold
// for an unenchanted player kill without fire or lightning.
"minecraft:entity_properties" | "minecraft:damage_source_properties" => false,
_ => true,
}
}
fn conditions(&self, o: &Value, random: &mut LegacyRandom) -> bool {
if let Some(c) = o.get("condition")
&& !self.condition(c, random)
{
return false;
}
o.get("conditions")
.and_then(Value::as_array)
.into_iter()
.flatten()
.all(|c| self.condition(c, random))
}
/// Expand entries; returns whether an entry succeeded (for alternatives).
fn expand(
&self,
e: &Value,
random: &mut LegacyRandom,
out: &mut Vec<(i32, ItemStack, Value)>,
) -> bool {
if !self.conditions(e, random) {
return false;
}
match e["type"].as_str().unwrap_or("") {
"minecraft:item" => {
out.push((
e["weight"].as_i64().unwrap_or(1) as i32,
ItemStack {
item: Arc::from(e["name"].as_str().unwrap_or("minecraft:air")),
count: 1,
damage: 0,
extra: None,
},
e["modifier"].clone(),
));
true
}
"minecraft:alternatives" => {
for child in e["children"].as_array().into_iter().flatten() {
if self.expand(child, random, out) {
return true;
}
}
false
}
"minecraft:group" => {
for child in e["children"].as_array().into_iter().flatten() {
self.expand(child, random, out);
}
true
}
"minecraft:sequence" => {
for child in e["children"].as_array().into_iter().flatten() {
if !self.expand(child, random, out) {
break;
}
}
true
}
"minecraft:empty" => {
out.push((
e["weight"].as_i64().unwrap_or(1) as i32,
ItemStack {
item: Arc::from("minecraft:air"),
count: 0,
damage: 0,
extra: None,
},
Value::Null,
));
true
}
_ => false,
}
}
fn choose(&self, entries: &[Value], random: &mut LegacyRandom, produced: &mut Vec<ItemStack>) {
let mut candidates = Vec::new();
for e in entries {
self.expand(e, random, &mut candidates);
}
if candidates.is_empty() {
return;
}
let pick = if candidates.len() == 1 {
0
} else {
let total: i32 = candidates.iter().map(|c| c.0.max(0)).sum();
let mut r = random.next_int_bound(total.max(1)).unwrap_or(0);
let mut chosen = 0;
for (i, c) in candidates.iter().enumerate() {
r -= c.0.max(0);
if r < 0 {
chosen = i;
break;
}
}
chosen
};
let (_, mut stack, modifiers) = candidates.swap_remove(pick);
self.modifiers(&modifiers, &mut stack, random);
produced.push(stack);
}
fn modifiers(&self, m: &Value, stack: &mut ItemStack, random: &mut LegacyRandom) {
let list: Vec<&Value> = match m {
Value::Array(a) => a.iter().collect(),
Value::Object(_) => vec![m],
_ => return,
};
for f in list {
if !self.conditions(f, random) {
continue;
}
match f["type"].as_str().or(f["function"].as_str()).unwrap_or("") {
"minecraft:set_count" => {
let n = self.int_number(&f["count"], random);
let base = if f["add"].as_bool() == Some(true) {
stack.count as i32
} else {
0
};
stack.count = (base + n).max(0) as u32;
}
"minecraft:apply_bonus" => {
let level =
self.enchantment_level(f["enchantment"].as_str().unwrap_or("")) as i32;
let count = stack.count as i32;
let params = &f["parameters"];
stack.count = match f["formula"].as_str().unwrap_or("") {
"minecraft:ore_drops" if level > 0 => {
let bonus = (random.next_int_bound(level + 2).unwrap_or(0) - 1).max(0);
count * (bonus + 1)
}
"minecraft:uniform_bonus_count" => {
let multiplier = params["bonusMultiplier"].as_i64().unwrap_or(1) as i32;
count + random.next_int_bound(multiplier * level + 1).unwrap_or(0)
}
"minecraft:binomial_with_bonus_count" => {
let (extra, p) = (
params["extra"].as_i64().unwrap_or(0) as i32,
params["probability"].as_f64().unwrap_or(0.0) as f32,
);
let mut n = count;
for _ in 0..level + extra {
if random.next_float() < p {
n += 1;
}
}
n
}
_ => count,
}
.max(0) as u32;
}
"minecraft:enchanted_count_increase" => {
// Attacker's level x count, rounded (looting).
let level = self.enchantment_level(f["enchantment"].as_str().unwrap_or(""));
if level > 0 && self.killed_by_player && self.block.is_empty() {
let add = level as f32 * self.number(&f["count"], random);
stack.count = (stack.count as i32 + add.round() as i32).max(0) as u32;
if let Some(limit) = f["limit"].as_i64().filter(|l| *l > 0) {
stack.count = stack.count.min(limit as u32);
}
}
}
"minecraft:limit_count" => {
let limit = &f["limit"];
if let Some(min) = limit["min"].as_i64() {
stack.count = stack.count.max(min as u32);
}
if let Some(max) = limit["max"].as_i64() {
stack.count = stack.count.min(max as u32);
}
}
_ => {}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn ctx<'a>(
items: &'a Items,
held: Option<&'a ItemStack>,
props: &'a BTreeMap<String, String>,
block: &'a str,
) -> LootContext<'a> {
LootContext {
items,
block,
properties: props,
held,
killed_by_player: block.is_empty(),
}
}
#[test]
fn fortune_silk_touch_and_looting() {
let items = Items::for_tests(HashMap::new());
let props = BTreeMap::new();
let mut pick = ItemStack::new("minecraft:diamond_pickaxe", 1);
pick.enchant("minecraft:fortune", 3);
let ore = json!({"pools":[{"rolls":1,"entries":[{"type":"minecraft:alternatives","children":[
{"type":"minecraft:item","condition":"minecraft:tool/can_silk_touch","name":"minecraft:diamond_ore"},
{"type":"minecraft:item","name":"minecraft:diamond","modifier":[{"type":"minecraft:apply_bonus","enchantment":"minecraft:fortune","formula":"minecraft:ore_drops"}]}]}]}]});
let mut random = LegacyRandom::new(7);
let n = 20000;
let total: u32 = (0..n)
.map(|_| {
ctx(&items, Some(&pick), &props, "minecraft:diamond_ore")
.evaluate(&ore, &mut random)
.iter()
.map(|s| s.count)
.sum::<u32>()
})
.sum();
// Fortune III ore drops average (1 + 1 + 2 + 3 + 4) / 5 = 2.2 diamonds.
let mean = f64::from(total) / f64::from(n);
assert!((mean - 2.2).abs() < 0.05, "{mean}");
let mut silk = ItemStack::new("minecraft:diamond_pickaxe", 1);
silk.enchant("minecraft:silk_touch", 1);
let drops =
ctx(&items, Some(&silk), &props, "minecraft:diamond_ore").evaluate(&ore, &mut random);
assert_eq!(&*drops[0].item, "minecraft:diamond_ore");
let plain = ItemStack::new("minecraft:diamond_pickaxe", 1);
let drops =
ctx(&items, Some(&plain), &props, "minecraft:diamond_ore").evaluate(&ore, &mut random);
assert_eq!((&*drops[0].item, drops[0].count), ("minecraft:diamond", 1));
// Looting III adds level x uniform(0, 1) on player kills.
let flesh = json!({"pools":[{"rolls":1,"entries":[{"type":"minecraft:item","name":"minecraft:rotten_flesh","modifier":[
{"type":"minecraft:set_count","count":{"type":"minecraft:uniform","min":0.0,"max":2.0}},
{"type":"minecraft:enchanted_count_increase","count":{"type":"minecraft:uniform","min":0.0,"max":1.0},"enchantment":"minecraft:looting"}]}]}]});
let mut sword = ItemStack::new("minecraft:iron_sword", 1);
sword.enchant("minecraft:looting", 3);
let looted: u32 = (0..n)
.map(|_| {
ctx(&items, Some(&sword), &props, "")
.evaluate(&flesh, &mut random)
.iter()
.map(|s| s.count)
.sum::<u32>()
})
.sum();
let base: u32 = (0..n)
.map(|_| {
ctx(&items, None, &props, "")
.evaluate(&flesh, &mut random)
.iter()
.map(|s| s.count)
.sum::<u32>()
})
.sum();
assert!(f64::from(looted) / f64::from(n) > f64::from(base) / f64::from(n) + 1.0);
// Table bonus uses the chance for the tool's level.
let leaves = json!({"pools":[{"rolls":1,"conditions":[{"type":"minecraft:table_bonus","enchantment":"minecraft:fortune","chances":[0.0,0.0,0.0,1.0]}],"entries":[{"type":"minecraft:item","name":"minecraft:apple"}]}]});
assert_eq!(
ctx(&items, Some(&pick), &props, "minecraft:oak_leaves")
.evaluate(&leaves, &mut random)
.len(),
1
);
assert!(
ctx(&items, Some(&plain), &props, "minecraft:oak_leaves")
.evaluate(&leaves, &mut random)
.is_empty()
);
}
}