1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

add example generated metadata.rs

This commit is contained in:
Ubuntu 2022-11-02 18:06:21 +00:00
parent 50f1cc47fa
commit e1ff4f7300
4 changed files with 57 additions and 8 deletions

15
Cargo.lock generated
View file

@ -329,6 +329,7 @@ dependencies = [
"azalea-core",
"azalea-nbt",
"azalea-registry",
"enum-as-inner 0.5.1",
"log",
"nohash-hasher",
"thiserror",
@ -649,6 +650,18 @@ dependencies = [
"syn",
]
[[package]]
name = "enum-as-inner"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9720bba047d567ffc8a3cba48bf19126600e249ab7f128e9233e6376976a116"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "env_logger"
version = "0.9.1"
@ -1969,7 +1982,7 @@ dependencies = [
"async-trait",
"cfg-if",
"data-encoding",
"enum-as-inner",
"enum-as-inner 0.3.4",
"futures-channel",
"futures-io",
"futures-util",

View file

@ -15,6 +15,7 @@ azalea-chat = {path = "../azalea-chat", version = "^0.3.0" }
azalea-core = {path = "../azalea-core", version = "^0.3.0" }
azalea-nbt = {path = "../azalea-nbt", version = "^0.3.0" }
azalea-registry = {path = "../azalea-registry", version = "^0.3.0" }
enum-as-inner = "0.5.1"
log = "0.4.17"
nohash-hasher = "0.2.0"
thiserror = "1.0.34"

View file

@ -2,6 +2,8 @@ use azalea_buf::{BufReadError, McBufVarReadable};
use azalea_buf::{McBuf, McBufReadable, McBufWritable};
use azalea_chat::Component;
use azalea_core::{BlockPos, Direction, GlobalPos, Particle, Slot};
use enum_as_inner::EnumAsInner;
use nohash_hasher::IntSet;
use std::io::{Cursor, Write};
use uuid::Uuid;
@ -42,7 +44,7 @@ impl McBufWritable for EntityMetadata {
}
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, EnumAsInner)]
pub enum EntityDataValue {
Byte(u8),
// varint
@ -53,7 +55,7 @@ pub enum EntityDataValue {
OptionalComponent(Option<Component>),
ItemStack(Slot),
Boolean(bool),
Rotations { x: f32, y: f32, z: f32 },
Rotations(Rotations),
BlockPos(BlockPos),
OptionalBlockPos(Option<BlockPos>),
Direction(Direction),
@ -73,6 +75,13 @@ pub enum EntityDataValue {
PaintingVariant(azalea_registry::PaintingVariant),
}
#[derive(Clone, Debug, McBuf)]
pub struct Rotations {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl McBufReadable for EntityDataValue {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let data_type = u32::var_read_from(buf)?;
@ -85,11 +94,7 @@ impl McBufReadable for EntityDataValue {
5 => EntityDataValue::OptionalComponent(Option::<Component>::read_from(buf)?),
6 => EntityDataValue::ItemStack(Slot::read_from(buf)?),
7 => EntityDataValue::Boolean(bool::read_from(buf)?),
8 => EntityDataValue::Rotations {
x: f32::read_from(buf)?,
y: f32::read_from(buf)?,
z: f32::read_from(buf)?,
},
8 => EntityDataValue::Rotations(Rotations::read_from(buf)?),
9 => EntityDataValue::BlockPos(BlockPos::read_from(buf)?),
10 => EntityDataValue::OptionalBlockPos(Option::<BlockPos>::read_from(buf)?),
11 => EntityDataValue::Direction(Direction::read_from(buf)?),
@ -156,3 +161,32 @@ pub struct VillagerData {
#[var]
level: u32,
}
impl TryFrom<EntityMetadata> for Vec<EntityDataValue> {
type Error = String;
fn try_from(data: EntityMetadata) -> Result<Self, Self::Error> {
let mut data = data.0;
data.sort_by(|a, b| a.index.cmp(&b.index));
let mut prev_indexes = IntSet::default();
let len = data.len();
// check to make sure it's valid, in vanilla this is guaranteed to pass
// but it's possible there's mods that mess with it so we want to make
// sure it's good
for item in &data {
if prev_indexes.contains(&item.index) {
return Err(format!("Index {} is duplicated", item.index));
}
if item.index as usize > len {
return Err(format!("Index {} is too big", item.index));
}
prev_indexes.insert(item.index);
}
let data = data.into_iter().map(|d| d.value).collect();
Ok(data)
}
}

View file

@ -1,5 +1,6 @@
mod data;
mod dimensions;
mod metadata;
use crate::Dimension;
use azalea_block::BlockState;