| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import java.util.Random;
|
| 4 | import net.minecraft.block.material.Material;
|
| 5 | import net.minecraft.block.properties.IProperty;
|
| 6 | import net.minecraft.block.properties.PropertyInteger;
|
| 7 | import net.minecraft.block.state.BlockState;
|
| 8 | import net.minecraft.block.state.IBlockState;
|
| 9 | import net.minecraft.entity.EntityLivingBase;
|
| 10 | import net.minecraft.init.Blocks;
|
| 11 | import net.minecraft.init.Items;
|
| 12 | import net.minecraft.item.EnumDyeColor;
|
| 13 | import net.minecraft.item.Item;
|
| 14 | import net.minecraft.item.ItemStack;
|
| 15 | import net.minecraft.util.AxisAlignedBB;
|
| 16 | import net.minecraft.util.BlockPos;
|
| 17 | import net.minecraft.util.EnumFacing;
|
| 18 | import net.minecraft.util.EnumWorldBlockLayer;
|
| 19 | import net.minecraft.world.IBlockAccess;
|
| 20 | import net.minecraft.world.World;
|
| 21 |
|
| 22 | public class BlockCocoa extends BlockDirectional implements IGrowable
|
| 23 | {
|
| 24 | public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 2);
|
| 25 |
|
| 26 | public BlockCocoa()
|
| 27 | {
|
| 28 | super(Material.plants);
|
| 29 | this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(AGE, Integer.valueOf(0)));
|
| 30 | this.setTickRandomly(true);
|
| 31 | }
|
| 32 |
|
| 33 | public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 34 | {
|
| 35 | if (!this.canBlockStay(worldIn, pos, state))
|
| 36 | {
|
| 37 | this.dropBlock(worldIn, pos, state);
|
| 38 | }
|
| 39 | else if (worldIn.rand.nextInt(5) == 0)
|
| 40 | {
|
| 41 | int i = ((Integer)state.getValue(AGE)).intValue();
|
| 42 |
|
| 43 | if (i < 2)
|
| 44 | {
|
| 45 | worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(i + 1)), 2);
|
| 46 | }
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | public boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state)
|
| 51 | {
|
| 52 | pos = pos.offset((EnumFacing)state.getValue(FACING));
|
| 53 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 54 | return iblockstate.getBlock() == Blocks.log && iblockstate.getValue(BlockPlanks.VARIANT) == BlockPlanks.EnumType.JUNGLE;
|
| 55 | }
|
| 56 |
|
| 57 | public boolean isFullCube()
|
| 58 | {
|
| 59 | return false;
|
| 60 | }
|
| 61 |
|
| 62 | /**
|
| 63 | * Used to determine ambient occlusion and culling when rebuilding chunks for render
|
| 64 | */
|
| 65 | public boolean isOpaqueCube()
|
| 66 | {
|
| 67 | return false;
|
| 68 | }
|
| 69 |
|
| 70 | public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
|
| 71 | {
|
| 72 | this.setBlockBoundsBasedOnState(worldIn, pos);
|
| 73 | return super.getCollisionBoundingBox(worldIn, pos, state);
|
| 74 | }
|
| 75 |
|
| 76 | public AxisAlignedBB getSelectedBoundingBox(World worldIn, BlockPos pos)
|
| 77 | {
|
| 78 | this.setBlockBoundsBasedOnState(worldIn, pos);
|
| 79 | return super.getSelectedBoundingBox(worldIn, pos);
|
| 80 | }
|
| 81 |
|
| 82 | @SuppressWarnings("incomplete-switch")
|
| 83 | public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
|
| 84 | {
|
| 85 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 86 | EnumFacing enumfacing = (EnumFacing)iblockstate.getValue(FACING);
|
| 87 | int i = ((Integer)iblockstate.getValue(AGE)).intValue();
|
| 88 | int j = 4 + i * 2;
|
| 89 | int k = 5 + i * 2;
|
| 90 | float f = (float)j / 2.0F;
|
| 91 |
|
| 92 | switch (enumfacing)
|
| 93 | {
|
| 94 | case SOUTH:
|
| 95 | this.setBlockBounds((8.0F - f) / 16.0F, (12.0F - (float)k) / 16.0F, (15.0F - (float)j) / 16.0F, (8.0F + f) / 16.0F, 0.75F, 0.9375F);
|
| 96 | break;
|
| 97 |
|
| 98 | case NORTH:
|
| 99 | this.setBlockBounds((8.0F - f) / 16.0F, (12.0F - (float)k) / 16.0F, 0.0625F, (8.0F + f) / 16.0F, 0.75F, (1.0F + (float)j) / 16.0F);
|
| 100 | break;
|
| 101 |
|
| 102 | case WEST:
|
| 103 | this.setBlockBounds(0.0625F, (12.0F - (float)k) / 16.0F, (8.0F - f) / 16.0F, (1.0F + (float)j) / 16.0F, 0.75F, (8.0F + f) / 16.0F);
|
| 104 | break;
|
| 105 |
|
| 106 | case EAST:
|
| 107 | this.setBlockBounds((15.0F - (float)j) / 16.0F, (12.0F - (float)k) / 16.0F, (8.0F - f) / 16.0F, 0.9375F, 0.75F, (8.0F + f) / 16.0F);
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | /**
|
| 112 | * Called by ItemBlocks after a block is set in the world, to allow post-place logic
|
| 113 | */
|
| 114 | public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
|
| 115 | {
|
| 116 | EnumFacing enumfacing = EnumFacing.fromAngle((double)placer.rotationYaw);
|
| 117 | worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
|
| 118 | }
|
| 119 |
|
| 120 | /**
|
| 121 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 122 | * IBlockstate
|
| 123 | */
|
| 124 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 125 | {
|
| 126 | if (!facing.getAxis().isHorizontal())
|
| 127 | {
|
| 128 | facing = EnumFacing.NORTH;
|
| 129 | }
|
| 130 |
|
| 131 | return this.getDefaultState().withProperty(FACING, facing.getOpposite()).withProperty(AGE, Integer.valueOf(0));
|
| 132 | }
|
| 133 |
|
| 134 | /**
|
| 135 | * Called when a neighboring block changes.
|
| 136 | */
|
| 137 | public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
|
| 138 | {
|
| 139 | if (!this.canBlockStay(worldIn, pos, state))
|
| 140 | {
|
| 141 | this.dropBlock(worldIn, pos, state);
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | private void dropBlock(World worldIn, BlockPos pos, IBlockState state)
|
| 146 | {
|
| 147 | worldIn.setBlockState(pos, Blocks.air.getDefaultState(), 3);
|
| 148 | this.dropBlockAsItem(worldIn, pos, state, 0);
|
| 149 | }
|
| 150 |
|
| 151 | /**
|
| 152 | * Spawns this Block's drops into the World as EntityItems.
|
| 153 | */
|
| 154 | public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
|
| 155 | {
|
| 156 | int i = ((Integer)state.getValue(AGE)).intValue();
|
| 157 | int j = 1;
|
| 158 |
|
| 159 | if (i >= 2)
|
| 160 | {
|
| 161 | j = 3;
|
| 162 | }
|
| 163 |
|
| 164 | for (int k = 0; k < j; ++k)
|
| 165 | {
|
| 166 | spawnAsEntity(worldIn, pos, new ItemStack(Items.dye, 1, EnumDyeColor.BROWN.getDyeDamage()));
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | public Item getItem(World worldIn, BlockPos pos)
|
| 171 | {
|
| 172 | return Items.dye;
|
| 173 | }
|
| 174 |
|
| 175 | /**
|
| 176 | * Gets the meta to use for the Pick Block ItemStack result
|
| 177 | */
|
| 178 | public int getDamageValue(World worldIn, BlockPos pos)
|
| 179 | {
|
| 180 | return EnumDyeColor.BROWN.getDyeDamage();
|
| 181 | }
|
| 182 |
|
| 183 | /**
|
| 184 | * Whether this IGrowable can grow
|
| 185 | */
|
| 186 | public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient)
|
| 187 | {
|
| 188 | return ((Integer)state.getValue(AGE)).intValue() < 2;
|
| 189 | }
|
| 190 |
|
| 191 | public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state)
|
| 192 | {
|
| 193 | return true;
|
| 194 | }
|
| 195 |
|
| 196 | public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
|
| 197 | {
|
| 198 | worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(((Integer)state.getValue(AGE)).intValue() + 1)), 2);
|
| 199 | }
|
| 200 |
|
| 201 | public EnumWorldBlockLayer getBlockLayer()
|
| 202 | {
|
| 203 | return EnumWorldBlockLayer.CUTOUT;
|
| 204 | }
|
| 205 |
|
| 206 | /**
|
| 207 | * Convert the given metadata into a BlockState for this Block
|
| 208 | */
|
| 209 | public IBlockState getStateFromMeta(int meta)
|
| 210 | {
|
| 211 | return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(AGE, Integer.valueOf((meta & 15) >> 2));
|
| 212 | }
|
| 213 |
|
| 214 | /**
|
| 215 | * Convert the BlockState into the correct metadata value
|
| 216 | */
|
| 217 | public int getMetaFromState(IBlockState state)
|
| 218 | {
|
| 219 | int i = 0;
|
| 220 | i = i | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex();
|
| 221 | i = i | ((Integer)state.getValue(AGE)).intValue() << 2;
|
| 222 | return i;
|
| 223 | }
|
| 224 |
|
| 225 | protected BlockState createBlockState()
|
| 226 | {
|
| 227 | return new BlockState(this, new IProperty[] {FACING, AGE});
|
| 228 | }
|
| 229 | }
|
| 230 |
|