Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions crates/mcrs_minecraft_item_stack/src/effect_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ const _: () = {
);
};

#[derive(Clone, Copy)]
pub(crate) struct EffectFields<'hidden> {
pub(crate) amplifier: u8,
pub(crate) duration: i32,
pub(crate) ambient: bool,
pub(crate) show_particles: bool,
pub(crate) show_icon: bool,
pub(crate) hidden_effect: Option<&'hidden EffectDetails>,
}

const _: () = {
assert!(
size_of::<EffectFields<'static>>() == 8 + size_of::<usize>(),
"EffectFields borrows optional child without allocation"
);
assert!(
align_of::<EffectFields<'static>>() == align_of::<EffectDetails>(),
"EffectFields matches EffectDetails alignment"
);
};

impl EffectDetails {
#[must_use]
pub const fn new() -> Self {
Expand All @@ -44,6 +65,18 @@ impl EffectDetails {
}
}

#[must_use]
pub(crate) fn fields(&self) -> EffectFields<'_> {
EffectFields {
amplifier: self.amplifier,
duration: self.duration,
ambient: self.ambient,
show_particles: self.show_particles,
show_icon: self.show_icon,
hidden_effect: self.hidden_effect.as_deref(),
}
}

pub(crate) fn drop_chain(head: &mut Option<Box<EffectDetails>>) {
let mut next = head.take();
while let Some(mut node) = next {
Expand Down
13 changes: 13 additions & 0 deletions crates/mcrs_minecraft_item_stack/src/effect_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::mem::{align_of, size_of};

use crate::EffectDetails;
use crate::MobEffectId;
use crate::effect_details::EffectFields;

#[non_exhaustive]
pub struct EffectInstance {
Expand Down Expand Up @@ -42,6 +43,18 @@ impl EffectInstance {
hidden_effect: None,
}
}

#[must_use]
pub(crate) fn fields(&self) -> EffectFields<'_> {
EffectFields {
amplifier: self.amplifier,
duration: self.duration,
ambient: self.ambient,
show_particles: self.show_particles,
show_icon: self.show_icon,
hidden_effect: self.hidden_effect.as_deref(),
}
}
}

impl Drop for EffectInstance {
Expand Down
2 changes: 2 additions & 0 deletions crates/mcrs_minecraft_item_stack/src/entity_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::Bees;
use crate::BlocksAttacks;
use crate::BundleContents;
use crate::ChargedProjectiles;
use crate::Consumable;
use crate::Container;
use crate::ContainerLoot;
use crate::CustomData;
Expand Down Expand Up @@ -58,6 +59,7 @@ impl EntityForm for Damage {}
impl EntityForm for Unbreakable {}

impl EntityForm for Food {}
impl EntityForm for Consumable {}
impl EntityForm for UseEffects {}
impl EntityForm for BlocksAttacks {}

Expand Down
1 change: 1 addition & 0 deletions crates/mcrs_minecraft_item_stack/src/for_each_builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ macro_rules! for_each_builtin {
RepairCost => "repair_cost",
UseEffects => "use_effects",
Food => "food",
Consumable => "consumable",
Tool => "tool",
BlocksAttacks => "blocks_attacks",
MapId => "map_id",
Expand Down
148 changes: 139 additions & 9 deletions crates/mcrs_minecraft_item_stack/src/saved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ use crate::BuiltinKey;
use crate::BundleContents;
use crate::ChargedProjectiles;
use crate::ComponentType;
use crate::Consumable;
use crate::ConsumeEffect;
use crate::Container;
use crate::ContainerLoot;
use crate::Count;
Expand Down Expand Up @@ -89,6 +91,8 @@ use crate::MapId;
use crate::MaxDamage;
use crate::MaxStackSize;
use crate::MobEffectId;
use crate::MobEffectSet;
use crate::MobEffectTagId;
use crate::ModifierDisplay;
use crate::NameLookup;
use crate::NameTable;
Expand Down Expand Up @@ -121,6 +125,7 @@ use crate::TrimMaterialId;
use crate::TrimPatternId;
use crate::Unbreakable;
use crate::UndecodedValue;
use crate::UseAnimation;
use crate::UseEffects;
use crate::Weapon;
use crate::builtin_key::NAMESPACE;
Expand Down Expand Up @@ -444,6 +449,7 @@ fn read_flat_value<L: NameLookup>(
}
BuiltinKey::TooltipDisplay => Builtin::TooltipDisplay(reader.tooltip_display(tag)?),
BuiltinKey::Food => Builtin::Food(reader.food(tag)?),
BuiltinKey::Consumable => Builtin::Consumable(reader.consumable(tag)?),
BuiltinKey::Tool => Builtin::Tool(reader.tool(tag)?),
BuiltinKey::Equippable => Builtin::Equippable(reader.equippable(tag)?),
BuiltinKey::PiercingWeapon => Builtin::PiercingWeapon(reader.piercing_weapon(tag)?),
Expand Down Expand Up @@ -526,6 +532,12 @@ struct Reader<'names, L> {
depth: u32,
}

#[derive(Clone, Copy)]
enum HiddenEffects {
Include,
Strip,
}

impl<L: NameLookup> Reader<'_, L> {
fn bad(&self) -> SavedError {
SavedError::BadValue(self.component)
Expand Down Expand Up @@ -992,41 +1004,152 @@ impl<L: NameLookup> Reader<'_, L> {
_ => self.map(tag)?,
};
let effect_depth = self.depth.saturating_add(4);
let custom_effects = self.listed(map, "custom_effects", |reader, tag| {
reader.effect_instance_at(tag, effect_depth, HiddenEffects::Strip)
})?;
Ok(PotionContents {
potion: self.optional(map, "potion", Self::potion)?,
custom_color: self.optional(map, "custom_color", |reader, tag| {
Ok(reader.number(tag)?.to_int())
})?,
custom_effects: self.listed(map, "custom_effects", |reader, tag| {
reader.effect_instance_stripping_hidden(tag, effect_depth)
})?,
custom_effects,
custom_name: self.optional(map, "custom_name", |reader, tag| {
Ok(reader.string(tag)?.to_string())
})?,
})
}

fn effect_instance_stripping_hidden(
fn mob_effect_set(&self, tag: &NbtTag) -> Result<MobEffectSet, SavedError> {
self.holder_set(
tag,
HolderSetSpecification {
tag_table: NameTable::MobEffectTag,
entry_table: NameTable::MobEffect,
make_tag: |id| MobEffectSet::Tag(MobEffectTagId(id)),
make_entry: MobEffectId,
make_list: MobEffectSet::Effects,
},
)
}

fn consumable(&self, tag: &NbtTag) -> Result<Consumable, SavedError> {
let map = self.map(tag)?;
let consume_seconds = match map.get("consume_seconds") {
Some(tag) => {
let seconds = self.float(tag)?;
if !is_use_duration(seconds) {
return Err(SavedError::OutOfRange(self.component));
}
seconds
}
None => Consumable::DEFAULT_CONSUME_SECONDS,
};
let animation = match map.get("animation") {
Some(tag) => UseAnimation::from_name(self.string(tag)?).ok_or(self.bad())?,
None => Consumable::DEFAULT_ANIMATION,
};
let sound = self.optional(map, "sound", Self::sound_event)?;
let has_consume_particles = self.flag(
map,
"has_consume_particles",
Consumable::DEFAULT_HAS_CONSUME_PARTICLES,
)?;
let on_consume_effects = self.listed(map, "on_consume_effects", Self::consume_effect)?;
Ok(Consumable {
consume_seconds,
animation,
sound,
has_consume_particles,
on_consume_effects,
})
}

fn consume_effect(&self, tag: &NbtTag) -> Result<ConsumeEffect, SavedError> {
let map = self.map(tag)?;
let kind = self.identifier(self.necessary(map, "type")?)?;
if kind.namespace() != NAMESPACE {
return Err(self.bad());
}
match kind.path() {
"apply_effects" => {
let effect_depth = self.depth.saturating_add(6);
let effects = self.each(
self.list(self.necessary(map, "effects")?)?,
|reader, tag| {
reader.effect_instance_at(tag, effect_depth, HiddenEffects::Include)
},
)?;
let probability = match map.get("probability") {
Some(tag) => {
let value = self.float(tag)?;
if !is_unit_float(value) {
return Err(SavedError::OutOfRange(self.component));
}
value
}
None => ConsumeEffect::DEFAULT_PROBABILITY,
};
Ok(ConsumeEffect::ApplyEffects {
effects,
probability,
})
}
"remove_effects" => Ok(ConsumeEffect::RemoveEffects(
self.mob_effect_set(self.necessary(map, "effects")?)?,
)),
"clear_all_effects" => Ok(ConsumeEffect::ClearAllEffects),
"teleport_randomly" => {
let diameter = match map.get("diameter") {
Some(tag) => {
let value = self.float(tag)?;
if !is_instrument_range(value) {
return Err(SavedError::OutOfRange(self.component));
}
value
}
None => ConsumeEffect::DEFAULT_TELEPORT_DIAMETER,
};
let directional_particles = self.flag(
map,
"directional_particles",
ConsumeEffect::DEFAULT_DIRECTIONAL_PARTICLES,
)?;
Ok(ConsumeEffect::TeleportRandomly {
diameter,
directional_particles,
})
}
"play_sound" => Ok(ConsumeEffect::PlaySound(
self.sound_event(self.necessary(map, "sound")?)?,
)),
_ => Err(self.bad()),
}
}

fn effect_instance_at(
&self,
tag: &NbtTag,
depth: u32,
hidden: HiddenEffects,
) -> Result<EffectInstance, SavedError> {
let map = self.map(tag)?;
let id = self.mob_effect(self.necessary(map, "id")?)?;
let details = self.effect_details_stripping_hidden(map, depth)?;
let mut details = self.effect_details(map, depth, hidden)?;
let mut effect = EffectInstance::new(id);
effect.amplifier = details.amplifier;
effect.duration = details.duration;
effect.ambient = details.ambient;
effect.show_particles = details.show_particles;
effect.show_icon = details.show_icon;
effect.hidden_effect = details.hidden_effect.take();
Ok(effect)
}

fn effect_details_stripping_hidden(
fn effect_details(
&self,
map: &NbtCompound,
depth: u32,
hidden: HiddenEffects,
) -> Result<EffectDetails, SavedError> {
if depth > MAX_DEPTH {
return Err(SavedError::TooDeep);
Expand All @@ -1041,9 +1164,16 @@ impl<L: NameLookup> Reader<'_, L> {
details.ambient = self.flag(map, "ambient", false)?;
details.show_particles = self.flag(map, "show_particles", true)?;
details.show_icon = self.flag(map, "show_icon", details.show_particles)?;
let _ = self.optional(map, "hidden_effect", |reader, tag| {
reader.effect_details_stripping_hidden(reader.map(tag)?, depth.saturating_add(1))
})?;
details.hidden_effect = self
.optional(map, "hidden_effect", |reader, tag| {
let nested =
reader.effect_details(reader.map(tag)?, depth.saturating_add(1), hidden)?;
Ok(match hidden {
HiddenEffects::Include => Some(Box::new(nested)),
HiddenEffects::Strip => None,
})
})?
.flatten();
Ok(details)
}

Expand Down
Loading
Loading