Jump to content

Recommended Posts

Posted

I have an IFluidHandler with 2 tanks in it which both work as there supposed to, however, the tanks will not save to nbt correctly.  Here is my code:

 

Reading Tank Data:

        NBTTagCompound tagWater = tagCompound.getCompoundTag("water");
        NBTTagCompound tagLava = tagCompound.getCompoundTag("lava");

        tankWater.readFromNBT(tagWater);
        tankLava.readFromNBT(tagLava);

 

Writing Tank Data:

        NBTTagCompound tagWater = new NBTTagCompound();
        NBTTagCompound tagLava = new NBTTagCompound();

        tankWater.writeToNBT(tagWater);
        tankLava.writeToNBT(tagLava);

        tagCompound.setTag("water", tagWater);
        tagCompound.setTag("lava", tagLava);

Posted
public class PowerFactoryTileEntity extends TileEntity implements IInventory, IFluidHandler {

    private ItemStack[] itemInventory;

    private FluidTank tankWater;
    private FluidTank tankLava;

    public PowerFactoryTileEntity() {

        itemInventory = new ItemStack[8];

        tankWater = new FluidTank(FluidRegistry.WATER, 0, FluidContainerRegistry.BUCKET_VOLUME * 4);
        tankLava = new FluidTank(FluidRegistry.LAVA, 0, FluidContainerRegistry.BUCKET_VOLUME * 4);

    }

    public String getInventoryName() {

        return "container.electricfactory:factoryPower";

    }

    public boolean hasCustomInventoryName() {

        return false;

    }

    public int getSizeInventory() {

        return itemInventory.length;

    }

    public int getInventoryStackLimit() {

        return 64;

    }

    public boolean isUseableByPlayer(EntityPlayer player) {

        return true;

    }

    public void openInventory() {

    }

    public void closeInventory() {

    }

    public boolean isItemValidForSlot(int index, ItemStack stack) {

        return true;

    }

    public ItemStack getStackInSlot(int index) {

        return itemInventory[index];

    }

    public ItemStack getStackInSlotOnClosing(int index) {

        if(itemInventory[index] != null) {

            itemInventory[index] = null;

            return itemInventory[index];

        }

        return null;

    }

    public ItemStack decrStackSize(int index, int amount) {

        ItemStack stack = getStackInSlot(index);

        if(stack != null) {

            if(stack.stackSize <= amount) {

                setInventorySlotContents(index, null);

            }
            else {

                stack = stack.splitStack(amount);

                if(stack.stackSize == 0)
                    setInventorySlotContents(index, null);

            }

        }

        return stack;

    }

    public void setInventorySlotContents(int index, ItemStack stack) {

        itemInventory[index] = stack;

        if(stack != null && stack.stackSize > getInventoryStackLimit())
            stack.stackSize = getInventoryStackLimit();

        markDirty();

    }

    public int fill(ForgeDirection side, FluidStack stack, boolean fill) {

        if(stack.getFluid() == FluidRegistry.WATER)
            return tankWater.fill(stack, fill);
        else if(stack.getFluid() == FluidRegistry.LAVA)
            return tankLava.fill(stack, fill);

        return 0;

    }

    public FluidStack drain(ForgeDirection side, FluidStack stack, boolean drain) {

        return null;

    }

    public FluidStack drain(ForgeDirection side, int max, boolean drain) {

        return null;

    }

    public boolean canFill(ForgeDirection side, Fluid fluid) {

        return fluid == FluidRegistry.WATER || fluid == FluidRegistry.LAVA;

    }

    public boolean canDrain(ForgeDirection side, Fluid fluid) {

        return false;

    }

    public FluidTankInfo[] getTankInfo(ForgeDirection side) {

        return new FluidTankInfo[] {tankWater.getInfo(), tankLava.getInfo()};

    }

    public void updateEntity() {

        for(int i = 0; i < getSizeInventory(); i++) {

            drainBucket(i);

        }

    }

    public void readFromNBT(NBTTagCompound tagCompound) {

        super.readFromNBT(tagCompound);

        NBTTagList tags = tagCompound.getTagList("items", 10);

        for(int i = 0; i < tags.tagCount(); i++) {

            NBTTagCompound tag = tags.getCompoundTagAt(i);

            byte index = tag.getByte("slot");

            if(index >= 0 && index < itemInventory.length)
                itemInventory[index] = ItemStack.loadItemStackFromNBT(tag);

        }

        NBTTagCompound tagWater = tagCompound.getCompoundTag("water");
        NBTTagCompound tagLava = tagCompound.getCompoundTag("lava");

        tankWater.readFromNBT(tagWater);
        tankLava.readFromNBT(tagLava);

    }

    public void writeToNBT(NBTTagCompound tagCompound) {

        super.writeToNBT(tagCompound);

        NBTTagList tags = new NBTTagList();

        for(int i = 0; i < itemInventory.length; i++) {

            if(itemInventory[i] != null) {

                NBTTagCompound tag = new NBTTagCompound();

                tag.setByte("slot", (byte)i);

                itemInventory[i].writeToNBT(tag);

                tags.appendTag(tag);

            }

        }

        tagCompound.setTag("items", tags);

        NBTTagCompound tagWater = new NBTTagCompound();
        NBTTagCompound tagLava = new NBTTagCompound();

        tankWater.writeToNBT(tagWater);
        tankLava.writeToNBT(tagLava);

        tagCompound.setTag("water", tagWater);
        tagCompound.setTag("lava", tagLava);

    }

    private void drainBucket(int index) {

        Fluid WATER = FluidRegistry.WATER;
        Fluid LAVA = FluidRegistry.LAVA;

        ItemStack bucket = getStackInSlot(index);

        if(bucket == null || !FluidContainerRegistry.isBucket(bucket) || FluidContainerRegistry.isEmptyContainer(bucket))
            return;

        if(FluidContainerRegistry.getFluidForFilledItem(bucket).getFluid() == WATER) {

            if(tankWater.getFluid().amount + FluidContainerRegistry.getFluidForFilledItem(bucket).amount <= tankWater.getInfo().capacity) {

                fill(ForgeDirection.UNKNOWN, FluidContainerRegistry.getFluidForFilledItem(bucket), true);

                setInventorySlotContents(index, new ItemStack(Items.bucket));

            }

        }
        else if(FluidContainerRegistry.getFluidForFilledItem(bucket).getFluid() == LAVA) {

            if(tankLava.getFluid().amount + FluidContainerRegistry.getFluidForFilledItem(bucket).amount <= tankLava.getInfo().capacity) {

                fill(ForgeDirection.UNKNOWN, FluidContainerRegistry.getFluidForFilledItem(bucket), true);

                setInventorySlotContents(index, new ItemStack(Items.bucket));

            }

        }
        else {

            return;

        }

    }

}

Posted

I have my gui show me a bar with how much of the liquid is stored.  The gui works just fine and shows me how much lava and water is stored when I put water and lava into it.  However, when I close the world and reopen it, all fluid in it disappears from what I had last time.

Posted

Thank You!  I ran a test and it is correctly reading the data, just not displaying it in the gui.  Do I want to setup a packet to send the information to the clients then in readFromNBT after I have read in the values?

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

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