Jump to content

Different Light Levels Based on Metadata


HenryRichard

Recommended Posts

I'm trying to make a block that changes light level based on it's metadata. I've got it to kind-sorta work, but not very well - it only works if I use /setblock. This is the code:

package com.henrich.epicwasteoftime.blocks;

import com.henrich.epicwasteoftime.EWOTCreativeTabs;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.util.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class PinkiteBlock extends Block {

public static final PropertyBool POWERED = PropertyBool.create("powered");

protected static final int onLight = (int)(15.0F * 1f);
protected static final int offLight = (int)(15.0F * 0f);

public PinkiteBlock(String unlocalizedName, Material material) {
	super(material);
	this.setUnlocalizedName(unlocalizedName);
	this.setDefaultState(this.blockState.getBaseState().withProperty(POWERED, false));
                this.setCreativeTab(EWOTCreativeTabs.ewotBlocks);
}

public PinkiteBlock(String unlocalizedName) {
	this(unlocalizedName, Material.rock);
}
    
    @Override
protected BlockState createBlockState() {
    return new BlockState(this, new IProperty[] { POWERED });
}
    
    @Override
public IBlockState getStateFromMeta(int meta) {
	switch(meta) {
	case 0:
		return getDefaultState().withProperty(POWERED, false);
	case 1:
		return getDefaultState().withProperty(POWERED, true);
	default:
		return getDefaultState().withProperty(POWERED, false);
	}
}
    
    @Override
public int getMetaFromState(IBlockState state) {
    return state == this.getDefaultState() ? 0 : 1;
}
    
    @Override
    /**
     * Get a light value for the block at the specified coordinates, normal ranges are between 0 and 15
     *
     * @param world The current world
     * @param pos Block position in world
     * @return The light value
     */
    public int getLightValue(IBlockAccess world, BlockPos pos)
    {
        Block block = world.getBlockState(pos).getBlock();
        if (block != this)
        {
            return block.getLightValue(world, pos);
        }
        return world.getBlockState(pos) == this.getDefaultState() ? offLight : onLight;
    }
    
    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
    {
        super.onBlockAdded(worldIn, pos, state);

    	worldIn.setBlockState(pos, this.blockState.getBaseState().withProperty(POWERED, worldIn.isBlockPowered(pos)));
    	
    	worldIn.markBlockForUpdate(pos);
    }

    /**
     * Called when a neighboring block changes.
     */
    public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
    {
    	worldIn.setBlockState(pos, this.blockState.getBaseState().withProperty(POWERED, worldIn.isBlockPowered(pos)));
    
    	worldIn.markBlockForUpdate(pos);
    }
}

I'll put something here when I have something of value I need to put at the end of every post. For now it's this mostly pointless text.

Link to comment
Share on other sites

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.