Jump to content

Recommended Posts

Posted

I am trying to make a custom cauldron and working out how to make it store water properly. I have it filling and emptying, but if it's filled, and I log out and then log in, it isn't filled anymore. I think it has something to do with my readFromNBT method, but I'm not sure. Can anyone point out where I went wrong?

 

Here is my Tile Entity class for it:

package jakoubeck503.alchemicalinstillation.common.tileentity;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidEvent;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import net.minecraftforge.fluids.IFluidTank;

public class TEBrewingCauldron extends TileEntity implements IFluidTank, IFluidHandler {

private FluidTank tank = new FluidTank(FluidContainerRegistry.BUCKET_VOLUME);
private boolean boiling = false;

public TEBrewingCauldron() {
	System.out.println("Creating a new tile entity...");
}

public void setBoiling(boolean par1) {
	this.boiling = par1;
	System.out.println("Cauldron " + (this.boiling==true ? "is" : "isn't") + " boiling!");
}

public boolean getBoiling() {
	return this.boiling;
}

public boolean isFillable() {
	if (tank.getFluid()!=null) {
		if (tank.getFluid().getFluid()==FluidRegistry.WATER && tank.getFluidAmount()<FluidContainerRegistry.BUCKET_VOLUME){
			return true;
		} else return false;
	} else return true;
}

@Override
    public void readFromNBT(NBTTagCompound nbt) {
	System.out.println("Reading from NBT...");
	super.readFromNBT(nbt);
        if (!nbt.hasKey("Empty")) {
            FluidStack fluid = this.tank.getFluid().loadFluidStackFromNBT(nbt);
            System.out.println(fluid.amount + "mB of " + fluid.getFluid());

            if (fluid!=null) {
                this.tank.setFluid(fluid);
            }
        }
        if (tank.getFluid().getFluid()!=FluidRegistry.WATER){
        	System.out.println("ERROR! Not equal to water...");
        }
    }

@Override
    public void writeToNBT(NBTTagCompound nbt) {
    	System.out.println("Writing to NBT...");
    	super.writeToNBT(nbt);
        if (tank.getFluid()!=null) {
        	tank.getFluid().writeToNBT(nbt);
        } else {
            nbt.setString("Empty", "");
        }
    }

@Override
public FluidStack getFluid() {
	return tank.getFluid();
}

@Override
public int getFluidAmount() {
	if (tank.getFluid()==null) {
		return 0;
	} else return tank.getFluidAmount();
}

@Override
public int getCapacity() {
	return tank.getCapacity();
}

@Override
public FluidTankInfo getInfo() {
	return tank.getInfo();
}

@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
	// TODO Auto-generated method stub
	return tank.fill(resource, doFill);
}

@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
	// TODO Auto-generated method stub
	return null;
}

    private void setFluid(FluidStack f) {
	this.tank.setFluid(f);
}

@Override
public int fill(FluidStack resource, boolean doFill) {
	return tank.fill(resource, doFill);
}

@Override
public FluidStack drain(int maxDrain, boolean doDrain) {
	return tank.drain(maxDrain, doDrain);
}


}

Posted

Now, I am not the best with NBT but I think I see a problem. Your are using the key Empty a bit oddly. Imagine this situation.

 

1. You place a cauldron down. It is empty.

2. writeToNBT gets called. Since the tank is empty, "Empty" gets set a value

nbt.setString("Empty", "");

3. You put water in, writeToNBT gets called again.

4. Quit the world and log back in.

5.  readFromNBT runs. Here is the problem. On the first if statement it checks if the key "Empty" exists. It does! You set it earlier, it hasn't been removed in any way. Because it exists the rest of the code between the brackets is ignored. Fluid never gets set.

 

NOTE: I could be COMPLETELY wrong. However this feels right to me. i recommend you change the Empty key completely. Maybe to some kind of boolean? Not too familiar with NBT so you'll have to figure it out.

 

Good Luck

An average guy who mods Minecraft. If you need help and are willing to use your brain, don't be afraid to ask.

 

Also, check out the Unofficial Minecraft Coder Pack (MCP) Prerelease Center for the latest from the MCP Team!

 

Was I helpful? Leave some karma/thanks! :)

Posted

I've tried to do it the way it is in TileFluidHandler, but still it doesn't work. Does that mean the problem is elsewhere? Like my cauldron block class:

package jakoubeck503.alchemicalinstillation.common.block;

import jakoubeck503.alchemicalinstillation.AlchemicalInstillation;
import jakoubeck503.alchemicalinstillation.common.tileentity.TEBrewingCauldron;

