Skip to content

Commit 2db0e5e

Browse files
committed
chore: fix style issues
1 parent 3de70d3 commit 2db0e5e

File tree

8 files changed

+132
-107
lines changed

8 files changed

+132
-107
lines changed

src/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct Args {
3131
#[arg(short, long, default_value = "25565", env = "LIMBO_PORT")]
3232
pub port: u16,
3333
// Whether or not to enable Velocity forwarding.
34-
#[arg(short, long, default_value = "true", env = "LIMBO_VELOCITY_FORWARDING")]
34+
#[arg(short, long, env = "LIMBO_VELOCITY_FORWARDING")]
3535
pub velocity_forwarding: bool,
3636
/// The x coordinate of the spawnpoint.
3737
#[arg(short = 'x', long, default_value = "0", env = "LIMBO_SPAWN_X")]

src/net/cache.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ use crate::{
2525
protocol::{
2626
datatypes::VarInt,
2727
packets::{
28-
login::registry::{AllRegistries, Registry, AllTags},
28+
login::registry::{AllRegistries, AllTags, Registry},
2929
play::ChunkDataUpdateLightC,
3030
},
3131
Encoder,
3232
},
3333
world::{blocks::Blocks, BlockEntity, Container, World},
3434
CrawlState,
3535
};
36-
use crate::protocol::packets::login::registry::RegistryItem;
3736

3837
#[derive(Debug)]
3938
pub struct WorldCache {
@@ -57,7 +56,7 @@ impl WorldCache {
5756

5857
let containers = chunks
5958
.iter()
60-
.map(|(_, c)| {
59+
.flat_map(|(_, c)| {
6160
c.block_entities
6261
.iter()
6362
.filter_map(|block_entity| {
@@ -78,9 +77,7 @@ impl WorldCache {
7877
},
7978
);
8079

81-
let Some(block_entity) = block_entity else {
82-
return None;
83-
};
80+
let block_entity = block_entity?;
8481

8582
match block_entity.id.as_str() {
8683
"minecraft:chest" | "minecraft:trapped_chest" | "minecraft:barrel" => {
@@ -97,7 +94,6 @@ impl WorldCache {
9794
})
9895
.collect::<Vec<((i32, i32, i32), Container)>>()
9996
})
100-
.flatten()
10197
.collect();
10298

10399
debug!("Containers: {:?}", containers);
@@ -133,12 +129,10 @@ impl From<&AllTags> for TagCache {
133129
fn from(tags: &AllTags) -> Self {
134130
let mut encoder = Encoder::new();
135131

136-
encoder
137-
.append_packet(tags)
138-
.expect("Failed to encode tags");
132+
encoder.append_packet(tags).expect("Failed to encode tags");
139133

140134
Self {
141-
encoded: encoder.take().to_vec()
135+
encoded: encoder.take().to_vec(),
142136
}
143137
}
144138
}

src/net/io.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl NetIo {
107107
if read_half
108108
.read_buf(&mut buf)
109109
.await
110-
.context("failed read_buf")?
110+
.wrap_err("failed read_buf")?
111111
== 0
112112
{
113113
let mut c = self.connected.write().await;
@@ -132,18 +132,24 @@ impl NetIo {
132132
trace!("raw packet is {} bytes", bytes.len());
133133
trace!("{:?}", bytes.to_vec());
134134
let mut writer = self.write_half.lock().await;
135-
Ok(writer.write_all(&bytes).await?)
135+
writer
136+
.write_all(&bytes)
137+
.await
138+
.wrap_err("failed to write bytes")
136139
}
137140

138141
pub async fn tx_raw(&self, packet: &[u8]) -> Result<()> {
139142
trace!("Sending packet {:?}", packet);
140143
let mut writer = self.write_half.lock().await;
141-
Ok(writer.write_all(packet).await?)
144+
writer
145+
.write_all(packet)
146+
.await
147+
.wrap_err("failed to write bytes")
142148
}
143149

144150
pub async fn rx_raw(&self) -> Result<Frame> {
145151
let mut decoder = self.decoder.lock().await;
146-
if let Some(frame) = decoder.try_read_next().context("failed try_read_next")? {
152+
if let Some(frame) = decoder.try_read_next().wrap_err("failed try_read_next")? {
147153
return Ok(frame);
148154
};
149155

@@ -152,7 +158,12 @@ impl NetIo {
152158

153159
{
154160
let mut reader = self.read_half.lock().await;
155-
if reader.read_buf(&mut buf).await.context("failed read_buf")? == 0 {
161+
if reader
162+
.read_buf(&mut buf)
163+
.await
164+
.wrap_err("failed read_buf")?
165+
== 0
166+
{
156167
let mut c = self.connected.write().await;
157168
*c = false;
158169
return Err(std::io::Error::from(ErrorKind::UnexpectedEof).into());

src/net/player.rs

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::{
2525
time::Duration,
2626
};
2727

28-
use color_eyre::eyre::{bail, Result};
28+
use color_eyre::eyre::{bail, Context, Result};
2929
use rand::Rng;
3030
use serde_json::json;
3131
use thiserror::Error;
@@ -51,13 +51,14 @@ use crate::{
5151
Frame, Packet, ProtocolState,
5252
},
5353
server::window::{Window, WindowType},
54+
world::Container,
5455
CrawlState,
5556
};
5657

58+
use super::{entity::Entity, io::NetIo};
5759
#[cfg(feature = "encryption")]
5860
use crate::protocol::{datatypes::Bytes, packets::login::PluginRequestC};
5961
use crate::protocol::{PacketDirection, PACKETS};
60-
use super::{entity::Entity, io::NetIo};
6162

6263
#[derive(Debug)]
6364
pub struct Player {
@@ -148,7 +149,7 @@ impl SharedPlayer {
148149
);
149150

150151
match self.begin_play().await {
151-
Ok(()) => debug!("Play loop for {} done.", self.id()),
152+
Ok(()) => debug!("Spawned play loop for player {}.", self.id()),
152153
Err(why) => error!("Failed to play player {}! {why}", self.id()),
153154
}
154155
}
@@ -214,9 +215,12 @@ impl SharedPlayer {
214215
self.0.io.tx(&res).await?;
215216
let ping: PingS = self.0.io.rx::<PingS>().await?.decode()?;
216217

217-
self.0.io.tx(&PingC {
218-
payload: ping.payload
219-
}).await?;
218+
self.0
219+
.io
220+
.tx(&PingC {
221+
payload: ping.payload,
222+
})
223+
.await?;
220224

221225
Ok(())
222226
}
@@ -532,7 +536,10 @@ impl SharedPlayer {
532536
let resource = PACKETS
533537
.get_resource_id(packet_state, PacketDirection::Serverbound, frame.id)
534538
.unwrap_or_else(|| {
535-
panic!("{} cannot map to any resource in state {:?}", frame.id, packet_state)
539+
panic!(
540+
"{} cannot map to any resource in state {:?}",
541+
frame.id, packet_state
542+
)
536543
})
537544
.as_str();
538545

@@ -569,7 +576,7 @@ impl SharedPlayer {
569576
}
570577

571578
id => {
572-
debug!(
579+
trace!(
573580
"Got packet with id {id} from player {}, ignoring",
574581
self.0.id
575582
);
@@ -583,49 +590,56 @@ impl SharedPlayer {
583590
let crawlstate = self.0.crawlstate.clone();
584591
let server = crawlstate.get_server().await;
585592

586-
let x = packet.location.x as i32;
587-
let y = packet.location.y as i32;
588-
let z = packet.location.z as i32;
593+
let x = packet.location.x;
594+
let y = packet.location.y;
595+
let z = packet.location.z;
589596

590597
debug!("Player {} clicked at {}, {}, {}", self.id(), x, y, z);
591598

592599
match server.get_container(x, y, z) {
593600
None => (),
594-
Some(container) => {
595-
let id = {
596-
let mut next_window_id = self.0.next_window_id.lock().await;
597-
let id = *next_window_id;
598-
*next_window_id = next_window_id.wrapping_add(1);
599-
if *next_window_id == 0 {
600-
*next_window_id = 1;
601-
}
602-
id
603-
};
604-
605-
let window = Window {
606-
id,
607-
kind: WindowType::Generic9x3,
608-
title: "Hi".into(),
609-
};
610-
611-
self.0.io.tx(&OpenScreenC::from(&window)).await?;
612-
613-
self.0
614-
.io
615-
.tx(&SetContainerContentC {
616-
window_id: id,
617-
// FIXME: track this correctly
618-
state_id: 0,
619-
slot_data: container.0,
620-
carried_item: Slot::default(),
621-
})
622-
.await?;
623-
624-
{
625-
let mut sw = self.0.window.write().await;
626-
*sw = Some(window);
627-
}
601+
Some(container) => self
602+
.open_container(container)
603+
.await
604+
.wrap_err_with(|| format!("failed to open container at {x}, {y}, {z}"))?,
605+
}
606+
607+
Ok(())
608+
}
609+
610+
async fn open_container(&self, container: Container) -> Result<()> {
611+
let id = {
612+
let mut next_window_id = self.0.next_window_id.lock().await;
613+
let id = *next_window_id;
614+
*next_window_id = next_window_id.wrapping_add(1);
615+
if *next_window_id == 0 {
616+
*next_window_id = 1;
628617
}
618+
id
619+
};
620+
621+
let window = Window {
622+
id,
623+
kind: WindowType::Generic9x3,
624+
title: "Hi".into(),
625+
};
626+
627+
self.0.io.tx(&OpenScreenC::from(&window)).await?;
628+
629+
self.0
630+
.io
631+
.tx(&SetContainerContentC {
632+
window_id: id,
633+
// FIXME: track this correctly
634+
state_id: 0,
635+
slot_data: container.0,
636+
carried_item: Slot::default(),
637+
})
638+
.await?;
639+
640+
{
641+
let mut sw = self.0.window.write().await;
642+
*sw = Some(window);
629643
}
630644

631645
Ok(())

src/protocol/datatypes/slot.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{protocol::Encode, server::registries::REGISTRIES, world::Item};
2121

2222
use super::VarInt;
2323

24-
#[derive(Debug, Clone)]
24+
#[derive(Debug, Clone, Default)]
2525
pub struct Slot {
2626
item_count: i8,
2727
item_id: Option<i32>,
@@ -52,17 +52,6 @@ impl From<Item> for Slot {
5252
}
5353
}
5454

55-
impl Default for Slot {
56-
fn default() -> Self {
57-
Self {
58-
item_count: 0,
59-
item_id: None,
60-
components_to_add: None,
61-
components_to_remove: None,
62-
}
63-
}
64-
}
65-
6655
impl Encode for Slot {
6756
fn encode(&self, mut w: impl std::io::Write) -> color_eyre::eyre::Result<()> {
6857
self.item_count.encode(&mut w)?;

0 commit comments

Comments
 (0)