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;
|
2021-01-14 23:15:02 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.Blocks;
|
|
|
|
import net.minecraft.block.IGrowable;
|
2020-02-26 19:32:42 +01:00
|
|
|
import net.minecraft.util.Direction;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2021-12-15 14:26:42 +01:00
|
|
|
import net.minecraft.level.BlockGetter;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.level.Level;
|
|
|
|
import net.minecraft.level.server.ServerLevel;
|
2020-02-26 19:32:42 +01:00
|
|
|
|
|
|
|
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
|
2021-12-04 15:40:09 +01:00
|
|
|
public void randomTick(BlockState state, ServerLevel levelIn, BlockPos pos, Random random) {
|
2020-02-26 19:32:42 +01:00
|
|
|
BlockPos up = pos.up();
|
2021-12-04 15:40:09 +01:00
|
|
|
BlockState upState = levelIn.getBlockState(up);
|
|
|
|
if (upState.isSolidSide(levelIn, up, Direction.DOWN))
|
|
|
|
levelIn.setBlockState(pos, Blocks.NETHERRACK.getDefaultState());
|
2020-02-26 19:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
2021-12-15 14:26:42 +01:00
|
|
|
public boolean canGrow(BlockGetter levelIn, BlockPos pos, BlockState state, boolean isClient) {
|
2021-12-04 15:40:09 +01:00
|
|
|
return levelIn.getBlockState(pos.up()).isAir(levelIn, pos.up());
|
2020-02-26 19:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public boolean canUseBonemeal(Level levelIn, Random rand, BlockPos pos, BlockState state) {
|
2020-02-26 19:32:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void grow(ServerLevel level, Random rand, BlockPos pos, BlockState state) {
|
2020-02-26 19:32:42 +01:00
|
|
|
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) {
|
2021-12-04 15:40:09 +01:00
|
|
|
BlockState blockstate2 = level.getBlockState(blockpos1);
|
2020-02-26 19:32:42 +01:00
|
|
|
if (blockstate2.getBlock() == blockstate.getBlock() && rand.nextInt(10) == 0) {
|
2021-12-04 15:40:09 +01:00
|
|
|
((IGrowable) blockstate.getBlock()).grow(level, rand, blockpos1, blockstate2);
|
2020-02-26 19:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!blockstate2.isAir()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
if (blockstate.isValidPosition(level, blockpos1)) {
|
|
|
|
level.setBlockState(blockpos1, blockstate, 3);
|
2020-02-26 19:32:42 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
blockpos1 = blockpos1.add(rand.nextInt(3) - 1, (rand.nextInt(3) - 1) * rand.nextInt(3) / 2, rand.nextInt(3) - 1);
|
2021-12-04 15:40:09 +01:00
|
|
|
if (level.getBlockState(blockpos1.down()).getBlock() != this || level.getBlockState(blockpos1).hasOpaqueCollisionShape(level, blockpos1)) {
|
2020-02-26 19:32:42 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|