Entity Custom NBT 1.8.8

Discussion in 'Spigot Plugin Development' started by MrAxeTv, Apr 23, 2017.

  1. Hello guys can some body expalin how to add custom nbt to Entity
    I have try this but in not working
    @EventHandler
    public void onCreatureSpawn(CreatureSpawnEvent e){
    net.minecraft.server.v1_8_R3.Entity nmsEntity = ((CraftEntity) e.getEntity()).getHandle();
    NBTTagCompound tag = nmsEntity.getNBTTag();
    if (tag == null){
    tag = new NBTTagCompound();
    }

    nmsEntity.c(tag);
    tag.setInt("SpawnerSp", 1);
    nmsEntity.f(tag);


    }
     
  2. With entities you can only work with tags that entities already recognize. You cant add your own new tags.
     
  3. Actually, that's not true, but the OP would encounter way too many problems with server loads, startups and storing the NBT that it's not worth it.
    OP, either go with Metadata if it's meant to be temporary, or use your own storage for such; unless you want to do it the hardest way I'm aware of.
     
  4. You're right I should have clarified.

    The vanilla server and spigot don't attach the nbttag to the entity but rather pull all of the data out of the tag and store the values in fields when you call Entity#f(NBTTagCompound) (at least that is the method name in 1_8_R3).

    If you take a look into that class you will see a lot of things like this
    Code (Text):
    this.fallDistance = nbttagcompound.getFloat("FallDistance");
    this.fireTicks = nbttagcompound.getShort("Fire");
    this.setAirTicks(nbttagcompound.getShort("Air"));
    where only the known tag names get pulled from the tag you pass in as the argument. After the call the reference to the tag is simply dropped. When you call Entity#c(NBTTagCompound) its the opposite where the fields get put back into a fresh tag that is returned. You would need to implement a custom entity and remember the tag on your own. It is definitely not worth the work for what it looks like you are trying to do.

    The reason custom tags work on itemstacks is because spigot was very helpful and supports serializing the unknown extra tags and the nms itemstack holds a reference to the tag (not sure if this was a spigot patch or not).

    As far as powernbt goes it looks like the closest it gets to supporting custom tags is using a field that Forge adds if the server is a forge server which is actually stored as a tag and could be used to hold custom data. I haven't tested it so if they have things working that's fantastic because I couldn't :p