package openblocks.common.block;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
import openmods.infobook.BookDocumentation;
import openmods.utils.BlockNotifyFlags;
import openmods.utils.render.RenderUtils;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@BookDocumentation(customName = "sky.normal")
public class BlockSky extends OpenBlock {

	public BlockSky() {
		super(Material.iron);
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void registerBlockIcons(IIconRegister registry) {
		blockIcon = registry.registerIcon("openblocks:sky_inactive");
	}

	@Override
	public int damageDropped(int meta) {
		return meta;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public int getBlockColor() {
		// randomness more or less intended
		return RenderUtils.getFogColor().getColor();
	}

	@Override
	public void onNeighborBlockChange(World world, int x, int y, int z, Block block) {
		if (!world.isRemote) {
			final int isPowered = world.isBlockIndirectlyGettingPowered(x, y, z)? 2 : 0;
			final int isActive = world.getBlockMetadata(x, y, z) & 2;

			if (isPowered != isActive) world.scheduleBlockUpdate(x, y, z, this, 1);
		}
	}

	@Override
	public void updateTick(World world, int x, int y, int z, Random random) {
		final int isPowered = world.isBlockIndirectlyGettingPowered(x, y, z)? 2 : 0;
		final int isInverted = world.getBlockMetadata(x, y, z) & 1;

		world.setBlockMetadataWithNotify(x, y, z, isPowered | isInverted, BlockNotifyFlags.ALL);
	}

	public static boolean isActive(int meta) {
		boolean isPowered = (meta & 2) != 0;
		boolean isInverted = (meta & 1) != 0;
		return isPowered ^ isInverted;
	}

	@Override
	public AxisAlignedBB getSelectedBoundingBoxFromPool(World world, int x, int y, int z) {
		int meta = world.getBlockMetadata(x, y, z);
		return isActive(meta)? AxisAlignedBB.getBoundingBox(0, 0, 0, 0, 0, 0) : super.getSelectedBoundingBoxFromPool(world, x, y, z);
	}

}

@@ -562,16 +562,116 @@
 lock %7B%0A%0A
+%09private static final int MASK_INVERTED = 1 %3C%3C 0;%0A%09private static final int MASK_POWERED = 1 %3C%3C 1;%0A%0A
 %09public 
@@ -683,16 +683,16 @@
 Sky() %7B%0A
-
 %09%09super(
@@ -934,16 +934,32 @@
 urn meta
+ & MASK_INVERTED
 ;%0A%09%7D%0A%0A%09@
@@ -1295,33 +1295,44 @@
 wered(x, y, z)? 
-2
+MASK_POWERED
  : 0;%0A%09%09%09final i
@@ -1379,17 +1379,28 @@
 y, z) & 
-2
+MASK_POWERED
 ;%0A%0A%09%09%09if
@@ -1635,17 +1635,28 @@
  y, z)? 
-2
+MASK_POWERED
  : 0;%0A%09%09
@@ -1712,17 +1712,29 @@
 y, z) & 
-1
+MASK_INVERTED
 ;%0A%0A%09%09wor
@@ -1901,9 +1901,20 @@
 a & 
-2
+MASK_POWERED
 ) !=
@@ -1948,17 +1948,29 @@
 (meta & 
-1
+MASK_INVERTED
 ) != 0;%0A

Fix #695 (thanks @shadowfacts)