import java.util.List;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class BlockBrewingCauldron extends BlockContainer {

    @SideOnly(Side.CLIENT)
    private IIcon iicon;
    
public BlockBrewingCauldron() {
	super(Material.iron);
	setCreativeTab(AlchemicalInstillation.tabAlchemicalInstillation);
	this.setBlockName("blockBrewingCauldron");
	this.setBlockTextureName(AlchemicalInstillation.modid + ":" + "brewingCauldron_side");
	this.setHardness(1.0F);
	this.setResistance(2.0F);
}

public TileEntity createNewTileEntity(World world, int i1) {
	return new TEBrewingCauldron();
}

public TileEntity getTileEntity(World world, int x, int y, int z) {
	return world.getTileEntity(x, y, z);
}

    public void setBlockBoundsForItemRender() {
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
    }
    
    public boolean isOpaqueCube() {
        return false;
    }
    
    public int getRenderType() {
        return -1;
    }

    public boolean renderAsNormalBlock() {
        return false;
    }
    
    public void addCollisionBoxesToList(World world, int x, int y, int z, AxisAlignedBB axis, List list, Entity entity) {
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.3125F, 1.0F);
        super.addCollisionBoxesToList(world, x, y, z, axis, list, entity);
        float f = 0.125F;
        this.setBlockBounds(0.0F, 0.0F, 0.0F, f, 1.0F, 1.0F);
        super.addCollisionBoxesToList(world, x, y, z, axis, list, entity);
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, f);
        super.addCollisionBoxesToList(world, x, y, z, axis, list, entity);
        this.setBlockBounds(1.0F - f, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
        super.addCollisionBoxesToList(world, x, y, z, axis, list, entity);
        this.setBlockBounds(0.0F, 0.0F, 1.0F - f, 1.0F, 1.0F, 1.0F);
        super.addCollisionBoxesToList(world, x, y, z, axis, list, entity);
        this.setBlockBoundsForItemRender();
    }
    
    @Override
    @SubscribeEvent
    public boolean onBlockEventReceived(World world, int x, int y, int z, int something, int different) {
    	System.out.println("BLOCK CHANGE!!!");
    	if (world.getBlock(x, y - 1, z)==Blocks.fire || world.getBlock(x, y - 1, z)==Blocks.lava) {
    		System.out.println("LAVA AND FIRE!!!");
    		TEBrewingCauldron entity =  (TEBrewingCauldron) world.getTileEntity(x, y, z);
    		entity.setBoiling(true);
    		return true;
    	} else return false;
    }
    
    @Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) {
    	TEBrewingCauldron tileEntity = (TEBrewingCauldron) world.getTileEntity(x, y, z);
    	
    	if (tileEntity==null || player.isSneaking()) {
    		return false;
    	}
    	
    	ItemStack heldItem = player.getCurrentEquippedItem();
    	
    	if (heldItem!=null) {
    		if (heldItem.getItem()==Items.water_bucket) {
    			if (tileEntity.isFillable()) {
    				tileEntity.fill(ForgeDirection.UP, new FluidStack(FluidRegistry.WATER, FluidContainerRegistry.BUCKET_VOLUME), true);
    				if (!player.capabilities.isCreativeMode)
    					player.inventory.setInventorySlotContents(player.inventory.currentItem, new ItemStack(Items.bucket));
    			}
    			return true;
    		}
    		
    		if (heldItem.getItem()==Items.glass_bottle && tileEntity.getFluidAmount()>=333){
    			tileEntity.drain(ForgeDirection.UP, 333, true);
    			if (!player.capabilities.isCreativeMode)
    				player.inventory.setInventorySlotContents(player.inventory.currentItem, FluidContainerRegistry.fillFluidContainer(
    						FluidRegistry.getFluidStack(FluidRegistry.WATER.getName(), 333), FluidContainerRegistry.EMPTY_BOTTLE));
    			return true;
    		}
    		if (heldItem.getItem()==Items.bucket && tileEntity.getFluidAmount()>=FluidContainerRegistry.BUCKET_VOLUME){
    			tileEntity.drain(ForgeDirection.UP, FluidContainerRegistry.BUCKET_VOLUME, true);
    			if (!player.capabilities.isCreativeMode)
    				player.inventory.setInventorySlotContents(player.inventory.currentItem, FluidContainerRegistry.fillFluidContainer(
    						FluidRegistry.getFluidStack(FluidRegistry.WATER.getName(), FluidContainerRegistry.BUCKET_VOLUME), FluidContainerRegistry.EMPTY_BUCKET));
    			return true;
    		}

    	}
    	return false;
    }


}


Posted

The problem is when the tile is unloaded and reloaded the nbt is only loaded on the server to fix this add the following methods to your tile.

