| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Random;
|
| 5 | import net.minecraft.block.material.Material;
|
| 6 | import net.minecraft.block.properties.IProperty;
|
| 7 | import net.minecraft.block.properties.PropertyInteger;
|
| 8 | import net.minecraft.block.state.BlockState;
|
| 9 | import net.minecraft.block.state.IBlockState;
|
| 10 | import net.minecraft.creativetab.CreativeTabs;
|
| 11 | import net.minecraft.entity.player.EntityPlayer;
|
| 12 | import net.minecraft.init.Blocks;
|
| 13 | import net.minecraft.item.Item;
|
| 14 | import net.minecraft.item.ItemStack;
|
| 15 | import net.minecraft.tileentity.TileEntity;
|
| 16 | import net.minecraft.tileentity.TileEntityDaylightDetector;
|
| 17 | import net.minecraft.util.BlockPos;
|
| 18 | import net.minecraft.util.EnumFacing;
|
| 19 | import net.minecraft.util.MathHelper;
|
| 20 | import net.minecraft.world.EnumSkyBlock;
|
| 21 | import net.minecraft.world.IBlockAccess;
|
| 22 | import net.minecraft.world.World;
|
| 23 |
|
| 24 | public class BlockDaylightDetector extends BlockContainer
|
| 25 | {
|
| 26 | public static final PropertyInteger POWER = PropertyInteger.create("power", 0, 15);
|
| 27 | private final boolean inverted;
|
| 28 |
|
| 29 | public BlockDaylightDetector(boolean inverted)
|
| 30 | {
|
| 31 | super(Material.wood);
|
| 32 | this.inverted = inverted;
|
| 33 | this.setDefaultState(this.blockState.getBaseState().withProperty(POWER, Integer.valueOf(0)));
|
| 34 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.375F, 1.0F);
|
| 35 | this.setCreativeTab(CreativeTabs.tabRedstone);
|
| 36 | this.setHardness(0.2F);
|
| 37 | this.setStepSound(soundTypeWood);
|
| 38 | this.setUnlocalizedName("daylightDetector");
|
| 39 | }
|
| 40 |
|
| 41 | public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
|
| 42 | {
|
| 43 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.375F, 1.0F);
|
| 44 | }
|
| 45 |
|
| 46 | public int getWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
|
| 47 | {
|
| 48 | return ((Integer)state.getValue(POWER)).intValue();
|
| 49 | }
|
| 50 |
|
| 51 | public void updatePower(World worldIn, BlockPos pos)
|
| 52 | {
|
| 53 | if (!worldIn.provider.getHasNoSky())
|
| 54 | {
|
| 55 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 56 | int i = worldIn.getLightFor(EnumSkyBlock.SKY, pos) - worldIn.getSkylightSubtracted();
|
| 57 | float f = worldIn.getCelestialAngleRadians(1.0F);
|
| 58 | float f1 = f < (float)Math.PI ? 0.0F : ((float)Math.PI * 2F);
|
| 59 | f = f + (f1 - f) * 0.2F;
|
| 60 | i = Math.round((float)i * MathHelper.cos(f));
|
| 61 | i = MathHelper.clamp_int(i, 0, 15);
|
| 62 |
|
| 63 | if (this.inverted)
|
| 64 | {
|
| 65 | i = 15 - i;
|
| 66 | }
|
| 67 |
|
| 68 | if (((Integer)iblockstate.getValue(POWER)).intValue() != i)
|
| 69 | {
|
| 70 | worldIn.setBlockState(pos, iblockstate.withProperty(POWER, Integer.valueOf(i)), 3);
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 76 | {
|
| 77 | if (playerIn.isAllowEdit())
|
| 78 | {
|
| 79 | if (worldIn.isRemote)
|
| 80 | {
|
| 81 | return true;
|
| 82 | }
|
| 83 | else
|
| 84 | {
|
| 85 | if (this.inverted)
|
| 86 | {
|
| 87 | worldIn.setBlockState(pos, Blocks.daylight_detector.getDefaultState().withProperty(POWER, state.getValue(POWER)), 4);
|
| 88 | Blocks.daylight_detector.updatePower(worldIn, pos);
|
| 89 | }
|
| 90 | else
|
| 91 | {
|
| 92 | worldIn.setBlockState(pos, Blocks.daylight_detector_inverted.getDefaultState().withProperty(POWER, state.getValue(POWER)), 4);
|
| 93 | Blocks.daylight_detector_inverted.updatePower(worldIn, pos);
|
| 94 | }
|
| 95 |
|
| 96 | return true;
|
| 97 | }
|
| 98 | }
|
| 99 | else
|
| 100 | {
|
| 101 | return super.onBlockActivated(worldIn, pos, state, playerIn, side, hitX, hitY, hitZ);
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | /**
|
| 106 | * Get the Item that this Block should drop when harvested.
|
| 107 | */
|
| 108 | public Item getItemDropped(IBlockState state, Random rand, int fortune)
|
| 109 | {
|
| 110 | return Item.getItemFromBlock(Blocks.daylight_detector);
|
| 111 | }
|
| 112 |
|
| 113 | public Item getItem(World worldIn, BlockPos pos)
|
| 114 | {
|
| 115 | return Item.getItemFromBlock(Blocks.daylight_detector);
|
| 116 | }
|
| 117 |
|
| 118 | public boolean isFullCube()
|
| 119 | {
|
| 120 | return false;
|
| 121 | }
|
| 122 |
|
| 123 | /**
|
| 124 | * Used to determine ambient occlusion and culling when rebuilding chunks for render
|
| 125 | */
|
| 126 | public boolean isOpaqueCube()
|
| 127 | {
|
| 128 | return false;
|
| 129 | }
|
| 130 |
|
| 131 | /**
|
| 132 | * The type of render function called. 3 for standard block models, 2 for TESR's, 1 for liquids, -1 is no render
|
| 133 | */
|
| 134 | public int getRenderType()
|
| 135 | {
|
| 136 | return 3;
|
| 137 | }
|
| 138 |
|
| 139 | /**
|
| 140 | * Can this block provide power. Only wire currently seems to have this change based on its state.
|
| 141 | */
|
| 142 | public boolean canProvidePower()
|
| 143 | {
|
| 144 | return true;
|
| 145 | }
|
| 146 |
|
| 147 | /**
|
| 148 | * Returns a new instance of a block's tile entity class. Called on placing the block.
|
| 149 | */
|
| 150 | public TileEntity createNewTileEntity(World worldIn, int meta)
|
| 151 | {
|
| 152 | return new TileEntityDaylightDetector();
|
| 153 | }
|
| 154 |
|
| 155 | /**
|
| 156 | * Convert the given metadata into a BlockState for this Block
|
| 157 | */
|
| 158 | public IBlockState getStateFromMeta(int meta)
|
| 159 | {
|
| 160 | return this.getDefaultState().withProperty(POWER, Integer.valueOf(meta));
|
| 161 | }
|
| 162 |
|
| 163 | /**
|
| 164 | * Convert the BlockState into the correct metadata value
|
| 165 | */
|
| 166 | public int getMetaFromState(IBlockState state)
|
| 167 | {
|
| 168 | return ((Integer)state.getValue(POWER)).intValue();
|
| 169 | }
|
| 170 |
|
| 171 | protected BlockState createBlockState()
|
| 172 | {
|
| 173 | return new BlockState(this, new IProperty[] {POWER});
|
| 174 | }
|
| 175 |
|
| 176 | /**
|
| 177 | * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
|
| 178 | */
|
| 179 | public void getSubBlocks(Item itemIn, CreativeTabs tab, List<ItemStack> list)
|
| 180 | {
|
| 181 | if (!this.inverted)
|
| 182 | {
|
| 183 | super.getSubBlocks(itemIn, tab, list);
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|