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();
}
}