ghast_ext/fishing.rs
1//! Fishing: cast, wait for a bite, reel in (see [`ghast_client::fishing`] for what the client
2//! knows about the bobber and how exact the bite is on each version).
3
4use ghast_client::fishing::{Bobber, FishingEvent};
5use ghast_client::{Buttons, Interests};
6
7use crate::events::Event;
8use crate::{Bot, Error};
9
10/// Ticks a cast or a reel-in waits for the server's answer.
11const ANSWER_TICKS: u64 = 40;
12
13impl Bot {
14 /// The bot's own fishing bobber, while one is out (see
15 /// [`ghast_client::TickContext::fishing`]).
16 ///
17 /// ```no_run
18 /// # async fn f(bot: ghast_ext::Bot) -> Result<(), ghast_ext::Error> {
19 /// if let Some(b) = bot.bobber().await? {
20 /// println!("bobber {} at {:?}, biting: {}", b.id, b.position, b.biting);
21 /// }
22 /// # Ok(()) }
23 /// ```
24 pub async fn bobber(&self) -> Result<Option<Bobber>, Error> {
25 self.with(|ctx| ctx.fishing().copied()).await
26 }
27
28 /// Casts the held fishing rod: one right click (a held button would repeat the use every
29 /// four ticks), then waits up to 40 ticks for the server's bobber. Asks for entity
30 /// tracking ([`Interests::ENTITY_TRACKING`]) when no tracking is on. Aim first (the server
31 /// throws the bobber along the look the movement packets carry). [`Error::NothingHeld`] /
32 /// [`Error::CannotUse`] without a rod in the main hand, [`Error::AlreadyCast`] when a
33 /// bobber is out (reel it in first), [`Error::Timeout`] when no bobber appears.
34 ///
35 /// ```no_run
36 /// use ghast_client::Vec3;
37 /// # async fn f(bot: ghast_ext::Bot) -> Result<(), ghast_ext::Error> {
38 /// bot.equip("fishing_rod").await?;
39 /// bot.look_at(Vec3::new(10.5, 62.0, 4.5)).await?;
40 /// let bobber = bot.cast().await?;
41 /// # let _ = bobber;
42 /// # Ok(()) }
43 /// ```
44 pub async fn cast(&self) -> Result<Bobber, Error> {
45 self.ensure_tracking().await;
46 let mut ev = self.events();
47 let mark = self.mark();
48 self.with(|ctx| -> Result<(), Error> {
49 if ctx.fishing().is_some() {
50 return Err(Error::AlreadyCast);
51 }
52 rod_held(ctx)
53 })
54 .await??;
55 self.rod_click().await?;
56 let wait = async {
57 while let Some(e) = ev.recv().await {
58 if let Event::Fishing(FishingEvent::Cast { .. }) = e
59 && let Some(b) = self.bobber().await?
60 {
61 return Ok(b);
62 }
63 }
64 Err(Error::Disconnected)
65 };
66 let r = self.timeout(ANSWER_TICKS, wait).await?;
67 self.check(mark)?;
68 r
69 }
70
71 /// Waits up to `ticks` for a fish to bite our bobber (exact on 1.16.5 and 26.x, the
72 /// splash-sound heuristic on 1.8.9 and 1.12.2: see [`ghast_client::fishing`]). A bite that
73 /// is still on returns at once. [`Error::NoBobber`] without a bobber or when it goes away
74 /// while waiting, [`Error::Timeout`] when nothing bites in time.
75 ///
76 /// ```no_run
77 /// # async fn f(bot: ghast_ext::Bot) -> Result<(), ghast_ext::Error> {
78 /// bot.cast().await?;
79 /// bot.wait_for_bite(20 * 60).await?;
80 /// bot.reel_in().await?;
81 /// # Ok(()) }
82 /// ```
83 pub async fn wait_for_bite(&self, ticks: u64) -> Result<(), Error> {
84 let mut ev = self.events();
85 let Some(b) = self.bobber().await? else {
86 return Err(Error::NoBobber);
87 };
88 if b.biting {
89 return Ok(());
90 }
91 let id = b.id;
92 let wait = async {
93 while let Some(e) = ev.recv().await {
94 match e {
95 Event::Fishing(FishingEvent::Bite { id: i }) if i == id => return Ok(()),
96 Event::Fishing(FishingEvent::Gone { id: i }) if i == id => {
97 return Err(Error::NoBobber);
98 }
99 _ => {}
100 }
101 }
102 Err(Error::Disconnected)
103 };
104 self.timeout(ticks, wait).await?
105 }
106
107 /// Reels the bobber in: one right click with the rod, then waits up to 40 ticks for the
108 /// server to remove the bobber (a fish that bit lands in the inventory a little later:
109 /// the server throws the catch towards the player as an item entity). [`Error::NoBobber`]
110 /// without a bobber, [`Error::Timeout`] when it stays.
111 pub async fn reel_in(&self) -> Result<(), Error> {
112 let mut ev = self.events();
113 let Some(b) = self.bobber().await? else {
114 return Err(Error::NoBobber);
115 };
116 self.with(|ctx| rod_held(ctx)).await??;
117 self.rod_click().await?;
118 let id = b.id;
119 let wait = async {
120 loop {
121 if self.bobber().await?.is_none_or(|b| b.id != id) {
122 return Ok(());
123 }
124 match ev.recv().await {
125 Some(Event::Fishing(FishingEvent::Gone { id: i })) if i == id => {
126 return Ok(());
127 }
128 Some(_) => {}
129 None => return Err(Error::Disconnected),
130 }
131 }
132 };
133 self.timeout(ANSWER_TICKS, wait).await?
134 }
135
136 /// One catch: holds a fishing rod (equips one when the hand has none), casts along the
137 /// current look, waits for a bite and reels in. A bobber that goes away without a bite
138 /// (it landed on the ground, the fish got away and the server removed it) is cast again.
139 /// Gives up after `ticks` in all ([`Error::Timeout`], the bobber reeled in again). The
140 /// catch flies to the player as an item entity; watch the inventory for it.
141 ///
142 /// ```no_run
143 /// use ghast_client::Vec3;
144 /// # async fn f(bot: ghast_ext::Bot) -> Result<(), ghast_ext::Error> {
145 /// bot.look_at(Vec3::new(10.5, 62.0, 4.5)).await?; // at the water
146 /// for _ in 0..10 {
147 /// bot.fish(20 * 120).await?;
148 /// bot.wait_ticks(10).await;
149 /// }
150 /// # Ok(()) }
151 /// ```
152 pub async fn fish(&self, ticks: u64) -> Result<(), Error> {
153 let rod = self
154 .held_item()
155 .await?
156 .is_some_and(|s| crate::item_name(self.version(), s.id()) == "fishing_rod");
157 if !rod {
158 self.equip("fishing_rod").await?;
159 }
160 let attempt = async {
161 loop {
162 if self.bobber().await?.is_none() {
163 self.cast().await?;
164 }
165 // the outer timeout ends the wait
166 match self.wait_for_bite(ticks).await {
167 Ok(()) => return self.reel_in().await,
168 Err(Error::NoBobber) => {}
169 Err(e) => return Err(e),
170 }
171 }
172 };
173 match self.timeout(ticks, attempt).await {
174 Ok(r) => r,
175 Err(e) => {
176 if self.bobber().await.ok().flatten().is_some() {
177 let _ = self.reel_in().await;
178 }
179 Err(e)
180 }
181 }
182 }
183
184 /// Turns entity tracking on for the bobber when no tracking is on.
185 async fn ensure_tracking(&self) {
186 let on = self
187 .with(|ctx| ctx.entities().is_some())
188 .await
189 .unwrap_or(true);
190 if !on {
191 self.require(Interests::ENTITY_TRACKING);
192 self.next_tick().await;
193 self.next_tick().await;
194 }
195 }
196
197 /// One right click: the button down for one tick, then up.
198 async fn rod_click(&self) -> Result<(), Error> {
199 self.with(|ctx| {
200 ctx.set_buttons(Buttons {
201 left: false,
202 right: true,
203 });
204 })
205 .await?;
206 self.next_tick().await;
207 self.with(|ctx| ctx.set_buttons(Buttons::default())).await
208 }
209}
210
211/// A fishing rod is in the main hand.
212fn rod_held(ctx: &ghast_client::TickContext<'_>) -> Result<(), Error> {
213 let held = ctx.inventory().held().ok_or(Error::NothingHeld)?;
214 if crate::item_name(ctx.version(), held.id()) == "fishing_rod" {
215 Ok(())
216 } else {
217 Err(Error::CannotUse)
218 }
219}