Jump to content

Custom furnace won't change texture when burning (or active)


Recommended Posts

Posted

Good day,

I tried everything and I can't seem to get it to work.

I have made a custom furnace and I want to make it change texture when active (things are "smelting"). I tried everything that I can do; tried vanilla code, tried some tutorials, but none of them worked.

 

Here's the actual block code:

package elementum;

import java.util.Random;
import elementum.Elementum;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class BlockCellingMachine extends BlockContainer
{
    private Random inputFurnaceRand;
    private final boolean isActive;
    private static boolean keepInventory = false;
    private static String textureName;
    
    @SideOnly(Side.CLIENT)
    private Icon topTexture;
    private Icon frontTexture;

    public BlockCellingMachine(int i, boolean flag)
    {
        super(i, Material.iron);
        inputFurnaceRand = new Random();
        isActive = flag;
        this.textureName = "cellingMachine";
    }

    @SideOnly(Side.CLIENT)
    public void registerIcons(IconRegister Icon)
    {
        this.blockIcon = Icon.registerIcon(Elementum.modid + ":" + "basicSteelBlock");
        this.frontTexture = Icon.registerIcon(isActive ? Elementum.modid + ":" + textureName + "_front_on" : Elementum.modid + ":" + textureName + "_front");
        this.topTexture = Icon.registerIcon(Elementum.modid + ":" + textureName + "_top");
    }

    @SideOnly(Side.CLIENT)
    public Icon getIcon(int side, int meta){
    	
    	return meta == 0 && side == 3 ? this.frontTexture : (side == meta ? this.frontTexture : side == 1 ? this.topTexture : this.blockIcon) ;
    	
    }
    
    public void onBlockAdded(World par1World, int par2, int par3, int par4)
    {
        super.onBlockAdded(par1World, par2, par3, par4);
        this.setDefaultDirection(par1World, par2, par3, par4);
    }

    private void setDefaultDirection(World par1World, int par2, int par3, int par4)
    {
        if (!par1World.isRemote)
        {
            int l = par1World.getBlockId(par2, par3, par4 - 1);
            int i1 = par1World.getBlockId(par2, par3, par4 + 1);
            int j1 = par1World.getBlockId(par2 - 1, par3, par4);
            int k1 = par1World.getBlockId(par2 + 1, par3, par4);
            byte b0 = 3;

            if (Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[i1])
            {
                b0 = 3;
            }

            if (Block.opaqueCubeLookup[i1] && !Block.opaqueCubeLookup[l])
            {
                b0 = 2;
            }

            if (Block.opaqueCubeLookup[j1] && !Block.opaqueCubeLookup[k1])
            {
                b0 = 5;
            }

            if (Block.opaqueCubeLookup[k1] && !Block.opaqueCubeLookup[j1])
            {
                b0 = 4;
            }

            par1World.setBlockMetadataWithNotify(par2, par3, par4, b0, 2);
        }
    }
    
    /*public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLivingBase par5EntityLivingBase, ItemStack par6ItemStack)
    {
        int l = MathHelper.floor_double((double)(par5EntityLivingBase.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

        if (l == 0)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 2, 2);
        }

        if (l == 1)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 5, 2);
        }

        if (l == 2)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 3, 2);
        }

        if (l == 3)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 4, 2);
        }
    }*/
        
    /**
     * Called upon block activation (right click on the block.)
     */
    public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    {
    	if (par1World.isRemote) 
	{
		return true;
	} 
	else if (!par5EntityPlayer.isSneaking()) 
	{
		TileEntityCellingMachine var10 = (TileEntityCellingMachine) par1World.getBlockTileEntity(par2, par3, par4);
		if (var10 != null) 
		{
			par5EntityPlayer.openGui(Elementum.instance, 0, par1World, par2, par3, par4);
		}
		return true;
	} 
	else 
	{
		return false;
	}
    }
    
    /**
     * Update which block ID the furnace is using depending on whether or not it is burning
     */
    public static void updateFurnaceBlockState(boolean active, World worldObj, int x, int y, int z)
    {
	int i = worldObj.getBlockMetadata(x, y, z);
	TileEntity tileentity = worldObj.getBlockTileEntity(x, y, z);
	keepInventory = true;

	if(active){
		worldObj.setBlock(x, y, z, Elementum.cellingMachineActive.blockID);
	}else{
		worldObj.setBlock(x, y, z, Elementum.cellingMachine.blockID);
	}

	keepInventory = false;

	worldObj.setBlockMetadataWithNotify(x, y, z, i, 2);

	if(tileentity != null){
		tileentity.validate();
		worldObj.setBlockTileEntity(x, y, z, tileentity);
	}
    }

    /**
     * Returns the TileEntity used by this block.
     */
    public TileEntity getBlockEntity()
    {
        return new TileEntityCellingMachine();
    }
    
    public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLivingBase par5EntityLivingBase, ItemStack par6ItemStack)
    {
        int l = MathHelper.floor_double((double)(par5EntityLivingBase.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

        if (l == 0)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 2, 2);
        }

        if (l == 1)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 5, 2);
        }

        if (l == 2)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 3, 2);
        }

        if (l == 3)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 4, 2);
        }

        if (par6ItemStack.hasDisplayName())
        {
            ((TileEntityCellingMachine)par1World.getBlockTileEntity(par2, par3, par4)).setCustomName(par6ItemStack.getDisplayName());
        }
    }
    
    /**
     * Called whenever the block is removed.
     */
    public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
    {
        if (!keepInventory)
        {
            TileEntityCellingMachine var7 = (TileEntityCellingMachine)par1World.getBlockTileEntity(par2, par3, par4);

            if (var7 != null)
            {
                for (int var8 = 0; var8 < var7.getSizeInventory(); ++var8)
                {
                    ItemStack itemstack = var7.getStackInSlot(var8);

                    if (itemstack != null)
                    {
                        float f = this.inputFurnaceRand.nextFloat() * 0.8F + 0.1F;
                        float f1 = this.inputFurnaceRand.nextFloat() * 0.8F + 0.1F;
                        float f2 = this.inputFurnaceRand.nextFloat() * 0.8F + 0.1F;

                        while (itemstack.stackSize > 0)
                        {
                            int k1 = this.inputFurnaceRand.nextInt(21) + 10;

                            if (k1 > itemstack.stackSize)
                            {
                                k1 = itemstack.stackSize;
                            }

                            itemstack.stackSize -= k1;
                            EntityItem entityitem = new EntityItem(par1World, (double)((float)par2 + f), (double)((float)par3 + f1), (double)((float)par4 + f2), new ItemStack(itemstack.itemID, k1, itemstack.getItemDamage()));

                            if (itemstack.hasTagCompound())
                            {
                                entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
                            }

                            float f3 = 0.05F;
                            entityitem.motionX = (double)((float)this.inputFurnaceRand.nextGaussian() * f3);
                            entityitem.motionY = (double)((float)this.inputFurnaceRand.nextGaussian() * f3 + 0.2F);
                            entityitem.motionZ = (double)((float)this.inputFurnaceRand.nextGaussian() * f3);
                            par1World.spawnEntityInWorld(entityitem);
                        }
                    }
                }

                par1World.func_96440_m(par2, par3, par4, par5);
            }
        }

        super.breakBlock(par1World, par2, par3, par4, par5, par6);
    }
    
    public boolean hasComparatorInputOverride() 
{
        return true;
    }

    public int getComparatorInputOverride(World par1World, int par2, int par3, int par4, int par5) 
{
        return Container.calcRedstoneFromInventory((IInventory)par1World.getBlockTileEntity(par2, par3, par4));
    }
    
    public int idPicked(World world, int x, int y, int z){
    	return Elementum.cellingMachine.blockID;
    }

@Override
public TileEntity createNewTileEntity(World world) 
{
	return new TileEntityCellingMachine();
}
}

Posted

Hi

 

The vanilla furnace changes its blockID when it becomes active, in updateFurnaceBlockState.  Have you used breakpoints or logging to check whether the ID is changing properly?  And that your TileEntity.updateEntity() being called properly?

 

-TGG

 

 

Posted

The vanilla furnace changes its blockID when it becomes active
But you shouldn't replicate that. With forge it's totally sufficient to change the metadata. Vanilla changes the blockId because the vanilla code doesn't allow different light levels based on metadata, but forge does.

 

So...what should we do then?

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.