ChiseledBookShelfBlock .java

net.minecraft.world.level.block.ChiseledBookShelfBlock (class, 189 lines): members: SLOT_OCCUPIED_PROPERTIES, BOOKS_PER_ROW, MAX_BOOKS_IN_STORAGE, getRows.

net.minecraft.world.level.blockclass189 linescommonextends BaseEntityBlock7.2 KB

Implements

API Surface (11)

int getRows()
int getColumns()
InteractionResult useItemOn(final ItemStack itemStack, final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final BlockHitResult hitResult)
InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston)
BlockState getStateForPlacement(final BlockPlaceContext context)
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)
boolean hasAnalogOutputSignal(final BlockState state)
int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction)

Fields & Constants (16)

FACING
SLOT_0_OCCUPIED
SLOT_1_OCCUPIED
SLOT_2_OCCUPIED
SLOT_3_OCCUPIED
SLOT_4_OCCUPIED
SLOT_5_OCCUPIED
MAX_BOOKS_IN_STORAGE
BOOKS_PER_ROW
SLOT_OCCUPIED_PROPERTIES
itemStack
state
level
pos
player
hand

Full Source

ChiseledBookShelfBlock.java189 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.List;
4
import java.util.OptionalInt;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.Direction;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.sounds.SoundEvent;
9
import net.minecraft.sounds.SoundEvents;
10
import net.minecraft.sounds.SoundSource;
11
import net.minecraft.stats.Stats;
12
import net.minecraft.tags.ItemTags;
13
import net.minecraft.util.Prediction;
14
import net.minecraft.world.Containers;
15
import net.minecraft.world.InteractionHand;
16
import net.minecraft.world.InteractionResult;
17
import net.minecraft.world.entity.player.Player;
18
import net.minecraft.world.item.ItemStack;
19
import net.minecraft.world.item.Items;
20
import net.minecraft.world.item.context.BlockPlaceContext;
21
import net.minecraft.world.level.Level;
22
import net.minecraft.world.level.block.entity.BlockEntity;
23
import net.minecraft.world.level.block.entity.ChiseledBookShelfBlockEntity;
24
import net.minecraft.world.level.block.state.BlockBehaviour;
25
import net.minecraft.world.level.block.state.BlockState;
26
import net.minecraft.world.level.block.state.StateDefinition;
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.block.state.properties.EnumProperty;
30
import net.minecraft.world.level.gameevent.GameEvent;
31
import net.minecraft.world.phys.BlockHitResult;
32
import org.jspecify.annotations.Nullable;
33
34
public class ChiseledBookShelfBlock extends BaseEntityBlock implements SelectableSlotContainer {
35
	public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
36
	public static final BooleanProperty SLOT_0_OCCUPIED = BlockStateProperties.SLOT_0_OCCUPIED;
37
	public static final BooleanProperty SLOT_1_OCCUPIED = BlockStateProperties.SLOT_1_OCCUPIED;
38
	public static final BooleanProperty SLOT_2_OCCUPIED = BlockStateProperties.SLOT_2_OCCUPIED;
39
	public static final BooleanProperty SLOT_3_OCCUPIED = BlockStateProperties.SLOT_3_OCCUPIED;
40
	public static final BooleanProperty SLOT_4_OCCUPIED = BlockStateProperties.SLOT_4_OCCUPIED;
41
	public static final BooleanProperty SLOT_5_OCCUPIED = BlockStateProperties.SLOT_5_OCCUPIED;
42
	private static final int MAX_BOOKS_IN_STORAGE = 6;
43
	private static final int BOOKS_PER_ROW = 3;
44
	public static final List<BooleanProperty> SLOT_OCCUPIED_PROPERTIES = List.of(
45
		SLOT_0_OCCUPIED, SLOT_1_OCCUPIED, SLOT_2_OCCUPIED, SLOT_3_OCCUPIED, SLOT_4_OCCUPIED, SLOT_5_OCCUPIED
46
	);
47
48
	@Override
49
	public int getRows() {
50
		return 2;
51
	}
52
53
	@Override
54
	public int getColumns() {
55
		return 3;
56
	}
57
58
	public ChiseledBookShelfBlock(final BlockBehaviour.Properties properties) {
59
		super(properties);
60
		BlockState defaultState = this.stateDefinition.any().setValue(FACING, Direction.NORTH);
61
62
		for (BooleanProperty property : SLOT_OCCUPIED_PROPERTIES) {
63
			defaultState = defaultState.setValue(property, false);
64
		}
65
66
		this.registerDefaultState(defaultState);
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 (level.getBlockEntity(pos) instanceof ChiseledBookShelfBlockEntity bookshelfBlock) {
80
			if (!itemStack.is(ItemTags.BOOKSHELF_BOOKS)) {
81
				return InteractionResult.TRY_WITH_EMPTY_HAND;
82
			}
83
84
			OptionalInt hitSlot = this.getHitSlot(hitResult, state.getValue(FACING));
85
			if (hitSlot.isEmpty()) {
86
				return InteractionResult.PASS;
87
			}
88
89
			if (state.getValue(SLOT_OCCUPIED_PROPERTIES.get(hitSlot.getAsInt()))) {
90
				return InteractionResult.TRY_WITH_EMPTY_HAND;
91
			}
92
93
			addBook(level, pos, player, bookshelfBlock, itemStack, hitSlot.getAsInt());
94
			return InteractionResult.SUCCESS;
95
		} else {
96
			return InteractionResult.PASS;
97
		}
98
	}
99
100
	@Override
101
	protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
102
		if (level.getBlockEntity(pos) instanceof ChiseledBookShelfBlockEntity bookshelfBlock) {
103
			OptionalInt hitSlot = this.getHitSlot(hitResult, state.getValue(FACING));
104
			if (hitSlot.isEmpty()) {
105
				return InteractionResult.PASS;
106
			}
107
108
			if (!state.getValue(SLOT_OCCUPIED_PROPERTIES.get(hitSlot.getAsInt()))) {
109
				return InteractionResult.CONSUME;
110
			}
111
112
			removeBook(level, pos, player, bookshelfBlock, hitSlot.getAsInt());
113
			return InteractionResult.SUCCESS;
114
		} else {
115
			return InteractionResult.PASS;
116
		}
117
	}
