Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package growthcraft.block;
- import growthcraft.Growthcraft;
- import growthcraft.GrowthcraftBlocks;
- import java.util.Random;
- import net.minecraft.block.Block;
- import net.minecraft.block.BlockPistonBase;
- import net.minecraft.block.IGrowable;
- import net.minecraft.block.material.Material;
- import net.minecraft.block.properties.IProperty;
- import net.minecraft.block.properties.PropertyDirection;
- import net.minecraft.block.properties.PropertyInteger;
- import net.minecraft.block.state.BlockState;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Blocks;
- import net.minecraft.init.Items;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.util.AxisAlignedBB;
- import net.minecraft.util.BlockPos;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.EnumWorldBlockLayer;
- import net.minecraft.world.IBlockAccess;
- import net.minecraft.world.World;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- public class BlockApple extends Block implements IGrowable
- {
- //CONSTANTS
- private final int GROWTH = 5;
- //PROPERTIES
- public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 2);
- public static final PropertyDirection FACING = PropertyDirection.create("facing");
- public BlockApple(String name)
- {
- super(Material.plants);
- this.setUnlocalizedName(Growthcraft.MODID + "." + name);
- this.setHardness(0.2F);
- this.setResistance(5.0F);
- this.setStepSound(soundTypeWood);
- this.setCreativeTab(null);
- this.setTickRandomly(true);
- this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(AGE, Integer.valueOf(0)));
- }
- /************
- * BLOCK UPDATE
- ************/
- @Override
- public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
- {
- if (!this.canBlockStay(worldIn, pos, state))
- {
- this.dropBlock(worldIn, pos, state);
- }
- else if (worldIn.rand.nextInt(this.GROWTH) == 0)
- {
- int currentAge = ((Integer)state.getValue(AGE)).intValue();
- if (currentAge < 2)
- {
- worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(currentAge + 1)), 2);
- }
- }
- }
- private boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state)
- {
- pos = pos.offset(((EnumFacing)state.getValue(FACING)));
- IBlockState iblockstate1 = worldIn.getBlockState(pos);
- if (iblockstate1.getBlock() == GrowthcraftBlocks.appleLeaves)
- {
- return (Boolean) iblockstate1.getValue(BlockAppleLeaves.DECAYABLE);
- }
- else
- {
- return false;
- }
- }
- /************
- * TRIGGERS
- ************/
- @Override
- public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
- {
- EnumFacing placerFacing = BlockPistonBase.getFacingFromEntity(worldIn, pos, placer);
- if (placerFacing == EnumFacing.UP)
- {
- EnumFacing enumfacing = EnumFacing.fromAngle((double)placer.rotationYaw);
- worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
- }
- else
- {
- worldIn.setBlockState(pos, state.withProperty(FACING, placerFacing), 2);
- }
- }
- @Override
- public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
- {
- //if (!facing.getAxis().isHorizontal())
- //{
- // facing = EnumFacing.NORTH;
- //}
- if (facing == EnumFacing.UP)
- {
- facing = EnumFacing.NORTH;
- }
- return this.getDefaultState().withProperty(FACING, facing).withProperty(AGE, Integer.valueOf(0));
- }
- @Override
- public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
- {
- if (!this.canBlockStay(worldIn, pos, state))
- {
- this.dropBlock(worldIn, pos, state);
- }
- }
- private void dropBlock(World worldIn, BlockPos pos, IBlockState state)
- {
- worldIn.setBlockState(pos, Blocks.air.getDefaultState(), 3);
- this.dropBlockAsItem(worldIn, pos, state, 0);
- }
- /************
- * BLOCK META/STATE
- ************/
- /*
- * IF bits 0x4 and 0x8 is equal to 3
- -> apple is downward
- check bits 0x1 and 0x2 for stage
- 0 = stage 0
- 1 = stage 2
- 2 = stage 3
- ELSE
- -> apple is sidewards
- check bits 0x1 and 0x2 for orientation
- 0 = attached to the north
- 1 = attached to the east
- 2 = attached to the south
- 3 = attached to the west
- check bits 0x4 and 0x8 for stage
- 0 = stage 0
- 1 = stage 1
- 2 = stage 2
- *
- */
- @Override
- public IBlockState getStateFromMeta(int meta)
- {
- EnumFacing enumfacing = EnumFacing.getHorizontal(meta);
- int i = (meta >> 2);
- if (i == 3)
- {
- enumfacing = EnumFacing.DOWN;
- i = (meta & 3);
- }
- return this.getDefaultState().withProperty(FACING, enumfacing).withProperty(AGE, Integer.valueOf(i));
- }
- @Override
- public int getMetaFromState(IBlockState state)
- {
- byte b0 = 0;
- EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
- int age = ((Integer)state.getValue(AGE)).intValue();
- int i;
- if (enumfacing == EnumFacing.DOWN)
- {
- i = b0 | age;
- i |= 12;
- }
- else
- {
- i = b0 | (enumfacing).getHorizontalIndex();
- i |= age << 2;
- }
- return i;
- }
- @Override
- protected BlockState createBlockState()
- {
- return new BlockState(this, new IProperty[] {FACING, AGE});
- }
- /************
- * STUFF
- ************/
- @Override
- @SideOnly(Side.CLIENT)
- public Item getItem(World worldIn, BlockPos pos)
- {
- return Items.apple;
- }
- @Override
- public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient)
- {
- return ((Integer)state.getValue(AGE)).intValue() < 2;
- }
- @Override public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state) { return true; }
- @Override
- public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
- {
- worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(((Integer)state.getValue(AGE)).intValue() + 1)), 2);
- }
- @Override public boolean canSilkHarvest(World world, BlockPos pos, IBlockState state, EntityPlayer player) { return false; }
- /************
- * DROPS
- ************/
- @Override
- public Item getItemDropped(IBlockState state, Random rand, int fortune)
- {
- return ((Integer)state.getValue(AGE)).intValue() == 2 ? Items.apple : null;
- }
- @Override
- public int quantityDropped(Random random)
- {
- return 1;
- }
- @Override
- public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
- {
- super.dropBlockAsItemWithChance(worldIn, pos, state, chance, 0);
- }
- /************
- * BOUNDING BOX
- ************/
- @Override
- public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
- {
- //this.setBlockBoundsBasedOnState(worldIn, pos);
- //return super.getCollisionBoundingBox(worldIn, pos, state);
- return null;
- }
- @Override
- @SideOnly(Side.CLIENT)
- public AxisAlignedBB getSelectedBoundingBox(World worldIn, BlockPos pos)
- {
- this.setBlockBoundsBasedOnState(worldIn, pos);
- return super.getSelectedBoundingBox(worldIn, pos);
- }
- @Override
- public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
- {
- IBlockState iblockstate = worldIn.getBlockState(pos);
- EnumFacing enumfacing = (EnumFacing)iblockstate.getValue(FACING);
- int age = ((Integer)iblockstate.getValue(AGE)).intValue();
- float f;
- if (age == 0)
- {
- f = 0.015625F;
- switch (BlockApple.SwitchEnumFacing.FACING_LOOKUP[enumfacing.ordinal()])
- {
- case 1:
- this.setBlockBounds(0.0F, 0.0F, 1.0F - f, 1.0F, 1.0F, 1.0F);
- break;
- case 2:
- this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, f);
- break;
- case 3:
- this.setBlockBounds(0.0F, 0.0F, 0.0F, f, 1.0F, 1.0F);
- break;
- case 4:
- this.setBlockBounds(1.0F - f, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
- break;
- case 5:
- this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, f, 1.0F);
- }
- }
- else
- {
- f = 0.125F + 0.03125F * (age + 1);
- switch (BlockApple.SwitchEnumFacing.FACING_LOOKUP[enumfacing.ordinal()])
- {
- case 1:
- this.setBlockBounds(0.5F - f, 0.5F - f, 1.0F - f, 0.5F + f, 0.5F + f, 1.0F);
- break;
- case 2:
- this.setBlockBounds(0.5F - f, 0.5F - f, 0.0F, 0.5F + f, 0.5F + f, f);
- break;
- case 3:
- this.setBlockBounds(0.0F, 0.5F - f, 0.5F - f, f, 0.5F + f, 0.5F + f);
- break;
- case 4:
- this.setBlockBounds(1.0F - f, 0.5F - f, 0.5F - f, 1.0F, 0.5F + f, 0.5F + f);
- break;
- case 5:
- this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f, 0.5F + f);
- }
- }
- }
- static final class SwitchEnumFacing
- {
- static final int[] FACING_LOOKUP = new int[EnumFacing.values().length];
- private static final String __OBFID = "CL_00002130";
- static
- {
- try
- {
- FACING_LOOKUP[EnumFacing.SOUTH.ordinal()] = 1;
- }
- catch (NoSuchFieldError var5)
- {
- ;
- }
- try
- {
- FACING_LOOKUP[EnumFacing.NORTH.ordinal()] = 2;
- }
- catch (NoSuchFieldError var4)
- {
- ;
- }
- try
- {
- FACING_LOOKUP[EnumFacing.WEST.ordinal()] = 3;
- }
- catch (NoSuchFieldError var3)
- {
- ;
- }
- try
- {
- FACING_LOOKUP[EnumFacing.EAST.ordinal()] = 4;
- }
- catch (NoSuchFieldError var2)
- {
- ;
- }
- try
- {
- FACING_LOOKUP[EnumFacing.DOWN.ordinal()] = 5;
- }
- catch (NoSuchFieldError var1)
- {
- ;
- }
- }
- }
- /************
- * RENDERING
- ************/
- @Override public boolean isFullCube() { return false; }
- @Override public boolean isOpaqueCube() { return false; }
- @Override
- @SideOnly(Side.CLIENT)
- public EnumWorldBlockLayer getBlockLayer()
- {
- return EnumWorldBlockLayer.CUTOUT;
- }
- //@Override
- //@SideOnly(Side.CLIENT)
- //public Block.EnumOffsetType getOffsetType()
- //{
- // return Block.EnumOffsetType.XYZ;
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement