CandleBlock .java

net.minecraft.world.level.block.CandleBlock (class, 175 lines): members: MAX_CANDLES, MIN_CANDLES, PARTICLE_OFFSETS, canBeLit, getParticleOffsets, CANDLES.

net.minecraft.world.level.blockclass175 linescommonextends AbstractCandleBlock6.6 KB

Implements

API Surface (12)

InteractionResult useItemOn(final ItemStack itemStack, final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final BlockHitResult hitResult)
boolean canBeReplaced(final BlockState state, final BlockPlaceContext context)
BlockState getStateForPlacement(final BlockPlaceContext context)
BlockState updateShape(final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random)
FluidState getFluidState(final BlockState state)
VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
boolean placeLiquid(final LevelAccessor level, final BlockPos pos, final BlockState state, final FluidState fluidState)
boolean canLight(final BlockState state)
Iterable<Vec3> getParticleOffsets(final BlockState state)
boolean canBeLit(final BlockState state)
boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos)

Fields & Constants (18)

MIN_CANDLES
MAX_CANDLES
CANDLES
LIT
WATERLOGGED
LIGHT_EMISSION
PARTICLE_OFFSETS
SHAPES
itemStack
state
level
pos
player
hand
ticks
directionToNeighbour
neighbourPos
neighbourState

Full Source

CandleBlock.java175 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
4
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
5
import java.util.List;
6
import java.util.function.ToIntFunction;
7
import net.minecraft.core.BlockPos;
8
import net.minecraft.core.Direction;
9
import net.minecraft.tags.BlockTags;
10
import net.minecraft.util.RandomSource;
11
import net.minecraft.util.Util;
12
import net.minecraft.world.InteractionHand;
13
import net.minecraft.world.InteractionResult;
14
import net.minecraft.world.entity.player.Player;
15
import net.minecraft.world.item.ItemStack;
16
import net.minecraft.world.item.context.BlockPlaceContext;
17
import net.minecraft.world.level.BlockGetter;
18
import net.minecraft.world.level.Level;
19
import net.minecraft.world.level.LevelAccessor;
20
import net.minecraft.world.level.LevelReader;
21
import net.minecraft.world.level.ScheduledTickAccess;
22
import net.minecraft.world.level.block.state.BlockBehaviour;
23
import net.minecraft.world.level.block.state.BlockState;
24
import net.minecraft.world.level.block.state.StateDefinition;
25
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
26
import net.minecraft.world.level.block.state.properties.BooleanProperty;
27
import net.minecraft.world.level.block.state.properties.IntegerProperty;
28
import net.minecraft.world.level.material.FluidState;
29
import net.minecraft.world.level.material.Fluids;
30
import net.minecraft.world.phys.BlockHitResult;
31
import net.minecraft.world.phys.Vec3;
32
import net.minecraft.world.phys.shapes.CollisionContext;
33
import net.minecraft.world.phys.shapes.VoxelShape;
34
35
public class CandleBlock extends AbstractCandleBlock implements SimpleWaterloggedBlock {
36
	public static final int MIN_CANDLES = 1;
37
	public static final int MAX_CANDLES = 4;
38
	public static final IntegerProperty CANDLES = BlockStateProperties.CANDLES;
39
	public static final BooleanProperty LIT = AbstractCandleBlock.LIT;
40
	public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
41
	public static final ToIntFunction<BlockState> LIGHT_EMISSION = state -> state.getValue(LIT) ? 3 * state.getValue(CANDLES) : 0;
42
	private static final Int2ObjectMap<List<Vec3>> PARTICLE_OFFSETS = Util.make(
43
		new Int2ObjectOpenHashMap<>(4),
44
		map -> {
45
			float s = 0.0625F;
46
			map.put(1, List.of(new Vec3(8.0, 8.0, 8.0).scale(0.0625)));
47
			map.put(2, List.of(new Vec3(6.0, 7.0, 8.0).scale(0.0625), new Vec3(10.0, 8.0, 7.0).scale(0.0625)));
48
			map.put(3, List.of(new Vec3(8.0, 5.0, 10.0).scale(0.0625), new Vec3(6.0, 7.0, 8.0).scale(0.0625), new Vec3(9.0, 8.0, 7.0).scale(0.0625)));
49
			map.put(
50
				4,
51
				List.of(
52
					new Vec3(7.0, 5.0, 9.0).scale(0.0625),
53
					new Vec3(10.0, 7.0, 9.0).scale(0.0625),
54
					new Vec3(6.0, 7.0, 6.0).scale(0.0625),
55
					new Vec3(9.0, 8.0, 6.0).scale(0.0625)
56
				)
57
			);
58
		}
59
	);
60
	private static final VoxelShape[] SHAPES = new VoxelShape[]{
61
		Block.column(2.0, 0.0, 6.0), Block.box(5.0, 0.0, 6.0, 11.0, 6.0, 9.0), Block.box(5.0, 0.0, 6.0, 10.0, 6.0, 11.0), Block.box(5.0, 0.0, 5.0, 11.0, 6.0, 10.0)
62
	};
63
64
	public CandleBlock(final BlockBehaviour.Properties properties) {
65
		super(properties);
66
		this.registerDefaultState(this.stateDefinition.any().setValue(CANDLES, 1).setValue(LIT, false).setValue(WATERLOGGED, false));
67
	}
68
69
	@Override
70
	protected InteractionResult useItemOn(
71
		final ItemStack itemStack,
72
		final BlockState state,
73
		final Level level,
74
		final BlockPos pos,
75
		final Player player,
76
		final InteractionHand hand,
77
		final BlockHitResult hitResult
78
	) {
79
		if (itemStack.isEmpty() && player.getAbilities().mayBuild && state.getValue(LIT)) {
80
			extinguish(player, state, level, pos);
81
			return InteractionResult.SUCCESS;
82
		} else {
83
			return super.useItemOn(itemStack, state, level, pos, player, hand, hitResult);
84
		}
85
	}
86
87
	@Override
88
	protected boolean canBeReplaced(final BlockState state, final BlockPlaceContext context) {
89
		return !context.isSecondaryUseActive() && context.getItemInHand().getItem() == this.asItem() && state.getValue(CANDLES) < 4
90
			? true
91
			: super.canBeReplaced(state, context);
92
	}
93
94
	@Override
95
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
96
		BlockState state = context.getLevel().getBlockState(context.getClickedPos());
97
		if (state.is(this)) {
98
			return state.cycle(CANDLES);
99
		}
100
101
		FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
102
		boolean isWaterSource = replacedFluidState.is(Fluids.WATER);
103
		return super.getStateForPlacement(context).setValue(WATERLOGGED, isWaterSource);
104
	}
105
106
	@Override
107
	protected BlockState updateShape(
108
		final BlockState state,
109
		final LevelReader level,
110
		final ScheduledTickAccess ticks,
111
		final BlockPos pos,
112
		final Direction directionToNeighbour,
113
		final BlockPos neighbourPos,
114
		final BlockState neighbourState,
115
		final RandomSource random
116
	) {
117
		if (state.getValue(WATERLOGGED)) {
118
			ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
119
		}
120
121
		return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
122
	}
123
124
	@Override
125
	protected FluidState getFluidState(final BlockState state) {
126
		return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
127
	}
128
129
	@Override
130
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
131
		return SHAPES[state.getValue(CANDLES) - 1];
132
	}
133
134
	@Override
135
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
136
		builder.add(CANDLES, LIT, WATERLOGGED);
137
	}
138
139
	@Override
140
	public boolean placeLiquid(final LevelAccessor level, final BlockPos pos, final BlockState state, final FluidState fluidState) {
141
		if (!state.getValue(WATERLOGGED) && fluidState.is(Fluids.WATER)) {
142
			BlockState newState = state.setValue(WATERLOGGED, true);
143
			if (state.getValue(LIT)) {
144
				extinguish(null, newState, level, pos);
145
			} else {
146
				level.setBlockAndUpdate(pos, newState);
147
			}
148
149
			level.scheduleTick(pos, fluidState.getType(), fluidState.getType().getTickDelay(level));
150
			return true;
151
		} else {
152
			return false;
153
		}
154
	}
155
156
	public static boolean canLight(final BlockState state) {
157
		return state.is(BlockTags.CANDLES, s -> s.hasProperty(LIT) && s.hasProperty(WATERLOGGED)) && !state.getValue(LIT) && !state.getValue(WATERLOGGED);
158
	}
159
160
	@Override
161
	protected Iterable<Vec3> getParticleOffsets(final BlockState state) {
162
		return PARTICLE_OFFSETS.get(state.getValue(CANDLES).intValue());
163
	}
164
165
	@Override
166
	protected boolean canBeLit(final BlockState state) {
167
		return !state.getValue(WATERLOGGED) && super.canBeLit(state);
168
	}
169
170
	@Override
171
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
172
		return Block.canSupportCenter(level, pos.below(), Direction.UP);
173
	}
174
}
175

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