Jump to content

[1.7.10] [SOLVED] Every furnace/campfire burns


Recommended Posts

Posted

Hello,

 

I've created a campfire that should work like a furnace with a difference:

 

  • The Campfire should only burn if the weather is clear or the weather is raining and the campfire block can't see the sky.
  • The second requirement should be that you need to right click with a flient and steel to set it on fire.

 

Now I have the following problem: Is one campfire is burning then every other campfire begins to burn, too. So you need only to activate one campfire to activate every campfire that is placed and that will be placed. If you remove them and place them a new campfire it starts automatically to burn. But I want it like the furnace with the two different/new requirements that I posted over this few lines.

 

So here is my code:

 

BlockPFCampFire

 

 

    /**
     * Ticks the block if it's been scheduled
     */
    public void updateTick(World world, int x, int y, int z, Random random) 
    {
    	if(world.isRemote)
    		return;
    	
    	if(TileEntityCampFire.burning(world, x, y, z, false))
    		if(world.isRaining() || world.isThundering())
    			if(world.canBlockSeeTheSky(x, y, z))
    				TileEntityCampFire.setBurning(false);
    }

    /**
     * Called upon block activation (right click on the block.)
     */
@Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float px, float py, float pz)
    {
	if(world.isRemote)
		return false;

	if(!player.isSneaking())
		if(player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().getItem() == Items.flint_and_steel)
			if(!world.isRaining() && !world.isThundering())
				TileEntityCampFire.setBurning(true);

	System.out.println("block1: " + TileEntityCampFire.burning(world, x, y, z, true));
	System.out.println("block2: " + TileEntityCampFire.burning(world, x, y, z, false));
	System.out.println("tile: " + TileEntityCampFire.isBurning());

	TileEntityCampFire tileentityCampfire = (TileEntityCampFire)world.getTileEntity(x, y, z);

	if(tileentityCampfire != null)
	{
		player.openGui(PrimevalForest.instance, 1, world, x, y, z);
		return true;
	}

	return false;
    }

    /**
     * Spawns flame particle over the camp fire
     */
    @Override
    @SideOnly(Side.CLIENT)
    public void randomDisplayTick(World world, int x, int y, int z, Random random)
    {
    	TileEntityCampFire tileentityCampfire = (TileEntityCampFire) world.getTileEntity(x, y, z);
    	
    	if(tileentityCampfire != null && TileEntityCampFire.burning(world, x, y, z, true) && TileEntityCampFire.isBurning())
    	{
    		for(int i = 0; i < 3; i++)
    		{
    			float motionY = (random.nextFloat() / 40F) + 0.025F;
    			
			double particleX = ((x + 0.5F) - 0.15F) + (random.nextInt(30) / 100F);
			double particleY = y + 0.1F + (random.nextInt(15) / 100F);
			double particleZ = ((z + 0.5F) - 0.15F) + (random.nextInt(30) / 100F);

			PrimevalForest.proxy.spawnFlame(world, particleX, particleY, particleZ, 0.0F, motionY, 0.0F, 16);
			world.spawnParticle("smoke", particleX, particleY, particleZ, 0.0D, 0.05D, 0.0D);
    		}
    	}
    }

 

 

 

TileEntityCampFire

 

/** Can the campfire burn or not */
public static boolean burning;

@Override
public void writeToNBT(NBTTagCompound nbtTagCompound)
{
	super.writeToNBT(nbtTagCompound);

	nbtTagCompound.setBoolean("isBurning", (boolean)burning);
	nbtTagCompound.setShort("BurnTime", (short)TileEntityCampFire.burnTime);
	nbtTagCompound.setShort("CookTime", (short)this.cookTime);
	nbtTagCompound.setShort("CurrentBurnTime", (short)this.currentItemBurnTime);

	NBTTagList list = new NBTTagList();

	for(int i = 0; i < this.slots.length; i++)
		if(this.slots[i] != null)
		{
			NBTTagCompound compound = new NBTTagCompound();
			compound.setByte("Slot", (byte)i);
			this.slots[i].writeToNBT(compound);
			list.appendTag(compound);
		}

	nbtTagCompound.setTag("Items", list);

	if(this.hasCustomInventoryName())
		nbtTagCompound.setString("CustomName", this.localizedName);
}

