Skip to content

Commit 27068bf

Browse files
committed
Merge branch 'ecs_rewrite' into ecs-inventory-interact
2 parents 5e150c7 + 8ea57ac commit 27068bf

File tree

15 files changed

+400
-27
lines changed

15 files changed

+400
-27
lines changed

crates/packet_inspector/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ edition = "2021"
55
description = "A simple Minecraft proxy for inspecting packets."
66

77
[dependencies]
8-
valence_protocol = { path = "../valence_protocol", version = "0.1.0", features = ["compression"] }
9-
clap = { version = "4.0.30", features = ["derive"] }
10-
tokio = { version = "1", features = ["full"] }
118
anyhow = "1"
9+
clap = { version = "4.0.30", features = ["derive"] }
1210
regex = "1.6.0"
11+
tokio = { version = "1", features = ["full"] }
12+
tracing-subscriber = "0.3.16"
13+
valence_protocol = { path = "../valence_protocol", version = "0.1.0", features = ["compression"] }

crates/packet_inspector/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ For instance, if you only want to print the packets `Foo`, `Bar`, and `Baz`, you
3131
as `^(Foo|Bar|Baz)$` with the `-i` flag.
3232

3333
```sh
34-
cargo r -r -p packet_inspector -- 127.0.0.1:25566 127.0.0.1:25565 '^(Foo|Bar|Baz)$'
34+
cargo r -r -p packet_inspector -- 127.0.0.1:25566 127.0.0.1:25565 -i '^(Foo|Bar|Baz)$'
3535
```
3636

3737
Packets are printed to `stdout` while errors are printed to `stderr`. If you only want to see errors in your terminal,

