Advertisement
Boy132

CoreDoubleCrop

Apr 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.09 KB | None | 0 0
  1. public abstract class CoreDoubleCrop extends CoreCrop
  2. {
  3.     public static final PropertyEnum<EnumCropHalf> HALF = PropertyEnum.<EnumCropHalf>create("half", EnumCropHalf.class);
  4.    
  5.     public CoreDoubleCrop(String name)
  6.     {
  7.         super(name);
  8.        
  9.         setDefaultState(withProperties(0, EnumCropHalf.LOWER));
  10.     }
  11.    
  12.     @Override
  13.     public void updateTick(World world, BlockPos pos, IBlockState state, Random rand)
  14.     {
  15.         if(!world.isAreaLoaded(pos, 1))
  16.             return; // Forge: prevent loading unloaded chunks when checking neighbor's light
  17.        
  18.         if(getHalf(state) == EnumCropHalf.UPPER)
  19.             return;
  20.        
  21.         if(world.getLightFromNeighbors(pos.up().up()) >= 9)
  22.         {
  23.             int i = getAge(state);
  24.  
  25.             if (i < getMaxAge())
  26.             {
  27.                 float f = getGrowthChance(this, world, pos);
  28.  
  29.                 if(ForgeHooks.onCropsGrowPre(world, pos, state, rand.nextInt((int) (25.0F / f) + 1) == 0))
  30.                 {
  31.                     boolean lower = world.setBlockState(pos, withProperties(i + 1, EnumCropHalf.LOWER), 2);
  32.                     boolean upper = world.setBlockState(pos.up(), withProperties(i + 1, EnumCropHalf.UPPER), 2);
  33.                     System.out.println("updateTick: lower = " + lower + ", upper = " + upper);
  34.                     ForgeHooks.onCropsGrowPost(world, pos, state, world.getBlockState(pos));
  35.                 }
  36.             }
  37.         }
  38.     }
  39.    
  40.     @Override
  41.     public void grow(World world, BlockPos pos, IBlockState state)
  42.     {
  43.         if(getHalf(state) == EnumCropHalf.UPPER)
  44.             return;
  45.        
  46.         int i = getAge(state) + getBonemealAgeIncrease(world);
  47.         int j = getMaxAge();
  48.  
  49.         if(i > j)
  50.             i = j;
  51.  
  52.         boolean lower = world.setBlockState(pos, withProperties(i, EnumCropHalf.LOWER), 2);
  53.         boolean upper = world.setBlockState(pos.up(), withProperties(i, EnumCropHalf.UPPER), 2);
  54.         System.out.println("grow: lower = " + lower + ", upper = " + upper);
  55.     }
  56.  
  57.     @Override
  58.     public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
  59.     {
  60.         if(getHalf(state) == EnumCropHalf.UPPER)
  61.         {
  62.             drops.clear();
  63.             return;
  64.         }
  65.        
  66.         super.getDrops(drops, world, pos, state, 0);
  67.        
  68.         int age = getAge(state);
  69.         Random rand = world instanceof World ? ((World) world).rand : new Random();
  70.  
  71.         if(age >= getMaxAge())
  72.         {
  73.             int k = 3 + fortune;
  74.  
  75.             for(int i = 0; i < 3 + fortune; ++i)
  76.             {
  77.                 if(rand.nextInt(2 * getMaxAge()) <= age)
  78.                     drops.add(new ItemStack(this.getSeed(), 1, 0));
  79.             }
  80.         }
  81.     }
  82.  
  83.     @Override
  84.     public void dropBlockAsItemWithChance(World world, BlockPos pos, IBlockState state, float chance, int fortune)
  85.     {
  86.         if(getHalf(state) == EnumCropHalf.UPPER)
  87.             return;
  88.        
  89.         super.dropBlockAsItemWithChance(world, pos, state, chance, fortune);
  90.  
  91.         if(false && !world.isRemote) // Forge: NOP all this.
  92.         {
  93.             int i = getAge(state);
  94.  
  95.             if(i >= getMaxAge())
  96.             {
  97.                 int j = 3 + fortune;
  98.  
  99.                 for(int k = 0; k < j; ++k)
  100.                 {
  101.                     if(world.rand.nextInt(2 * getMaxAge()) <= i)
  102.                         spawnAsEntity(world, pos, new ItemStack(getSeed()));
  103.                 }
  104.             }
  105.         }
  106.     }
  107.  
  108.     @Override
  109.     public Item getItemDropped(IBlockState state, Random rand, int fortune)
  110.     {
  111.         if(getHalf(state) == EnumCropHalf.UPPER)
  112.             return Items.AIR;
  113.        
  114.         return isMaxAge(state) ? getCrop() : getSeed();
  115.     }
  116.  
  117.     @Override
  118.     public ItemStack getItem(World world, BlockPos pos, IBlockState state)
  119.     {
  120.         return new ItemStack(getSeed());
  121.     }
  122.    
  123.     @Override
  124.     public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te, ItemStack stack)
  125.     {
  126.         if(getHalf(state) == EnumCropHalf.UPPER)
  127.              world.destroyBlock(pos.down(), true);
  128.         else if(getHalf(state) == EnumCropHalf.LOWER)
  129.             world.setBlockToAir(pos.up());
  130.            
  131.         super.harvestBlock(world, player, pos, state, te, stack);
  132.     }
  133.    
  134.     @Override
  135.     public boolean canBlockStay(World world, BlockPos pos, IBlockState state)
  136.     {
  137.         if(state.getBlock() != this)
  138.             return super.canBlockStay(world, 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.
  139.        
  140.         if(getHalf(state) == EnumCropHalf.UPPER)
  141.             return world.getBlockState(pos.down()).getBlock() == this;
  142.         else
  143.             return world.getBlockState(pos.up()).getBlock() == this;
  144.     }
  145.    
  146.     @Override
  147.     public boolean canPlaceBlockAt(World world, BlockPos pos)
  148.     {
  149.         return super.canPlaceBlockAt(world, pos) && world.isAirBlock(pos.up());
  150.     }
  151.    
  152.     @Override
  153.     public void onBlockAdded(World world, BlockPos pos, IBlockState state)
  154.     {
  155.         if(getHalf(state) == EnumCropHalf.LOWER)
  156.             world.setBlockState(pos.up(), getDefaultState().withProperty(HALF, EnumCropHalf.UPPER), 2);
  157.     }
  158.  
  159.     @Override
  160.     public boolean canGrow(World world, BlockPos pos, IBlockState state, boolean isClient)
  161.     {
  162.         return !isMaxAge(state);
  163.     }
  164.  
  165.     @Override
  166.     public boolean canUseBonemeal(World world, Random rand, BlockPos pos, IBlockState state)
  167.     {
  168.         return getHalf(state) == EnumCropHalf.LOWER;
  169.     }
  170.  
  171.     @Override
  172.     public void grow(World world, Random rand, BlockPos pos, IBlockState state)
  173.     {
  174.         if(getHalf(state) == EnumCropHalf.LOWER)
  175.             grow(world, pos, state);
  176.     }
  177.  
  178.     @Override
  179.     public IBlockState getStateFromMeta(int meta)
  180.     {
  181.         int age = meta % 10;
  182.         EnumCropHalf half = meta >= 10 ? EnumCropHalf.LOWER : EnumCropHalf.UPPER;
  183.        
  184.         return withProperties(age, half);
  185.     }
  186.  
  187.     @Override
  188.     public int getMetaFromState(IBlockState state)
  189.     {
  190.         int meta = getAge(state);
  191.        
  192.         if(getHalf(state) == EnumCropHalf.LOWER)
  193.             meta += 10;
  194.  
  195.         return meta;
  196.     }
  197.    
  198.     protected PropertyEnum<EnumCropHalf> getHalfProperty()
  199.     {
  200.         return HALF;
  201.     }
  202.  
  203.     protected EnumCropHalf getHalf(IBlockState state)
  204.     {
  205.         return state.getValue(getHalfProperty());
  206.     }
  207.    
  208.     public IBlockState withProperties(int age, EnumCropHalf half)
  209.     {
  210.         return getDefaultState().withProperty(getAgeProperty(), Integer.valueOf(age)).withProperty(getHalfProperty(), half);
  211.     }
  212.    
  213.     @Override
  214.     public abstract AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos);
  215.    
  216.     protected abstract PropertyInteger getAgeProperty();
  217.    
  218.     @Override
  219.     protected abstract BlockStateContainer createBlockState();
  220.    
  221.     public static enum EnumCropHalf implements IStringSerializable
  222.     {
  223.         UPPER,
  224.         LOWER;
  225.  
  226.         public String toString()
  227.         {
  228.             return this.getName();
  229.         }
  230.  
  231.         public String getName()
  232.         {
  233.             return this == UPPER ? "upper" : "lower";
  234.         }
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement