2025-11-24 22:52:51 +03:00

153 lines
6.4 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.mojang.serialization.MapCodec
* org.jspecify.annotations.Nullable
*/
package net.minecraft.world.level.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.PrimedTnt;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
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.BooleanProperty;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.gamerules.GameRules;
import net.minecraft.world.level.redstone.Orientation;
import net.minecraft.world.phys.BlockHitResult;
import org.jspecify.annotations.Nullable;
public class TntBlock
extends Block {
public static final MapCodec<TntBlock> CODEC = TntBlock.simpleCodec(TntBlock::new);
public static final BooleanProperty UNSTABLE = BlockStateProperties.UNSTABLE;
public MapCodec<TntBlock> codec() {
return CODEC;
}
public TntBlock(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState((BlockState)this.defaultBlockState().setValue(UNSTABLE, false));
}
@Override
protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) {
if (oldState.is(state.getBlock())) {
return;
}
if (level.hasNeighborSignal(pos) && TntBlock.prime(level, pos)) {
level.removeBlock(pos, false);
}
}
@Override
protected void neighborChanged(BlockState state, Level level, BlockPos pos, Block block, @Nullable Orientation orientation, boolean movedByPiston) {
if (level.hasNeighborSignal(pos) && TntBlock.prime(level, pos)) {
level.removeBlock(pos, false);
}
}
@Override
public BlockState playerWillDestroy(Level level, BlockPos pos, BlockState state, Player player) {
if (!level.isClientSide() && !player.getAbilities().instabuild && state.getValue(UNSTABLE).booleanValue()) {
TntBlock.prime(level, pos);
}
return super.playerWillDestroy(level, pos, state, player);
}
@Override
public void wasExploded(ServerLevel level, BlockPos pos, Explosion explosion) {
if (!level.getGameRules().get(GameRules.TNT_EXPLODES).booleanValue()) {
return;
}
PrimedTnt primed = new PrimedTnt(level, (double)pos.getX() + 0.5, pos.getY(), (double)pos.getZ() + 0.5, explosion.getIndirectSourceEntity());
int fuse = primed.getFuse();
primed.setFuse((short)(level.random.nextInt(fuse / 4) + fuse / 8));
level.addFreshEntity(primed);
}
public static boolean prime(Level level, BlockPos pos) {
return TntBlock.prime(level, pos, null);
}
private static boolean prime(Level level, BlockPos pos, @Nullable LivingEntity source) {
ServerLevel serverLevel;
if (!(level instanceof ServerLevel) || !(serverLevel = (ServerLevel)level).getGameRules().get(GameRules.TNT_EXPLODES).booleanValue()) {
return false;
}
PrimedTnt tnt = new PrimedTnt(level, (double)pos.getX() + 0.5, pos.getY(), (double)pos.getZ() + 0.5, source);
level.addFreshEntity(tnt);
level.playSound(null, tnt.getX(), tnt.getY(), tnt.getZ(), SoundEvents.TNT_PRIMED, SoundSource.BLOCKS, 1.0f, 1.0f);
level.gameEvent((Entity)source, GameEvent.PRIME_FUSE, pos);
return true;
}
@Override
protected InteractionResult useItemOn(ItemStack itemStack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
ServerLevel serverLevel;
if (!itemStack.is(Items.FLINT_AND_STEEL) && !itemStack.is(Items.FIRE_CHARGE)) {
return super.useItemOn(itemStack, state, level, pos, player, hand, hitResult);
}
if (TntBlock.prime(level, pos, player)) {
level.setBlock(pos, Blocks.AIR.defaultBlockState(), 11);
Item item = itemStack.getItem();
if (itemStack.is(Items.FLINT_AND_STEEL)) {
itemStack.hurtAndBreak(1, (LivingEntity)player, hand.asEquipmentSlot());
} else {
itemStack.consume(1, player);
}
player.awardStat(Stats.ITEM_USED.get(item));
} else if (level instanceof ServerLevel && !(serverLevel = (ServerLevel)level).getGameRules().get(GameRules.TNT_EXPLODES).booleanValue()) {
player.displayClientMessage(Component.translatable("block.minecraft.tnt.disabled"), true);
return InteractionResult.PASS;
}
return InteractionResult.SUCCESS;
}
@Override
protected void onProjectileHit(Level level, BlockState state, BlockHitResult blockHit, Projectile projectile) {
if (level instanceof ServerLevel) {
ServerLevel serverLevel = (ServerLevel)level;
BlockPos pos = blockHit.getBlockPos();
Entity owner = projectile.getOwner();
if (projectile.isOnFire() && projectile.mayInteract(serverLevel, pos) && TntBlock.prime(level, pos, owner instanceof LivingEntity ? (LivingEntity)owner : null)) {
level.removeBlock(pos, false);
}
}
}
@Override
public boolean dropFromExplosion(Explosion explosion) {
return false;
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(UNSTABLE);
}
}