1use std::ops::RangeInclusive;
2
3use azalea_buf::AzBuf;
4use azalea_registry::item::{ItemStack, ItemStackData, MaxStackSizeExt};
5
6use crate::{
7 AnvilMenuLocation, BeaconMenuLocation, BlastFurnaceMenuLocation, BrewingStandMenuLocation,
8 CartographyTableMenuLocation, Crafter3x3MenuLocation, CraftingMenuLocation,
9 EnchantmentMenuLocation, FurnaceMenuLocation, Generic3x3MenuLocation, Generic9x1MenuLocation,
10 Generic9x2MenuLocation, Generic9x3MenuLocation, Generic9x4MenuLocation, Generic9x5MenuLocation,
11 Generic9x6MenuLocation, GrindstoneMenuLocation, HopperMenuLocation, LecternMenuLocation,
12 LoomMenuLocation, Menu, MenuLocation, MerchantMenuLocation, Player, PlayerMenuLocation,
13 ShulkerBoxMenuLocation, SmithingMenuLocation, SmokerMenuLocation, StonecutterMenuLocation,
14};
15
16#[derive(Clone, Debug)]
18pub enum ClickOperation {
19 Pickup(PickupClick),
20 QuickMove(QuickMoveClick),
21 Swap(SwapClick),
22 Clone(CloneClick),
23 Throw(ThrowClick),
24 QuickCraft(QuickCraftClick),
25 PickupAll(PickupAllClick),
26}
27
28#[derive(Clone, Debug)]
29pub enum PickupClick {
30 Left { slot: Option<u16> },
34 Right { slot: Option<u16> },
38 LeftOutside,
40 RightOutside,
42}
43impl From<PickupClick> for ClickOperation {
44 fn from(click: PickupClick) -> Self {
45 ClickOperation::Pickup(click)
46 }
47}
48
49#[derive(Clone, Debug)]
51pub enum QuickMoveClick {
52 Left { slot: u16 },
54 Right { slot: u16 },
56}
57impl From<QuickMoveClick> for ClickOperation {
58 fn from(click: QuickMoveClick) -> Self {
59 ClickOperation::QuickMove(click)
60 }
61}
62
63#[derive(Clone, Debug)]
65pub struct SwapClick {
66 pub source_slot: u16,
67 pub target_slot: u8,
70}
71
72impl From<SwapClick> for ClickOperation {
73 fn from(click: SwapClick) -> Self {
74 ClickOperation::Swap(click)
75 }
76}
77#[derive(Clone, Debug)]
80pub struct CloneClick {
81 pub slot: u16,
82}
83impl From<CloneClick> for ClickOperation {
84 fn from(click: CloneClick) -> Self {
85 ClickOperation::Clone(click)
86 }
87}
88#[derive(Clone, Debug)]
89pub enum ThrowClick {
90 Single { slot: u16 },
92 All { slot: u16 },
94}
95impl From<ThrowClick> for ClickOperation {
96 fn from(click: ThrowClick) -> Self {
97 ClickOperation::Throw(click)
98 }
99}
100#[derive(Clone, Debug, Eq, PartialEq)]
101pub struct QuickCraftClick {
102 pub kind: QuickCraftKind,
103 pub status: QuickCraftStatus,
104}
105#[derive(Clone, Debug, Eq, PartialEq)]
106pub enum QuickCraftKind {
107 Left,
108 Right,
109 Middle,
110}
111#[derive(Clone, Debug, Eq, PartialEq)]
112pub enum QuickCraftStatusKind {
113 Start,
115 Add,
117 End,
119}
120#[derive(Clone, Debug, Eq, PartialEq)]
121pub enum QuickCraftStatus {
122 Start,
124 Add { slot: u16 },
126 End,
128}
129impl From<QuickCraftStatus> for QuickCraftStatusKind {
130 fn from(status: QuickCraftStatus) -> Self {
131 match status {
132 QuickCraftStatus::Start => QuickCraftStatusKind::Start,
133 QuickCraftStatus::Add { .. } => QuickCraftStatusKind::Add,
134 QuickCraftStatus::End => QuickCraftStatusKind::End,
135 }
136 }
137}
138
139#[derive(Clone, Debug)]
141pub struct PickupAllClick {
142 pub slot: u16,
147 pub reversed: bool,
149}
150impl From<PickupAllClick> for ClickOperation {
151 fn from(click: PickupAllClick) -> Self {
152 ClickOperation::PickupAll(click)
153 }
154}
155
156impl ClickOperation {
157 pub fn slot_num(&self) -> Option<u16> {
161 match self {
162 ClickOperation::Pickup(pickup) => match pickup {
163 PickupClick::Left { slot } => *slot,
164 PickupClick::Right { slot } => *slot,
165 PickupClick::LeftOutside => None,
166 PickupClick::RightOutside => None,
167 },
168 ClickOperation::QuickMove(quick_move) => match quick_move {
169 QuickMoveClick::Left { slot } => Some(*slot),
170 QuickMoveClick::Right { slot } => Some(*slot),
171 },
172 ClickOperation::Swap(swap) => Some(swap.source_slot),
173 ClickOperation::Clone(clone) => Some(clone.slot),
174 ClickOperation::Throw(throw) => match throw {
175 ThrowClick::Single { slot } => Some(*slot),
176 ThrowClick::All { slot } => Some(*slot),
177 },
178 ClickOperation::QuickCraft(quick_craft) => match quick_craft.status {
179 QuickCraftStatus::Start => None,
180 QuickCraftStatus::Add { slot } => Some(slot),
181 QuickCraftStatus::End => None,
182 },
183 ClickOperation::PickupAll(pickup_all) => Some(pickup_all.slot),
184 }
185 }
186
187 pub fn button_num(&self) -> u8 {
188 match self {
189 ClickOperation::Pickup(pickup) => match pickup {
190 PickupClick::Left { .. } => 0,
191 PickupClick::Right { .. } => 1,
192 PickupClick::LeftOutside => 0,
193 PickupClick::RightOutside => 1,
194 },
195 ClickOperation::QuickMove(quick_move) => match quick_move {
196 QuickMoveClick::Left { .. } => 0,
197 QuickMoveClick::Right { .. } => 1,
198 },
199 ClickOperation::Swap(swap) => swap.target_slot,
200 ClickOperation::Clone(_) => 2,
201 ClickOperation::Throw(throw) => match throw {
202 ThrowClick::Single { .. } => 0,
203 ThrowClick::All { .. } => 1,
204 },
205 ClickOperation::QuickCraft(quick_craft) => match quick_craft {
206 QuickCraftClick {
207 kind: QuickCraftKind::Left,
208 status: QuickCraftStatus::Start,
209 } => 0,
210 QuickCraftClick {
211 kind: QuickCraftKind::Right,
212 status: QuickCraftStatus::Start,
213 } => 4,
214 QuickCraftClick {
215 kind: QuickCraftKind::Middle,
216 status: QuickCraftStatus::Start,
217 } => 8,
218 QuickCraftClick {
219 kind: QuickCraftKind::Left,
220 status: QuickCraftStatus::Add { .. },
221 } => 1,
222 QuickCraftClick {
223 kind: QuickCraftKind::Right,
224 status: QuickCraftStatus::Add { .. },
225 } => 5,
226 QuickCraftClick {
227 kind: QuickCraftKind::Middle,
228 status: QuickCraftStatus::Add { .. },
229 } => 9,
230 QuickCraftClick {
231 kind: QuickCraftKind::Left,
232 status: QuickCraftStatus::End,
233 } => 2,
234 QuickCraftClick {
235 kind: QuickCraftKind::Right,
236 status: QuickCraftStatus::End,
237 } => 6,
238 QuickCraftClick {
239 kind: QuickCraftKind::Middle,
240 status: QuickCraftStatus::End,
241 } => 10,
242 },
243 ClickOperation::PickupAll(_) => 0,
244 }
245 }
246
247 pub fn click_type(&self) -> ClickType {
248 match self {
249 ClickOperation::Pickup(_) => ClickType::Pickup,
250 ClickOperation::QuickMove(_) => ClickType::QuickMove,
251 ClickOperation::Swap(_) => ClickType::Swap,
252 ClickOperation::Clone(_) => ClickType::Clone,
253 ClickOperation::Throw(_) => ClickType::Throw,
254 ClickOperation::QuickCraft(_) => ClickType::QuickCraft,
255 ClickOperation::PickupAll(_) => ClickType::PickupAll,
256 }
257 }
258}
259
260#[derive(AzBuf, Clone, Copy, Debug, PartialEq)]
261pub enum ClickType {
262 Pickup = 0,
263 QuickMove = 1,
264 Swap = 2,
265 Clone = 3,
266 Throw = 4,
267 QuickCraft = 5,
268 PickupAll = 6,
269}
270
271impl Menu {
272 pub fn quick_move_stack(&mut self, slot_index: usize) -> ItemStack {
277 let slot = self.slot(slot_index);
278 if slot.is_none() {
279 return ItemStack::Empty;
280 };
281
282 let slot_location = self
283 .location_for_slot(slot_index)
284 .expect("we just checked to make sure the slot is Some above, so this shouldn't be able to error");
285 match slot_location {
286 MenuLocation::Player(l) => match l {
287 PlayerMenuLocation::CraftResult => {
288 self.try_move_item_to_slots(slot_index, Player::INVENTORY_SLOTS);
289 }
290 PlayerMenuLocation::Craft => {
291 self.try_move_item_to_slots(slot_index, Player::INVENTORY_SLOTS);
292 }
293 PlayerMenuLocation::Armor => {
294 self.try_move_item_to_slots(slot_index, Player::INVENTORY_SLOTS);
295 }
296 _ => {
297 if l == PlayerMenuLocation::Inventory {
305 if Player::is_hotbar_slot(slot_index) {
308 self.try_move_item_to_slots(
309 slot_index,
310 Player::INVENTORY_WITHOUT_HOTBAR_SLOTS,
311 );
312 } else {
313 self.try_move_item_to_slots(slot_index, Player::HOTBAR_SLOTS);
314 }
315 } else {
316 self.try_move_item_to_slots(slot_index, self.player_slots_range());
317 }
318 }
319 },
320 MenuLocation::Generic9x1(l) => match l {
321 Generic9x1MenuLocation::Contents => {
322 self.try_move_item_to_slots(slot_index, self.player_slots_range());
323 }
324 Generic9x1MenuLocation::Player => {
325 self.try_move_item_to_slots_or_toggle_hotbar(
326 slot_index,
327 Menu::GENERIC9X1_CONTENTS_SLOTS,
328 );
329 }
330 },
331 MenuLocation::Generic9x2(l) => match l {
332 Generic9x2MenuLocation::Contents => {
333 self.try_move_item_to_slots(slot_index, self.player_slots_range());
334 }
335 Generic9x2MenuLocation::Player => {
336 self.try_move_item_to_slots_or_toggle_hotbar(
337 slot_index,
338 Menu::GENERIC9X2_CONTENTS_SLOTS,
339 );
340 }
341 },
342 MenuLocation::Generic9x3(l) => match l {
343 Generic9x3MenuLocation::Contents => {
344 self.try_move_item_to_slots(slot_index, self.player_slots_range());
345 }
346 Generic9x3MenuLocation::Player => {
347 self.try_move_item_to_slots_or_toggle_hotbar(
348 slot_index,
349 Menu::GENERIC9X3_CONTENTS_SLOTS,
350 );
351 }
352 },
353 MenuLocation::Generic9x4(l) => match l {
354 Generic9x4MenuLocation::Contents => {
355 self.try_move_item_to_slots(slot_index, self.player_slots_range());
356 }
357 Generic9x4MenuLocation::Player => {
358 self.try_move_item_to_slots_or_toggle_hotbar(
359 slot_index,
360 Menu::GENERIC9X4_CONTENTS_SLOTS,
361 );
362 }
363 },
364 MenuLocation::Generic9x5(l) => match l {
365 Generic9x5MenuLocation::Contents => {
366 self.try_move_item_to_slots(slot_index, self.player_slots_range());
367 }
368 Generic9x5MenuLocation::Player => {
369 self.try_move_item_to_slots_or_toggle_hotbar(
370 slot_index,
371 Menu::GENERIC9X5_CONTENTS_SLOTS,
372 );
373 }
374 },
375 MenuLocation::Generic9x6(l) => match l {
376 Generic9x6MenuLocation::Contents => {
377 self.try_move_item_to_slots(slot_index, self.player_slots_range());
378 }
379 Generic9x6MenuLocation::Player => {
380 self.try_move_item_to_slots_or_toggle_hotbar(
381 slot_index,
382 Menu::GENERIC9X6_CONTENTS_SLOTS,
383 );
384 }
385 },
386 MenuLocation::Generic3x3(l) => match l {
387 Generic3x3MenuLocation::Contents => {
388 self.try_move_item_to_slots(slot_index, self.player_slots_range());
389 }
390 Generic3x3MenuLocation::Player => {
391 self.try_move_item_to_slots_or_toggle_hotbar(
392 slot_index,
393 Menu::GENERIC3X3_CONTENTS_SLOTS,
394 );
395 }
396 },
397 MenuLocation::Crafter3x3(l) => match l {
398 Crafter3x3MenuLocation::Contents => {
399 self.try_move_item_to_slots(slot_index, self.player_slots_range());
400 }
401 Crafter3x3MenuLocation::Player => {
402 self.try_move_item_to_slots_or_toggle_hotbar(
403 slot_index,
404 Menu::GENERIC3X3_CONTENTS_SLOTS,
405 );
406 }
407 },
408 MenuLocation::Anvil(l) => match l {
409 AnvilMenuLocation::Player => {
410 self.try_move_item_to_slots_or_toggle_hotbar(
411 slot_index,
412 Menu::ANVIL_FIRST_SLOT..=Menu::ANVIL_SECOND_SLOT,
413 );
414 }
415 _ => {
416 self.try_move_item_to_slots(slot_index, self.player_slots_range());
417 }
418 },
419 MenuLocation::Beacon(l) => match l {
420 BeaconMenuLocation::Payment => {
421 self.try_move_item_to_slots(slot_index, self.player_slots_range());
422 }
423 BeaconMenuLocation::Player => {
424 self.try_move_item_to_slots(
425 slot_index,
426 Menu::BEACON_PAYMENT_SLOT..=Menu::BEACON_PAYMENT_SLOT,
427 );
428 }
429 },
430 MenuLocation::BlastFurnace(l) => match l {
431 BlastFurnaceMenuLocation::Player => {
432 self.try_move_item_to_slots(
433 slot_index,
434 Menu::BLAST_FURNACE_INGREDIENT_SLOT..=Menu::BLAST_FURNACE_FUEL_SLOT,
435 );
436 }
437 _ => {
438 self.try_move_item_to_slots(slot_index, self.player_slots_range());
439 }
440 },
441 MenuLocation::BrewingStand(l) => match l {
442 BrewingStandMenuLocation::Player => {
443 self.try_move_item_to_slots(
444 slot_index,
445 *Menu::BREWING_STAND_BOTTLES_SLOTS.start()
446 ..=Menu::BREWING_STAND_INGREDIENT_SLOT,
447 );
448 }
449 _ => {
450 self.try_move_item_to_slots(slot_index, self.player_slots_range());
451 }
452 },
453 MenuLocation::Crafting(l) => match l {
454 CraftingMenuLocation::Player => {
455 self.try_move_item_to_slots_or_toggle_hotbar(
456 slot_index,
457 Menu::CRAFTING_GRID_SLOTS,
458 );
459 }
460 _ => {
461 self.try_move_item_to_slots(slot_index, self.player_slots_range());
462 }
463 },
464 MenuLocation::Enchantment(l) => match l {
465 EnchantmentMenuLocation::Player => {
466 self.try_move_item_to_slots_or_toggle_hotbar(
467 slot_index,
468 Menu::ENCHANTMENT_ITEM_SLOT..=Menu::ENCHANTMENT_LAPIS_SLOT,
469 );
470 }
471 _ => {
472 self.try_move_item_to_slots(slot_index, self.player_slots_range());
473 }
474 },
475 MenuLocation::Furnace(l) => match l {
476 FurnaceMenuLocation::Player => {
477 self.try_move_item_to_slots(
478 slot_index,
479 Menu::FURNACE_INGREDIENT_SLOT..=Menu::FURNACE_FUEL_SLOT,
480 );
481 }
482 _ => {
483 self.try_move_item_to_slots(slot_index, self.player_slots_range());
484 }
485 },
486 MenuLocation::Grindstone(l) => match l {
487 GrindstoneMenuLocation::Player => {
488 self.try_move_item_to_slots_or_toggle_hotbar(
489 slot_index,
490 Menu::GRINDSTONE_INPUT_SLOT..=Menu::GRINDSTONE_ADDITIONAL_SLOT,
491 );
492 }
493 _ => {
494 self.try_move_item_to_slots(slot_index, self.player_slots_range());
495 }
496 },
497 MenuLocation::Hopper(l) => match l {
498 HopperMenuLocation::Player => {
499 self.try_move_item_to_slots_or_toggle_hotbar(
500 slot_index,
501 Menu::HOPPER_CONTENTS_SLOTS,
502 );
503 }
504 _ => {
505 self.try_move_item_to_slots(slot_index, self.player_slots_range());
506 }
507 },
508 MenuLocation::Lectern(l) => match l {
509 LecternMenuLocation::Player => {
510 self.try_move_item_to_slots_or_toggle_hotbar(
511 slot_index,
512 Menu::LECTERN_BOOK_SLOT..=Menu::LECTERN_BOOK_SLOT,
513 );
514 }
515 _ => {
516 self.try_move_item_to_slots(slot_index, self.player_slots_range());
517 }
518 },
519 MenuLocation::Loom(l) => match l {
520 LoomMenuLocation::Player => {
521 self.try_move_item_to_slots_or_toggle_hotbar(
522 slot_index,
523 Menu::LOOM_BANNER_SLOT..=Menu::LOOM_PATTERN_SLOT,
524 );
525 }
526 _ => {
527 self.try_move_item_to_slots(slot_index, self.player_slots_range());
528 }
529 },
530 MenuLocation::Merchant(l) => match l {
531 MerchantMenuLocation::Player => {
532 self.try_move_item_to_slots_or_toggle_hotbar(
533 slot_index,
534 Menu::MERCHANT_PAYMENTS_SLOTS,
535 );
536 }
537 _ => {
538 self.try_move_item_to_slots(slot_index, self.player_slots_range());
539 }
540 },
541 MenuLocation::ShulkerBox(l) => match l {
542 ShulkerBoxMenuLocation::Player => {
543 self.try_move_item_to_slots_or_toggle_hotbar(
544 slot_index,
545 Menu::SHULKER_BOX_CONTENTS_SLOTS,
546 );
547 }
548 _ => {
549 self.try_move_item_to_slots(slot_index, self.player_slots_range());
550 }
551 },
552 MenuLocation::Smithing(l) => match l {
553 SmithingMenuLocation::Player => {
554 self.try_move_item_to_slots_or_toggle_hotbar(
555 slot_index,
556 Menu::SMITHING_TEMPLATE_SLOT..=Menu::SMITHING_ADDITIONAL_SLOT,
557 );
558 }
559 _ => {
560 self.try_move_item_to_slots(slot_index, self.player_slots_range());
561 }
562 },
563 MenuLocation::Smoker(l) => match l {
564 SmokerMenuLocation::Player => {
565 self.try_move_item_to_slots(
566 slot_index,
567 Menu::SMOKER_INGREDIENT_SLOT..=Menu::SMOKER_FUEL_SLOT,
568 );
569 }
570 _ => {
571 self.try_move_item_to_slots(slot_index, self.player_slots_range());
572 }
573 },
574 MenuLocation::CartographyTable(l) => match l {
575 CartographyTableMenuLocation::Player => {
576 self.try_move_item_to_slots_or_toggle_hotbar(
577 slot_index,
578 Menu::CARTOGRAPHY_TABLE_MAP_SLOT..=Menu::CARTOGRAPHY_TABLE_ADDITIONAL_SLOT,
579 );
580 }
581 _ => {
582 self.try_move_item_to_slots(slot_index, self.player_slots_range());
583 }
584 },
585 MenuLocation::Stonecutter(l) => match l {
586 StonecutterMenuLocation::Player => {
587 self.try_move_item_to_slots_or_toggle_hotbar(
588 slot_index,
589 Menu::STONECUTTER_INPUT_SLOT..=Menu::STONECUTTER_INPUT_SLOT,
590 );
591 }
592 _ => {
593 self.try_move_item_to_slots(slot_index, self.player_slots_range());
594 }
595 },
596 }
597
598 ItemStack::Empty
599 }
600
601 fn try_move_item_to_slots_or_toggle_hotbar(
602 &mut self,
603 slot_index: usize,
604 target_slot_indexes: RangeInclusive<usize>,
605 ) {
606 if !self.try_move_item_to_slots(slot_index, target_slot_indexes) {
607 self.try_move_item_to_slots(
608 slot_index,
609 if self.is_hotbar_slot(slot_index) {
610 self.player_slots_without_hotbar_range()
611 } else {
612 self.hotbar_slots_range()
613 },
614 );
615 }
616 }
617
618 pub fn may_place(&self, _target_slot_index: usize, _item: &ItemStackData) -> bool {
622 true
623 }
624
625 pub fn may_pickup(&self, _source_slot_index: usize) -> bool {
629 true
630 }
631
632 pub fn allow_modification(&self, target_slot_index: usize) -> bool {
634 if !self.may_pickup(target_slot_index) {
635 return false;
636 }
637 let item = self.slot(target_slot_index).unwrap();
638 item.as_present()
641 .is_some_and(|item| self.may_place(target_slot_index, item))
642 }
643
644 pub fn max_stack_size(&self, _target_slot_index: usize) -> i32 {
646 64
647 }
648
649 fn try_move_item_to_slots(
653 &mut self,
654 item_slot_index: usize,
655 target_slot_indexes: RangeInclusive<usize>,
656 ) -> bool {
657 let mut item_slot = self.slot(item_slot_index).unwrap().clone();
658
659 if item_slot.kind().stackable() {
661 for target_slot_index in target_slot_indexes.clone() {
662 self.move_item_to_slot_if_stackable(&mut item_slot, target_slot_index);
663 if item_slot.is_empty() {
664 break;
665 }
666 }
667 }
668
669 if item_slot.is_present() {
671 for target_slot_index in target_slot_indexes {
672 self.move_item_to_slot_if_empty(&mut item_slot, target_slot_index);
673 if item_slot.is_empty() {
674 break;
675 }
676 }
677 }
678
679 let is_source_slot_now_empty = item_slot.is_empty();
680
681 *self.slot_mut(item_slot_index).unwrap() = item_slot;
682 is_source_slot_now_empty
683 }
684
685 fn move_item_to_slot_if_stackable(
688 &mut self,
689 item_slot: &mut ItemStack,
690 target_slot_index: usize,
691 ) {
692 let ItemStack::Present(item) = item_slot else {
693 return;
694 };
695 let target_slot = self.slot(target_slot_index).unwrap();
696 if let ItemStack::Present(target_item) = target_slot {
697 if self.may_place(target_slot_index, item)
699 && target_item.is_same_item_and_components(item)
700 {
701 let slot_item_limit = self.max_stack_size(target_slot_index);
702 let new_target_slot_data = item.split(i32::min(slot_item_limit, item.count) as u32);
703
704 let target_slot = self.slot_mut(target_slot_index).unwrap();
707 *target_slot = ItemStack::from(new_target_slot_data);
708
709 item_slot.update_empty();
710 }
711 }
712 }
713
714 fn move_item_to_slot_if_empty(
715 &mut self,
716 source_item: &mut ItemStack,
717 target_slot_index: usize,
718 ) {
719 let ItemStack::Present(source_item_data) = source_item else {
720 return;
721 };
722 let target_slot = self.slot(target_slot_index).unwrap();
723 if target_slot.is_empty() && self.may_place(target_slot_index, source_item_data) {
724 let slot_item_limit = self.max_stack_size(target_slot_index);
725 let new_target_slot_data =
726 source_item_data.split(i32::min(slot_item_limit, source_item_data.count) as u32);
727 source_item.update_empty();
728
729 let target_slot = self.slot_mut(target_slot_index).unwrap();
730 *target_slot = new_target_slot_data.into();
731 }
732 }
733}