Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.modmcdl.magitech.blocks;
- import java.util.List;
- import java.util.Random;
- import javax.annotation.Nullable;
- import com.modmcdl.magitech.Magitech;
- import com.modmcdl.magitech.init.ModBlocks;
- import net.minecraft.block.Block;
- import net.minecraft.block.BlockBush;
- import net.minecraft.block.BlockHorizontal;
- import net.minecraft.block.IGrowable;
- import net.minecraft.block.SoundType;
- import net.minecraft.block.material.Material;
- import net.minecraft.block.properties.IProperty;
- import net.minecraft.block.properties.PropertyEnum;
- import net.minecraft.block.state.BlockStateContainer;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.creativetab.CreativeTabs;
- 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.stats.StatList;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.IStringSerializable;
- import net.minecraft.util.NonNullList;
- import net.minecraft.util.math.AxisAlignedBB;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.IBlockAccess;
- import net.minecraft.world.World;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- public class BlockTwoPlant extends BlockBush implements IGrowable, net.minecraftforge.common.IShearable
- {
- public static final PropertyEnum<BlockTwoPlant.EnumPlantType> VARIANT = PropertyEnum.<BlockTwoPlant.EnumPlantType>create("variant", BlockTwoPlant.EnumPlantType.class);
- public static final PropertyEnum<BlockTwoPlant.EnumBlockHalf> HALF = PropertyEnum.<BlockTwoPlant.EnumBlockHalf>create("half", BlockTwoPlant.EnumBlockHalf.class);
- public static final PropertyEnum<EnumFacing> FACING = BlockHorizontal.FACING;
- public BlockTwoPlant()
- {
- super(Material.PLANTS);
- this.setDefaultState(this.blockState.getBaseState().withProperty(HALF, BlockTwoPlant.EnumBlockHalf.LOWER));
- this.setHardness(0.0F);
- this.setCreativeTab(Magitech.tabMagitech);
- this.setSoundType(SoundType.PLANT);
- this.setUnlocalizedName("doublePlant");
- }
- public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
- {
- return FULL_BLOCK_AABB;
- }
- private BlockTwoPlant.EnumPlantType getType(IBlockAccess blockAccess, BlockPos pos, IBlockState state)
- {
- if (state.getBlock() == this)
- {
- state = state.getActualState(blockAccess, pos);
- return (BlockTwoPlant.EnumPlantType)state.getValue(VARIANT);
- }
- else
- {
- return BlockTwoPlant.EnumPlantType.ASPHODELPLANT;
- }
- }
- public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
- {
- return super.canPlaceBlockAt(worldIn, pos) && worldIn.isAirBlock(pos.up());
- }
- /**
- * Whether this Block can be replaced directly by other blocks (true for e.g. tall grass)
- */
- public boolean isReplaceable(IBlockAccess worldIn, BlockPos pos)
- {
- IBlockState iblockstate = worldIn.getBlockState(pos);
- if (iblockstate.getBlock() != this)
- {
- return true;
- }
- else
- {
- BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype = (BlockTwoPlant.EnumPlantType)iblockstate.getActualState(worldIn, pos).getValue(VARIANT);
- return blockasphodelplant$enumplanttype == BlockTwoPlant.EnumPlantType.ASPHODELPLANT || blockasphodelplant$enumplanttype == BlockTwoPlant.EnumPlantType.ASPHODELPLANT;
- }
- }
- protected void checkAndDropBlock(World worldIn, BlockPos pos, IBlockState state)
- {
- if (!this.canBlockStay(worldIn, pos, state))
- {
- boolean flag = state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER;
- BlockPos blockpos = flag ? pos : pos.up();
- BlockPos blockpos1 = flag ? pos.down() : pos;
- Block block = (Block)(flag ? this : worldIn.getBlockState(blockpos).getBlock());
- Block block1 = (Block)(flag ? worldIn.getBlockState(blockpos1).getBlock() : this);
- if (!flag) this.dropBlockAsItem(worldIn, pos, state, 0); //Forge move above the setting to air.
- if (block == this)
- {
- worldIn.setBlockState(blockpos, Blocks.AIR.getDefaultState(), 2);
- }
- if (block1 == this)
- {
- worldIn.setBlockState(blockpos1, Blocks.AIR.getDefaultState(), 3);
- }
- }
- }
- public boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state)
- {
- if (state.getBlock() != this) return super.canBlockStay(worldIn, pos, state); //Forge: This function is called during world gen and placement, before this block is set, so if we are not 'here' then assume it's the pre-check.
- if (state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER)
- {
- return worldIn.getBlockState(pos.down()).getBlock() == this;
- }
- else
- {
- IBlockState iblockstate = worldIn.getBlockState(pos.up());
- return iblockstate.getBlock() == this && super.canBlockStay(worldIn, pos, iblockstate);
- }
- }
- /**
- * Get the Item that this Block should drop when harvested.
- */
- public Item getItemDropped(IBlockState state, Random rand, int fortune)
- {
- if (state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER)
- {
- return Items.AIR;
- }
- else
- {
- BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype = (BlockTwoPlant.EnumPlantType)state.getValue(VARIANT);
- return blockasphodelplant$enumplanttype == BlockTwoPlant.EnumPlantType.ASPHODELPLANT ? Items.AIR : (blockasphodelplant$enumplanttype == BlockTwoPlant.EnumPlantType.ASPHODELPLANT ? (rand.nextInt(8) == 0 ? Items.WHEAT_SEEDS : Items.AIR) : super.getItemDropped(state, rand, fortune));
- }
- }
- /**
- * Gets the metadata of the item this Block can drop. This method is called when the block gets destroyed. It
- * returns the metadata of the dropped item based on the old metadata of the block.
- */
- public int damageDropped(IBlockState state)
- {
- return state.getValue(HALF) != BlockTwoPlant.EnumBlockHalf.UPPER && state.getValue(VARIANT) != BlockTwoPlant.EnumPlantType.ASPHODELPLANT ? ((BlockTwoPlant.EnumPlantType)state.getValue(VARIANT)).getMeta() : 0;
- }
- public void placeAt(World worldIn, BlockPos lowerPos, BlockTwoPlant.EnumPlantType variant, int flags)
- {
- worldIn.setBlockState(lowerPos, this.getDefaultState().withProperty(HALF, BlockTwoPlant.EnumBlockHalf.LOWER).withProperty(VARIANT, variant), flags);
- worldIn.setBlockState(lowerPos.up(), this.getDefaultState().withProperty(HALF, BlockTwoPlant.EnumBlockHalf.UPPER), flags);
- }
- /**
- * Called by ItemBlocks after a block is set in the world, to allow post-place logic
- */
- public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
- {
- worldIn.setBlockState(pos.up(), this.getDefaultState().withProperty(HALF, BlockTwoPlant.EnumBlockHalf.UPPER), 2);
- }
- public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack)
- {
- {
- super.harvestBlock(worldIn, player, pos, state, te, stack);
- }
- }
- public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player)
- {
- if (state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER)
- {
- if (worldIn.getBlockState(pos.down()).getBlock() == this)
- {
- if (player.capabilities.isCreativeMode)
- {
- worldIn.setBlockToAir(pos.down());
- }
- else
- {
- IBlockState iblockstate = worldIn.getBlockState(pos.down());
- BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype = (BlockTwoPlant.EnumPlantType)iblockstate.getValue(VARIANT);
- if (blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT && blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT)
- {
- worldIn.destroyBlock(pos.down(), true);
- }
- else if (worldIn.isRemote)
- {
- worldIn.setBlockToAir(pos.down());
- }
- else if (!player.getHeldItemMainhand().isEmpty() && player.getHeldItemMainhand().getItem() == Items.SHEARS)
- {
- this.onHarvest(worldIn, pos, iblockstate, player);
- worldIn.setBlockToAir(pos.down());
- }
- else
- {
- worldIn.destroyBlock(pos.down(), true);
- }
- }
- }
- }
- else if (worldIn.getBlockState(pos.up()).getBlock() == this)
- {
- worldIn.setBlockState(pos.up(), Blocks.AIR.getDefaultState(), 2);
- }
- super.onBlockHarvested(worldIn, pos, state, player);
- }
- private boolean onHarvest(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player)
- {
- BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype = (BlockTwoPlant.EnumPlantType)state.getValue(VARIANT);
- if (blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT && blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT)
- {
- return false;
- }
- else
- {
- player.addStat(StatList.getBlockStats(this));
- return true;
- }
- }
- /**
- * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
- */
- @SideOnly(Side.CLIENT)
- public void getSubBlocks(Item itemIn, CreativeTabs tab, NonNullList<ItemStack> list)
- {
- for (BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype : BlockTwoPlant.EnumPlantType.values())
- {
- list.add(new ItemStack(itemIn, 1, blockasphodelplant$enumplanttype.getMeta()));
- }
- }
- public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
- {
- return new ItemStack(this, 1, this.getType(worldIn, pos, state).getMeta());
- }
- /**
- * Whether this IGrowable can grow
- */
- public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient)
- {
- BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype = this.getType(worldIn, pos, state);
- return blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT && blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT;
- }
- public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state)
- {
- return true;
- }
- public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
- {
- spawnAsEntity(worldIn, pos, new ItemStack(this, 1, this.getType(worldIn, pos, state).getMeta()));
- }
- /**
- * Convert the given metadata into a BlockState for this Block
- */
- public IBlockState getStateFromMeta(int meta)
- {
- return (meta & 8) > 0 ? this.getDefaultState().withProperty(HALF, BlockTwoPlant.EnumBlockHalf.UPPER) : this.getDefaultState().withProperty(HALF, BlockTwoPlant.EnumBlockHalf.LOWER).withProperty(VARIANT, BlockTwoPlant.EnumPlantType.byMetadata(meta & 7));
- }
- /**
- * Get the actual Block state of this Block at the given position. This applies properties not visible in the
- * metadata, such as fence connections.
- */
- public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
- {
- if (state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER)
- {
- IBlockState iblockstate = worldIn.getBlockState(pos.down());
- if (iblockstate.getBlock() == this)
- {
- state = state.withProperty(VARIANT, iblockstate.getValue(VARIANT));
- }
- }
- return state;
- }
- /**
- * Convert the BlockState into the correct metadata value
- */
- public int getMetaFromState(IBlockState state)
- {
- return state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER ? 8 | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex() : ((BlockTwoPlant.EnumPlantType)state.getValue(VARIANT)).getMeta();
- }
- protected BlockStateContainer createBlockState()
- {
- return new BlockStateContainer(this, new IProperty[] {HALF, VARIANT, FACING});
- }
- /**
- * Get the OffsetType for this Block. Determines if the model is rendered slightly offset.
- */
- public Block.EnumOffsetType getOffsetType()
- {
- return Block.EnumOffsetType.XZ;
- }
- @Override
- public boolean isShearable(ItemStack item, IBlockAccess world, BlockPos pos)
- {
- IBlockState state = world.getBlockState(pos);
- EnumPlantType type = (EnumPlantType)state.getValue(VARIANT);
- return state.getValue(HALF) == EnumBlockHalf.LOWER && (type == EnumPlantType.ASPHODELPLANT || type == EnumPlantType.ASPHODELPLANT);
- }
- @Override
- public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest)
- {
- //Forge: Break both parts on the client to prevent the top part flickering as default type for a few frames.
- if (state.getBlock() == this && state.getValue(HALF) == EnumBlockHalf.LOWER && world.getBlockState(pos.up()).getBlock() == this)
- world.setBlockToAir(pos.up());
- return world.setBlockToAir(pos);
- }
- public static enum EnumBlockHalf implements IStringSerializable
- {
- UPPER,
- LOWER;
- public String toString()
- {
- return this.getName();
- }
- public String getName()
- {
- return this == UPPER ? "upper" : "lower";
- }
- }
- public static enum EnumPlantType implements IStringSerializable
- {
- ASPHODELPLANT(0, "asphodelplant");
- private static final BlockTwoPlant.EnumPlantType[] META_LOOKUP = new BlockTwoPlant.EnumPlantType[values().length];
- private final int meta;
- private final String name;
- private final String unlocalizedName;
- private EnumPlantType(int meta, String name)
- {
- this(meta, name, name);
- }
- private EnumPlantType(int meta, String name, String unlocalizedName)
- {
- this.meta = meta;
- this.name = name;
- this.unlocalizedName = unlocalizedName;
- }
- public int getMeta()
- {
- return this.meta;
- }
- public String toString()
- {
- return this.name;
- }
- public static BlockTwoPlant.EnumPlantType byMetadata(int meta)
- {
- if (meta < 0 || meta >= META_LOOKUP.length)
- {
- meta = 1;
- }
- return META_LOOKUP[meta];
- }
- public String getName()
- {
- return this.name;
- }
- public String getUnlocalizedName()
- {
- return this.unlocalizedName;
- }
- static
- {
- for (BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype : values())
- {
- META_LOOKUP[blockasphodelplant$enumplanttype.getMeta()] = blockasphodelplant$enumplanttype;
- }
- }
- }
- @Override
- public List<ItemStack> onSheared(ItemStack item, IBlockAccess world, BlockPos pos, int fortune) {
- // TODO Auto-generated method stub
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement