1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
use proc_macro2::TokenStream;
use quote::quote;
use syn::Ident;
use crate::{
parse_macro::{DeclareMenus, Menu},
utils::{to_pascal_case, to_snake_case},
};
pub fn generate(input: &DeclareMenus) -> TokenStream {
let mut slot_mut_match_variants = quote! {};
let mut slot_match_variants = quote! {};
let mut len_match_variants = quote! {};
let mut kind_match_variants = quote! {};
let mut slots_match_variants = quote! {};
let mut contents_match_variants = quote! {};
let mut location_match_variants = quote! {};
let mut player_slots_range_match_variants = quote! {};
let mut player_consts = quote! {};
let mut menu_consts = quote! {};
let mut hotbar_slots_start = 0;
let mut hotbar_slots_end = 0;
let mut inventory_without_hotbar_slots_start = 0;
let mut inventory_without_hotbar_slots_end = 0;
for menu in &input.menus {
slot_mut_match_variants.extend(generate_match_variant_for_slot_mut(menu, true));
slot_match_variants.extend(generate_match_variant_for_slot_mut(menu, false));
len_match_variants.extend(generate_match_variant_for_len(menu));
kind_match_variants.extend(generate_match_variant_for_kind(menu));
slots_match_variants.extend(generate_match_variant_for_slots(menu));
contents_match_variants.extend(generate_match_variant_for_contents(menu));
location_match_variants.extend(generate_match_variant_for_location(menu));
player_slots_range_match_variants
.extend(generate_match_variant_for_player_slots_range(menu));
// this part is only used to generate `Player::is_hotbar_slot`
if menu.name == "Player" {
let mut i = 0;
for field in &menu.fields {
let field_name = &field.name;
let start = i;
i += field.length;
let end = i - 1;
if field_name == "inventory" {
// it only subtracts 8 here since it's inclusive (there's 9 total hotbar slots)
hotbar_slots_start = end - 8;
hotbar_slots_end = end;
inventory_without_hotbar_slots_start = start;
inventory_without_hotbar_slots_end = end - 9;
}
if start == end {
let const_name = Ident::new(
&format!("{}_SLOT", field_name.to_string().to_uppercase()),
field_name.span(),
);
player_consts.extend(quote! {
pub const #const_name: usize = #start;
});
} else {
let const_name = Ident::new(
&format!("{}_SLOTS", field_name.to_string().to_uppercase()),
field_name.span(),
);
player_consts.extend(quote! {
pub const #const_name: RangeInclusive<usize> = #start..=#end;
});
}
}
} else {
menu_consts.extend(generate_menu_consts(menu));
}
}
assert!(hotbar_slots_start != 0 && hotbar_slots_end != 0);
quote! {
impl Player {
pub const HOTBAR_SLOTS: RangeInclusive<usize> = #hotbar_slots_start..=#hotbar_slots_end;
pub const INVENTORY_WITHOUT_HOTBAR_SLOTS: RangeInclusive<usize> = #inventory_without_hotbar_slots_start..=#inventory_without_hotbar_slots_end;
#player_consts
/// Returns whether the given protocol index is in the player's hotbar.
///
/// Equivalent to `Player::HOTBAR_SLOTS.contains(&i)`.
pub fn is_hotbar_slot(i: usize) -> bool {
Self::HOTBAR_SLOTS.contains(&i)
}
}
impl Menu {
#menu_consts
/// Get a mutable reference to the [`ItemStack`] at the given protocol index.
///
/// If you're trying to get an item in a menu without caring about
/// protocol indexes, you should just `match` it and index the
/// [`ItemStack`] you get.
///
/// Use [`Menu::slot`] if you don't need a mutable reference to the slot.
///
/// # Errors
///
/// Returns `None` if the index is out of bounds.
#[inline]
pub fn slot_mut(&mut self, i: usize) -> Option<&mut ItemStack> {
Some(match self {
#slot_mut_match_variants
})
}
/// Get a reference to the [`ItemStack`] at the given protocol index.
///
/// If you're trying to get an item in a menu without caring about
/// protocol indexes, you should just `match` it and index the
/// [`ItemStack`] you get.
///
/// Use [`Menu::slot_mut`] if you need a mutable reference to the slot.
///
/// # Errors
///
/// Returns `None` if the index is out of bounds.
pub fn slot(&self, i: usize) -> Option<&ItemStack> {
Some(match self {
#slot_match_variants
})
}
/// Returns the number of slots in the menu.
#[allow(clippy::len_without_is_empty)]
pub const fn len(&self) -> usize {
match self {
#len_match_variants
}
}
pub fn from_kind(kind: azalea_registry::builtin::MenuKind) -> Self {
match kind {
#kind_match_variants
}
}
/// Return the contents of the menu, including the player's inventory.
///
/// The indexes in this will match up with [`Menu::slot_mut`].
///
/// If you don't want to include the player's inventory, use [`Menu::contents`]
/// instead.
///
/// If you *only* want to include the players inventory, then you can filter by only
/// using the slots in [`Self::player_slots_range`].
pub fn slots(&self) -> Vec<ItemStack> {
match self {
#slots_match_variants
}
}
/// Return the contents of the menu, not including the player's inventory.
///
/// If you want to include the player's inventory, use [`Menu::slots`] instead.
pub fn contents(&self) -> Vec<ItemStack> {
match self {
#contents_match_variants
}
}
pub fn location_for_slot(&self, i: usize) -> Option<MenuLocation> {
Some(match self {
#location_match_variants
})
}
/// Get the range of slot indexes that contain the player's inventory.
///
/// This may be different for each menu.
pub fn player_slots_range(&self) -> RangeInclusive<usize> {
match self {
#player_slots_range_match_variants
}
}
/// Get the range of slot indexes that contain the player's hotbar.
///
/// This may be different for each menu.
///
/// ```
/// # let inventory = azalea_inventory::Menu::Player(azalea_inventory::Player::default());
/// let hotbar_items = &inventory.slots()[inventory.hotbar_slots_range()];
/// ```
pub fn hotbar_slots_range(&self) -> RangeInclusive<usize> {
// hotbar is always last 9 slots in the player's inventory
((*self.player_slots_range().end() - 8)..=*self.player_slots_range().end())
}
/// Get the range of slot indexes that contain the player's inventory, not including the hotbar.
///
/// This may be different for each menu.
pub fn player_slots_without_hotbar_range(&self) -> RangeInclusive<usize> {
(*self.player_slots_range().start()..=*self.player_slots_range().end() - 9)
}
/// Returns whether the given index would be in the player's hotbar.
///
/// Equivalent to `self.hotbar_slots_range().contains(&i)`.
pub fn is_hotbar_slot(&self, i: usize) -> bool {
self.hotbar_slots_range().contains(&i)
}
}
}
}
/// Menu::Player {
/// craft_result,
/// craft,
/// armor,
/// inventory,
/// offhand,
/// } => {
/// match i {
/// 0 => craft_result,
/// 1..=4 => craft,
/// 5..=8 => armor,
/// // ...
/// _ => return None,
/// }
/// } // ...
pub fn generate_match_variant_for_slot_mut(menu: &Menu, mutable: bool) -> TokenStream {
let mut match_arms = quote! {};
let mut i = 0;
for field in &menu.fields {
let field_name = &field.name;
let start = i;
i += field.length;
let end = i - 1;
match_arms.extend(if start == end {
quote! { #start => #field_name, }
} else if start == 0 {
if mutable {
quote! { #start..=#end => &mut #field_name[i], }
} else {
quote! { #start..=#end => &#field_name[i], }
}
} else if mutable {
quote! { #start..=#end => &mut #field_name[i - #start], }
} else {
quote! { #start..=#end => &#field_name[i - #start], }
});
}
generate_matcher(
menu,
"e! {
match i {
#match_arms
_ => return None
}
},
true,
)
}
pub fn generate_match_variant_for_len(menu: &Menu) -> TokenStream {
let length = menu.fields.iter().map(|f| f.length).sum::<usize>();
generate_matcher(
menu,
"e! {
#length
},
false,
)
}
pub fn generate_match_variant_for_kind(menu: &Menu) -> TokenStream {
// azalea_registry::builtin::MenuKind::Generic9x3 => Menu::Generic9x3 {
// contents: Default::default(), player: Default::default() },
let menu_name = &menu.name;
let menu_field_names = if menu.name == "Player" {
// player isn't in MenuKind
return quote! {};
} else {
let mut menu_field_names = quote! {};
for field in &menu.fields {
let field_name = &field.name;
menu_field_names.extend(quote! { #field_name: Default::default(), })
}
quote! { { #menu_field_names } }
};
quote! {
azalea_registry::builtin::MenuKind::#menu_name => Menu::#menu_name #menu_field_names,
}
}
pub fn generate_match_variant_for_slots(menu: &Menu) -> TokenStream {
let mut instructions = quote! {};
let mut length = 0;
for field in &menu.fields {
let field_name = &field.name;
instructions.extend(if field.length == 1 {
quote! { items.push(#field_name.clone()); }
} else {
quote! { items.extend(#field_name.iter().cloned()); }
});
length += field.length;
}
generate_matcher(
menu,
"e! {
let mut items = Vec::with_capacity(#length);
#instructions
items
},
true,
)
}
pub fn generate_match_variant_for_contents(menu: &Menu) -> TokenStream {
let mut instructions = quote! {};
let mut length = 0;
for field in &menu.fields {
let field_name = &field.name;
if field_name == "player" {
continue;
}
instructions.extend(if field.length == 1 {
quote! { items.push(#field_name.clone()); }
} else {
quote! { items.extend(#field_name.iter().cloned()); }
});
length += field.length;
}
generate_matcher(
menu,
"e! {
let mut items = Vec::with_capacity(#length);
#instructions
items
},
true,
)
}
pub fn generate_match_variant_for_location(menu: &Menu) -> TokenStream {
let mut match_arms = quote! {};
let mut i = 0;
let menu_name = Ident::new(&to_pascal_case(&menu.name.to_string()), menu.name.span());
let menu_enum_name = Ident::new(&format!("{menu_name}MenuLocation"), menu_name.span());
for field in &menu.fields {
let field_name = Ident::new(&to_pascal_case(&field.name.to_string()), field.name.span());
let start = i;
i += field.length;
let end = i - 1;
match_arms.extend(if start == end {
quote! { #start => #menu_enum_name::#field_name, }
} else {
quote! { #start..=#end => #menu_enum_name::#field_name, }
});
}
generate_matcher(
menu,
"e! {
MenuLocation::#menu_name(match i {
#match_arms
_ => return None
})
},
false,
)
}
pub fn generate_match_variant_for_player_slots_range(menu: &Menu) -> TokenStream {
// Menu::Player(Player { .. }) => Player::INVENTORY_SLOTS_RANGE,,
// Menu::Generic9x3 { .. } => Menu::GENERIC9X3_SLOTS_RANGE,
// ..
match menu.name.to_string().as_str() {
"Player" => {
quote! {
Menu::Player(Player { .. }) => Player::INVENTORY_SLOTS,
}
}
_ => {
let menu_name = &menu.name;
let menu_slots_range_name = Ident::new(
&format!(
"{}_PLAYER_SLOTS",
to_snake_case(&menu.name.to_string()).to_uppercase()
),
menu.name.span(),
);
quote! {
Menu::#menu_name { .. } => Menu::#menu_slots_range_name,
}
}
}
}
fn generate_menu_consts(menu: &Menu) -> TokenStream {
let mut menu_consts = quote! {};
let mut i = 0;
for field in &menu.fields {
let field_name_start = format!(
"{}_{}",
to_snake_case(&menu.name.to_string()).to_uppercase(),
to_snake_case(&field.name.to_string()).to_uppercase()
);
let field_index_start = i;
i += field.length;
let field_index_end = i - 1;
if field.length == 1 {
let field_name = Ident::new(
format!("{field_name_start}_SLOT").as_str(),
field.name.span(),
);
menu_consts.extend(quote! { pub const #field_name: usize = #field_index_start; });
} else {
let field_name = Ident::new(
format!("{field_name_start}_SLOTS").as_str(),
field.name.span(),
);
menu_consts.extend(quote! { pub const #field_name: RangeInclusive<usize> = #field_index_start..=#field_index_end; });
}
}
menu_consts
}
pub fn generate_matcher(menu: &Menu, match_arms: &TokenStream, needs_fields: bool) -> TokenStream {
let menu_name = &menu.name;
let menu_field_names = if needs_fields {
let mut menu_field_names = quote! {};
for field in &menu.fields {
let field_name = &field.name;
menu_field_names.extend(quote! { #field_name, })
}
menu_field_names
} else {
quote! { .. }
};
let matcher = if menu.name == "Player" {
quote! { (Player { #menu_field_names }) }
} else {
quote! { { #menu_field_names } }
};
quote! {
Menu::#menu_name #matcher => {
#match_arms
},
}
}
|