Deepslate

repository·main·Indexed 42 days ago

https://github.com/misode/deepslate

A library for rendering and emulating parts of Minecraft, including NBT file manipulation, structure rendering via WebGL, and noise generation. Version 0.27.1 provides tools for handling BlockPos, ChunkPos, and registry values via the Holder interface, as well as mathematical utilities for linear algebra and procedural generation.

Tokens
19.1K
Snippets
68
Records
95
Agent score
54%

What's inside Deepslate

  1. Use NbtRegion.Ref for serialized regions

    main

    An NbtRegion.Ref is a lightweight version of an NbtRegion created via NbtRegion.fromJson(). It is used when the region data is serialized to JSON to avoid including heavy raw chunk data in the payload.

    Key differences from NbtRegion:

    • It works with NbtChunk.Ref instead of NbtChunk.
    • It does not have write() or toJson() methods.
    • It requires a chunkResolver to asynchronously load chunk data when accessed.
  2. Install deepslate

    main

    You can install deepslate using npm for Node.js/TypeScript projects, or include it via a CDN script tag for browser-based usage.

    npm install deepslate
    <script src="https://unpkg.com/deepslate@0.27.1"></script>
  3. Create an NbtRegion from a Uint8Array

    main

    To create an NbtRegion instance, use NbtRegion.read() and pass it a Uint8Array containing the region file data. This method reads the region file header but does not immediately load any chunks into memory.

    const response = await fetch('./r.0.0.mca')
    const arrayBuffer = await response.arrayBuffer()
    const region = NbtRegion.read(new Uint8Array(arrayBuffer, {}))
  4. Deploy the website to GitHub Pages

    main

    To deploy the website to GitHub Pages, use the yarn deploy command. You must provide your GitHub username via the GIT_USER environment variable. If you are using SSH for Git operations, set USE_SSH=true. This command builds the site and pushes the content to the gh-pages branch.

    $ GIT_USER=<Your GitHub username> USE_SSH=true yarn deploy
  5. Modify and persist NbtChunk data

    main

    To manipulate the data within a chunk, use getRoot() to access the root NbtCompound.

    Important: Whenever you make changes to the chunk's root, you must call markDirty() to ensure changes are tracked. If you call getRaw() on a dirty chunk, the library will automatically write the file first and then mark the chunk as no longer dirty.

  6. Run the website in local development mode

    main

    Start a local development server using yarn start. This command will automatically open a browser window and supports live reloading, meaning most changes are reflected immediately without a server restart.

    $ yarn start
  7. Create an NbtFile from binary data or empty state

    main

    You can instantiate an NbtFile using NbtFile.read when you have an existing Uint8Array of NBT data, or use NbtFile.create to start with an empty file.

    When using NbtFile.read, the library attempts to detect options like compression from the binary data, but littleEndian always defaults to false unless explicitly provided.

    // Reading from an existing file
    const response = await fetch('./example.nbt')
    const arrayBuffer = await response.arrayBuffer()
    const file = NbtFile.read(new Uint8Array(arrayBuffer))
    
    // Creating a new empty file
    const file = NbtFile.create({})
  8. How item model selection logic works

    main

    Deepslate uses several specialized classes to implement complex item rendering logic via the ItemModel interface:

    Condition

    Uses a boolean property to choose between on_true and on_false models. Supported properties include:

    • fishing_rod/cast, selected, carried, extended_view (from context)
    • view_entity (from context)
    • using_item (based on use_duration)
    • broken or damaged (based on damage vs max_damage components)
    • has_component (checks for a specific component ID)
    • keybind_down (checks context for specific keybinds)
    • custom_model_data (checks custom_model_data component flags)

    Select

    Selects a model based on a string value returned by a property. Common properties include:

    • main_hand (e.g., right, left)
    • display_context (e.g., gui, ground)
    • context_dimension (the dimension string)
    • charge_type (e.g., rocket, arrow for fireworks)
    • trim_material (from the trim component)
    • block_state (from the block_state component)
    • custom_model_data (from the strings list in the component)

    RangeDispatch

    Selects a model based on a numeric value. It supports a scale and a list of entries with threshold values. Common properties include:

    • bundle/fullness (calculated weight of bundle contents)
    • damage (can be normalized 0-1 or raw)
    • count (can be normalized 0-1 or raw)
    • cooldown (percentage of cooldown remaining)
    • time (based on daytime, moon_phase, random, or linear game time)
    • use_duration (remaining or elapsed use time)
    • custom_model_data (from the floats list in the component)
  9. Understand NoiseAquifer for complex aquifer generation

    main

    The NoiseAquifer class is a high-performance implementation of the Aquifer interface that uses density functions and a grid-based sampling system to generate complex, noise-driven fluid structures.

    It relies on:

    • AquiferConfig: To drive the noise parameters.
    • PositionalRandom: For jittering grid locations.
    • DensityVolume: To define the bounds of the generation.
    • FluidPicker: To provide the base fluid types (e.g., water vs lava) that the noise will manipulate.