@Override
public Packet getDescriptionPacket() {
	NBTTagCompound tagCompound = new NBTTagCompound();
	writeToNBT(tagCompound);
	return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, tagCompound);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
	readFromNBT(pkt.func_148857_g());
}

 

they will read the nbt from the server and send it to the client. they are called every time the tile is loaded or the block is updated.

I am the author of Draconic Evolution

Posted

You need to all markDirty() whenever something in your tile entity changes, so that it will be written out when the world is saved.

 

Also, you need to implement getDescriptionPacket() and onDescriptionPacket() even for a single-player environment. There is still a server and a client in single player (they just exist in the same process) and they need to be kept in sync.

Posted

I am positively flummoxed now. I've marked the tile entity dirty and added this piece of code:

    @Override
    public void updateEntity() {
    	System.out.println(this.getFluidAmount() + "mB");
    }

When I place the cauldron down, it prints out "0mB" continuously, as expected. When I fill it, again it continuously prints "1000mB". However if leave it full and re-log, the console does this (if I leave the cauldron empty, it's normal after re-logging):

0mB
0mB
0mB
1000mB
0mB
1000mB
0mB
1000mB
0mB
1000mB
0mB
1000mB
0mB
1000mB
0mB
1000mB
0mB
1000mB
1000mB
0mB
1000mB
0mB
1000mB
0mB
1000mB
0mB
1000mB
0mB
1000mB
1000mB

 

Does anybody have any explanation as to what is going on here?

Posted

The

readFromNBT()

method only gets called on the server side. So the server knows the saved value (1000mB), but the client still has the default value (0mB). To solve this add the methods from brandon3055 post.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

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

    • Reach Out To Rapid Digital: What sapp Info: +1 41 4 80 7 14 85 Email INFO: rap iddi gita lrecov ery @ exe cs. com Hello, my name is Jayson, and I’m 35 years old from the United Kingdom. My family and I recently endured an incredibly challenging experience that I wouldn’t wish on anyone. We became victims of a cryptocurrency investment fraud scheme that saw us lose a staggering $807,000 in USDT and Bitcoins. The fraudsters had created a convincing facade, and we were lured into investing, only to discover later that the platform was a complete scam. We were left devastated, not just financially, but emotionally, as we had trusted these people and believed in the legitimacy of the investment. After the initial shock wore off, we desperately searched for ways to recover the lost funds. It seemed like an impossible task, and we felt as though there was no hope. That’s when, by sheer luck, we stumbled across a post about Rapid Digital Recovery, a cryptocurrency and funds recovery organization with a proven track record in cybersecurity and fraud recovery. We decided to reach out to them, and from the first interaction, we were impressed with their professionalism and transparency. They explained the recovery process in detail and reassured us that they had the skills and expertise to track down the perpetrators and recover our funds. This gave us a renewed sense of hope, something we hadn’t felt in months. What truly stood out during our experience with Rapid Digital Recovery was their dedication to the recovery process. The team went above and beyond, using sophisticated tracking tools and cyber forensics to gather critical information. Within a matter of weeks, they had successfully located the funds and traced the scam back to the fraudsters responsible. They worked with the authorities to ensure the criminals were held accountable for their actions. To our relief, the team at Rapid Digital Recovery was able to recover every single penny we had lost. The funds were returned in full, and the sense of closure we felt was invaluable. We couldn’t have imagined such a positive outcome in the early stages of our recovery journey, and we are deeply grateful for the work they did. If you ever find yourself in a similar situation, I highly recommend contacting Rapid Digital Recovery. Their expertise, transparency, and dedication to their clients make them the go-to choice for anyone seeking to recover lost cryptocurrency or funds. They truly gave us back our financial future.  
    • This is my first time modding anything, so maybe just skill issue. I'm using Forge 54.0.12 and Temurin 21.0.5+11-LTS I wanted to create a custom keybind and to check whether it works I'd like to send a chat message. I tried using Minecraft.getInstance().player.sendSystemMessage(Component.literal("test")); but IntelliJ couldnt resolve sendSystemMessage(...). Since I saw people using it in earlier versions, I tried the same thing with 1.20.6(- 50.1.0), where it works fine, now I can't figure out if this is intentional and whether there are other options for sending chat messages. On that note, is there more documentation than https://docs.minecraftforge.net/en/1.21.x/? It seems very incomplete compared to something like the Oracle Java docs
    • Hi, i'm having this error and I wanna fix it. we try: -Reload drivers -Eliminate .minecraft -Eliminate Java -Restart launcher -Verify if minecraft is using gpu -Mods  in .minecraft is empty -Install the latest and recomended version of forge idk what i have to do, help me pls. the lastest log is: https://mclo.gs/WAMao8x  
    • Read the FAQ, Rule #2. (https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/)  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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