flinbein / PowerNBT

[MineCraft] Minecraft PowerNBT plugin
https://www.spigotmc.org/resources/powernbt.9098/
MIT License
39 stars 25 forks source link

Unable to get NBTTagCompound from NBTTagList #3

Closed Master-chan closed 10 years ago

Master-chan commented 10 years ago

This method looking wrong [NBTTagList.java:122]

    public NBTTagList getList(int i) {
        NBTBase t = get(i);
        if (t instanceof NBTTagList) return ((NBTTagList) t);
        return null;
    }

    public NBTTagList getCompound(int i) {
        NBTBase t = get(i);
        if (t instanceof NBTTagList) return ((NBTTagList) t);
        return null;
    }
DPOH-VAR commented 10 years ago

you're right. I'm already working on a new API

Master-chan commented 10 years ago

Sorry for bothering, but can you explain how to save edited tag back to NBTContainerEntity? The setTag method seems to write only in root compound, while I need to write modified player attributes.

DPOH-VAR commented 10 years ago

You need to read root tag, then modify it's attributes, and save tag back to container. No other way, because the tags are read and written fully

Master-chan commented 10 years ago

I'm reading tag like this:

            NBTContainerEntity con = new NBTContainerEntity(entity);
            NBTTagCompound compound = (NBTTagCompound) con.getTag();
            NBTTagList attributes = compound.getList("Attributes");
            compound = (NBTTagCompound) attributes.get(1);
            NBTTagDouble kbResist = (NBTTagDouble) compound.get("Base");

But I don't understand how to save my NBTTagDouble after editing.

DPOH-VAR commented 10 years ago
NBTContainerEntity con = new NBTContainerEntity(entity);
NBTTagCompound compound = (NBTTagCompound) con.getTag();
NBTTagList attributes = compound.getList("Attributes");
NBTTagCompound attr = (NBTTagCompound) attributes.get(1);
// read value
NBTTagDouble kbResist = (NBTTagDouble) attr.get("Base");
// set new value
attr.put("Base",(double) newKbResist )
// and save
con.setTag(compound) // compound has been changed

it should work

Master-chan commented 10 years ago

Thank you very much, i was puzzling over this for 2 hours :+1: