Component organization in NBT hierarchy
Adevald opened this issue ยท 5 comments
How to make and store/serialize components in NBT hierarchy?
How to make nbt_compound and store all nbt tags inside it?
Sorry, what is your question exactly? If you want to serialize a component yourself, you can call toTag
. If you want to use an "nbt_compound" when serializing your component, you can use the CompoundTag
class.
Thanks for answering.
I want to create research system, and try to store all open researches inside https://minecraft.gamepedia.com/Player.dat_format in the same way as it is done in "Inventory" section:
- Research (List)
- (Compund)
- research_id : "test_id" (String)
- stage: "3" (Byte)
- (Compund)
- research_id : "test_id_2" (String)
- stage: "1" (Byte)
- (Compund)
- research_id : "test_id_3" (String)
- stage: "0" (Byte)
- (Compund)
As i understand CCF has its own NBT serialization method, but it doesnt support for creating complex hierarchy "out of the box"
So, i dont know how to work Component Serialization exactly, but could here makes some serializaion/deserialization issues if i will use my own Component ser./deser. implementation.
OK, so what I would do is make a ResearchComponent
as such:
public class ResearchComponent implements Component {
private final List<Project> research = new ArrayList<>();
// TODO methods to interact with the rest of your project, like startProject(String researchId),
// or getStage(String researchId)
public CompoundTag toTag(CompoundTag nbt) {
ListTag list = new ListTag();
for (Project project : research) {
CompoundTag projectNbt = new CompoundTag();
projectNbt.putString("research_id", project.id);
projectNbt.putByte("stage", project.stage);
list.add(projectNbt);
}
nbt.put("research", list);
return nbt;
}
public void fromTag(CompoundTag nbt) {
this.research.clear();
ListTag list = nbt.getList("research", NbtType.COMPOUND);
for (CompoundTag projectNbt : list) {
String researchId = projectNbt.getString("research_id");
byte stage = projectNbt.getByte("stage");
this.research.add(new Project(researchId, stage));
}
}
public static class Project {
public final String researchId;
public final byte stage;
// TODO constructor
}
}