fish_lib/models/item/properties/
stackable.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
4pub struct StackableComponent {
5 #[serde(default = "default_one")]
6 count: u64,
7}
8
9fn default_one() -> u64 {
10 1
11}
12
13impl StackableComponent {
14 pub fn new(count: u64) -> Self {
15 Self { count }
16 }
17
18 pub fn get_count(&self) -> u64 {
19 self.count
20 }
21
22 pub fn set_count(&mut self, count: u64) {
23 self.count = count;
24 }
25
26 pub fn on_use(&mut self, times: u64) {
28 self.count = self.count.saturating_sub(times);
29 }
30
31 pub fn on_add(&mut self, amount: u64) {
32 self.count = self.count.saturating_add(amount);
33 }
34
35 pub fn on_remove(&mut self, amount: u64) {
36 self.count = self.count.saturating_sub(amount);
37 }
38
39 pub fn should_consume(&self) -> bool {
40 self.count == 0
41 }
42}