| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.network.chat.Component;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.sounds.SoundEvents;
|
| 7 | import net.minecraft.sounds.SoundSource;
|
| 8 | import net.minecraft.stats.Stats;
|
| 9 | import net.minecraft.world.InteractionHand;
|
| 10 | import net.minecraft.world.InteractionResult;
|
| 11 | import net.minecraft.world.entity.Entity;
|
| 12 | import net.minecraft.world.entity.LivingEntity;
|
| 13 | import net.minecraft.world.entity.item.PrimedTnt;
|
| 14 | import net.minecraft.world.entity.player.Player;
|
| 15 | import net.minecraft.world.entity.projectile.Projectile;
|
| 16 | import net.minecraft.world.entity.projectile.arrow.AbstractArrow;
|
| 17 | import net.minecraft.world.item.Item;
|
| 18 | import net.minecraft.world.item.ItemStack;
|
| 19 | import net.minecraft.world.item.Items;
|
| 20 | import net.minecraft.world.level.Explosion;
|
| 21 | import net.minecraft.world.level.GameType;
|
| 22 | import net.minecraft.world.level.Level;
|
| 23 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 24 | import net.minecraft.world.level.block.state.BlockState;
|
| 25 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 26 | import net.minecraft.world.level.block.state.pattern.BlockInWorld;
|
| 27 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 28 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 29 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 30 | import net.minecraft.world.level.gamerules.GameRules;
|
| 31 | import net.minecraft.world.level.redstone.Orientation;
|
| 32 | import net.minecraft.world.phys.BlockHitResult;
|
| 33 | import org.jspecify.annotations.Nullable;
|
| 34 |
|
| 35 | public class TntBlock extends Block {
|
| 36 | public static final BooleanProperty UNSTABLE = BlockStateProperties.UNSTABLE;
|
| 37 |
|
| 38 | public TntBlock(final BlockBehaviour.Properties properties) {
|
| 39 | super(properties);
|
| 40 | this.registerDefaultState(this.defaultBlockState().setValue(UNSTABLE, false));
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 45 | if (!oldState.is(state.getBlock())) {
|
| 46 | if (level.hasNeighborSignal(pos) && prime(level, pos)) {
|
| 47 | level.removeBlock(pos, false);
|
| 48 | }
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected void neighborChanged(
|
| 54 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 55 | ) {
|
| 56 | if (level.hasNeighborSignal(pos) && prime(level, pos)) {
|
| 57 | level.removeBlock(pos, false);
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
|
| 63 | if (!level.isClientSide() && !player.getAbilities().instabuild && state.getValue(UNSTABLE)) {
|
| 64 | prime(level, pos);
|
| 65 | }
|
| 66 |
|
| 67 | return super.playerWillDestroy(level, pos, state, player);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public void wasExploded(final ServerLevel level, final BlockPos pos, final Explosion explosion) {
|
| 72 | if (level.getGameRules().get(GameRules.TNT_EXPLODES)) {
|
| 73 | PrimedTnt primed = new PrimedTnt(level, pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, explosion.getIndirectSourceEntity());
|
| 74 | primed.setFuse(PrimedTnt.getRandomShortFuse(primed.getFuse(), level.getRandom()));
|
| 75 | level.addFreshEntity(primed);
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | public static boolean prime(final Level level, final BlockPos pos) {
|
| 80 | return prime(level, pos, null, ItemStack.EMPTY);
|
| 81 | }
|
| 82 |
|
| 83 | public static boolean prime(final Level level, final BlockPos pos, final @Nullable LivingEntity source, final ItemStack itemStack) {
|
| 84 | if (!(level instanceof ServerLevel serverLevel && serverLevel.getGameRules().get(GameRules.TNT_EXPLODES))) {
|
| 85 | return false;
|
| 86 | } else if (source instanceof Player player
|
| 87 | && player.gameMode() == GameType.ADVENTURE
|
| 88 | && !itemStack.canBreakBlockInAdventureMode(new BlockInWorld(level, pos, false))) {
|
| 89 | return false;
|
| 90 | } else {
|
| 91 | PrimedTnt tnt = new PrimedTnt(level, pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, source);
|
| 92 | level.addFreshEntity(tnt);
|
| 93 | level.playSound(null, tnt.getX(), tnt.getY(), tnt.getZ(), SoundEvents.TNT_PRIMED, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 94 | level.gameEvent(source, GameEvent.PRIME_FUSE, pos);
|
| 95 | return true;
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | protected InteractionResult useItemOn(
|
| 101 | final ItemStack itemStack,
|
| 102 | final BlockState state,
|
| 103 | final Level level,
|
| 104 | final BlockPos pos,
|
| 105 | final Player player,
|
| 106 | final InteractionHand hand,
|
| 107 | final BlockHitResult hitResult
|
| 108 | ) {
|
| 109 | if (!itemStack.is(Items.FLINT_AND_STEEL) && !itemStack.is(Items.FIRE_CHARGE)) {
|
| 110 | return super.useItemOn(itemStack, state, level, pos, player, hand, hitResult);
|
| 111 | }
|
| 112 |
|
| 113 | if (prime(level, pos, player, itemStack)) {
|
| 114 | level.setBlock(pos, Blocks.AIR.defaultBlockState(), 11);
|
| 115 | Item item = itemStack.getItem();
|
| 116 | if (itemStack.is(Items.FLINT_AND_STEEL)) {
|
| 117 | itemStack.hurtAndBreak(1, player, hand.asEquipmentSlot());
|
| 118 | } else {
|
| 119 | itemStack.consume(1, player);
|
| 120 | }
|
| 121 |
|
| 122 | player.awardStat(Stats.ITEM_USED.get(item));
|
| 123 | } else if (level instanceof ServerLevel serverLevel && !serverLevel.getGameRules().get(GameRules.TNT_EXPLODES)) {
|
| 124 | player.sendOverlayMessage(Component.translatable("block.minecraft.tnt.disabled"));
|
| 125 | return InteractionResult.PASS;
|
| 126 | }
|
| 127 |
|
| 128 | return InteractionResult.SUCCESS;
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | protected void onProjectileHit(final Level level, final BlockState state, final BlockHitResult blockHit, final Projectile projectile) {
|
| 133 | if (level instanceof ServerLevel serverLevel) {
|
| 134 | BlockPos pos = blockHit.getBlockPos();
|
| 135 | Entity owner = projectile.getOwner();
|
| 136 | if (projectile.isOnFire()
|
| 137 | && projectile.mayInteract(serverLevel, pos)
|
| 138 | && prime(
|
| 139 | level,
|
| 140 | pos,
|
| 141 | owner instanceof LivingEntity livingEntity ? livingEntity : null,
|
| 142 | projectile instanceof AbstractArrow arr ? arr.getPickupItemStackOrigin() : ItemStack.EMPTY
|
| 143 | )) {
|
| 144 | level.removeBlock(pos, false);
|
| 145 | }
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | public boolean dropFromExplosion(final Explosion explosion) {
|
| 151 | return false;
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 156 | builder.add(UNSTABLE);
|
| 157 | }
|
| 158 | }
|
| 159 |
|