Add VarInt discriminant type (#15)

This commit is contained in:
vagola
2021-09-22 23:29:00 +03:00
committed by GitHub
parent 024786e618
commit 05cf9c6e6b
8 changed files with 250 additions and 75 deletions

View File

@@ -65,7 +65,7 @@ pub enum DecodeError {
},
/// Type id was not parsed as valid enum value.
UnknownEnumType {
type_id: u8,
type_id: usize,
},
TagDecodeError {
tag_decode_error: TagDecodeError,

View File

@@ -1,5 +1,7 @@
use crate::data::chat::Message;
use crate::decoder::Decoder;
use crate::decoder::DecoderReadExt;
use crate::encoder::EncoderWriteExt;
use crate::error::DecodeError;
use byteorder::{ReadBytesExt, WriteBytesExt};
use minecraft_protocol_derive::{Decoder, Encoder};
@@ -19,6 +21,7 @@ pub enum GameClientBoundPacket {
ChunkData(ChunkData),
GameDisconnect(GameDisconnect),
BossBar(BossBar),
EntityAction(EntityAction),
}
impl GameServerBoundPacket {
@@ -55,6 +58,7 @@ impl GameClientBoundPacket {
GameClientBoundPacket::ChunkData(_) => 0x21,
GameClientBoundPacket::JoinGame(_) => 0x25,
GameClientBoundPacket::BossBar(_) => 0x0D,
GameClientBoundPacket::EntityAction(_) => 0x1B,
}
}
@@ -305,6 +309,29 @@ impl BossBar {
}
}
#[derive(Encoder, Decoder, Debug, PartialEq)]
pub struct EntityAction {
#[data_type(with = "var_int")]
pub entity_id: i32,
pub action_id: EntityActionId,
#[data_type(with = "var_int")]
pub jump_boost: i32,
}
#[derive(Encoder, Decoder, Debug, PartialEq)]
#[data_type(with = "var_int")]
pub enum EntityActionId {
StartSneaking,
StopSneaking,
LeaveBad,
StartSprinting,
StopSprinting,
StartJumpWithHorse,
StopJumpWithHorse,
OpenHorseInventory,
StartFlyingWithElytra,
}
#[cfg(test)]
mod tests {
use crate::data::chat::Payload;
@@ -626,4 +653,37 @@ mod tests {
action: BossBarAction::Remove,
}
}
#[test]
fn test_entity_action_encode() {
let entity_action = EntityAction {
entity_id: 12345,
action_id: EntityActionId::StartFlyingWithElytra,
jump_boost: i32::MAX,
};
let mut vec = Vec::new();
entity_action.encode(&mut vec).unwrap();
assert_eq!(
vec,
include_bytes!("../../../test/packet/game/entity_action.dat").to_vec()
);
}
#[test]
fn test_entity_action_decode() {
let mut cursor =
Cursor::new(include_bytes!("../../../test/packet/game/entity_action.dat").to_vec());
let entity_action = EntityAction::decode(&mut cursor).unwrap();
assert_eq!(
entity_action,
EntityAction {
entity_id: 12345,
action_id: EntityActionId::StartFlyingWithElytra,
jump_boost: i32::MAX,
}
);
}
}