diamondfire_actiondump/
schema.rs

1//! Structs defining the action dump format for use with [`serde`].
2
3use serde::{Deserialize, Serialize};
4
5/// An RGB color.
6#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
7pub struct IconColor {
8    red: u8,
9    green: u8,
10    blue: u8,
11}
12
13/// An argument to a code action.
14///
15/// Sometimes used for intermediate notes.
16#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
17pub struct Argument {
18    /// The input type of the argument.
19    #[serde(rename = "type")]
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub df_type: Option<String>,
22
23    /// Additional text to display with the argument.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub text: Option<String>,
26
27    /// Whether the argument can be repeated.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub plural: Option<bool>,
30    /// Whether the argument is optional.
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub optional: Option<bool>,
33
34    /// The description of the argument.
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub description: Option<Vec<String>>,
37    /// Any notes going along with the argument.
38    ///
39    /// Each element in the outer [`Vec`] is one note.
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub notes: Option<Vec<Vec<String>>>,
42}
43
44/// The display information for a [`CodeData`].
45#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
46#[serde(rename_all = "camelCase")]
47pub struct CodeIcon {
48    /// The display name of this code item.
49    pub name: String,
50    /// The item material.
51    ///
52    /// This is a bukkit material name
53    pub material: String,
54    /// Whether this code item is considered advanced.
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub advanced: Option<bool>,
57
58    /// A description of this code item.
59    pub description: Vec<String>,
60    /// An example of the usage of this code item.
61    pub example: Vec<String>,
62    /// Things that this code item works on/with.
63    pub works_with: Vec<String>,
64    /// Information about the deprecation of this code item.
65    pub deprecated_note: Vec<String>,
66    /// Any additional information about this code item.
67    pub additional_info: Vec<Vec<String>>,
68
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub arguments: Option<Vec<Argument>>,
71
72    /// The return type of this game value.
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub return_type: Option<String>,
75    /// The description for the returned value.
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub return_description: Option<Vec<String>>,
78
79    /// The rank required to use this code item.
80    pub required_rank: String,
81    /// Whether this code item requires a purchase in the token shop to use.
82    pub require_tokens: bool,
83    /// Supposedly `true` when the following conditions are met:
84    /// 1. `required_rank` is not empty
85    /// 2. `require_tokens` is `true`
86    ///
87    /// Seems to be broken and always `false`.
88    pub require_rank_and_tokens: bool,
89
90    /// Whether this action can only be used on mobs.
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub mobs_only: Option<bool>,
93
94    /// Whether this event can be cancelled.
95    #[serde(skip_serializing_if = "Option::is_none")]
96    pub cancellable: Option<bool>,
97    /// Whether this event is automatically cancelled.
98    #[serde(skip_serializing_if = "Option::is_none")]
99    pub cancelled_automatically: Option<bool>,
100
101    /// The texture data for the item if the material is `PLAYER_HEAD`.
102    #[serde(skip_serializing_if = "Option::is_none")]
103    pub head: Option<String>,
104    /// The color data for the item.
105    #[serde(skip_serializing_if = "Option::is_none")]
106    pub color: Option<IconColor>,
107
108    /// A boolean encoded as 0 or 1 denoting whether this action has any tags.
109    #[serde(skip_serializing_if = "Option::is_none")]
110    pub tags: Option<u8>,
111    /// Unknown.
112    ///
113    /// Always an empty [`String`].
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub loaded_item: Option<String>,
116}
117
118impl CodeIcon {
119    /// Returns whether this code item is considered legacy.
120    ///
121    /// This is determined by testing if the material is `STONE`
122    pub fn is_legacy(&self) -> bool {
123        self.material == "STONE"
124    }
125
126    /// Returns whether this code item is deprecated.
127    ///
128    /// This is determined by whether the deprecated note is not empty
129    pub fn is_deprecated(&self) -> bool {
130        !self.deprecated_note.is_empty()
131    }
132}
133
134/// A data element in the DiamondFire action dump.
135#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
136#[serde(rename_all = "camelCase")]
137pub struct CodeData {
138    /// The human readable name of this code item.
139    #[serde(skip_serializing_if = "Option::is_none")]
140    pub name: Option<String>,
141
142    /// The identifier of this code item.
143    #[serde(alias = "id")]
144    #[serde(skip_serializing_if = "Option::is_none")]
145    pub identifier: Option<String>,
146
147    /// The bukkit enum constant name for this code item.
148    ///
149    /// Only applicable for particles, potions, and sounds.
150    #[serde(alias = "particle")]
151    #[serde(alias = "potion")]
152    #[serde(alias = "sound")]
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub bukkit_name: Option<String>,
155
156    /// The [`CodeIcon`] used to display this code item.
157    #[serde(alias = "icon")]
158    #[serde(skip_serializing_if = "Option::is_none")]
159    pub item: Option<CodeIcon>,
160
161    /// Alternate names that may be used to identify this code item.
162    #[serde(skip_serializing_if = "Option::is_none")]
163    pub aliases: Option<Vec<String>>,
164
165    /// The name of this codeblock.
166    #[serde(skip_serializing_if = "Option::is_none")]
167    pub codeblock_name: Option<String>,
168    // TODO: Documentation
169    pub sub_action_blocks: Option<Vec<String>>,
170
171    /// The category of this code item.
172    ///
173    /// Only used for game values, particles, and sounds.
174    #[serde(skip_serializing_if = "Option::is_none")]
175    pub category: Option<String>,
176    /// Whether this category has any sub-categories.
177    #[serde(skip_serializing_if = "Option::is_none")]
178    pub has_sub_categories: Option<bool>,
179
180    /// The tag options for this code item.
181    #[serde(skip_serializing_if = "Option::is_none")]
182    pub tags: Option<Vec<CodeData>>,
183
184    /// The fields of this particle effect.
185    #[serde(skip_serializing_if = "Option::is_none")]
186    pub fields: Option<Vec<String>>,
187
188    /// The default option for this tag.
189    #[serde(skip_serializing_if = "Option::is_none")]
190    pub default_option: Option<String>,
191    /// The list of options in this tag.
192    #[serde(skip_serializing_if = "Option::is_none")]
193    pub options: Option<Vec<CodeData>>,
194
195    /// The slot this item goes in within an inventory.
196    #[serde(alias = "guiSlot")]
197    #[serde(skip_serializing_if = "Option::is_none")]
198    pub slot: Option<u8>,
199
200    /// The purchasable items within this shop.
201    #[serde(skip_serializing_if = "Option::is_none")]
202    pub purchasables: Option<Vec<CodeData>>,
203
204    /// The price of this shop item.
205    #[serde(skip_serializing_if = "Option::is_none")]
206    pub price: Option<u16>,
207    /// The currency used to purchase this shop item.
208    #[serde(skip_serializing_if = "Option::is_none")]
209    pub currency_type: Option<String>,
210}
211
212impl CodeData {
213    /// Returns whether this code item is considered legacy.
214    ///
215    /// This forwards to the method on `item` if it is present, otherwise returning false.
216    pub fn is_legacy(&self) -> bool {
217        match &self.item {
218            Some(item) => item.is_legacy(),
219            _ => false,
220        }
221    }
222
223    /// Returns whether this code item is deprecated.
224    ///
225    /// This forwards to the method on `item` if it is present, otherwise returning false.
226    pub fn is_deprecated(&self) -> bool {
227        match &self.item {
228            Some(item) => item.is_deprecated(),
229            _ => false,
230        }
231    }
232
233    /// Returns whether this code action is dynamic.
234    ///
235    /// This occurs when a code block does not have any selectable actions but needs to take in options.
236    /// This is tested by determining if the name is `dynamic`.
237    pub fn is_dynamic(&self) -> bool {
238        match &self.name {
239            Some(name) => name == "dynamic",
240            _ => false,
241        }
242    }
243}