NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/BlockNetherGrass.java

83 lines
3 KiB
Java
Raw Normal View History

2020-02-26 19:32:42 +01:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.data.BlockStateGenerator;
import de.ellpeck.naturesaura.reg.ICustomBlockState;
2020-10-17 21:13:45 +02:00
import net.minecraft.block.*;
2020-02-26 19:32:42 +01:00
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import java.util.Random;
public class BlockNetherGrass extends BlockImpl implements ICustomBlockState, IGrowable {
public BlockNetherGrass() {
2020-10-17 21:13:45 +02:00
super("nether_grass", Properties.from(Blocks.NETHERRACK).tickRandomly());
2020-02-26 19:32:42 +01:00
}
@Override
public void randomTick(BlockState state, ServerWorld worldIn, BlockPos pos, Random random) {
BlockPos up = pos.up();
BlockState upState = worldIn.getBlockState(up);
if (upState.isSolidSide(worldIn, up, Direction.DOWN))
worldIn.setBlockState(pos, Blocks.NETHERRACK.getDefaultState());
}
@Override
public void generateCustomBlockState(BlockStateGenerator generator) {
generator.simpleBlock(this, generator.models().cubeBottomTop(this.getBaseName(),
generator.modLoc("block/" + this.getBaseName()),
generator.mcLoc("block/netherrack"),
generator.modLoc("block/" + this.getBaseName() + "_top")));
}
@Override
public boolean canGrow(IBlockReader worldIn, BlockPos pos, BlockState state, boolean isClient) {
return worldIn.getBlockState(pos.up()).isAir(worldIn, pos.up());
}
@Override
public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, BlockState state) {
return true;
}
@Override
public void grow(ServerWorld world, Random rand, BlockPos pos, BlockState state) {
BlockPos blockpos = pos.up();
BlockState blockstate = Blocks.GRASS.getDefaultState();
for (int i = 0; i < 128; ++i) {
BlockPos blockpos1 = blockpos;
int j = 0;
while (true) {
if (j >= i / 16) {
BlockState blockstate2 = world.getBlockState(blockpos1);
if (blockstate2.getBlock() == blockstate.getBlock() && rand.nextInt(10) == 0) {
((IGrowable) blockstate.getBlock()).grow(world, rand, blockpos1, blockstate2);
}
if (!blockstate2.isAir()) {
break;
}
if (blockstate.isValidPosition(world, blockpos1)) {
world.setBlockState(blockpos1, blockstate, 3);
}
break;
}
blockpos1 = blockpos1.add(rand.nextInt(3) - 1, (rand.nextInt(3) - 1) * rand.nextInt(3) / 2, rand.nextInt(3) - 1);
2020-09-22 03:17:02 +02:00
if (world.getBlockState(blockpos1.down()).getBlock() != this || world.getBlockState(blockpos1).hasOpaqueCollisionShape(world, blockpos1)) {
2020-02-26 19:32:42 +01:00
break;
}
++j;
}
}
}
}