Coder Social home page Coder Social logo

rusty-quartz / quartz_nbt Goto Github PK

View Code? Open in Web Editor NEW
25.0 3.0 10.0 167 KB

Provides support for encoding and decoding Minecraft's NBT format. This crate supports both zlib and gz compression, and also provides tools for converting NBT data to stringified NBT (SNBT) and vice versa.

Home Page: https://crates.io/crates/quartz_nbt

License: MIT License

Rust 100.00%
minecraft nbt nbt-parser nbt-library

quartz_nbt's Introduction

quartz_nbt

Documentation Crates.io

Provides support for encoding and decoding Minecraft's NBT format. This crate supports both zlib and gz compression, and also provides tools for converting NBT data to stringified NBT (SNBT) and vice versa.

This crate is the standalone NBT crate for Quartz, a Minecraft server implementation in Rust.

Usage

View the documentation here for examples.

quartz_nbt's People

Contributors

cassy343 avatar esper89 avatar iaiao avatar maddymakesgames avatar vonr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

quartz_nbt's Issues

Strings that look like negative numbers are not quoted

let snbt = quartz_nbt::compound! {"str": "-1"}.to_string(); // "{str:-1}"
assert_eq!(quartz_nbt::snbt::parse(&snbt).unwrap(), quartz_nbt::compound! {"str": -1});

Strings that look like negative numbers are not quoted, potentially leading to incorrect parsing.

This appears to stem from this part of the code

if first.is_whitespace() || first.is_ascii_digit() {

Quartz_nbt Deserializer misses fields depending on the Deserialize implementation of another field.

I'm using quartz_nbt together with serde to deserialize chunk information, and I am getting the following possibly erroneous behaviour.

#[derive(Serialize, Deserialize, Debug)]
pub struct ChunkV1_17 {
    #[serde(rename = "Level")]
    pub level: LevelV1_17,
    #[serde(rename = "DataVersion")]
    pub data_version: i32,
}

pub struct LevelV1_17 {
    #[serde(rename = "xPos")]
    x_pos: i32,

    #[serde(rename = "Biomes")]
    biomes: Option<BiomesV1_17>,
}

type BiomesV1_17 = Array3D<u8, 4, 64, 4>;

impl Serialize for BiomesV1_17 {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        todo!()
    }
}

impl<'de> Deserialize<'de> for BiomesV1_17 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {   
        let data: Vec<i32> = Deserialize::deserialize(deserializer)?;

        Ok( BiomesV1_17::default() )
        
    }
}

Works, but if I don't deserialize any data in my deserialize function like so:

#[derive(Serialize, Deserialize, Debug)]
pub struct ChunkV1_17 {
    #[serde(rename = "Level")]
    pub level: LevelV1_17,
    #[serde(rename = "DataVersion")]
    pub data_version: i32,
}

pub struct LevelV1_17 {
    #[serde(rename = "xPos")]
    x_pos: i32,

    #[serde(rename = "Biomes")]
    biomes: Option<BiomesV1_17>,
}

type BiomesV1_17 = Array3D<u8, 4, 64, 4>;

impl Serialize for BiomesV1_17 {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        todo!()
    }
}

impl<'de> Deserialize<'de> for BiomesV1_17 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {   
        // let data: Vec<i32> = Deserialize::deserialize(deserializer)?;

        Ok( BiomesV1_17::default() )
        
    }
}

I get the following error:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Custom("missing field `xPos`")'

Help using get method for NbtTag types

Hello! I read your documentation on the get method for NbtCompounds:

let mut compound = NbtCompound::new();
compound.insert("test", 1.0f64);
assert_eq!(compound.get::<_, f64>("test"), Ok(1.0f64));

I'm trying to access the value of my 'blocks' tag in my NBT file.
What would the type annotation look like? I've tried the following:

let mut file = File::open("1x1x1-dirt.nbt").unwrap();
let compound = read_nbt_gz_compressed(&mut file).unwrap().0;
let compound = compound.get::<_, NbtTag>("blocks");

Compound looks like:
"{size:[1,1,1],DataVersion:2586,palette:[{Name:\"minecraft:dirt\"}],entities:[],blocks:[{state:0,pos:[0,0,0]}]}"

Appreciate the guidance ๐Ÿ‘

Reading Bedrock level.dat/.mcstructure files

I am trying to use quartz_nbt to read nbt data from Bedrock files but I can't seem to get it to work.
Im getting an Error: TagTypeMismatch { expected: 10, found: 8 } with this code:

use std::fs::File;
use quartz_nbt::io::{self, Flavor, NbtIoError};

fn main() -> Result<(), NbtIoError> {
    let mut bedrock_test = File::open("assets/bedrock/level.dat")?;
    //let mut java_test = File::open("assets/java/level.dat")?;
    
    let nbt = io::read_nbt(&mut bedrock_test, Flavor::Uncompressed)?;

    println!("uncompressed: {:?}", nbt);
    
    Ok(())
}

this works fine with the java files though when the "Flavor" is also set to GzCompressed.
Any help? I do know Bedrock nbt files are Uncompressed I believe.

I can also provide the nbt files if they are needed.

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.