minecraft-source/src/net/minecraft/world/level/block/TurtleEggBlock.java

161 lines
7.3 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.GameRules;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.animal.Turtle;
import net.minecraft.world.level.BlockGetter;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.monster.Zombie;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.shapes.VoxelShape;
public class TurtleEggBlock extends Block {
private static final VoxelShape ONE_EGG_AABB;
private static final VoxelShape MULTIPLE_EGGS_AABB;
public static final IntegerProperty HATCH;
public static final IntegerProperty EGGS;
public TurtleEggBlock(final Properties c) {
super(c);
this.registerDefaultState((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)TurtleEggBlock.HATCH, 0)).<Comparable, Integer>setValue((Property<Comparable>)TurtleEggBlock.EGGS, 1));
}
@Override
public void stepOn(final Level bjt, final BlockPos fk, final Entity akn) {
this.destroyEgg(bjt, fk, akn, 100);
super.stepOn(bjt, fk, akn);
}
@Override
public void fallOn(final Level bjt, final BlockPos fk, final Entity akn, final float float4) {
if (!(akn instanceof Zombie)) {
this.destroyEgg(bjt, fk, akn, 3);
}
super.fallOn(bjt, fk, akn, float4);
}
private void destroyEgg(final Level bjt, final BlockPos fk, final Entity akn, final int integer) {
if (!this.canDestroyEgg(bjt, akn)) {
super.stepOn(bjt, fk, akn);
return;
}
if (!bjt.isClientSide && bjt.random.nextInt(integer) == 0) {
this.decreaseEggs(bjt, fk, bjt.getBlockState(fk));
}
}
private void decreaseEggs(final Level bjt, final BlockPos fk, final BlockState byg) {
bjt.playSound(null, fk, SoundEvents.TURTLE_EGG_BREAK, SoundSource.BLOCKS, 0.7f, 0.9f + bjt.random.nextFloat() * 0.2f);
final int integer5 = byg.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS);
if (integer5 <= 1) {
bjt.destroyBlock(fk, false);
}
else {
bjt.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Integer>setValue((Property<Comparable>)TurtleEggBlock.EGGS, integer5 - 1), 2);
bjt.levelEvent(2001, fk, Block.getId(byg));
}
}
@Override
public void tick(final BlockState byg, final ServerLevel xd, final BlockPos fk, final Random random) {
if (this.shouldUpdateHatchLevel(xd) && this.onSand(xd, fk)) {
final int integer6 = byg.<Integer>getValue((Property<Integer>)TurtleEggBlock.HATCH);
if (integer6 < 2) {
xd.playSound(null, fk, SoundEvents.TURTLE_EGG_CRACK, SoundSource.BLOCKS, 0.7f, 0.9f + random.nextFloat() * 0.2f);
xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Integer>setValue((Property<Comparable>)TurtleEggBlock.HATCH, integer6 + 1), 2);
}
else {
xd.playSound(null, fk, SoundEvents.TURTLE_EGG_HATCH, SoundSource.BLOCKS, 0.7f, 0.9f + random.nextFloat() * 0.2f);
xd.removeBlock(fk, false);
for (int integer7 = 0; integer7 < byg.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS); ++integer7) {
xd.levelEvent(2001, fk, Block.getId(byg));
final Turtle atx8 = EntityType.TURTLE.create(xd);
atx8.setAge(-24000);
atx8.setHomePos(fk);
atx8.moveTo(fk.getX() + 0.3 + integer7 * 0.2, fk.getY(), fk.getZ() + 0.3, 0.0f, 0.0f);
xd.addFreshEntity(atx8);
}
}
}
}
private boolean onSand(final BlockGetter bjd, final BlockPos fk) {
return bjd.getBlockState(fk.below()).getBlock() == Blocks.SAND;
}
@Override
public void onPlace(final BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
if (this.onSand(bjt, fk) && !bjt.isClientSide) {
bjt.levelEvent(2005, fk, 0);
}
}
private boolean shouldUpdateHatchLevel(final Level bjt) {
final float float3 = bjt.getTimeOfDay(1.0f);
return (float3 < 0.69 && float3 > 0.65) || bjt.random.nextInt(500) == 0;
}
@Override
public void playerDestroy(final Level bjt, final Player ayg, final BlockPos fk, final BlockState byg, @Nullable final BlockEntity bwi, final ItemStack bek) {
super.playerDestroy(bjt, ayg, fk, byg, bwi, bek);
this.decreaseEggs(bjt, fk, byg);
}
@Override
public boolean canBeReplaced(final BlockState byg, final BlockPlaceContext bcn) {
return (bcn.getItemInHand().getItem() == this.asItem() && byg.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS) < 4) || super.canBeReplaced(byg, bcn);
}
@Nullable
@Override
public BlockState getStateForPlacement(final BlockPlaceContext bcn) {
final BlockState byg3 = bcn.getLevel().getBlockState(bcn.getClickedPos());
if (byg3.getBlock() == this) {
return ((AbstractStateHolder<O, BlockState>)byg3).<Comparable, Integer>setValue((Property<Comparable>)TurtleEggBlock.EGGS, Math.min(4, byg3.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS) + 1));
}
return super.getStateForPlacement(bcn);
}
@Override
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
if (byg.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS) > 1) {
return TurtleEggBlock.MULTIPLE_EGGS_AABB;
}
return TurtleEggBlock.ONE_EGG_AABB;
}
@Override
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
a.add(TurtleEggBlock.HATCH, TurtleEggBlock.EGGS);
}
private boolean canDestroyEgg(final Level bjt, final Entity akn) {
return !(akn instanceof Turtle) && (!(akn instanceof LivingEntity) || akn instanceof Player || bjt.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING));
}
static {
ONE_EGG_AABB = Block.box(3.0, 0.0, 3.0, 12.0, 7.0, 12.0);
MULTIPLE_EGGS_AABB = Block.box(1.0, 0.0, 1.0, 15.0, 7.0, 15.0);
HATCH = BlockStateProperties.HATCH;
EGGS = BlockStateProperties.EGGS;
}
}