Skip to content

Commit

Permalink
Clean up berry bush code
Browse files Browse the repository at this point in the history
  • Loading branch information
ACGaming committed May 4, 2024
1 parent 9d0c300 commit 22a7abd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.progwml6.natura.nether.block.bush;

import java.util.Random;

import com.progwml6.natura.common.block.BlockEnumBerryBush;
import javax.annotation.Nonnull;

import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
Expand All @@ -12,6 +11,8 @@
import net.minecraftforge.common.EnumPlantType;
import net.minecraftforge.common.ForgeHooks;

import com.progwml6.natura.common.block.BlockEnumBerryBush;

public class BlockNetherBerryBush extends BlockEnumBerryBush
{
public BlockNetherBerryBush()
Expand All @@ -30,57 +31,48 @@ public EnumPlantType getPlantType(IBlockAccess world, BlockPos pos)
@Override
public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
{
int age = state.getValue(AGE).intValue();
int age = state.getValue(AGE);

if (age < 2)
{
int setMeta = rand.nextInt(2) + 1 + age;

worldIn.setBlockState(pos, this.getDefaultState().withProperty(AGE, Integer.valueOf(setMeta)), 4);
worldIn.setBlockState(pos, this.getDefaultState().withProperty(AGE, setMeta), 4);

return;
}

IBlockState upBlock = worldIn.getBlockState(pos.up());

if (upBlock == null || worldIn.isAirBlock(pos.up()))
if (worldIn.isAirBlock(pos.up()) && rand.nextInt(3) == 0)
{
if (rand.nextInt(3) == 0)
{
worldIn.setBlockState(pos.up(), this.getDefaultState().withProperty(AGE, Integer.valueOf(0)), 2);
}
return;
worldIn.setBlockState(pos.up(), this.getDefaultState().withProperty(AGE, 0), 2);
}

return;
}

@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
public void updateTick(@Nonnull World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull Random rand)
{
super.updateTick(worldIn, pos, state, rand);

int height;

for (height = 1; worldIn.getBlockState(pos.down(height)).getBlock() == this; ++height)
{
;
}

boolean canGrow = (rand.nextInt(75) == 0);

if (ForgeHooks.onCropsGrowPre(worldIn, pos, state, canGrow))
{
int age = state.getValue(AGE).intValue();
int age = state.getValue(AGE);

if (age < 3)
{
worldIn.setBlockState(pos, this.getDefaultState().withProperty(AGE, Integer.valueOf(age + 1)), 2);
worldIn.setBlockState(pos, this.getDefaultState().withProperty(AGE, age + 1), 2);
}

if (rand.nextInt(3) == 0 && height < 3 && worldIn.getBlockState(pos.up()).getBlock() == Blocks.AIR && age >= 2)
{
worldIn.setBlockState(pos.up(), this.getDefaultState().withProperty(AGE, Integer.valueOf(0)), 2);
worldIn.setBlockState(pos.up(), this.getDefaultState().withProperty(AGE, 0), 2);
}

ForgeHooks.onCropsGrowPost(worldIn, pos, state, worldIn.getBlockState(pos));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.progwml6.natura.overworld.block.bush;

import java.util.Random;

import com.progwml6.natura.common.block.BlockEnumBerryBush;
import javax.annotation.Nonnull;

import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
Expand All @@ -12,6 +11,8 @@
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;

import com.progwml6.natura.common.block.BlockEnumBerryBush;

public class BlockOverworldBerryBush extends BlockEnumBerryBush
{
public BlockOverworldBerryBush()
Expand All @@ -24,68 +25,56 @@ public BlockOverworldBerryBush()
@Override
public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
{
int age = state.getValue(AGE).intValue();
int age = state.getValue(AGE);

if (age < 2)
{
int setMeta = rand.nextInt(2) + 1 + age;

worldIn.setBlockState(pos, this.getDefaultState().withProperty(AGE, Integer.valueOf(setMeta)), 4);
worldIn.setBlockState(pos, this.getDefaultState().withProperty(AGE, setMeta), 4);

return;
}

IBlockState upBlock = worldIn.getBlockState(pos.up());

if (upBlock == null || worldIn.isAirBlock(pos.up()))
if (worldIn.isAirBlock(pos.up()) && rand.nextInt(3) == 0)
{
if (rand.nextInt(3) == 0)
{
worldIn.setBlockState(pos.up(), this.getDefaultState().withProperty(AGE, Integer.valueOf(0)), 2);
}
return;
worldIn.setBlockState(pos.up(), this.getDefaultState().withProperty(AGE, 0), 2);
}

return;
}

@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
public void updateTick(@Nonnull World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull Random rand)
{
super.updateTick(worldIn, pos, state, rand);

int height;

for (height = 1; worldIn.getBlockState(pos.down(height)).getBlock() == this; ++height)
{
;
}

boolean canGrow = (rand.nextInt(20) == 0);

if (worldIn.getLightFromNeighbors(pos) >= 8)
if (worldIn.getLightFromNeighbors(pos) >= 8 && ForgeHooks.onCropsGrowPre(worldIn, pos, state, canGrow))
{
if (ForgeHooks.onCropsGrowPre(worldIn, pos, state, canGrow))
{
int age = state.getValue(AGE).intValue();
int age = state.getValue(AGE);

if (age < 3)
{
worldIn.setBlockState(pos, this.getDefaultState().withProperty(AGE, Integer.valueOf(age + 1)), 2);
}

if (rand.nextInt(3) == 0 && height < 3 && worldIn.getBlockState(pos.up()).getBlock() == Blocks.AIR && age >= 2)
{
worldIn.setBlockState(pos.up(), this.getDefaultState().withProperty(AGE, Integer.valueOf(0)), 2);
}
if (age < 3)
{
worldIn.setBlockState(pos, this.getDefaultState().withProperty(AGE, age + 1), 2);
}

ForgeHooks.onCropsGrowPost(worldIn, pos, state, worldIn.getBlockState(pos));
if (rand.nextInt(3) == 0 && height < 3 && worldIn.getBlockState(pos.up()).getBlock() == Blocks.AIR && age >= 2)
{
worldIn.setBlockState(pos.up(), this.getDefaultState().withProperty(AGE, 0), 2);
}

ForgeHooks.onCropsGrowPost(worldIn, pos, state, worldIn.getBlockState(pos));
}
}

@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
public boolean canSustainPlant(@Nonnull IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull EnumFacing direction, @Nonnull net.minecraftforge.common.IPlantable plantable)
{
if (plantable instanceof BlockOverworldBerryBush && state.getPropertyKeys().contains(AGE))
{
Expand Down

0 comments on commit 22a7abd

Please sign in to comment.