Solved How to get NBTBase from Entities

Discussion in 'Plugin Development' started by jonathanpecany, Nov 30, 2020.

  1. Offline

    jonathanpecany

    I am getting a bit annoyed now as I am trying to test some things with NBT. I have managed to make work with it for Items very easily as I can get the proper NBTBase for an item, but not an Entity and spent a good couple of hours trying to figure it out. So can anyone help me with getting the NBTBase from an entity, and I mean the top level of the Entity, if you all know what I mean as I managed to figure out what everything does for NBT configuration from testing it on Items. It would be also very nice to be able to get the NBTBase from Player as well as I know that might be as difficult as an Entity.

    Also, I know how much people love to say it is impossible here and such, well don't say it here as I am just going to call you a liar. as it worked with items, so especially, don't say anything otherwise.

    I'll give any extra info if it will help. Again though, I have tried looking literally everywhere on the internet and found nothing at all, which is what most of my time is spent. You think though, there would be some good documentation on this stuff, but no, there is not.

    Thanks in response.

    Update: A quick update it that I still don't know what to do. It is giving me a headache and hope someone knows how to do. I searched literally everywhere and some odd reason no one seems to have the same question. I know it is possible.

    Here is some starting code in how I was doing this. This is not copied and pasted so don't mention any errors as the actual code works perfectly. <Entity NBTBase> is where I need the Entity NBTBase at for it to work. This works perfectly for an Item and know it will work perfectly for an Entity if I just had the Entity NBTBase. But what this code would do is create a compound in the Entity NBT, I can test if it works but using the Universal Minecraft Editor.
    Code:
    NBTTagCompound tag = new NBTTagCompound();
    tag.set("Tag", <Entity NBTBase>);
     
    Last edited: Dec 1, 2020
  2. Offline

    gochi9

    I should start by saying that you cannot assign custom tags on entities like items

    Also this should work for 1.8.8 but i just took it from google and might not work with newer version minecraft but it's a starting point
    Code:
    net.minecraft.server.v1_8_R3.Entity nmsEntity = ((CraftEntity) e).getHandle();
    
            NBTTagCompound tag = new NBTTagCompound();
    
            nmsEntity.c(tag);
    
            tag.setBoolean("NoAI", true);
    
            EntityLiving el = (EntityLiving) nmsEntity;
            el.a(tag);
     
  3. Offline

    jonathanpecany

    @gochi9 How come I can't assign NBT custom tags to entities like items? Also, how come Bukkit manage to do it with NBTTagCompound, and I checked in the code and Bukkit does in fact use NBTTagCompound to change NBT values in Entities, Items, etc. And how can I get the persistantData as a NBTBase, as it might just be easier to do then.

    But I did manage to change the NBT data to the entity, just don't know how to save it. But I am very close to figuring it out.

    Sorry though, but this code is useless as I am using the latest version of Minecraft, but thanks for trying. But I did come a by this code actually and it doesn't have the correct arguments to be able to assign NBT data.
     
  4. Offline

    jonathanpecany

  5. Offline

    MrDaniel

    Bukkit does not save entity NBT data. What you can do, is before server shutdown, loop over all the entities, check if they have NBT data, and if they do save it to config along with their entity ID. Once the server starts you would loop over the config and assign the data to the entities.
     
  6. Offline

    jonathanpecany

    That isn't actually efficient enough as it can lag the server heavily on start-up and possibly have a chance of crashing, depending on how many Entities are spawned.

    ------------------
    But anyway though, I found out an alternative way instead. Something I didn't see and that is using TAG_CONTAINER for PersistentDataType.

    Here is an example Code snippet for anyone else that may need help.
    Sorry if there are any typeos as this is just something simple.
    Code:
    PersistentDataContainer newTag = Entity.getPersistentDataContainer.getAdapterContext().newPersistentDataContainer();
    PersistentDataContainer pdc = Entity.getPersistentDataContainer.set(new NamespacedKey(Plugin,"Tag"),PersistentDataType.TAG_CONTAINER, newTag);
    pdc.set(new NamespacedKey(Plugin,"String"),PersistentDataType.STRING, "Hello World!");
     
    Last edited: Dec 6, 2020

Share This Page