Advertisement
ModMCdl

BlockTwoPlant

Oct 6th, 2017
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.09 KB | None | 0 0
  1. package com.modmcdl.magitech.blocks;
  2. import java.util.List;
  3. import java.util.Random;
  4. import javax.annotation.Nullable;
  5.  
  6. import com.modmcdl.magitech.Magitech;
  7. import com.modmcdl.magitech.init.ModBlocks;
  8.  
  9. import net.minecraft.block.Block;
  10. import net.minecraft.block.BlockBush;
  11. import net.minecraft.block.BlockHorizontal;
  12. import net.minecraft.block.IGrowable;
  13. import net.minecraft.block.SoundType;
  14. import net.minecraft.block.material.Material;
  15. import net.minecraft.block.properties.IProperty;
  16. import net.minecraft.block.properties.PropertyEnum;
  17. import net.minecraft.block.state.BlockStateContainer;
  18. import net.minecraft.block.state.IBlockState;
  19. import net.minecraft.creativetab.CreativeTabs;
  20. import net.minecraft.entity.EntityLivingBase;
  21. import net.minecraft.entity.player.EntityPlayer;
  22. import net.minecraft.init.Blocks;
  23. import net.minecraft.init.Items;
  24. import net.minecraft.item.Item;
  25. import net.minecraft.item.ItemStack;
  26. import net.minecraft.stats.StatList;
  27. import net.minecraft.tileentity.TileEntity;
  28. import net.minecraft.util.EnumFacing;
  29. import net.minecraft.util.IStringSerializable;
  30. import net.minecraft.util.NonNullList;
  31. import net.minecraft.util.math.AxisAlignedBB;
  32. import net.minecraft.util.math.BlockPos;
  33. import net.minecraft.world.IBlockAccess;
  34. import net.minecraft.world.World;
  35. import net.minecraftforge.fml.relauncher.Side;
  36. import net.minecraftforge.fml.relauncher.SideOnly;
  37.  
  38. public class BlockTwoPlant extends BlockBush implements IGrowable, net.minecraftforge.common.IShearable
  39. {
  40.     public static final PropertyEnum<BlockTwoPlant.EnumPlantType> VARIANT = PropertyEnum.<BlockTwoPlant.EnumPlantType>create("variant", BlockTwoPlant.EnumPlantType.class);
  41.     public static final PropertyEnum<BlockTwoPlant.EnumBlockHalf> HALF = PropertyEnum.<BlockTwoPlant.EnumBlockHalf>create("half", BlockTwoPlant.EnumBlockHalf.class);
  42.     public static final PropertyEnum<EnumFacing> FACING = BlockHorizontal.FACING;
  43.  
  44.     public BlockTwoPlant()
  45.     {
  46.         super(Material.PLANTS);
  47.         this.setDefaultState(this.blockState.getBaseState().withProperty(HALF, BlockTwoPlant.EnumBlockHalf.LOWER));
  48.         this.setHardness(0.0F);
  49.         this.setCreativeTab(Magitech.tabMagitech);
  50.         this.setSoundType(SoundType.PLANT);
  51.         this.setUnlocalizedName("doublePlant");
  52.     }
  53.  
  54.     public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
  55.     {
  56.         return FULL_BLOCK_AABB;
  57.     }
  58.  
  59.     private BlockTwoPlant.EnumPlantType getType(IBlockAccess blockAccess, BlockPos pos, IBlockState state)
  60.     {
  61.         if (state.getBlock() == this)
  62.         {
  63.             state = state.getActualState(blockAccess, pos);
  64.             return (BlockTwoPlant.EnumPlantType)state.getValue(VARIANT);
  65.         }
  66.         else
  67.         {
  68.             return BlockTwoPlant.EnumPlantType.ASPHODELPLANT;
  69.         }
  70.     }
  71.  
  72.     public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
  73.     {
  74.         return super.canPlaceBlockAt(worldIn, pos) && worldIn.isAirBlock(pos.up());
  75.     }
  76.  
  77.     /**
  78.      * Whether this Block can be replaced directly by other blocks (true for e.g. tall grass)
  79.      */
  80.     public boolean isReplaceable(IBlockAccess worldIn, BlockPos pos)
  81.     {
  82.         IBlockState iblockstate = worldIn.getBlockState(pos);
  83.  
  84.         if (iblockstate.getBlock() != this)
  85.         {
  86.             return true;
  87.         }
  88.         else
  89.         {
  90.             BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype = (BlockTwoPlant.EnumPlantType)iblockstate.getActualState(worldIn, pos).getValue(VARIANT);
  91.             return blockasphodelplant$enumplanttype == BlockTwoPlant.EnumPlantType.ASPHODELPLANT || blockasphodelplant$enumplanttype == BlockTwoPlant.EnumPlantType.ASPHODELPLANT;
  92.         }
  93.     }
  94.  
  95.     protected void checkAndDropBlock(World worldIn, BlockPos pos, IBlockState state)
  96.     {
  97.         if (!this.canBlockStay(worldIn, pos, state))
  98.         {
  99.             boolean flag = state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER;
  100.             BlockPos blockpos = flag ? pos : pos.up();
  101.             BlockPos blockpos1 = flag ? pos.down() : pos;
  102.             Block block = (Block)(flag ? this : worldIn.getBlockState(blockpos).getBlock());
  103.             Block block1 = (Block)(flag ? worldIn.getBlockState(blockpos1).getBlock() : this);
  104.  
  105.             if (!flag) this.dropBlockAsItem(worldIn, pos, state, 0); //Forge move above the setting to air.
  106.  
  107.             if (block == this)
  108.             {
  109.                 worldIn.setBlockState(blockpos, Blocks.AIR.getDefaultState(), 2);
  110.             }
  111.  
  112.             if (block1 == this)
  113.             {
  114.                 worldIn.setBlockState(blockpos1, Blocks.AIR.getDefaultState(), 3);
  115.             }
  116.         }
  117.     }
  118.  
  119.     public boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state)
  120.     {
  121.         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.
  122.         if (state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER)
  123.         {
  124.             return worldIn.getBlockState(pos.down()).getBlock() == this;
  125.         }
  126.         else
  127.         {
  128.             IBlockState iblockstate = worldIn.getBlockState(pos.up());
  129.             return iblockstate.getBlock() == this && super.canBlockStay(worldIn, pos, iblockstate);
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Get the Item that this Block should drop when harvested.
  135.      */
  136.     public Item getItemDropped(IBlockState state, Random rand, int fortune)
  137.     {
  138.         if (state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER)
  139.         {
  140.             return Items.AIR;
  141.         }
  142.         else
  143.         {
  144.             BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype = (BlockTwoPlant.EnumPlantType)state.getValue(VARIANT);
  145.             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));
  146.         }
  147.     }
  148.  
  149.     /**
  150.      * Gets the metadata of the item this Block can drop. This method is called when the block gets destroyed. It
  151.      * returns the metadata of the dropped item based on the old metadata of the block.
  152.      */
  153.     public int damageDropped(IBlockState state)
  154.     {
  155.         return state.getValue(HALF) != BlockTwoPlant.EnumBlockHalf.UPPER && state.getValue(VARIANT) != BlockTwoPlant.EnumPlantType.ASPHODELPLANT ? ((BlockTwoPlant.EnumPlantType)state.getValue(VARIANT)).getMeta() : 0;
  156.     }
  157.  
  158.     public void placeAt(World worldIn, BlockPos lowerPos, BlockTwoPlant.EnumPlantType variant, int flags)
  159.     {
  160.         worldIn.setBlockState(lowerPos, this.getDefaultState().withProperty(HALF, BlockTwoPlant.EnumBlockHalf.LOWER).withProperty(VARIANT, variant), flags);
  161.         worldIn.setBlockState(lowerPos.up(), this.getDefaultState().withProperty(HALF, BlockTwoPlant.EnumBlockHalf.UPPER), flags);
  162.     }
  163.  
  164.     /**
  165.      * Called by ItemBlocks after a block is set in the world, to allow post-place logic
  166.      */
  167.     public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
  168.     {
  169.         worldIn.setBlockState(pos.up(), this.getDefaultState().withProperty(HALF, BlockTwoPlant.EnumBlockHalf.UPPER), 2);
  170.     }
  171.  
  172.     public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack)
  173.     {
  174.         {
  175.             super.harvestBlock(worldIn, player, pos, state, te, stack);
  176.         }
  177.     }
  178.  
  179.     public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player)
  180.     {
  181.         if (state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER)
  182.         {
  183.             if (worldIn.getBlockState(pos.down()).getBlock() == this)
  184.             {
  185.                 if (player.capabilities.isCreativeMode)
  186.                 {
  187.                     worldIn.setBlockToAir(pos.down());
  188.                 }
  189.                 else
  190.                 {
  191.                     IBlockState iblockstate = worldIn.getBlockState(pos.down());
  192.                     BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype = (BlockTwoPlant.EnumPlantType)iblockstate.getValue(VARIANT);
  193.  
  194.                     if (blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT && blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT)
  195.                     {
  196.                         worldIn.destroyBlock(pos.down(), true);
  197.                     }
  198.                     else if (worldIn.isRemote)
  199.                     {
  200.                         worldIn.setBlockToAir(pos.down());
  201.                     }
  202.                     else if (!player.getHeldItemMainhand().isEmpty() && player.getHeldItemMainhand().getItem() == Items.SHEARS)
  203.                     {
  204.                         this.onHarvest(worldIn, pos, iblockstate, player);
  205.                         worldIn.setBlockToAir(pos.down());
  206.                     }
  207.                     else
  208.                     {
  209.                         worldIn.destroyBlock(pos.down(), true);
  210.                     }
  211.                 }
  212.             }
  213.         }
  214.         else if (worldIn.getBlockState(pos.up()).getBlock() == this)
  215.         {
  216.             worldIn.setBlockState(pos.up(), Blocks.AIR.getDefaultState(), 2);
  217.         }
  218.  
  219.         super.onBlockHarvested(worldIn, pos, state, player);
  220.     }
  221.  
  222.     private boolean onHarvest(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player)
  223.     {
  224.         BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype = (BlockTwoPlant.EnumPlantType)state.getValue(VARIANT);
  225.  
  226.         if (blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT && blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT)
  227.         {
  228.             return false;
  229.         }
  230.         else
  231.         {
  232.             player.addStat(StatList.getBlockStats(this));
  233.             return true;
  234.         }
  235.     }
  236.  
  237.     /**
  238.      * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
  239.      */
  240.     @SideOnly(Side.CLIENT)
  241.     public void getSubBlocks(Item itemIn, CreativeTabs tab, NonNullList<ItemStack> list)
  242.     {
  243.         for (BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype : BlockTwoPlant.EnumPlantType.values())
  244.         {
  245.             list.add(new ItemStack(itemIn, 1, blockasphodelplant$enumplanttype.getMeta()));
  246.         }
  247.     }
  248.  
  249.     public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
  250.     {
  251.         return new ItemStack(this, 1, this.getType(worldIn, pos, state).getMeta());
  252.     }
  253.  
  254.     /**
  255.      * Whether this IGrowable can grow
  256.      */
  257.     public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient)
  258.     {
  259.         BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype = this.getType(worldIn, pos, state);
  260.         return blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT && blockasphodelplant$enumplanttype != BlockTwoPlant.EnumPlantType.ASPHODELPLANT;
  261.     }
  262.  
  263.     public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state)
  264.     {
  265.         return true;
  266.     }
  267.    
  268.     public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
  269.     {
  270.         spawnAsEntity(worldIn, pos, new ItemStack(this, 1, this.getType(worldIn, pos, state).getMeta()));
  271.     }
  272.    
  273.  
  274.     /**
  275.      * Convert the given metadata into a BlockState for this Block
  276.      */
  277.     public IBlockState getStateFromMeta(int meta)
  278.     {
  279.         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));
  280.     }
  281.  
  282.     /**
  283.      * Get the actual Block state of this Block at the given position. This applies properties not visible in the
  284.      * metadata, such as fence connections.
  285.      */
  286.     public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
  287.     {
  288.         if (state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER)
  289.         {
  290.             IBlockState iblockstate = worldIn.getBlockState(pos.down());
  291.  
  292.             if (iblockstate.getBlock() == this)
  293.             {
  294.                 state = state.withProperty(VARIANT, iblockstate.getValue(VARIANT));
  295.             }
  296.         }
  297.  
  298.         return state;
  299.     }
  300.  
  301.     /**
  302.      * Convert the BlockState into the correct metadata value
  303.      */
  304.     public int getMetaFromState(IBlockState state)
  305.     {
  306.         return state.getValue(HALF) == BlockTwoPlant.EnumBlockHalf.UPPER ? 8 | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex() : ((BlockTwoPlant.EnumPlantType)state.getValue(VARIANT)).getMeta();
  307.     }
  308.  
  309.     protected BlockStateContainer createBlockState()
  310.     {
  311.         return new BlockStateContainer(this, new IProperty[] {HALF, VARIANT, FACING});
  312.     }
  313.  
  314.     /**
  315.      * Get the OffsetType for this Block. Determines if the model is rendered slightly offset.
  316.      */
  317.     public Block.EnumOffsetType getOffsetType()
  318.     {
  319.         return Block.EnumOffsetType.XZ;
  320.     }
  321.  
  322.     @Override
  323.     public boolean isShearable(ItemStack item, IBlockAccess world, BlockPos pos)
  324.     {
  325.         IBlockState state = world.getBlockState(pos);
  326.         EnumPlantType type = (EnumPlantType)state.getValue(VARIANT);
  327.         return state.getValue(HALF) == EnumBlockHalf.LOWER && (type == EnumPlantType.ASPHODELPLANT || type == EnumPlantType.ASPHODELPLANT);
  328.     }
  329.  
  330.  
  331.     @Override
  332.     public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest)
  333.     {
  334.         //Forge: Break both parts on the client to prevent the top part flickering as default type for a few frames.
  335.         if (state.getBlock() ==  this && state.getValue(HALF) == EnumBlockHalf.LOWER && world.getBlockState(pos.up()).getBlock() == this)
  336.             world.setBlockToAir(pos.up());
  337.         return world.setBlockToAir(pos);
  338.     }
  339.  
  340.     public static enum EnumBlockHalf implements IStringSerializable
  341.     {
  342.         UPPER,
  343.         LOWER;
  344.  
  345.         public String toString()
  346.         {
  347.             return this.getName();
  348.         }
  349.  
  350.         public String getName()
  351.         {
  352.             return this == UPPER ? "upper" : "lower";
  353.         }
  354.     }
  355.  
  356.     public static enum EnumPlantType implements IStringSerializable
  357.     {
  358.         ASPHODELPLANT(0, "asphodelplant");
  359.  
  360.         private static final BlockTwoPlant.EnumPlantType[] META_LOOKUP = new BlockTwoPlant.EnumPlantType[values().length];
  361.         private final int meta;
  362.         private final String name;
  363.         private final String unlocalizedName;
  364.  
  365.         private EnumPlantType(int meta, String name)
  366.         {
  367.             this(meta, name, name);
  368.         }
  369.  
  370.         private EnumPlantType(int meta, String name, String unlocalizedName)
  371.         {
  372.             this.meta = meta;
  373.             this.name = name;
  374.             this.unlocalizedName = unlocalizedName;
  375.         }
  376.  
  377.         public int getMeta()
  378.         {
  379.             return this.meta;
  380.         }
  381.  
  382.         public String toString()
  383.         {
  384.             return this.name;
  385.         }
  386.  
  387.         public static BlockTwoPlant.EnumPlantType byMetadata(int meta)
  388.         {
  389.             if (meta < 0 || meta >= META_LOOKUP.length)
  390.             {
  391.                 meta = 1;
  392.             }
  393.  
  394.             return META_LOOKUP[meta];
  395.         }
  396.  
  397.         public String getName()
  398.         {
  399.             return this.name;
  400.         }
  401.  
  402.         public String getUnlocalizedName()
  403.         {
  404.             return this.unlocalizedName;
  405.         }
  406.  
  407.         static
  408.         {
  409.             for (BlockTwoPlant.EnumPlantType blockasphodelplant$enumplanttype : values())
  410.             {
  411.                 META_LOOKUP[blockasphodelplant$enumplanttype.getMeta()] = blockasphodelplant$enumplanttype;
  412.             }
  413.         }
  414.     }
  415.  
  416.     @Override
  417.     public List<ItemStack> onSheared(ItemStack item, IBlockAccess world, BlockPos pos, int fortune) {
  418.         // TODO Auto-generated method stub
  419.         return null;
  420.     }
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement