[TUTORIAL] How to get/set VANILLA NBT data for entities

Started by RedWirePlatinum on

Topic category: User side tutorials

Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[TUTORIAL] How to get/set VANILLA NBT data for entities
Wed, 10/18/2023 - 07:03 (edited)

How to get NBT data:
For this example, we're going to get the NBT data of the target/event entity. With a custom code snippet, put this inside:
CompoundTag nbtdata = entity.saveWithoutId(new CompoundTag());

(If getting data from target entity, source entity or immediate source entity, make sure it is shown under "required dependencies")
To get the value of "HurtTime", use getShort since its a short-type number

nbtdata.getShort("HurtTime")

How to set NBT data:
To set the value of something (such as HurtTime), use this:
nbtdata.putShort("HurtTime", 0);

and then load the new nbtdata onto the entity:
entity.load(nbtdata);


If you want to get a list of values (such as ForgeData) within the nbtdata, you can use the following:
CompoundTag forgedata = nbtdata.get("ForgeData")

Edited by RedWirePlatinum on Wed, 10/18/2023 - 07:03
Joined Apr 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I want to set a zombie's…
Sat, 01/03/2026 - 08:51

I want to set a zombie's CanPickUpLoot data to be always true

Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Well, given my example…
Thu, 01/08/2026 - 22:47

Well, given my example... Instead of just grabbing a CompoundTag of all NBT data, writing to one, then applying it back to an entity using HurtTime for example, you can literally just do:
if (entity instanceof LivingEntity live) {
live.hurtTime = 0;
}
It's an incredibly easy-to-miss feature, but if you Select Window > Show workspace file browser (or select the tiny little arrows thats to the very leftmost of the screen, just above the "WORKSPACE" title), you can see the list of libraries that MCreator uses. Search for something like LivingEntity.java and you'll see everything that it does

Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Also, to answer TacoGamings…
Thu, 01/08/2026 - 22:49

Also, to answer TacoGamings question, this might differ in minecraft versions, but you should be able to just do the following:
if (entity instanceof Mob mob) {
_mob.setCanPickUpLoot(true);
}