Sponge Interface for NBT?

Is there a Sponge interface for NBT that is implemented by Minecraft’s NBTTagCompound?

It’d be a lot easier for me if there were as I’m writing a plugin for a mod but don’t want the hassle of setting up a Forge environment.

There are no interfaces for NBT in sponge, and there likely won’t be any in the future. In sponge you are for the most part supposed to use the data API. You could go the route of using an external library that has NBT. I write a library sine time ago to work especially well with sponge, but I wouldn’t consider it completely non experimental yet. https://github.com/Katrix-/SpongeBT

You could also add SpongeCommon as a dependency as it has a NBT library, however by doing this your plugin won’t work on implementations that dont utilize SpongeCommon. Currently that’s not a big deal because both SpongeForge and SpongeVanilla do.

Could also use this

Just out of interest would DataFormats.NBT work?

Depends on what you need. If you just need to read a NBT file, and don’t care about handling a data container it works just fine. However if you want to read a NbtTagCompound from an NBT file and handle it as such you still need another library.

What do you mean when you say handle it?

How would I write a custom data handler?

@ChrisMack
When I say handle it I mean what type of object you want to get back.

Here is an example showing how they differ using DataFormat.NBT (which does not require a library) and SpongeBT (and scala java8 compat).

First let’s make an NBT file.

try(FileOutputStream stream = new FileOutputStream("nbtFile.nbt")) {
   NBTCompound compound = new NBTCompound();
   NBTCompound childCompound = new NBTCompound();

   childCompound.setString("stringTag", "someString");
   compound.setTag("childCompound", childCompound);

   NBTStreamTools.writeTo(stream, compound, "", false);
}
catch(IOException e) {
   e.printStackTrace();
}

Here is how you would read the file using SpongeBT (and scala java8 compat).

try(FileInputStream stream = new FileInputStream("nbtFile.nbt")) {
   NBTCompound compound = NBTStreamTools.readFrom(stream, false)._2;
   Optional<String> string = compound.getJava("childCompound")
         .flatMap(t -> OptionConverters.toJava(t.asInstanceOfNBTCompound()))
         .flatMap(t -> t.getJava("stringTag"))
         .flatMap(s -> OptionConverters.toJava(s.asInstanceOfNBTString()))
         .map(NBTString::value);
   System.out.println(string);
}
catch(IOException e) {
   e.printStackTrace();
}

Here is how you would do the same task using DataFormats.NBT

try(FileInputStream stream = new FileInputStream("nbtFile.nbt")) {
   DataContainer container = DataFormats.NBT.readFrom(stream);
   Optional<String> string = container.getString(DataQuery.of("childCompound", "stringTag"));
   System.out.println(string);
}
catch(IOException e) {
   e.printStackTrace();
}

In both cases you would get the same string back. The difference lies on how you go about doing it.

@CrazyPyroEagle
I assume you meant how to write a custom DataFormat. Kind of depends on what sort of data format you are dealing with. First you need to write a way to write and read this dataformat from a stream. Then you need to also write a converter for your format that converts your format into a DataView. The write the DataFormat just combine them.

2 Likes