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

85 lines
3.1 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;
2021-12-15 16:24:53 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.BonemealableBlock;
import net.minecraft.world.level.block.state.BlockState;
2020-02-26 19:32:42 +01:00
import java.util.Random;
2021-12-15 16:24:53 +01:00
public class BlockNetherGrass extends BlockImpl implements ICustomBlockState, BonemealableBlock {
2020-02-26 19:32:42 +01:00
public BlockNetherGrass() {
2021-12-15 16:24:53 +01:00
super("nether_grass", Properties.copy(Blocks.NETHERRACK).randomTicks());
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) {
2021-12-15 16:30:22 +01:00
var up = pos.above();
var upState = levelIn.getBlockState(up);
2021-12-15 16:24:53 +01:00
if (upState.isFaceSturdy(levelIn, up, Direction.DOWN))
levelIn.setBlockAndUpdate(pos, Blocks.NETHERRACK.defaultBlockState());
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 16:24:53 +01:00
public boolean isValidBonemealTarget(BlockGetter levelIn, BlockPos pos, BlockState state, boolean isClient) {
return levelIn.getBlockState(pos.above()).isAir();
2020-02-26 19:32:42 +01:00
}
@Override
2021-12-15 16:24:53 +01:00
public boolean isBonemealSuccess(Level levelIn, Random rand, BlockPos pos, BlockState state) {
2020-02-26 19:32:42 +01:00
return true;
}
@Override
2021-12-15 16:24:53 +01:00
public void performBonemeal(ServerLevel level, Random rand, BlockPos pos, BlockState state) {
2021-12-15 16:30:22 +01:00
var blockpos = pos.above();
var blockstate = Blocks.GRASS.defaultBlockState();
2020-02-26 19:32:42 +01:00
2021-12-15 16:30:22 +01:00
for (var i = 0; i < 128; ++i) {
var blockpos1 = blockpos;
var j = 0;
2020-02-26 19:32:42 +01:00
while (true) {
if (j >= i / 16) {
2021-12-15 16:30:22 +01:00
var blockstate2 = level.getBlockState(blockpos1);
2020-02-26 19:32:42 +01:00
if (blockstate2.getBlock() == blockstate.getBlock() && rand.nextInt(10) == 0) {
2021-12-15 16:24:53 +01:00
((BonemealableBlock) blockstate.getBlock()).performBonemeal(level, rand, blockpos1, blockstate2);
2020-02-26 19:32:42 +01:00
}
if (!blockstate2.isAir()) {
break;
}
2021-12-15 16:24:53 +01:00
if (blockstate.canSurvive(level, blockpos1)) {
level.setBlock(blockpos1, blockstate, 3);
2020-02-26 19:32:42 +01:00
}
break;
}
2021-12-15 16:24:53 +01:00
blockpos1 = blockpos1.offset(rand.nextInt(3) - 1, (rand.nextInt(3) - 1) * rand.nextInt(3) / 2, rand.nextInt(3) - 1);
if (level.getBlockState(blockpos1.below()).getBlock() != this || level.getBlockState(blockpos1).isCollisionShapeFullBlock(level, blockpos1)) {
2020-02-26 19:32:42 +01:00
break;
}
++j;
}
}
}
}