Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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);

  • Author
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;

        }

    }

}

  • Author

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.

  • Author

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?

  • Author

I decided to just setup a packet and I set it as the description packet and it works.  Thank You!

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.