@Override
public void readFromNBT(NBTTagCompound nbtTagCompound)
{
	super.readFromNBT(nbtTagCompound);

	NBTTagList list = nbtTagCompound.getTagList("Items", 10);
	this.slots = new ItemStack[this.getSizeInventory()];

	for(int i = 0; i < list.tagCount(); i++)
	{
		NBTTagCompound compound = list.getCompoundTagAt(i);
		byte b = compound.getByte("Slot");

		if(b >= 0 && b < this.slots.length)
			this.slots[b] = ItemStack.loadItemStackFromNBT(compound);
	}

	setBurning(nbtTagCompound.getBoolean("isBurning"));
	TileEntityCampFire.burnTime = nbtTagCompound.getShort("BurnTime");
	this.cookTime = nbtTagCompound.getShort("CookTime");
	this.currentItemBurnTime = nbtTagCompound.getShort("CurrentBurnTime");

	if(nbtTagCompound.hasKey("CustomName", )
		this.localizedName = nbtTagCompound.getString("CustomName");
}

    /**
     * Camp Fire isBurning
     */
    public static boolean isBurning()
    {
        return TileEntityCampFire.burnTime > 0;
    }
    
    public static boolean burning(World world, int x, int y, int z, boolean lastArgNeeded)
    {
    	return (world.isRaining() || world.isThundering()) && world.canBlockSeeTheSky(x, y, z) && lastArgNeeded ? burning : true;
    }

@Override
public void updateEntity()
{
	boolean flag = false;

	if(TileEntityCampFire.isBurning() && burning(worldObj, xCoord, yCoord, zCoord, true))
		TileEntityCampFire.burnTime--;

	if(!this.worldObj.isRemote)
	{
		if(TileEntityCampFire.burnTime == 0 && burning(worldObj, xCoord, yCoord, zCoord, false))
		{
			this.currentItemBurnTime = TileEntityCampFire.burnTime = getItemBurnTime(this.slots[1]);

			if(TileEntityCampFire.isBurning())
			{
				flag = true;

				if(this.slots[1] != null)
					this.slots[1].stackSize--;

				if(this.slots[1].stackSize == 0)
					this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);
			}
		}
		else if(!burning(worldObj, xCoord, yCoord, zCoord, true))
			this.currentItemBurnTime = TileEntityCampFire.burnTime = 0;

		if(TileEntityCampFire.isBurning() && this.canSmelt() && burning(worldObj, xCoord, yCoord, zCoord, true))
		{
			this.cookTime++;

			if(this.cookTime == this.campFireSpeed)
			{
				this.cookTime = 0;
				this.smeltItem();
				flag = true;
			}
		}
		else
			this.cookTime = 0;
	}

	if(flag)
		this.markDirty();
}

    /**
     * @param burning is the camp fire burning
     */
    public static void setBurning(boolean burning)
    {
    	TileEntityCampFire.burning = burning;
    }

 

 

I used the method isBurning in the render class and the GUI class, too.

 

I hope someone knows how to fix this big issue.

Bektor

Developer of Primeval Forest.

Posted

Do I not see a createNewTileEntity() in your block class, or you just didn't post the full code?

I try my best, so apologies if I said something obviously stupid!

Posted

Sigh....

 

This is basic Java. You are incorrectly using the static modifier. The static modifier means that it will be the same for all other instances of that class, it is basically like a universal variable, that is the same everywhere. Your burning boolean is static, so if one is burning, every other instance of your tileentity will also be burning.

 

Read up on the static modifier.

I ask complicated questions, and apparently like to write really long detailed posts. But I also help others when I can.

Posted

Do I not see a createNewTileEntity() in your block class, or you just didn't post the full code?

Well, I didn't post the complete code. Why should I post methods that are not using the lines of code from that the problem is coming.

 

Sigh....

 

This is basic Java. You are incorrectly using the static modifier. The static modifier means that it will be the same for all other instances of that class, it is basically like a universal variable, that is the same everywhere. Your burning boolean is static, so if one is burning, every other instance of your tileentity will also be burning.

 

Read up on the static modifier.

