Jump to content

Recommended Posts

Posted

I ran into some trouble when using the NBT read/write methods of FluidTank. The writeToNBT() method creates a string tag with the key "Empty" if the fluid being saved is null. However, this tag is never removed if the same compound is written to with fluid present. This means that the first time the tank saves while empty, it will never be able to be read from properly again, because the readFromNBT() method simply sets the fluid to null if the "Empty" tag is present. I understand that I can solve this problem by creating a new tag every time I write and save all of my other data to it as well, however this problem seems like an oversight.

Posted

I don't think that's true.

public FluidTank readFromNBT(NBTTagCompound nbt)
    {
        if (!nbt.hasKey("Empty"))
        {
            FluidStack fluid = FluidStack.loadFluidStackFromNBT(nbt);
            setFluid(fluid);
        }
        else
        {
            setFluid(null);
        }
        return this;
    }

    public NBTTagCompound writeToNBT(NBTTagCompound nbt)
    {
        if (fluid != null)
        {
            fluid.writeToNBT(nbt);
        }
        else
        {
            nbt.setString("Empty", "");
        }
        return nbt;
    }

Posted

I dont know why you showed me that piece of code since it has no relation to what I told you but thats just my point of view.

If you change the fluidstack inside at runtime it wont be null, so that code wont write empty to the compound

Posted

I think you're misunderstanding. That code is entirely relevant, it's the two methods this post is about in the first place. You're claiming that they function in a way they do not.

Yes, the fluidstack the tank creates will be null, because the "empty" tag is present. You are correct in saying that the data saved to the compound in writeToNBT() will not be null, however that is irrelevant as it will never be read.

Posted

why should it never be read?

if you create e.g. a tileentity with a fluidtank in the tileentities readFromNBT you will read the tank from nbt, which will get saved to disk in writeToNBT of the tileentity if you code it that way

Posted

It will never be read because the "Empty" tag is present. It doesn't matter what other data is there, if the "Empty" tag is present, the fluid is set to null.

The "Empty" tag is created in writeToNBT() of FluidTank if there is no fluid. Meaning that if I ever save the tank while it is empty, it will never be read from properly again. This is because the "Empty" tag is not removed if I write with fluid present.

Posted

You really don't understand. I am using one NBTTagCompound. Let's say I write some tank data to it while the tank has some water in it. This works fine and can be read later. Now, I remove the water and save. Because the fluid was null, the "empty" tag was added to the compound. If I add water again and write, it will still be read as empty because the tag was not removed on write.

Posted

dont reuse nbttagcompounds.

where are you even using those? I guess most of the confusion comes from the lack of informations provided.

in each normal case you wouldn't be write to the same compound you read from (e.g. tileentities)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.