Advertisement
Guest User

Sapling

a guest
Feb 23rd, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.52 KB | None | 0 0
  1. public class BlockVlumSapling extends BlockBush implements IGrowable {
  2.     public static final PropertyEnum<BlockVlumSapling.EnumType> TYPE = PropertyEnum.<BlockVlumSapling.EnumType>create(
  3.             "type", BlockVlumSapling.EnumType.class);
  4.     public static final PropertyInteger STAGE = PropertyInteger.create("stage", 0, 1);
  5.     protected static final AxisAlignedBB SAPLING_AABB = new AxisAlignedBB(0.09999999403953552D, 0.0D,
  6.             0.09999999403953552D, 0.8999999761581421D, 0.800000011920929D, 0.8999999761581421D);
  7.  
  8.     public BlockVlumSapling(String name) {
  9.         setUnlocalizedName(name);
  10.         setRegistryName(name);
  11.         setCreativeTab(Main.MAIN_TAB);
  12.         setSoundType(SoundType.PLANT);
  13.         this.setDefaultState(this.blockState.getBaseState().withProperty(TYPE, BlockVlumSapling.EnumType.VLUM)
  14.                 .withProperty(STAGE, Integer.valueOf(0)));
  15.  
  16.         BlockInit.BLOCKS.add(this);
  17.         ItemInit.ITEMS.add(new ItemBlock(this).setRegistryName(this.getRegistryName()));
  18.     }
  19.  
  20.     public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
  21.         return SAPLING_AABB;
  22.     }
  23.  
  24.     public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) {
  25.         if (!worldIn.isRemote) {
  26.             super.updateTick(worldIn, pos, state, rand);
  27.  
  28.             if (!worldIn.isAreaLoaded(pos, 1))
  29.                 return; // Forge: prevent loading unloaded chunks when checking neighbor's light
  30.             if (worldIn.getLightFromNeighbors(pos.up()) >= 9 && rand.nextInt(7) == 0) {
  31.                 this.grow(worldIn, pos, state, rand);
  32.             }
  33.         }
  34.     }
  35.  
  36.     public void grow(World worldIn, BlockPos pos, IBlockState state, Random rand) {
  37.         if (((Integer) state.getValue(STAGE)).intValue() == 0) {
  38.             worldIn.setBlockState(pos, state.cycleProperty(STAGE), 4);
  39.         } else {
  40.             this.generateTree(worldIn, pos, state, rand);
  41.         }
  42.     }
  43.  
  44.     public void generateTree(World worldIn, BlockPos pos, IBlockState state, Random rand) {
  45.         if (!net.minecraftforge.event.terraingen.TerrainGen.saplingGrowTree(worldIn, rand, pos))
  46.             return;
  47.         WorldGenerator worldgenerator = (WorldGenerator) (rand.nextInt(10) == 0 ? new WorldGenVlumTree()
  48.                 : new WorldGenVlumTree());
  49.         int i = 0;
  50.         int j = 0;
  51.         boolean flag = false;
  52.  
  53.         IBlockState iblockstate2 = Blocks.AIR.getDefaultState();
  54.  
  55.         if (flag) {
  56.             worldIn.setBlockState(pos.add(i, 0, j), iblockstate2, 4);
  57.             worldIn.setBlockState(pos.add(i + 1, 0, j), iblockstate2, 4);
  58.             worldIn.setBlockState(pos.add(i, 0, j + 1), iblockstate2, 4);
  59.             worldIn.setBlockState(pos.add(i + 1, 0, j + 1), iblockstate2, 4);
  60.         } else {
  61.             worldIn.setBlockState(pos, iblockstate2, 4);
  62.         }
  63.  
  64.         if (!worldgenerator.generate(worldIn, rand, pos.add(i, 0, j))) {
  65.             if (flag) {
  66.                 worldIn.setBlockState(pos.add(i, 0, j), state, 4);
  67.                 worldIn.setBlockState(pos.add(i + 1, 0, j), state, 4);
  68.                 worldIn.setBlockState(pos.add(i, 0, j + 1), state, 4);
  69.                 worldIn.setBlockState(pos.add(i + 1, 0, j + 1), state, 4);
  70.             } else {
  71.                 worldIn.setBlockState(pos, state, 4);
  72.             }
  73.         }
  74.     }
  75.  
  76.     /**
  77.      * Check whether the given BlockPos has a Sapling of the given type
  78.      */
  79.     public boolean isTypeAt(World worldIn, BlockPos pos, BlockVlumSapling.EnumType type) {
  80.         IBlockState iblockstate = worldIn.getBlockState(pos);
  81.         return iblockstate.getBlock() == this && iblockstate.getValue(TYPE) == type;
  82.     }
  83.  
  84.     /**
  85.      * Gets the metadata of the item this Block can drop. This method is called when
  86.      * the block gets destroyed. It returns the metadata of the dropped item based
  87.      * on the old metadata of the block.
  88.      */
  89.     public int damageDropped(IBlockState state) {
  90.         return ((BlockVlumSapling.EnumType) state.getValue(TYPE)).getMetadata();
  91.     }
  92.  
  93.     /**
  94.      * returns a list of blocks with the same ID, but different meta (eg: wood
  95.      * returns 4 blocks)
  96.      */
  97.     public void getSubBlocks(CreativeTabs itemIn, NonNullList<ItemStack> items) {
  98.         for (BlockVlumSapling.EnumType blockplanks$enumtype : BlockVlumSapling.EnumType.values()) {
  99.             items.add(new ItemStack(this, 1, blockplanks$enumtype.getMetadata()));
  100.         }
  101.     }
  102.  
  103.     /**
  104.      * Whether this IGrowable can grow
  105.      */
  106.     public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient) {
  107.         return true;
  108.     }
  109.  
  110.     public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state) {
  111.         return (double) worldIn.rand.nextFloat() < 0.45D;
  112.     }
  113.  
  114.     public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state) {
  115.         this.grow(worldIn, pos, state, rand);
  116.     }
  117.  
  118.     /**
  119.      * Convert the given metadata into a BlockState for this Block
  120.      */
  121.     public IBlockState getStateFromMeta(int meta) {
  122.         return this.getDefaultState().withProperty(TYPE, BlockVlumSapling.EnumType.byMetadata(meta & 7))
  123.                 .withProperty(STAGE, Integer.valueOf((meta & 8) >> 3));
  124.     }
  125.  
  126.     /**
  127.      * Convert the BlockState into the correct metadata value
  128.      */
  129.     public int getMetaFromState(IBlockState state) {
  130.         int i = 0;
  131.         i = i | ((BlockVlumSapling.EnumType) state.getValue(TYPE)).getMetadata();
  132.         i = i | ((Integer) state.getValue(STAGE)).intValue() << 3;
  133.         return i;
  134.     }
  135.  
  136.     protected BlockStateContainer createBlockState() {
  137.         return new BlockStateContainer(this, new IProperty[] { TYPE, STAGE });
  138.     }
  139.  
  140.     public static enum EnumType implements IStringSerializable {
  141.         VLUM(0, "vlum", MapColor.BLUE);
  142.  
  143.         private static final BlockVlumSapling.EnumType[] META_LOOKUP = new BlockVlumSapling.EnumType[values().length];
  144.         private final int meta;
  145.         private final String name;
  146.         private final String unlocalizedName;
  147.         /** The color that represents this entry on a map. */
  148.         private final MapColor mapColor;
  149.  
  150.         private EnumType(int metaIn, String nameIn, MapColor mapColorIn) {
  151.             this(metaIn, nameIn, nameIn, mapColorIn);
  152.         }
  153.  
  154.         private EnumType(int metaIn, String nameIn, String unlocalizedNameIn, MapColor mapColorIn) {
  155.             this.meta = metaIn;
  156.             this.name = nameIn;
  157.             this.unlocalizedName = unlocalizedNameIn;
  158.             this.mapColor = mapColorIn;
  159.         }
  160.  
  161.         public int getMetadata() {
  162.             return this.meta;
  163.         }
  164.  
  165.         /**
  166.          * The color which represents this entry on a map.
  167.          */
  168.         public MapColor getMapColor() {
  169.             return this.mapColor;
  170.         }
  171.  
  172.         public String toString() {
  173.             return this.name;
  174.         }
  175.  
  176.         public static BlockVlumSapling.EnumType byMetadata(int meta) {
  177.             if (meta < 0 || meta >= META_LOOKUP.length) {
  178.                 meta = 0;
  179.             }
  180.  
  181.             return META_LOOKUP[meta];
  182.         }
  183.  
  184.         public String getName() {
  185.             return this.name;
  186.         }
  187.  
  188.         public String getUnlocalizedName() {
  189.             return this.unlocalizedName;
  190.         }
  191.  
  192.         static {
  193.             for (BlockVlumSapling.EnumType blockplanks$enumtype : values()) {
  194.                 META_LOOKUP[blockplanks$enumtype.getMetadata()] = blockplanks$enumtype;
  195.             }
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement