1use yog_data::kinds::Item;
5use yog_data::with::RefTo;
6
7yog_data::kind! {
10 Attribute = yog_ids::kind::ATTRIBUTE,
11 Effect = yog_ids::kind::EFFECT,
12 Potion = yog_ids::kind::POTION,
13}
14
15#[derive(yog_data::Schema, Debug, Clone, PartialEq)]
19pub struct AttributeDef {
20 pub id: String,
21 pub default: f64,
23 pub min: f64,
24 pub max: f64,
25}
26
27#[derive(yog_data::Schema, Debug, Clone, Copy, PartialEq, Eq)]
30pub enum EffectCategory {
31 Beneficial,
32 Harmful,
33 Neutral,
34}
35
36#[derive(yog_data::Schema, Debug, Clone, Copy, PartialEq, Eq)]
38pub enum ModifierOperation {
39 Add,
41 MultiplyBase,
43 MultiplyTotal,
45}
46
47#[derive(yog_data::Schema, Debug, Clone, PartialEq)]
50pub struct EffectModifier {
51 #[data(with = "RefTo<Attribute>")]
53 pub attribute: String,
54 pub amount: f64,
55 pub operation: ModifierOperation,
56}
57
58#[derive(yog_data::Schema, Debug, Clone, PartialEq)]
62pub struct EffectDef {
63 pub id: String,
64 pub category: EffectCategory,
65 pub color: u32,
67 #[data(default)]
68 pub modifiers: Vec<EffectModifier>,
69}
70
71#[derive(yog_data::Schema, Debug, Clone, PartialEq)]
73pub struct PotionEffect {
74 #[data(with = "RefTo<Effect>")]
76 pub effect: String,
77 pub duration: u32,
79 #[data(default)]
81 pub amplifier: u32,
82}
83
84#[derive(yog_data::Schema, Debug, Clone, PartialEq)]
86pub struct Brew {
87 #[data(with = "RefTo<Item>")]
89 pub from: String,
90 #[data(with = "RefTo<Item>")]
92 pub with: String,
93}
94
95#[derive(yog_data::Schema, Debug, Clone, PartialEq)]
99pub struct PotionDef {
100 pub id: String,
101 pub name: Option<String>,
104 pub effects: Vec<PotionEffect>,
105 #[data(default)]
106 pub brewed: Vec<Brew>,
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112 use yog_data::Schema;
113
114 #[test]
115 fn effects_and_potions_cross_as_yog_data() {
116 let e = EffectDef {
117 id: "mymod:clarity".into(),
118 category: EffectCategory::Beneficial,
119 color: 0xc875ff,
120 modifiers: vec![EffectModifier {
121 attribute: "mymod:grid_zoom".into(),
122 amount: 0.25,
123 operation: ModifierOperation::MultiplyTotal,
124 }],
125 };
126 let back =
127 yog_data::read_bytes::<EffectDef>(&yog_data::binary::encode(&e.write())).unwrap();
128 assert_eq!(back, e);
129 let text = yog_data::text::to_string(&e.write());
130 assert!(
131 text.contains("\"multiply_total\"") && text.contains("\"beneficial\""),
132 "{text}"
133 );
134 let p = PotionDef {
135 id: "mymod:clarity".into(),
136 name: None,
137 effects: vec![PotionEffect {
138 effect: "mymod:clarity".into(),
139 duration: 3600,
140 amplifier: 0,
141 }],
142 brewed: vec![Brew {
143 from: "minecraft:awkward".into(),
144 with: "mymod:dust".into(),
145 }],
146 };
147 assert_eq!(
148 yog_data::read_bytes::<PotionDef>(&yog_data::binary::encode(&p.write())).unwrap(),
149 p
150 );
151 }
152}