Posted March 9, 20169 yr 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.
March 9, 20169 yr you dont need to remove the tag, because everytime writeToNBT is called they get a clear new NBTTagCompound, which means that the old "empty" key wont be inside of it
March 9, 20169 yr Author 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; }
March 9, 20169 yr 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
March 9, 20169 yr Author 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.
March 9, 20169 yr 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
March 9, 20169 yr Author 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.
March 9, 20169 yr The empty tag only gets added if the fluidstack is null. read the code. if stack not null do stuff else write empty
March 9, 20169 yr Author 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.
March 9, 20169 yr 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.