Skip to main content

ghast_inventory/
mount.rs

1//! Mount inventories (horse, donkey, mule, zombie / skeleton horse, 1.12+ llama) on 1.8.9,
2//! 1.12.2 and 1.16.5: the saddle and armor slots' rules, which depend on the entity, and the
3//! screen's shift-click routes.
4
5use crate::rules::Version;
6use crate::sim::{R, Sim, Unpredictable};
7use crate::stack::Stack;
8
9/// What a mount's saddle and armor slots accept, read off the entity the screen belongs to
10/// (vanilla's `HorseScreenHandler` asks the entity). Give it with
11/// [`Inventory::set_mount_rules`](crate::Inventory::set_mount_rules); without it only stacks the
12/// rules cannot concern (no saddle, horse armor or carpet) are predicted in those slots.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
14#[non_exhaustive]
15pub struct MountRules {
16    /// The saddle slot takes a saddle: 1.8 always; 1.12 every mount but llamas
17    /// (`canBeSaddled`); 1.16.5 a tame adult horse, donkey, mule, zombie or skeleton horse.
18    pub saddle: bool,
19    /// What the armor slot takes.
20    pub armor: MountArmor,
21}
22
23impl MountRules {
24    /// Rules from their parts.
25    #[must_use]
26    pub fn new(saddle: bool, armor: MountArmor) -> MountRules {
27        MountRules { saddle, armor }
28    }
29}
30
31/// The item a mount wears in its armor slot.
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
33#[non_exhaustive]
34pub enum MountArmor {
35    /// Nothing (donkeys, mules, zombie and skeleton horses).
36    #[default]
37    None,
38    /// Horse armor (1.8: horse type 0; 1.12 / 1.16.5: horses): iron, gold and diamond, 1.16.5
39    /// also leather.
40    HorseArmor,
41    /// Carpets (1.12 / 1.16.5 llamas and trader llamas): any colour.
42    Carpet,
43}
44
45impl Version {
46    pub(crate) fn is_saddle(self, s: &Stack) -> bool {
47        match self {
48            Version::V1_8 | Version::V1_12 => s.id == 329,
49            Version::V1_16_5 => self.name(s.id) == "saddle",
50            _ => false,
51        }
52    }
53
54    pub(crate) fn is_horse_armor(self, s: &Stack) -> bool {
55        match self {
56            Version::V1_8 | Version::V1_12 => matches!(s.id, 417..=419),
57            Version::V1_16_5 => matches!(
58                self.name(s.id),
59                "leather_horse_armor"
60                    | "iron_horse_armor"
61                    | "golden_horse_armor"
62                    | "diamond_horse_armor"
63            ),
64            _ => false,
65        }
66    }
67
68    /// 1.12 the carpet block item (171, any colour); 1.16.5 the vanilla `carpets` tag.
69    pub(crate) fn is_carpet(self, s: &Stack) -> bool {
70        match self {
71            Version::V1_12 => s.id == 171,
72            Version::V1_16_5 => self.name(s.id).ends_with("_carpet"),
73            _ => false,
74        }
75    }
76}
77
78/// The saddle slot's `canInsert` (an occupied slot takes nothing; `Err`: the rules are needed
79/// and unknown).
80pub(crate) fn saddle_accepts(
81    v: Version,
82    rules: Option<MountRules>,
83    s: &Stack,
84    occupied: bool,
85) -> R<bool> {
86    if !v.is_saddle(s) || occupied {
87        return Ok(false);
88    }
89    if v == Version::V1_8 {
90        return Ok(true);
91    }
92    rules.map(|r| r.saddle).ok_or(Unpredictable)
93}
94
95/// The armor slot's `canInsert`.
96pub(crate) fn armor_accepts(
97    v: Version,
98    tags: Option<&crate::tags16::ItemTags16>,
99    rules: Option<MountRules>,
100    s: &Stack,
101) -> R<bool> {
102    let horse = v.is_horse_armor(s);
103    let carpet = crate::tags16::carpet(v, tags, s)?;
104    if !horse && !carpet {
105        return Ok(false);
106    }
107    Ok(match rules.ok_or(Unpredictable)?.armor {
108        MountArmor::None => false,
109        MountArmor::HorseArmor => horse,
110        MountArmor::Carpet => carpet,
111    })
112}
113
114impl Sim<'_> {
115    pub(crate) fn mount_saddle_accepts(&self, s: &Stack) -> R<bool> {
116        saddle_accepts(self.v, self.m.mount, s, self.get(0).is_some())
117    }
118
119    pub(crate) fn mount_armor_accepts(&self, s: &Stack) -> R<bool> {
120        armor_accepts(self.v, self.tags, self.m.mount, s)
121    }
122
123    /// `HorseScreenHandler.transferSlot`: the mount's slots go to the player part (last slot
124    /// first); a player stack goes into an empty armor slot it fits, else the saddle slot, else
125    /// the chest. 1.16.5 then moves it between main inventory and hotbar and returns EMPTY
126    /// (`Ok(None)` after the move); 1.8 / 1.12 give up. `Ok(Some(ok))`: the ordinary end, which
127    /// on this screen never compares counts.
128    pub(crate) fn transfer_mount(&mut self, i: usize, st: &mut Stack) -> R<Option<bool>> {
129        let (n, len) = (self.lay.n, self.lay.len);
130        if i < n {
131            return self.insert(st, n, len, true).map(Some);
132        }
133        if n > 1 && self.mount_armor_accepts(st)? && self.get(1).is_none() {
134            return self.insert(st, 1, 2, false).map(Some);
135        }
136        if n > 0 && self.mount_saddle_accepts(st)? {
137            return self.insert(st, 0, 1, false).map(Some);
138        }
139        if n > 2 && self.insert(st, 2, n, false)? {
140            return Ok(Some(true));
141        }
142        if self.v != Version::V1_16_5 {
143            return Ok(Some(false));
144        }
145        let k = n + 27;
146        if (k..len).contains(&i) {
147            self.insert(st, n, k, false)?;
148        } else {
149            self.insert(st, k, len, false)?;
150        }
151        Ok(None)
152    }
153}