AbstractCandleBlock .java

net.minecraft.world.level.block.AbstractCandleBlock (class, 103 lines): members: LIGHT_PER_CANDLE, extinguish, canBeLit, getParticleOffsets, isLit, LIT.

net.minecraft.world.level.blockclass103 linescommonextends Block4.0 KB

API Surface (6)

boolean isLit(final BlockState state)
void onProjectileHit(final Level level, final BlockState state, final BlockHitResult blockHit, final Projectile projectile)
boolean canBeLit(final BlockState state)
void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random)
void extinguish(final @Nullable Player player, final BlockState state, final LevelAccessor level, final BlockPos pos)
void onExplosionHit(final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit)

Fields & Constants (3)

LIGHT_PER_CANDLE
LIT
explosion

Full Source

AbstractCandleBlock.java103 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.function.BiConsumer;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.particles.ParticleTypes;
6
import net.minecraft.server.level.ServerLevel;
7
import net.minecraft.sounds.SoundEvents;
8
import net.minecraft.sounds.SoundSource;
9
import net.minecraft.tags.BlockTags;
10
import net.minecraft.util.RandomSource;
11
import net.minecraft.world.entity.player.Player;
12
import net.minecraft.world.entity.projectile.Projectile;
13
import net.minecraft.world.item.ItemStack;
14
import net.minecraft.world.level.Explosion;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.level.LevelAccessor;
17
import net.minecraft.world.level.block.state.BlockBehaviour;
18
import net.minecraft.world.level.block.state.BlockState;
19
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
20
import net.minecraft.world.level.block.state.properties.BooleanProperty;
21
import net.minecraft.world.level.gameevent.GameEvent;
22
import net.minecraft.world.phys.BlockHitResult;
23
import net.minecraft.world.phys.Vec3;
24
import org.jspecify.annotations.Nullable;
25
26
public abstract class AbstractCandleBlock extends Block {
27
	public static final int LIGHT_PER_CANDLE = 3;
28
	public static final BooleanProperty LIT = BlockStateProperties.LIT;
29
30
	protected AbstractCandleBlock(final BlockBehaviour.Properties properties) {
31
		super(properties);
32
	}
33
34
	protected abstract Iterable<Vec3> getParticleOffsets(final BlockState state);
35
36
	public static boolean isLit(final BlockState state) {
37
		return state.hasProperty(LIT) && (state.is(BlockTags.CANDLES) || state.is(BlockTags.CANDLE_CAKES)) && state.getValue(LIT);
38
	}
39
40
	@Override
41
	protected void onProjectileHit(final Level level, final BlockState state, final BlockHitResult blockHit, final Projectile projectile) {
42
		if (!level.isClientSide() && projectile.isOnFire() && this.canBeLit(state)) {
43
			setLit(level, state, blockHit.getBlockPos(), true);
44
		}
45
	}
46
47
	protected boolean canBeLit(final BlockState state) {
48
		return !state.getValue(LIT);
49
	}
50
51
	@Override
52
	public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
53
		if (state.getValue(LIT)) {
54
			this.getParticleOffsets(state).forEach(particlePos -> addParticlesAndSound(level, particlePos.add(pos.getX(), pos.getY(), pos.getZ()), random));
55
		}
56
	}
57
58
	private static void addParticlesAndSound(final Level level, final Vec3 pos, final RandomSource random) {
59
		float chance = random.nextFloat();
60
		if (chance < 0.3F) {
61
			level.addParticle(ParticleTypes.SMOKE, pos.x, pos.y, pos.z, 0.0, 0.0, 0.0);
62
			if (chance < 0.17F) {
63
				level.playLocalSound(
64
					pos.x + 0.5, pos.y + 0.5, pos.z + 0.5, SoundEvents.CANDLE_AMBIENT, SoundSource.BLOCKS, 1.0F + random.nextFloat(), random.nextFloat() * 0.7F + 0.3F, false
65
				);
66
			}
67
		}
68
69
		level.addParticle(ParticleTypes.SMALL_FLAME, pos.x, pos.y, pos.z, 0.0, 0.0, 0.0);
70
	}
71
72
	public static void extinguish(final @Nullable Player player, final BlockState state, final LevelAccessor level, final BlockPos pos) {
73
		setLit(level, state, pos, false);
74
		if (state.getBlock() instanceof AbstractCandleBlock) {
75
			((AbstractCandleBlock)state.getBlock())
76
				.getParticleOffsets(state)
77
				.forEach(
78
					particlePos -> level.addParticle(
79
						ParticleTypes.SMOKE, pos.getX() + particlePos.x(), pos.getY() + particlePos.y(), pos.getZ() + particlePos.z(), 0.0, 0.1F, 0.0
80
					)
81
				);
82
		}
83
84
		level.playSound(null, pos, SoundEvents.CANDLE_EXTINGUISH, SoundSource.BLOCKS, 1.0F, 1.0F);
85
		level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
86
	}
87
88
	private static void setLit(final LevelAccessor level, final BlockState state, final BlockPos pos, final boolean lit) {
89
		level.setBlock(pos, state.setValue(LIT, lit), 11);
90
	}
91
92
	@Override
93
	protected void onExplosionHit(
94
		final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit
95
	) {
96
		if (explosion.canTriggerBlocks() && state.getValue(LIT)) {
97
			extinguish(null, state, level, pos);
98
		}
99
100
		super.onExplosionHit(state, level, pos, explosion, onHit);
101
	}
102
}
103

Decompiled Minecraft 26.3 source of AbstractCandleBlock (net/minecraft/world/level/block/AbstractCandleBlock.java) with Mojang official mappings, stored verbatim on the site.