Jump to content

Help on understanding CompoundTag and Tag class for structure .nbt file


tios

Recommended Posts

Hello,

I want to create a mod where player create a village to gain ressources and more functions. (mod version is 1.18.1)

To start, i want player to right click on a block and spawn the structure like a miner house or the chef house.

I use the function NBT to see what is inside the CompoundTag and understand it. The problem is that I can get data from the Compound only with the compound.get() even if I get Type of the tag, i can't get ListTag even for blocks in the nbt file.

All method to get data from CompoundTag or Tag doesn't work.

Could someone explain or link a solution, a path to understand ?

This is the Structure class that I have created :

package com.tios.villageum.world.structure;

import com.tios.villageum.Villageum;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.*;
import net.minecraft.network.chat.NbtComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;

import java.io.*;
import java.util.List;
import java.util.zip.GZIPInputStream;

public class Structure {

    public int length;  // X
    public int height;  // Y
    public int width;   // Z

    protected String fileName;
    protected InputStream fileStream;
    private BlockState[][][] blocks;
    private int[][][] blockData;
    private CompoundTag[] entities;
    private CompoundTag[] tileEntities;

    public Structure(String filename){
        this.fileName = filename;
        this.fileStream = Structure.class.getResourceAsStream("/assets/"+Villageum.MOD_ID+"/structs/" + filename + ".nbt");
    }


    public void createStructure(Level level, BlockPos pos){
    }

    public boolean emptyInSizedSpace(Level level, BlockPos pos) {
        return level.getChunkAt(pos).isEmpty();
    }


    public void readFromFile()
    {
        CompoundTag nbtTagCompound = null;
        DataInputStream dataInputStream;
        try
        {
            dataInputStream = new DataInputStream(new GZIPInputStream(this.fileStream));
            nbtTagCompound = NbtIo.read(dataInputStream);
            dataInputStream.close();
        }
        catch (Exception e)
        {
            System.err.println("Villageum Mod: Error loading structure '" + this.fileName + "'");
            return;
        }

        var keys = nbtTagCompound.getAllKeys();

        var size1 = nbtTagCompound.get("size").getAsString().replace("[","").replace("]","").split(",");
        // In schematics, length is z and width is x. Here it is reversed.
        this.length = Integer.parseInt(size1[0]);
        this.height = Integer.parseInt(size1[1]);
        this.width = Integer.parseInt(size1[2]);

        int size = this.length * this.width * this.height;


        blocks = new BlockState[this.height][this.width][this.length];
        blockData = new int[this.height][this.width][this.length];
        NBT(nbtTagCompound, "blocks");
        var tag = nbtTagCompound.get("blocks");
        byte[] blockIdsByte = nbtTagCompound.getByteArray("blocks");
        byte[] blockDataByte = nbtTagCompound.getByteArray("palette");
        int x = 1, y = 1, z = 1;
        for (int i = 0; i < blockIdsByte.length; i++)
        {
            int blockId = (short) (blockIdsByte[i] & 0xFF);
            blocks[y - 1][z - 1][x - 1] = Block.stateById(blockId);
            blockData[y - 1][z - 1][x - 1] = blockDataByte[i];
            x++;
            if (x > this.length)
            {
                x = 1;
                z++;
            }
            if (z > this.width)
            {
                z = 1;
                y++;
            }
        }

        ListTag entityList = nbtTagCompound.getList("Entities", 10);
        entities = new CompoundTag[entityList.size()];
        for (int i = 0; i < entityList.size(); i++)
            entities[i] = entityList.getCompound(i);

        /*
        ListTag tileEntityList = nbtTagCompound.getList("TileEntities", 10);
        tileEntities = new CompoundTag[tileEntityList.size()];
        for (int i = 0; i < tileEntityList.size(); i++)
            tileEntities[i] = tileEntityList.getCompound(i);*/
    }


    private void NBT(CompoundTag compoundTag,String key){
        var id = compoundTag.getId();
        var keys = compoundTag.getAllKeys();
        var contain = compoundTag.contains(key);
        var tag = compoundTag.get(key);
        var type = tag.getType();
        var astring =tag.getAsString();
    }
}
Link to comment
Share on other sites

I am guessing from the import that this is related to StructureTemplates?

Look at the value for StructureTemplate.ENTITIES_TAG and you will see it is all lower case. i.e. you need to change Entities -> entities

 

If that's not the problem, use NBTUtils.prettyPrint() to show the structure of what you are parsing.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.