195 lines
7.2 KiB
Java
195 lines
7.2 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.serialization.MapCodec
|
|
*/
|
|
package net.minecraft.world.level.block;
|
|
|
|
import com.mojang.serialization.MapCodec;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.util.Mth;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.world.entity.Entity;
|
|
import net.minecraft.world.entity.InsideBlockEffectApplier;
|
|
import net.minecraft.world.entity.monster.Ravager;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.item.Items;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.ItemLike;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.LevelReader;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.Blocks;
|
|
import net.minecraft.world.level.block.BonemealableBlock;
|
|
import net.minecraft.world.level.block.FarmBlock;
|
|
import net.minecraft.world.level.block.VegetationBlock;
|
|
import net.minecraft.world.level.block.state.BlockBehaviour;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
|
import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
|
import net.minecraft.world.level.gamerules.GameRules;
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
|
|
public class CropBlock
|
|
extends VegetationBlock
|
|
implements BonemealableBlock {
|
|
public static final MapCodec<CropBlock> CODEC = CropBlock.simpleCodec(CropBlock::new);
|
|
public static final int MAX_AGE = 7;
|
|
public static final IntegerProperty AGE = BlockStateProperties.AGE_7;
|
|
private static final VoxelShape[] SHAPES = Block.boxes(7, age -> Block.column(16.0, 0.0, 2 + age * 2));
|
|
|
|
public MapCodec<? extends CropBlock> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
protected CropBlock(BlockBehaviour.Properties properties) {
|
|
super(properties);
|
|
this.registerDefaultState((BlockState)((BlockState)this.stateDefinition.any()).setValue(this.getAgeProperty(), 0));
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
|
|
return SHAPES[this.getAge(state)];
|
|
}
|
|
|
|
@Override
|
|
protected boolean mayPlaceOn(BlockState state, BlockGetter level, BlockPos pos) {
|
|
return state.is(Blocks.FARMLAND);
|
|
}
|
|
|
|
protected IntegerProperty getAgeProperty() {
|
|
return AGE;
|
|
}
|
|
|
|
public int getMaxAge() {
|
|
return 7;
|
|
}
|
|
|
|
public int getAge(BlockState state) {
|
|
return state.getValue(this.getAgeProperty());
|
|
}
|
|
|
|
public BlockState getStateForAge(int age) {
|
|
return (BlockState)this.defaultBlockState().setValue(this.getAgeProperty(), age);
|
|
}
|
|
|
|
public final boolean isMaxAge(BlockState state) {
|
|
return this.getAge(state) >= this.getMaxAge();
|
|
}
|
|
|
|
@Override
|
|
protected boolean isRandomlyTicking(BlockState state) {
|
|
return !this.isMaxAge(state);
|
|
}
|
|
|
|
@Override
|
|
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
|
|
float growthSpeed;
|
|
int age;
|
|
if (level.getRawBrightness(pos, 0) >= 9 && (age = this.getAge(state)) < this.getMaxAge() && random.nextInt((int)(25.0f / (growthSpeed = CropBlock.getGrowthSpeed(this, level, pos))) + 1) == 0) {
|
|
level.setBlock(pos, this.getStateForAge(age + 1), 2);
|
|
}
|
|
}
|
|
|
|
public void growCrops(Level level, BlockPos pos, BlockState state) {
|
|
int age = Math.min(this.getMaxAge(), this.getAge(state) + this.getBonemealAgeIncrease(level));
|
|
level.setBlock(pos, this.getStateForAge(age), 2);
|
|
}
|
|
|
|
protected int getBonemealAgeIncrease(Level level) {
|
|
return Mth.nextInt(level.random, 2, 5);
|
|
}
|
|
|
|
protected static float getGrowthSpeed(Block type, BlockGetter level, BlockPos pos) {
|
|
boolean vertical;
|
|
float speed = 1.0f;
|
|
BlockPos below = pos.below();
|
|
for (int xx = -1; xx <= 1; ++xx) {
|
|
for (int zz = -1; zz <= 1; ++zz) {
|
|
float blockSpeed = 0.0f;
|
|
BlockState blockState = level.getBlockState(below.offset(xx, 0, zz));
|
|
if (blockState.is(Blocks.FARMLAND)) {
|
|
blockSpeed = 1.0f;
|
|
if (blockState.getValue(FarmBlock.MOISTURE) > 0) {
|
|
blockSpeed = 3.0f;
|
|
}
|
|
}
|
|
if (xx != 0 || zz != 0) {
|
|
blockSpeed /= 4.0f;
|
|
}
|
|
speed += blockSpeed;
|
|
}
|
|
}
|
|
BlockPos north = pos.north();
|
|
BlockPos south = pos.south();
|
|
BlockPos west = pos.west();
|
|
BlockPos east = pos.east();
|
|
boolean horizontal = level.getBlockState(west).is(type) || level.getBlockState(east).is(type);
|
|
boolean bl = vertical = level.getBlockState(north).is(type) || level.getBlockState(south).is(type);
|
|
if (horizontal && vertical) {
|
|
speed /= 2.0f;
|
|
} else {
|
|
boolean diagonal;
|
|
boolean bl2 = diagonal = level.getBlockState(west.north()).is(type) || level.getBlockState(east.north()).is(type) || level.getBlockState(east.south()).is(type) || level.getBlockState(west.south()).is(type);
|
|
if (diagonal) {
|
|
speed /= 2.0f;
|
|
}
|
|
}
|
|
return speed;
|
|
}
|
|
|
|
@Override
|
|
protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
|
|
return CropBlock.hasSufficientLight(level, pos) && super.canSurvive(state, level, pos);
|
|
}
|
|
|
|
protected static boolean hasSufficientLight(LevelReader level, BlockPos pos) {
|
|
return level.getRawBrightness(pos, 0) >= 8;
|
|
}
|
|
|
|
@Override
|
|
protected void entityInside(BlockState state, Level level, BlockPos pos, Entity entity, InsideBlockEffectApplier effectApplier, boolean isPrecise) {
|
|
if (level instanceof ServerLevel) {
|
|
ServerLevel serverLevel = (ServerLevel)level;
|
|
if (entity instanceof Ravager && serverLevel.getGameRules().get(GameRules.MOB_GRIEFING).booleanValue()) {
|
|
serverLevel.destroyBlock(pos, true, entity);
|
|
}
|
|
}
|
|
super.entityInside(state, level, pos, entity, effectApplier, isPrecise);
|
|
}
|
|
|
|
protected ItemLike getBaseSeedId() {
|
|
return Items.WHEAT_SEEDS;
|
|
}
|
|
|
|
@Override
|
|
protected ItemStack getCloneItemStack(LevelReader level, BlockPos pos, BlockState state, boolean includeData) {
|
|
return new ItemStack(this.getBaseSeedId());
|
|
}
|
|
|
|
@Override
|
|
public boolean isValidBonemealTarget(LevelReader level, BlockPos pos, BlockState state) {
|
|
return !this.isMaxAge(state);
|
|
}
|
|
|
|
@Override
|
|
public boolean isBonemealSuccess(Level level, RandomSource random, BlockPos pos, BlockState state) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void performBonemeal(ServerLevel level, RandomSource random, BlockPos pos, BlockState state) {
|
|
this.growCrops(level, pos, state);
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
|
builder.add(AGE);
|
|
}
|
|
}
|
|
|