Skip to content

Commit

Permalink
Clean up sapling code
Browse files Browse the repository at this point in the history
  • Loading branch information
ACGaming committed May 4, 2024
1 parent 7599c79 commit 9d0c300
Show file tree
Hide file tree
Showing 6 changed files with 429 additions and 455 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,8 @@

import java.util.Locale;
import java.util.Random;

import javax.annotation.Nonnull;

import com.progwml6.natura.Natura;
import com.progwml6.natura.library.NaturaRegistry;
import com.progwml6.natura.nether.NaturaNether;
import com.progwml6.natura.nether.block.leaves.BlockNetherLeaves;
import com.progwml6.natura.nether.block.leaves.BlockNetherLeaves2;
import com.progwml6.natura.nether.block.logs.BlockNetherLog;
import com.progwml6.natura.world.worldgen.trees.BaseTreeGenerator;
import com.progwml6.natura.world.worldgen.trees.nether.DarkwoodTreeGenerator;
import com.progwml6.natura.world.worldgen.trees.nether.FusewoodTreeGenerator;
import com.progwml6.natura.world.worldgen.trees.nether.GhostwoodTreeGenerator;

import net.minecraft.block.Block;
import net.minecraft.block.BlockSapling;
import net.minecraft.block.SoundType;
Expand All @@ -35,11 +23,22 @@
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;

import com.progwml6.natura.Natura;
import com.progwml6.natura.library.NaturaRegistry;
import com.progwml6.natura.nether.NaturaNether;
import com.progwml6.natura.nether.block.leaves.BlockNetherLeaves;
import com.progwml6.natura.nether.block.leaves.BlockNetherLeaves2;
import com.progwml6.natura.nether.block.logs.BlockNetherLog;
import com.progwml6.natura.world.worldgen.trees.BaseTreeGenerator;
import com.progwml6.natura.world.worldgen.trees.nether.DarkwoodTreeGenerator;
import com.progwml6.natura.world.worldgen.trees.nether.FusewoodTreeGenerator;
import com.progwml6.natura.world.worldgen.trees.nether.GhostwoodTreeGenerator;
import slimeknights.mantle.block.EnumBlock;

public class BlockNetherSapling extends BlockSapling
{
public static PropertyEnum<SaplingType> FOLIAGE = PropertyEnum.create("foliage", SaplingType.class);
public static final PropertyEnum<SaplingType> FOLIAGE = PropertyEnum.create("foliage", SaplingType.class);

public BlockNetherSapling()
{
Expand All @@ -50,118 +49,58 @@ public BlockNetherSapling()
}

@Override
public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> list)
{
for (SaplingType type : SaplingType.values())
{
list.add(new ItemStack(this, 1, this.getMetaFromState(this.getDefaultState().withProperty(FOLIAGE, type))));
}
}

@Nonnull
@Override
protected BlockStateContainer createBlockState()
{
// TYPE has to be included because of the BlockSapling constructor.. but it's never used.
return new BlockStateContainer(this, FOLIAGE, STAGE, TYPE);
}

/**
* Convert the given metadata into a BlockState for this Block
*/
@Nonnull
@Override
public IBlockState getStateFromMeta(int meta)
{
if (meta < 0 || meta >= SaplingType.values().length)
{
meta = 0;
}

SaplingType sapling = SaplingType.values()[meta];

return this.getDefaultState().withProperty(FOLIAGE, sapling);
}

/**
* Convert the BlockState into the correct metadata value
*/
@Override
public int getMetaFromState(IBlockState state)
{
return state.getValue(FOLIAGE).ordinal();
}

@Override
public int damageDropped(IBlockState state)
{
return this.getMetaFromState(state);
}

@Override
public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
public boolean canPlaceBlockAt(World worldIn, @Nonnull BlockPos pos)
{
Block block = worldIn.getBlockState(pos).getBlock();

if (block == null || block.isReplaceable(worldIn, pos))
if (block.isReplaceable(worldIn, pos))
{
IBlockState soilBlockState = worldIn.getBlockState(pos.down());
Block netherSoil = soilBlockState.getBlock();

if (netherSoil != null)
{
if (this.canGrowOnBlock(netherSoil) || netherSoil.canSustainPlant(soilBlockState, worldIn, pos.down(), EnumFacing.UP, this))
{
return true;
}
}
return this.canGrowOnBlock(netherSoil) || netherSoil.canSustainPlant(soilBlockState, worldIn, pos.down(), EnumFacing.UP, this);
}
return false;
}