Didn't fix the issue and I found a new one: If you create a new world then every campfire that you are placing is burning and after some time it stops burning. If you put then fuel into it, every placed Campfire will start burning!

 

TileEntityCampFire

/** Can the campfire burn or not */
public static boolean burning;

@Override
public void writeToNBT(NBTTagCompound nbtTagCompound)
{
	super.writeToNBT(nbtTagCompound);

	nbtTagCompound.setBoolean("isBurning", (boolean)burning);
	nbtTagCompound.setShort("BurnTime", (short)TileEntityCampFire.burnTime);
	nbtTagCompound.setShort("CookTime", (short)this.cookTime);
	nbtTagCompound.setShort("CurrentBurnTime", (short)this.currentItemBurnTime);

	NBTTagList list = new NBTTagList();

	for(int i = 0; i < this.slots.length; i++)
		if(this.slots[i] != null)
		{
			NBTTagCompound compound = new NBTTagCompound();
			compound.setByte("Slot", (byte)i);
			this.slots[i].writeToNBT(compound);
			list.appendTag(compound);
		}

	nbtTagCompound.setTag("Items", list);

	if(this.hasCustomInventoryName())
		nbtTagCompound.setString("CustomName", this.localizedName);
}

@Override
public void readFromNBT(NBTTagCompound nbtTagCompound)
{
	super.readFromNBT(nbtTagCompound);

	NBTTagList list = nbtTagCompound.getTagList("Items", 10);
	this.slots = new ItemStack[this.getSizeInventory()];

	for(int i = 0; i < list.tagCount(); i++)
	{
		NBTTagCompound compound = list.getCompoundTagAt(i);
		byte b = compound.getByte("Slot");

		if(b >= 0 && b < this.slots.length)
			this.slots[b] = ItemStack.loadItemStackFromNBT(compound);
	}

	setBurning(nbtTagCompound.getBoolean("isBurning"));
	TileEntityCampFire.burnTime = nbtTagCompound.getShort("BurnTime");
	this.cookTime = nbtTagCompound.getShort("CookTime");
	this.currentItemBurnTime = nbtTagCompound.getShort("CurrentBurnTime");

	if(nbtTagCompound.hasKey("CustomName", )
		this.localizedName = nbtTagCompound.getString("CustomName");
}

    /**
     * Camp Fire isBurning
     */
    public boolean isBurning()
    {
        return TileEntityCampFire.burnTime > 0;
    }
    
    public boolean burning(World world, int x, int y, int z, boolean lastArgNeeded)
    {
    	return (world.isRaining() || world.isThundering()) && world.canBlockSeeTheSky(x, y, z) && lastArgNeeded ? burning : true;
    }

@Override
public void updateEntity()
{
	boolean flag = false;

	if(this.isBurning() && burning(worldObj, xCoord, yCoord, zCoord, true))
		TileEntityCampFire.burnTime--;

	if(!this.worldObj.isRemote)
	{
		if(TileEntityCampFire.burnTime == 0 && burning(worldObj, xCoord, yCoord, zCoord, false))
		{
			this.currentItemBurnTime = TileEntityCampFire.burnTime = getItemBurnTime(this.slots[1]);

			if(this.isBurning())
			{
				flag = true;

				if(this.slots[1] != null)
					this.slots[1].stackSize--;

				if(this.slots[1].stackSize == 0)
					this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);
			}
		}
		else if(!burning(worldObj, xCoord, yCoord, zCoord, true) || !isBurning())
			this.currentItemBurnTime = TileEntityCampFire.burnTime = 0;

		if(this.isBurning() && this.canSmelt() && burning(worldObj, xCoord, yCoord, zCoord, true))
		{
			this.cookTime++;

			if(this.cookTime == this.campFireSpeed)
			{
				this.cookTime = 0;
				this.smeltItem();
				flag = true;
			}
		}
		else
			this.cookTime = 0;
	}

	if(flag)
		this.markDirty();
}

    /**
     * @param burning is the camp fire burning
     */
    public void setBurning(boolean burning)
    {
    	TileEntityCampFire.burning = burning;
    }

 

