From 2777e30e10cd7e9e2f7d1f90da4785d3e09672c8 Mon Sep 17 00:00:00 2001 From: Wyatt Herkamp Date: Sun, 19 Jun 2022 22:10:11 -0400 Subject: [PATCH] Improves NativeType::Switch --- src/api/tests/protocol.rs | 8 +++----- src/models/protocol/types.rs | 23 +++++++++++++++++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/api/tests/protocol.rs b/src/api/tests/protocol.rs index 4b8fdb2..235c46c 100644 --- a/src/api/tests/protocol.rs +++ b/src/api/tests/protocol.rs @@ -1,6 +1,6 @@ use crate::api::protocol::Protocol; use crate::api::tests::get_test_versions; -use crate::models::protocol::PacketDataType; +use crate::models::protocol::{NativeType, PacketDataType}; use std::sync::Arc; use crate::DataResult; @@ -12,12 +12,10 @@ pub fn simple_test() { let protocol = Protocol::new(arc.clone()); let protocol1 = protocol.get_protocol(); match protocol1 { - Ok(_) => { - - } + Ok(_) => {} Err(error) => { println!("{:?} On Version {}", error, arc.minecraft_version); } } } -} +} \ No newline at end of file diff --git a/src/models/protocol/types.rs b/src/models/protocol/types.rs index 098de22..9607956 100644 --- a/src/models/protocol/types.rs +++ b/src/models/protocol/types.rs @@ -11,6 +11,13 @@ pub struct BitField { pub signed: bool, } +#[derive(Debug)] +pub enum SwitchType { + Packet(String), + Type(Box), + Unknown(Value), +} + /// These data types should be available in every version. /// However, they won't break anything if not present /// This can also be known as the Native Types @@ -48,7 +55,7 @@ pub enum NativeType { Container(Vec<(String, Box)>), Switch { compare_to: String, - fields: HashMap, + fields: HashMap, default: Option, }, Void, @@ -184,7 +191,19 @@ impl NativeType { Some( fields .into_iter() - .map(|(k, v)| (k, v.as_str().unwrap_or_default().to_string())) + .map(|(k, v)| { + if let Value::String(value) = v { + if value.starts_with("packet") { + (k, SwitchType::Packet(value)) + } else { + (k, SwitchType::Type(build_inner_type(Value::String(value)))) + } + } else if let Value::Array(array) = v { + (k, SwitchType::Type(build_inner_type(Value::Array(array)))) + } else { + (k, SwitchType::Unknown(v)) + } + }) .collect(), ) } else {