121 lines
5.4 KiB
Java
121 lines
5.4 KiB
Java
package net.minecraft.world.level.block;
|
|
|
|
import net.minecraft.world.level.block.state.AbstractStateHolder;
|
|
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
import net.minecraft.util.Mth;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.ItemLike;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import javax.annotation.Nullable;
|
|
import net.minecraft.world.item.Items;
|
|
import net.minecraft.world.item.Item;
|
|
import net.minecraft.core.Direction;
|
|
import java.util.Random;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.block.state.properties.Property;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
|
|
|
public class StemBlock extends BushBlock implements BonemealableBlock {
|
|
public static final IntegerProperty AGE;
|
|
protected static final VoxelShape[] SHAPE_BY_AGE;
|
|
private final StemGrownBlock fruit;
|
|
|
|
protected StemBlock(final StemGrownBlock bus, final Properties c) {
|
|
super(c);
|
|
this.fruit = bus;
|
|
this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)StemBlock.AGE, 0));
|
|
}
|
|
|
|
@Override
|
|
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
|
|
return StemBlock.SHAPE_BY_AGE[byg.<Integer>getValue((Property<Integer>)StemBlock.AGE)];
|
|
}
|
|
|
|
@Override
|
|
protected boolean mayPlaceOn(final BlockState byg, final BlockGetter bjd, final BlockPos fk) {
|
|
return byg.getBlock() == Blocks.FARMLAND;
|
|
}
|
|
|
|
@Override
|
|
public void tick(BlockState byg, final ServerLevel xd, final BlockPos fk, final Random random) {
|
|
super.tick(byg, xd, fk, random);
|
|
if (xd.getRawBrightness(fk, 0) < 9) {
|
|
return;
|
|
}
|
|
final float float6 = CropBlock.getGrowthSpeed(this, xd, fk);
|
|
if (random.nextInt((int)(25.0f / float6) + 1) == 0) {
|
|
final int integer7 = byg.<Integer>getValue((Property<Integer>)StemBlock.AGE);
|
|
if (integer7 < 7) {
|
|
byg = ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Integer>setValue((Property<Comparable>)StemBlock.AGE, integer7 + 1);
|
|
xd.setBlock(fk, byg, 2);
|
|
}
|
|
else {
|
|
final Direction fp8 = Direction.Plane.HORIZONTAL.getRandomDirection(random);
|
|
final BlockPos fk2 = fk.relative(fp8);
|
|
final Block bpe10 = xd.getBlockState(fk2.below()).getBlock();
|
|
if (xd.getBlockState(fk2).isAir() && (bpe10 == Blocks.FARMLAND || bpe10 == Blocks.DIRT || bpe10 == Blocks.COARSE_DIRT || bpe10 == Blocks.PODZOL || bpe10 == Blocks.GRASS_BLOCK)) {
|
|
xd.setBlockAndUpdate(fk2, this.fruit.defaultBlockState());
|
|
xd.setBlockAndUpdate(fk, ((AbstractStateHolder<O, BlockState>)this.fruit.getAttachedStem().defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)HorizontalDirectionalBlock.FACING, fp8));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Nullable
|
|
protected Item getSeedItem() {
|
|
if (this.fruit == Blocks.PUMPKIN) {
|
|
return Items.PUMPKIN_SEEDS;
|
|
}
|
|
if (this.fruit == Blocks.MELON) {
|
|
return Items.MELON_SEEDS;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public ItemStack getCloneItemStack(final BlockGetter bjd, final BlockPos fk, final BlockState byg) {
|
|
final Item bef5 = this.getSeedItem();
|
|
return (bef5 == null) ? ItemStack.EMPTY : new ItemStack(bef5);
|
|
}
|
|
|
|
@Override
|
|
public boolean isValidBonemealTarget(final BlockGetter bjd, final BlockPos fk, final BlockState byg, final boolean boolean4) {
|
|
return byg.<Integer>getValue((Property<Integer>)StemBlock.AGE) != 7;
|
|
}
|
|
|
|
@Override
|
|
public boolean isBonemealSuccess(final Level bjt, final Random random, final BlockPos fk, final BlockState byg) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void performBonemeal(final ServerLevel xd, final Random random, final BlockPos fk, final BlockState byg) {
|
|
final int integer6 = Math.min(7, byg.<Integer>getValue((Property<Integer>)StemBlock.AGE) + Mth.nextInt(xd.random, 2, 5));
|
|
final BlockState byg2 = ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Integer>setValue((Property<Comparable>)StemBlock.AGE, integer6);
|
|
xd.setBlock(fk, byg2, 2);
|
|
if (integer6 == 7) {
|
|
byg2.tick(xd, fk, xd.random);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
|
|
a.add(StemBlock.AGE);
|
|
}
|
|
|
|
public StemGrownBlock getFruit() {
|
|
return this.fruit;
|
|
}
|
|
|
|
static {
|
|
AGE = BlockStateProperties.AGE_7;
|
|
SHAPE_BY_AGE = new VoxelShape[] { Block.box(7.0, 0.0, 7.0, 9.0, 2.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 4.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 6.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 8.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 10.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 12.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 14.0, 9.0), Block.box(7.0, 0.0, 7.0, 9.0, 16.0, 9.0) };
|
|
}
|
|
}
|