1
1
//! See <https://minecraft.fandom.com/wiki/Attribute>.
2
2
3
- use std:: {
4
- collections:: HashMap ,
5
- io:: { Cursor , Write } ,
6
- } ;
3
+ use std:: collections:: HashMap ;
7
4
8
- use azalea_buf:: { BufReadError , McBuf , McBufReadable , McBufWritable } ;
5
+ use azalea_buf:: McBuf ;
6
+ use azalea_core:: resource_location:: ResourceLocation ;
9
7
use bevy_ecs:: component:: Component ;
10
8
use thiserror:: Error ;
11
- use uuid:: { uuid, Uuid } ;
12
9
13
10
#[ derive( Clone , Debug , Component ) ]
14
11
pub struct Attributes {
@@ -19,7 +16,7 @@ pub struct Attributes {
19
16
#[ derive( Clone , Debug ) ]
20
17
pub struct AttributeInstance {
21
18
pub base : f64 ,
22
- modifiers_by_uuid : HashMap < Uuid , AttributeModifier > ,
19
+ modifiers_by_id : HashMap < ResourceLocation , AttributeModifier > ,
23
20
}
24
21
25
22
#[ derive( Clone , Debug , Error ) ]
@@ -30,13 +27,13 @@ impl AttributeInstance {
30
27
pub fn new ( base : f64 ) -> Self {
31
28
Self {
32
29
base,
33
- modifiers_by_uuid : HashMap :: new ( ) ,
30
+ modifiers_by_id : HashMap :: new ( ) ,
34
31
}
35
32
}
36
33
37
34
pub fn calculate ( & self ) -> f64 {
38
35
let mut total = self . base ;
39
- for modifier in self . modifiers_by_uuid . values ( ) {
36
+ for modifier in self . modifiers_by_id . values ( ) {
40
37
match modifier. operation {
41
38
AttributeModifierOperation :: Addition => total += modifier. amount ,
42
39
AttributeModifierOperation :: MultiplyBase => total += self . base * modifier. amount ,
@@ -52,8 +49,8 @@ impl AttributeInstance {
52
49
/// Add a new modifier to this attribute.
53
50
pub fn insert ( & mut self , modifier : AttributeModifier ) -> Result < ( ) , AlreadyPresentError > {
54
51
if self
55
- . modifiers_by_uuid
56
- . insert ( modifier. uuid , modifier)
52
+ . modifiers_by_id
53
+ . insert ( modifier. id . clone ( ) , modifier)
57
54
. is_some ( )
58
55
{
59
56
Err ( AlreadyPresentError )
@@ -62,17 +59,16 @@ impl AttributeInstance {
62
59
}
63
60
}
64
61
65
- /// Remove the modifier with the given UUID from this attribute, returning
62
+ /// Remove the modifier with the given ID from this attribute, returning
66
63
/// the previous modifier is present.
67
- pub fn remove ( & mut self , uuid : & Uuid ) -> Option < AttributeModifier > {
68
- self . modifiers_by_uuid . remove ( uuid )
64
+ pub fn remove ( & mut self , id : & ResourceLocation ) -> Option < AttributeModifier > {
65
+ self . modifiers_by_id . remove ( id )
69
66
}
70
67
}
71
68
72
- #[ derive( Clone , Debug ) ]
69
+ #[ derive( Clone , Debug , McBuf ) ]
73
70
pub struct AttributeModifier {
74
- pub uuid : Uuid ,
75
- pub name : String ,
71
+ pub id : ResourceLocation ,
76
72
pub amount : f64 ,
77
73
pub operation : AttributeModifierOperation ,
78
74
}
@@ -86,50 +82,16 @@ pub enum AttributeModifierOperation {
86
82
87
83
pub fn sprinting_modifier ( ) -> AttributeModifier {
88
84
AttributeModifier {
89
- uuid : uuid ! ( "662A6B8D-DA3E-4C1C-8813-96EA6097278D" ) ,
90
- name : "Sprinting speed boost" . to_string ( ) ,
85
+ id : ResourceLocation :: new ( "sprinting" ) ,
91
86
amount : 0.30000001192092896 ,
92
87
operation : AttributeModifierOperation :: MultiplyTotal ,
93
88
}
94
89
}
95
90
96
- pub static BASE_ATTACK_SPEED_UUID : Uuid = uuid ! ( "FA233E1C-4180-4865-B01B-BCCE9785ACA3" ) ;
97
- pub fn weapon_attack_speed_modifier ( amount : f64 ) -> AttributeModifier {
98
- AttributeModifier {
99
- uuid : BASE_ATTACK_SPEED_UUID ,
100
- name : "Weapon modifier" . to_string ( ) ,
101
- amount,
102
- operation : AttributeModifierOperation :: Addition ,
103
- }
104
- }
105
- pub fn tool_attack_speed_modifier ( amount : f64 ) -> AttributeModifier {
91
+ pub fn base_attack_speed_modifier ( amount : f64 ) -> AttributeModifier {
106
92
AttributeModifier {
107
- uuid : BASE_ATTACK_SPEED_UUID ,
108
- name : "Tool modifier" . to_string ( ) ,
93
+ id : ResourceLocation :: new ( "base_attack_speed" ) ,
109
94
amount,
110
95
operation : AttributeModifierOperation :: Addition ,
111
96
}
112
97
}
113
-
114
- impl McBufReadable for AttributeModifier {
115
- fn read_from ( buf : & mut Cursor < & [ u8 ] > ) -> Result < Self , BufReadError > {
116
- let uuid = Uuid :: read_from ( buf) ?;
117
- let amount = f64:: read_from ( buf) ?;
118
- let operation = AttributeModifierOperation :: read_from ( buf) ?;
119
- Ok ( Self {
120
- uuid,
121
- name : "Unknown synced attribute modifier" . to_string ( ) ,
122
- amount,
123
- operation,
124
- } )
125
- }
126
- }
127
-
128
- impl McBufWritable for AttributeModifier {
129
- fn write_into ( & self , buf : & mut impl Write ) -> Result < ( ) , std:: io:: Error > {
130
- self . uuid . write_into ( buf) ?;
131
- self . amount . write_into ( buf) ?;
132
- self . operation . write_into ( buf) ?;
133
- Ok ( ( ) )
134
- }
135
- }
0 commit comments