BlockPFCampFire

    /**
     * Ticks the block if it's been scheduled
     */
    public void updateTick(World world, int x, int y, int z, Random random) 
    {
    	if(world.isRemote)
    		return;
    	
    	TileEntityCampFire tileentityCampfire = (TileEntityCampFire)world.getTileEntity(x, y, z);
    	
    	if(tileentityCampfire != null)
    	{
        	if(tileentityCampfire.burning(world, x, y, z, false))
        		if(world.isRaining() || world.isThundering())
        			if(world.canBlockSeeTheSky(x, y, z))
        				tileentityCampfire.setBurning(false);
    	}
    }

    /**
     * Called upon block activation (right click on the block.)
     */
@Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float px, float py, float pz)
    {
	TileEntityCampFire tileentityCampfire = (TileEntityCampFire)world.getTileEntity(x, y, z);

	if(tileentityCampfire != null)
	{

		if(world.isRemote)
			return false;

		if(!player.isSneaking())
			if(player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().getItem() == Items.flint_and_steel)
				if(!world.isRaining() && !world.isThundering())
					tileentityCampfire.setBurning(true);


		player.openGui(PrimevalForest.instance, 1, world, x, y, z);
		return true;
	}

	return false;
    }

    /**
     * Spawns flame particle over the camp fire
     */
    @Override
    @SideOnly(Side.CLIENT)
    public void randomDisplayTick(World world, int x, int y, int z, Random random)
    {
    	TileEntityCampFire tileentityCampfire = (TileEntityCampFire) world.getTileEntity(x, y, z);
    	
    	if(tileentityCampfire != null && tileentityCampfire.burning(world, x, y, z, true) && tileentityCampfire.isBurning())
    	{
    		for(int i = 0; i < 3; i++)
    		{
    			float motionY = (random.nextFloat() / 40F) + 0.025F;
    			
			double particleX = ((x + 0.5F) - 0.15F) + (random.nextInt(30) / 100F);
			double particleY = y + 0.1F + (random.nextInt(15) / 100F);
			double particleZ = ((z + 0.5F) - 0.15F) + (random.nextInt(30) / 100F);

			PrimevalForest.proxy.spawnFlame(world, particleX, particleY, particleZ, 0.0F, motionY, 0.0F, 16);
			world.spawnParticle("smoke", particleX, particleY, particleZ, 0.0D, 0.05D, 0.0D);
    		}
    	}
    }

    /**
     * Triggered whenever an entity collides with this block (enters into the block). Args: world, x, y, z, entity
     */
    @Override
    public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity)
    {
    	super.onEntityCollidedWithBlock(world, x, y, z, entity);
    	
    	TileEntityCampFire tileentityCampfire = (TileEntityCampFire)world.getTileEntity(x, y, z);
    	
    	if(tileentityCampfire != null)
    	{
        	if(tileentityCampfire.burning(world, x, y, z, true))
        	{
        		entity.attackEntityFrom(DamageSource.inFire, 1.0F);
        		entity.setFire(;
        	}
    	}
    }

 

So anyone who has an idea on how to fix this two issues?

Developer of Primeval Forest.

Posted

Your burning and burntime should not be static. This is basic Java - the reason it's affecting all of your fires is because they all share the same class field for burning and burnTime. And to check if something is burning you wouldn't have static methods (unless they're just convenience methods).

 

So to check if it's burning would become

TileEntityCampFire tileEntity = (TileEntityCampFire) world.getTileEntity(x, y, z);
if(tileEntity.isBurning())
{
    ...
}
[/code

BEFORE ASKING FOR HELP READ THE EAQ!

 

I'll help if I can. Apologies if I do something obviously stupid. :D

 

If you don't know basic Java yet, go and follow these tutorials.

Posted

Your burning and burntime should not be static. This is basic Java - the reason it's affecting all of your fires is because they all share the same class field for burning and burnTime. And to check if something is burning you wouldn't have static methods (unless they're just convenience methods).

 

So to check if it's burning would become

TileEntityCampFire tileEntity = (TileEntityCampFire) world.getTileEntity(x, y, z);
if(tileEntity.isBurning())
{
    ...
}
[/code

Thanks. I changed everything to non-static and forgot the boolean itself...

Developer of Primeval Forest.

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.