118
119
	private static void addBook(
120
		final Level level, final BlockPos pos, final Player player, final ChiseledBookShelfBlockEntity bookshelfBlock, final ItemStack itemStack, final int slot
121
	) {
122
		if (!level.isClientSide()) {
123
			player.awardStat(Stats.ITEM_USED.get(itemStack.getItem()));
124
			SoundEvent soundEvent = itemStack.is(Items.ENCHANTED_BOOK) ? SoundEvents.CHISELED_BOOKSHELF_INSERT_ENCHANTED : SoundEvents.CHISELED_BOOKSHELF_INSERT;
125
			bookshelfBlock.setItem(slot, itemStack.consumeAndReturn(1, player));
126
			level.playSound(null, pos, soundEvent, SoundSource.BLOCKS, 1.0F, 1.0F);
127
		}
128
	}
129
130
	private static void removeBook(final Level level, final BlockPos pos, final Player player, final ChiseledBookShelfBlockEntity bookshelfBlock, final int slot) {
131
		if (!level.isClientSide()) {
132
			ItemStack retrievedBook = bookshelfBlock.removeItem(slot, 1);
133
			SoundEvent soundEvent = retrievedBook.is(Items.ENCHANTED_BOOK) ? SoundEvents.CHISELED_BOOKSHELF_PICKUP_ENCHANTED : SoundEvents.CHISELED_BOOKSHELF_PICKUP;
134
			level.playSound(null, pos, soundEvent, SoundSource.BLOCKS, 1.0F, 1.0F);
135
			if (!player.getInventory().add(retrievedBook)) {
136
				player.drop(retrievedBook, false, Prediction.SERVER_ONLY);
137
			}
138
139
			level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
140
		}
141
	}
142
143
	@Override
144
	public @Nullable BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
145
		return new ChiseledBookShelfBlockEntity(worldPosition, blockState);
146
	}
147
148
	@Override
149
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
150
		builder.add(FACING);
151
		StateDefinition.Builder var2 = builder;
152
		SLOT_OCCUPIED_PROPERTIES.forEach(xva$0 -> var2.add(xva$0));
153
	}
154
155
	@Override
156
	protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
157
		Containers.updateNeighboursAfterDestroy(state, level, pos);
158
	}
159
160
	@Override
161
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
162
		return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());
163
	}
164
165
	@Override
166
	public BlockState rotate(final BlockState state, final Rotation rotation) {
167
		return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
168
	}
169
170
	@Override
171
	public BlockState mirror(final BlockState state, final Mirror mirror) {
172
		return state.rotate(mirror.getRotation(state.getValue(FACING)));
173
	}
174
175
	@Override
176
	protected boolean hasAnalogOutputSignal(final BlockState state) {
177
		return true;
178
	}
179
180
	@Override
181
	protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
182
		if (level.isClientSide()) {
183
			return 0;
184
		} else {
185
			return level.getBlockEntity(pos) instanceof ChiseledBookShelfBlockEntity blockEntity ? blockEntity.getLastInteractedSlot() + 1 : 0;
186
		}
187
	}
188
}
189

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