Posted July 14, 201510 yr 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.
July 18, 201510 yr Author Still haven't figured this out. Is there a method in World to update lighting? I can't find it if it exists. 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.
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.