@Override
public boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state)
public boolean canBlockStay(@Nonnull World worldIn, @Nonnull BlockPos pos, IBlockState state)
{
switch (state.getValue(FOLIAGE))
{
case DARKWOOD:
case FUSEWOOD:
case GHOSTWOOD:
IBlockState soilBlockState = worldIn.getBlockState(pos.down());
Block netherSoil = soilBlockState.getBlock();

if (netherSoil == null)
{
return false;
}

return this.canGrowOnBlock(netherSoil) || netherSoil.canSustainPlant(soilBlockState, worldIn, pos.down(), EnumFacing.UP, this);
default:
return true;
case DARKWOOD:
case FUSEWOOD:
case GHOSTWOOD:
IBlockState soilBlockState = worldIn.getBlockState(pos.down());
Block netherSoil = soilBlockState.getBlock();

return this.canGrowOnBlock(netherSoil) || netherSoil.canSustainPlant(soilBlockState, worldIn, pos.down(), EnumFacing.UP, this);
default:
return true;
}
}

public boolean canGrowOnBlock(Block block)
{
return block == Blocks.SOUL_SAND || block == Blocks.NETHERRACK || block == NaturaNether.netherTaintedSoil;
}

@Nonnull
@Override
public EnumPlantType getPlantType(IBlockAccess world, BlockPos pos)
public EnumPlantType getPlantType(@Nonnull IBlockAccess world, @Nonnull BlockPos pos)
{
return EnumPlantType.Nether;
}

public boolean canGrowOnBlock(Block block)
{
return block == Blocks.SOUL_SAND || block == Blocks.NETHERRACK || block == NaturaNether.netherTaintedSoil;
}

@Override
public boolean isReplaceable(IBlockAccess worldIn, @Nonnull BlockPos pos)
public boolean isReplaceable(@Nonnull IBlockAccess worldIn, @Nonnull BlockPos pos)
{
return false;
}

@Nonnull
@Override
public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player)
public ItemStack getPickBlock(@Nonnull IBlockState state, @Nonnull RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EntityPlayer player)
{
IBlockState iblockstate = world.getBlockState(pos);
int meta = iblockstate.getBlock().getMetaFromState(iblockstate);
Expand All @@ -186,32 +125,32 @@ public void generateTree(@Nonnull World worldIn, @Nonnull BlockPos pos, @Nonnull

switch (state.getValue(FOLIAGE))
{
case DARKWOOD:
log = NaturaNether.netherLog.getDefaultState().withProperty(BlockNetherLog.TYPE, BlockNetherLog.LogType.DARKWOOD);
flowering = NaturaNether.netherLeaves2.getDefaultState().withProperty(BlockNetherLeaves2.TYPE, BlockNetherLeaves2.LeavesType.DARKWOOD_FLOWERING);
fruiting = NaturaNether.netherLeaves2.getDefaultState().withProperty(BlockNetherLeaves2.TYPE, BlockNetherLeaves2.LeavesType.DARKWOOD_FRUIT);
leaves = NaturaNether.netherLeaves2.getDefaultState().withProperty(BlockNetherLeaves2.TYPE, BlockNetherLeaves2.LeavesType.DARKWOOD);
case DARKWOOD:
log = NaturaNether.netherLog.getDefaultState().withProperty(BlockNetherLog.TYPE, BlockNetherLog.LogType.DARKWOOD);
flowering = NaturaNether.netherLeaves2.getDefaultState().withProperty(BlockNetherLeaves2.TYPE, BlockNetherLeaves2.LeavesType.DARKWOOD_FLOWERING);
fruiting = NaturaNether.netherLeaves2.getDefaultState().withProperty(BlockNetherLeaves2.TYPE, BlockNetherLeaves2.LeavesType.DARKWOOD_FRUIT);
leaves = NaturaNether.netherLeaves2.getDefaultState().withProperty(BlockNetherLeaves2.TYPE, BlockNetherLeaves2.LeavesType.DARKWOOD);

gen = new DarkwoodTreeGenerator(3, log, leaves, flowering, fruiting);
gen = new DarkwoodTreeGenerator(3, log, leaves, flowering, fruiting);

break;
case FUSEWOOD:
log = NaturaNether.netherLog.getDefaultState().withProperty(BlockNetherLog.TYPE, BlockNetherLog.LogType.FUSEWOOD);
leaves = NaturaNether.netherLeaves.getDefaultState().withProperty(BlockNetherLeaves.TYPE, BlockNetherLeaves.LeavesType.FUSEWOOD);
break;
case FUSEWOOD:
log = NaturaNether.netherLog.getDefaultState().withProperty(BlockNetherLog.TYPE, BlockNetherLog.LogType.FUSEWOOD);
leaves = NaturaNether.netherLeaves.getDefaultState().withProperty(BlockNetherLeaves.TYPE, BlockNetherLeaves.LeavesType.FUSEWOOD);

gen = new FusewoodTreeGenerator(3, log, leaves, false);
gen = new FusewoodTreeGenerator(3, log, leaves, false);

break;
case GHOSTWOOD:
log = NaturaNether.netherLog.getDefaultState().withProperty(BlockNetherLog.TYPE, BlockNetherLog.LogType.GHOSTWOOD);
leaves = NaturaNether.netherLeaves.getDefaultState().withProperty(BlockNetherLeaves.TYPE, BlockNetherLeaves.LeavesType.GHOSTWOOD);
break;
case GHOSTWOOD:
log = NaturaNether.netherLog.getDefaultState().withProperty(BlockNetherLog.TYPE, BlockNetherLog.LogType.GHOSTWOOD);
leaves = NaturaNether.netherLeaves.getDefaultState().withProperty(BlockNetherLeaves.TYPE, BlockNetherLeaves.LeavesType.GHOSTWOOD);

gen = new GhostwoodTreeGenerator(log, leaves, false);
gen = new GhostwoodTreeGenerator(log, leaves, false);

break;
default:
Natura.log.warn("BlockNetherSapling Warning: Invalid sapling meta/foliage, " + state.getValue(FOLIAGE) + ". Please report!");
break;
break;
default:
Natura.log.warn("BlockNetherSapling Warning: Invalid sapling meta/foliage, " + state.getValue(FOLIAGE) + ". Please report!");
break;

}

Expand All @@ -229,6 +168,55 @@ public void generateTree(@Nonnull World worldIn, @Nonnull BlockPos pos, @Nonnull
}
}

@Override
public int damageDropped(@Nonnull IBlockState state)
{
return this.getMetaFromState(state);
}

@Override
public void getSubBlocks(@Nonnull CreativeTabs tab, @Nonnull NonNullList<ItemStack> list)
{
for (SaplingType type : SaplingType.values())
{
list.add(new ItemStack(this, 1, this.getMetaFromState(this.getDefaultState().withProperty(FOLIAGE, type))));
}
}

/**
* Convert the given metadata into a BlockState for this Block
*/
@Nonnull
@Override
public IBlockState getStateFromMeta(int meta)
{
if (meta < 0 || meta >= SaplingType.values().length)
{
meta = 0;
}

SaplingType sapling = SaplingType.values()[meta];

return this.getDefaultState().withProperty(FOLIAGE, sapling);
}

/**
* Convert the BlockState into the correct metadata value
*/
@Override
public int getMetaFromState(IBlockState state)
{
return state.getValue(FOLIAGE).ordinal();
}

@Nonnull
@Override
protected BlockStateContainer createBlockState()
{
// TYPE has to be included because of the BlockSapling constructor, but it's never used.
return new BlockStateContainer(this, FOLIAGE, STAGE, TYPE);
}

public enum SaplingType implements IStringSerializable, EnumBlock.IEnumMeta
{
GHOSTWOOD, FUSEWOOD, DARKWOOD;
Expand All @@ -240,6 +228,7 @@ public enum SaplingType implements IStringSerializable, EnumBlock.IEnumMeta
this.meta = this.ordinal();
}

@Nonnull
@Override
public String getName()
{
Expand Down
Loading

0 comments on commit 9d0c300

Please sign in to comment.