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

279 lines
14 KiB
Java

package net.minecraft.world.level.block;
import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.core.particles.SimpleParticleType;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.projectile.Fireball;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import java.util.Random;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelReader;
import javax.annotation.Nullable;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.Containers;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import java.util.Optional;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.stats.Stats;
import net.minecraft.world.item.crafting.CampfireCookingRecipe;
import net.minecraft.world.level.block.entity.CampfireBlockEntity;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.core.Direction;
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.DirectionProperty;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.shapes.VoxelShape;
public class CampfireBlock extends BaseEntityBlock implements SimpleWaterloggedBlock {
protected static final VoxelShape SHAPE;
public static final BooleanProperty LIT;
public static final BooleanProperty SIGNAL_FIRE;
public static final BooleanProperty WATERLOGGED;
public static final DirectionProperty FACING;
private static final VoxelShape VIRTUAL_FENCE_POST;
public CampfireBlock(final Properties c) {
super(c);
this.registerDefaultState((((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)CampfireBlock.LIT, true)).setValue((Property<Comparable>)CampfireBlock.SIGNAL_FIRE, false)).setValue((Property<Comparable>)CampfireBlock.WATERLOGGED, false)).<Comparable, Direction>setValue((Property<Comparable>)CampfireBlock.FACING, Direction.NORTH));
}
@Override
public InteractionResult use(final BlockState byg, final Level bjt, final BlockPos fk, final Player ayg, final InteractionHand ajh, final BlockHitResult cvd) {
if (byg.<Boolean>getValue((Property<Boolean>)CampfireBlock.LIT)) {
final BlockEntity bwi8 = bjt.getBlockEntity(fk);
if (bwi8 instanceof CampfireBlockEntity) {
final CampfireBlockEntity bwl9 = (CampfireBlockEntity)bwi8;
final ItemStack bek10 = ayg.getItemInHand(ajh);
final Optional<CampfireCookingRecipe> optional11 = bwl9.getCookableRecipe(bek10);
if (optional11.isPresent()) {
if (!bjt.isClientSide && bwl9.placeFood(ayg.abilities.instabuild ? bek10.copy() : bek10, optional11.get().getCookingTime())) {
ayg.awardStat(Stats.INTERACT_WITH_CAMPFIRE);
return InteractionResult.SUCCESS;
}
return InteractionResult.CONSUME;
}
}
}
return InteractionResult.PASS;
}
@Override
public void entityInside(final BlockState byg, final Level bjt, final BlockPos fk, final Entity akn) {
if (!akn.fireImmune() && byg.<Boolean>getValue((Property<Boolean>)CampfireBlock.LIT) && akn instanceof LivingEntity && !EnchantmentHelper.hasFrostWalker((LivingEntity)akn)) {
akn.hurt(DamageSource.IN_FIRE, 1.0f);
}
super.entityInside(byg, bjt, fk, akn);
}
@Override
public void onRemove(final BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
if (byg1.getBlock() == byg4.getBlock()) {
return;
}
final BlockEntity bwi7 = bjt.getBlockEntity(fk);
if (bwi7 instanceof CampfireBlockEntity) {
Containers.dropContents(bjt, fk, ((CampfireBlockEntity)bwi7).getItems());
}
super.onRemove(byg1, bjt, fk, byg4, boolean5);
}
@Nullable
@Override
public BlockState getStateForPlacement(final BlockPlaceContext bcn) {
final LevelAccessor bju3 = bcn.getLevel();
final BlockPos fk4 = bcn.getClickedPos();
final boolean boolean5 = bju3.getFluidState(fk4).getType() == Fluids.WATER;
return (((((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)CampfireBlock.WATERLOGGED, boolean5)).setValue((Property<Comparable>)CampfireBlock.SIGNAL_FIRE, this.isSmokeSource(bju3.getBlockState(fk4.below())))).setValue((Property<Comparable>)CampfireBlock.LIT, !boolean5)).<Comparable, Direction>setValue((Property<Comparable>)CampfireBlock.FACING, bcn.getHorizontalDirection());
}
@Override
public BlockState updateShape(final BlockState byg1, final Direction fp, final BlockState byg3, final LevelAccessor bju, final BlockPos fk5, final BlockPos fk6) {
if (byg1.<Boolean>getValue((Property<Boolean>)CampfireBlock.WATERLOGGED)) {
bju.getLiquidTicks().scheduleTick(fk5, Fluids.WATER, Fluids.WATER.getTickDelay(bju));
}
if (fp == Direction.DOWN) {
return ((AbstractStateHolder<O, BlockState>)byg1).<Comparable, Boolean>setValue((Property<Comparable>)CampfireBlock.SIGNAL_FIRE, this.isSmokeSource(byg3));
}
return super.updateShape(byg1, fp, byg3, bju, fk5, fk6);
}
private boolean isSmokeSource(final BlockState byg) {
return byg.getBlock() == Blocks.HAY_BLOCK;
}
@Override
public int getLightEmission(final BlockState byg) {
return byg.<Boolean>getValue((Property<Boolean>)CampfireBlock.LIT) ? super.getLightEmission(byg) : 0;
}
@Override
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
return CampfireBlock.SHAPE;
}
@Override
public RenderShape getRenderShape(final BlockState byg) {
return RenderShape.MODEL;
}
@Override
public void animateTick(final BlockState byg, final Level bjt, final BlockPos fk, final Random random) {
if (!byg.<Boolean>getValue((Property<Boolean>)CampfireBlock.LIT)) {
return;
}
if (random.nextInt(10) == 0) {
bjt.playLocalSound(fk.getX() + 0.5f, fk.getY() + 0.5f, fk.getZ() + 0.5f, SoundEvents.CAMPFIRE_CRACKLE, SoundSource.BLOCKS, 0.5f + random.nextFloat(), random.nextFloat() * 0.7f + 0.6f, false);
}
if (random.nextInt(5) == 0) {
for (int integer6 = 0; integer6 < random.nextInt(1) + 1; ++integer6) {
bjt.addParticle(ParticleTypes.LAVA, fk.getX() + 0.5f, fk.getY() + 0.5f, fk.getZ() + 0.5f, random.nextFloat() / 2.0f, 5.0E-5, random.nextFloat() / 2.0f);
}
}
}
@Override
public boolean placeLiquid(final LevelAccessor bju, final BlockPos fk, final BlockState byg, final FluidState cog) {
if (!byg.<Boolean>getValue((Property<Boolean>)BlockStateProperties.WATERLOGGED) && cog.getType() == Fluids.WATER) {
final boolean boolean6 = byg.<Boolean>getValue((Property<Boolean>)CampfireBlock.LIT);
if (boolean6) {
if (bju.isClientSide()) {
for (int integer7 = 0; integer7 < 20; ++integer7) {
makeParticles(bju.getLevel(), fk, byg.<Boolean>getValue((Property<Boolean>)CampfireBlock.SIGNAL_FIRE), true);
}
}
else {
bju.playSound(null, fk, SoundEvents.GENERIC_EXTINGUISH_FIRE, SoundSource.BLOCKS, 1.0f, 1.0f);
}
final BlockEntity bwi7 = bju.getBlockEntity(fk);
if (bwi7 instanceof CampfireBlockEntity) {
((CampfireBlockEntity)bwi7).dowse();
}
}
bju.setBlock(fk, (((AbstractStateHolder<O, BlockState>)byg).setValue((Property<Comparable>)CampfireBlock.WATERLOGGED, true)).<Comparable, Boolean>setValue((Property<Comparable>)CampfireBlock.LIT, false), 3);
bju.getLiquidTicks().scheduleTick(fk, cog.getType(), cog.getType().getTickDelay(bju));
return true;
}
return false;
}
@Nullable
private Entity getShooter(final Entity akn) {
if (akn instanceof Fireball) {
return ((Fireball)akn).owner;
}
if (akn instanceof AbstractArrow) {
return ((AbstractArrow)akn).getOwner();
}
return null;
}
@Override
public void onProjectileHit(final Level bjt, final BlockState byg, final BlockHitResult cvd, final Entity akn) {
if (!bjt.isClientSide) {
final boolean boolean6 = akn instanceof Fireball || (akn instanceof AbstractArrow && akn.isOnFire());
if (boolean6) {
final Entity akn2 = this.getShooter(akn);
final boolean boolean7 = akn2 == null || akn2 instanceof Player || bjt.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING);
if (boolean7 && !byg.<Boolean>getValue((Property<Boolean>)CampfireBlock.LIT) && !byg.<Boolean>getValue((Property<Boolean>)CampfireBlock.WATERLOGGED)) {
final BlockPos fk9 = cvd.getBlockPos();
bjt.setBlock(fk9, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Boolean>setValue((Property<Comparable>)BlockStateProperties.LIT, true), 11);
}
}
}
}
public static void makeParticles(final Level bjt, final BlockPos fk, final boolean boolean3, final boolean boolean4) {
final Random random5 = bjt.getRandom();
final SimpleParticleType gw6 = boolean3 ? ParticleTypes.CAMPFIRE_SIGNAL_SMOKE : ParticleTypes.CAMPFIRE_COSY_SMOKE;
bjt.addAlwaysVisibleParticle(gw6, true, fk.getX() + 0.5 + random5.nextDouble() / 3.0 * (random5.nextBoolean() ? 1 : -1), fk.getY() + random5.nextDouble() + random5.nextDouble(), fk.getZ() + 0.5 + random5.nextDouble() / 3.0 * (random5.nextBoolean() ? 1 : -1), 0.0, 0.07, 0.0);
if (boolean4) {
bjt.addParticle(ParticleTypes.SMOKE, fk.getX() + 0.25 + random5.nextDouble() / 2.0 * (random5.nextBoolean() ? 1 : -1), fk.getY() + 0.4, fk.getZ() + 0.25 + random5.nextDouble() / 2.0 * (random5.nextBoolean() ? 1 : -1), 0.0, 0.005, 0.0);
}
}
public static boolean isSmokeyPos(final Level bjt, final BlockPos fk, final int integer) {
for (int integer2 = 1; integer2 <= integer; ++integer2) {
final BlockPos fk2 = fk.below(integer2);
final BlockState byg6 = bjt.getBlockState(fk2);
if (isLitCampfire(byg6)) {
return true;
}
final boolean boolean7 = Shapes.joinIsNotEmpty(CampfireBlock.VIRTUAL_FENCE_POST, byg6.getCollisionShape(bjt, fk, CollisionContext.empty()), BooleanOp.AND);
if (boolean7) {
final BlockState byg7 = bjt.getBlockState(fk2.below());
return isLitCampfire(byg7);
}
}
return false;
}
private static boolean isLitCampfire(final BlockState byg) {
return byg.getBlock() == Blocks.CAMPFIRE && byg.<Boolean>getValue((Property<Boolean>)CampfireBlock.LIT);
}
@Override
public FluidState getFluidState(final BlockState byg) {
if (byg.<Boolean>getValue((Property<Boolean>)CampfireBlock.WATERLOGGED)) {
return Fluids.WATER.getSource(false);
}
return super.getFluidState(byg);
}
@Override
public BlockState rotate(final BlockState byg, final Rotation btr) {
return ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Direction>setValue((Property<Comparable>)CampfireBlock.FACING, btr.rotate(byg.<Direction>getValue((Property<Direction>)CampfireBlock.FACING)));
}
@Override
public BlockState mirror(final BlockState byg, final Mirror bsr) {
return byg.rotate(bsr.getRotation(byg.<Direction>getValue((Property<Direction>)CampfireBlock.FACING)));
}
@Override
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
a.add(CampfireBlock.LIT, CampfireBlock.SIGNAL_FIRE, CampfireBlock.WATERLOGGED, CampfireBlock.FACING);
}
@Override
public BlockEntity newBlockEntity(final BlockGetter bjd) {
return new CampfireBlockEntity();
}
@Override
public boolean isPathfindable(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final PathComputationType cqo) {
return false;
}
static {
SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 7.0, 16.0);
LIT = BlockStateProperties.LIT;
SIGNAL_FIRE = BlockStateProperties.SIGNAL_FIRE;
WATERLOGGED = BlockStateProperties.WATERLOGGED;
FACING = BlockStateProperties.HORIZONTAL_FACING;
VIRTUAL_FENCE_POST = Block.box(6.0, 0.0, 6.0, 10.0, 16.0, 10.0);
}
}