crates/packet_inspector/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
1313
use tokio::net::{TcpListener, TcpStream};
1414
use tokio::sync::Semaphore;
1515
use tokio::task::JoinHandle;
16+
use tracing_subscriber::filter::LevelFilter;
1617
use valence_protocol::packets::c2s::handshake::Handshake;
1718
use valence_protocol::packets::c2s::login::{EncryptionResponse, LoginStart};
1819
use valence_protocol::packets::c2s::play::C2sPlayPacket;
@@ -111,6 +112,10 @@ impl State {
111112

112113
#[tokio::main]
113114
async fn main() -> Result<(), Box<dyn Error>> {
115+
tracing_subscriber::fmt()
116+
.with_max_level(LevelFilter::DEBUG)
117+
.init();
118+
114119
let cli = Arc::new(Cli::parse());
115120

116121
let sema = Arc::new(Semaphore::new(cli.max_connections.unwrap_or(100_000)));

crates/valence/src/server/packet_manager.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt;
12
use std::io::ErrorKind;
23
use std::time::Duration;
34

@@ -60,7 +61,7 @@ where
6061

6162
pub async fn recv_packet<'a, P>(&'a mut self) -> Result<P>
6263
where
63-
P: DecodePacket<'a>,
64+
P: DecodePacket<'a> + fmt::Debug,
6465
{
6566
timeout(self.timeout, async {
6667
while !self.dec.has_next_packet()? {
@@ -259,7 +260,7 @@ pub struct PlayPacketReceiver {
259260
impl PlayPacketReceiver {
260261
pub fn try_next_packet<'a, P>(&'a mut self) -> Result<Option<P>>
261262
where
262-
P: DecodePacket<'a>,
263+
P: DecodePacket<'a> + fmt::Debug,
263264
{
264265
self.dec.try_next_packet()
265266
}

crates/valence_nbt/src/from_binary_slice.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ pub fn from_binary_slice(slice: &mut &[u8]) -> Result<(Compound, String)> {
1313
let mut state = DecodeState { slice, depth: 0 };
1414

1515
let root_tag = state.read_tag()?;
16+
17+
// For cases such as Block Entity Data in the
18+
// ChunkUpdateAndUpdateLight Packet
19+
// https://wiki.vg/Protocol#Chunk_Data_and_Update_Light
20+
if root_tag == Tag::End {
21+
return Ok((Compound::new(), String::new()));
22+
}
23+
1624
if root_tag != Tag::Compound {
1725
return Err(Error::new_owned(format!(
1826
"expected root tag for compound (got {root_tag})",

crates/valence_protocol/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ build = "build/main.rs"
77
[dependencies]
88
aes = { version = "0.7.5", optional = true }
99
anyhow = "1.0.66"
10-
bitfield-struct = "0.1.7"
10+
bitfield-struct = "0.3.1"
1111
byteorder = "1.4.3"
1212
bytes = "1.2.1"
1313
cfb8 = { version = "0.7.1", optional = true }
1414
flate2 = { version = "1.0.24", optional = true }
1515
serde = { version = "1.0.147", features = ["derive"] }
1616
serde_json = "1.0.87"
1717
thiserror = "1.0.37"
18-
uuid = "1.2.1"
18+
tracing = "0.1.37"
19+
uuid = { version = "1.2.1", features = ["serde"] }
1920
valence_derive = { version = "0.1.0", path = "../valence_derive" }
2021
valence_nbt = { version = "0.5.0", path = "../valence_nbt" }
2122

crates/valence_protocol/src/codec.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
use std::fmt;
2+
13
#[cfg(feature = "encryption")]
24
use aes::cipher::{AsyncStreamCipher, NewCipher};
35
use anyhow::{bail, ensure};
46
use bytes::{Buf, BufMut, BytesMut};
7+
use tracing::debug;
58

69
use crate::var_int::{VarInt, VarIntDecodeError};
710
use crate::{DecodePacket, Encode, EncodePacket, Result, MAX_PACKET_SIZE};
@@ -285,7 +288,7 @@ impl PacketDecoder {
285288

286289
pub fn try_next_packet<'a, P>(&'a mut self) -> Result<Option<P>>
287290
where
288-
P: DecodePacket<'a>,
291+
P: DecodePacket<'a> + fmt::Debug,
289292
{
290293
self.buf.advance(self.cursor);
291294
self.cursor = 0;
@@ -345,11 +348,13 @@ impl PacketDecoder {
345348
#[cfg(not(feature = "compression"))]
346349
let packet = P::decode_packet(&mut r)?;
347350

348-
ensure!(
349-
r.is_empty(),
350-
"packet contents were not read completely ({} bytes remain)",
351-
r.len()
352-
);
351+
if !r.is_empty() {
352+
let remaining = r.len();
353+
354+
debug!("packet after partial decode ({remaining} bytes remain): {packet:?}");
355+
356+
bail!("packet contents were not read completely ({remaining} bytes remain)");
357+
}
353358

354359
let total_packet_len = VarInt(packet_len).written_size() + packet_len as usize;
355360
self.cursor = total_packet_len;

crates/valence_protocol/src/packets/c2s.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,12 @@ pub mod play {
143143
pub message: &'a str,
144144
pub timestamp: u64,
145145
pub salt: u64,
146-
pub signature: &'a [u8],
147-
pub signed_preview: bool,
148-
pub acknowledgement: MessageAcknowledgment<'a>,
146+
pub signature: Option<&'a [u8; 256]>,
147+
pub message_count: VarInt,
148+
// This is a bitset of 20; each bit represents one
149+
// of the last 20 messages received and whether or not
150+
// the message was acknowledged by the client
151+
pub acknowledgement: &'a [u8; 3],
149152
}
150153

151154
#[derive(Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]

crates/valence_protocol/src/packets/s2c.rs

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::raw_bytes::RawBytes;
1010
use crate::text::Text;
1111
use crate::types::{
1212
AttributeProperty, BossBarAction, ChunkDataBlockEntity, Difficulty, GameEventKind, GameMode,
13-
GlobalPos, PlayerAbilitiesFlags, SignedProperty, SoundCategory, SyncPlayerPosLookFlags,
14-
TagGroup,
13+
GlobalPos, PlayerAbilitiesFlags, SignedProperty, SoundCategory, Statistic,
14+
SyncPlayerPosLookFlags, TagGroup,
1515
};
1616
use crate::username::Username;
1717
use crate::var_int::VarInt;
@@ -21,7 +21,10 @@ use crate::LengthPrefixedArray;
2121
pub mod commands;
2222
pub mod declare_recipes;
2323
pub mod particle;
24+
pub mod player_chat_message;
2425
pub mod player_info_update;
26+
pub mod set_equipment;
27+
pub mod update_advancements;
2528
pub mod update_recipe_book;
2629

2730
pub mod status {
@@ -102,7 +105,10 @@ pub mod login {
102105
pub mod play {
103106
use commands::Node;
104107
pub use particle::ParticleS2c;
108+
pub use player_chat_message::PlayerChatMessage;
105109
pub use player_info_update::PlayerInfoUpdate;
110+
pub use set_equipment::SetEquipment;
111+
pub use update_advancements::UpdateAdvancements;
106112
pub use update_recipe_book::UpdateRecipeBook;
107113

108114
use super::*;
@@ -149,6 +155,12 @@ pub mod play {
149155
pub animation: u8, // TODO: use Animation enum.
150156
}
151157

158+
#[derive(Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]
159+
#[packet_id = 0x04]
160+
pub struct AwardStatistics {
161+
pub statistics: Vec<Statistic>,
162+
}
163+
152164
#[derive(Copy, Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]
153165
#[packet_id = 0x05]
154166
pub struct AcknowledgeBlockChange {
@@ -351,6 +363,29 @@ pub mod play {
351363
pub block_light_arrays: &'a [LengthPrefixedArray<u8, 2048>],
352364
}
353365

366+
#[derive(Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]
367+
#[packet_id = 0x21]
368+
pub struct WorldEvent {
369+
pub event: i32,
370+
pub location: BlockPos,
371+
pub data: i32,
372+
pub disable_relative_volume: bool,
373+
}
374+
375+
#[derive(Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]
376+
#[packet_id = 0x23]
377+
pub struct UpdateLight {
378+
pub chunk_x: VarInt,
379+
pub chunk_z: VarInt,
380+
pub trust_edges: bool,
381+
pub sky_light_mask: Vec<u64>,
382+
pub block_light_mask: Vec<u64>,
383+
pub empty_sky_light_mask: Vec<u64>,
384+
pub empty_block_light_mask: Vec<u64>,
385+
pub sky_light_arrays: Vec<LengthPrefixedArray<u8, 2048>>,
386+
pub block_light_arrays: Vec<LengthPrefixedArray<u8, 2048>>,
387+
}
388+
354389
#[derive(Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]
355390
#[packet_id = 0x24]
356391
pub struct LoginPlay<'a> {
@@ -440,13 +475,6 @@ pub mod play {
440475
pub fov_modifier: f32,
441476
}
442477

443-
#[derive(Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]
444-
#[packet_id = 0x31]
445-
pub struct PlayerChatMessage<'a> {
446-
// TODO: A bunch of crap.
447-
pub data: RawBytes<'a>,
448-
}
449-
450478
#[derive(Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]
451479
#[packet_id = 0x34]
452480
pub struct CombatDeath {
@@ -610,6 +638,14 @@ pub mod play {
610638
pub food_saturation: f32,
611639
}
612640

641+
#[derive(Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]
642+
#[packet_id = 0x55]
643+
pub struct SetPassengers {
644+
/// Vehicle's entity id
645+
pub entity_id: VarInt,
646+
pub passengers: Vec<VarInt>,
647+
}
648+
613649
#[derive(Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]
614650
#[packet_id = 0x59]
615651
pub struct SetSubtitleText(pub Text);
@@ -676,6 +712,14 @@ pub mod play {
676712
pub footer: Text,
677713
}
678714

715+
#[derive(Copy, Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]
716+
#[packet_id = 0x63]
717+
pub struct PickupItem {
718+
pub collected_entity_id: VarInt,
719+
pub collector_entity_id: VarInt,
720+
pub pickup_item_count: VarInt,
721+
}
722+
679723
#[derive(Copy, Clone, Debug, Encode, EncodePacket, Decode, DecodePacket)]
680724
#[packet_id = 0x64]
681725
pub struct TeleportEntity {
@@ -716,6 +760,7 @@ pub mod play {
716760
SpawnExperienceOrb,
717761
SpawnPlayer,
718762
EntityAnimationS2c,
763+
AwardStatistics,
719764
AcknowledgeBlockChange,
720765
SetBlockDestroyStage,
721766
BlockEntityData,
@@ -737,6 +782,8 @@ pub mod play {
737782
WorldBorderInitialize,
738783
KeepAliveS2c,
739784
ChunkDataAndUpdateLight<'a>,
785+
WorldEvent,
786+
UpdateLight,
740787
ParticleS2c,
741788
LoginPlay<'a>,
742789
UpdateEntityPosition,
@@ -763,8 +810,10 @@ pub mod play {
763810
SetDefaultSpawnPosition,
764811
SetEntityMetadata<'a>,
765812
SetEntityVelocity,
813+
SetEquipment,
766814
SetExperience,
767815
SetHealth,
816+
SetPassengers,
768817
SetSubtitleText,
769818
UpdateTime,
770819
SetTitleText,
@@ -773,7 +822,9 @@ pub mod play {
773822
SoundEffect,
774823
SystemChatMessage,
775824
SetTabListHeaderAndFooter,
825+
PickupItem,
776826
TeleportEntity,
827+
UpdateAdvancements<'a>,
777828
UpdateAttributes<'a>,
778829
FeatureFlags<'a>,
779830
DeclareRecipes<'a>,

0 commit comments

Comments
 (0)