Sueta

NBT Reflection API

1.1.0

Simple API for work with NBT tags.

·Plugin

NBTReflectionAPI
minecraft 1.13 - 1.15 | version 1.1.0

This plugin is very simple API for work with NBT tags without version dependency. API is small, but its functional is enough for implementation a lot of tasks. API is based on reflection java api, that make executing of code slower. If you use small parts of code - no problem, else import NMS classes or use multithread.

Implementation is very simple. If you find bugs, contact me.

Exist some classes for work:

  • NMSItem - representation of ItemStack from NMS packege.
  • NMSEntity - representation of Entity from NMS packege.
  • NMSTileEntity - representation of TileEntity from NMS packege. (BlockState in bukkit).
  • NBTTagCompound - representation of NBTTagCompound from NMS packege.
  • NBTTagList - representation of NBTTagList from NMS packege.

NMSItem

Create instance:

By bukkit ItemStack:

ItemStack itemStack = new ItemStack(Material.DIAMOND);
NMSItem nmsItem = new NMSItem(itemStack);

By NMS ItemStack:

Object nms = Some.thing.getNetMinecraftServerItem();
NMSItem nmsItem = new NMSItem(nms);

By api NBTTagCompound:

NBTTagCompound tag = new NBTTagCompound();
NMSItem nmsItem = new NMSItem(tag);

Methods:

Getting NBT tag:

NBTTagCompound tag = nmsItem.getNBT();

Setting NBT tag:

nmsItem.setNBT(tag);

Getting bukkit copy:

ItemStack item = nmsItem.asBukkit();

NMSEntity

Create instance:

By bukkit Entity:

Entity entity = world.spawnEntity(location, EntityType.CREEPER);
NMSEntity nmsEntity = new NMSEntity(entity);

Methods:

Getting NBT tag:

NBTTagCompound tag = nmsEntity.getNBT();

Setting NBT tag:

nmsEntity.setNBT(tag);

Getting bukkit copy:

Entity entity = nmsEntity.asBukkit();

NMSTileEntity

Create instance:

By bukkit Location:

Location location = block.getLocation();
NMSTileEntity tile = new NMSTileEntity(location);

Note. If there is no block entity in the location, then all the results of the methods will return null, and NMSTileEntity#getNBT() will return an empty tag.

Methods:

Getting NBT tag:

NBTTagCompound tag = tile.getNBT();

Setting NBT tag:

tile.setNBT(tag);

Updating block state:

tile.update();

Getting bukkit copy:

BlockState state = tile.asBukkit();

NBTTagCompound:

Create instance:

Empty tag:

NBTTagCompound tag = new NBTTagCompound();

By NMS NBTTagCompound:

Object nmsNbtTagCompound = Some.thing.getNetMinecraftServerNbtTagCompound();
NBTTagCompound tag = new NBTTagCompound(nmsNbtTagCompound);

Methods:

Setting values to key:

tag.setInt("Health", 20);
tag.setIntArray("Colors", new int[] {443, 3453, 234, -34});
tag.setByte("Count", (byte) 5);
tag.setByteArray("Colors", new byte[] {1,2,3,4,5});
tag.setLong("Variable", 122435346L);
tag.setShort("Power", (short) 24);
tag.setDouble("x", 234.45);
tag.setFloat("y", 1245.4F);
tag.setBoolean("NoAI", false);
tag.setString("Uuid","4sd5-df5wsed4-4345");
tag.set("passengers", new NBTTagCompound());

Getting NBTTagCompound by key:

NBTTagCompound subTag = tag.get("passengers");

Getting NMS copy:

Object nmsTag = tag.asNMS();

NBTTagList

Create instance:

Empty list:

NBTTagList nbtList = new NBTTagList();

By NMS NBTTagList:

Object nmsNbtList = Some.thing.getNetMinecraftServerNbtTagList();
NBTTagList nbtList = new NBTTagList(nmsNbtList);

Methods:

Adding element to list:

nbtList.add(new NBTTagCompound());
nbtList.add(new NBTTagCompound(), 2);

Getting size of list:

int size = nbtList.getSize();

Getting NMS copy:

Object nmsNbtList = nbtList.asNMS();

Similar projects

Plugin →