CrafterBlock .java

net.minecraft.world.level.block.CrafterBlock (class, 239 lines): members: CRAFTER_ADVANCEMENT_DIAMETER, CRAFTING_TICK_DELAY, getPotentialResults, results.

net.minecraft.world.level.blockclass239 linescommonextends BaseEntityBlock9.7 KB

API Surface (14)

boolean hasAnalogOutputSignal(final BlockState state)
int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction)
void neighborChanged(final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston)
void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState)
BlockState getStateForPlacement(final BlockPlaceContext context)
void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack)
void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston)
InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult)
void dispenseFrom(final BlockState state, final ServerLevel level, final BlockPos pos)
Optional<RecipeHolder<CraftingRecipe>> getPotentialResults(final ServerLevel level, final CraftingInput input)
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)

Fields & Constants (13)

CRAFTING
TRIGGERED
ORIENTATION
MAX_CRAFTING_TICKS
CRAFTING_TICK_DELAY
RECIPE_CACHE
CRAFTER_ADVANCEMENT_DIAMETER
orientation
level
pos
blockEntity
results
blockState

Full Source

CrafterBlock.java239 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.Optional;
4
import net.minecraft.advancements.triggers.CriteriaTriggers;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.Direction;
7
import net.minecraft.core.FrontAndTop;
8
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
9
import net.minecraft.server.level.ServerLevel;
10
import net.minecraft.server.level.ServerPlayer;
11
import net.minecraft.util.RandomSource;
12
import net.minecraft.world.Container;
13
import net.minecraft.world.Containers;
14
import net.minecraft.world.InteractionResult;
15
import net.minecraft.world.entity.LivingEntity;
16
import net.minecraft.world.entity.player.Player;
17
import net.minecraft.world.item.ItemStack;
18
import net.minecraft.world.item.context.BlockPlaceContext;
19
import net.minecraft.world.item.crafting.CraftingInput;
20
import net.minecraft.world.item.crafting.CraftingRecipe;
21
import net.minecraft.world.item.crafting.RecipeCache;
22
import net.minecraft.world.item.crafting.RecipeHolder;
23
import net.minecraft.world.level.Level;
24
import net.minecraft.world.level.block.entity.BlockEntity;
25
import net.minecraft.world.level.block.entity.BlockEntityTicker;
26
import net.minecraft.world.level.block.entity.BlockEntityType;
27
import net.minecraft.world.level.block.entity.BlockEntityTypes;
28
import net.minecraft.world.level.block.entity.CrafterBlockEntity;
29
import net.minecraft.world.level.block.entity.HopperBlockEntity;
30
import net.minecraft.world.level.block.state.BlockBehaviour;
31
import net.minecraft.world.level.block.state.BlockState;
32
import net.minecraft.world.level.block.state.StateDefinition;
33
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
34
import net.minecraft.world.level.block.state.properties.BooleanProperty;
35
import net.minecraft.world.level.block.state.properties.EnumProperty;
36
import net.minecraft.world.level.redstone.Orientation;
37
import net.minecraft.world.phys.AABB;
38
import net.minecraft.world.phys.BlockHitResult;
39
import net.minecraft.world.phys.Vec3;
40
import org.jspecify.annotations.Nullable;
41
42
public class CrafterBlock extends BaseEntityBlock {
43
	public static final BooleanProperty CRAFTING = BlockStateProperties.CRAFTING;
44
	public static final BooleanProperty TRIGGERED = BlockStateProperties.TRIGGERED;
45
	private static final EnumProperty<FrontAndTop> ORIENTATION = BlockStateProperties.ORIENTATION;
46
	private static final int MAX_CRAFTING_TICKS = 6;
47
	private static final int CRAFTING_TICK_DELAY = 4;
48
	private static final RecipeCache RECIPE_CACHE = new RecipeCache(10);
49
	private static final int CRAFTER_ADVANCEMENT_DIAMETER = 17;
50
51
	public CrafterBlock(final BlockBehaviour.Properties properties) {
52
		super(properties);
53
		this.registerDefaultState(this.stateDefinition.any().setValue(ORIENTATION, FrontAndTop.NORTH_UP).setValue(TRIGGERED, false).setValue(CRAFTING, false));
54
	}
55
56
	@Override
57
	protected boolean hasAnalogOutputSignal(final BlockState state) {
58
		return true;
59
	}
60
61
	@Override
62
	protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
63
		return level.getBlockEntity(pos) instanceof CrafterBlockEntity crafterBlockEntity ? crafterBlockEntity.getRedstoneSignal() : 0;
64
	}
65
66
	@Override
67
	protected void neighborChanged(
68
		final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
69
	) {
70
		boolean shouldTrigger = level.hasNeighborSignal(pos);
71
		boolean isTriggered = state.getValue(TRIGGERED);
72
		BlockEntity blockEntity = level.getBlockEntity(pos);
73
		if (shouldTrigger && !isTriggered) {
74
			level.scheduleTick(pos, this, 4);
75
			level.setBlock(pos, state.setValue(TRIGGERED, true), 2);
76
			this.setBlockEntityTriggered(blockEntity, true);
77
		} else if (!shouldTrigger && isTriggered) {
78
			level.setBlock(pos, state.setValue(TRIGGERED, false).setValue(CRAFTING, false), 2);
79
			this.setBlockEntityTriggered(blockEntity, false);
80
		}
81
	}
82
83
	@Override
84
	protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
85
		this.dispenseFrom(state, level, pos);
86
	}
87
88
	@Override
89
	public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
90
		return level.isClientSide() ? null : createTickerHelper(type, BlockEntityTypes.CRAFTER, CrafterBlockEntity::serverTick);
91
	}
92
93
	private void setBlockEntityTriggered(final @Nullable BlockEntity blockEntity, final boolean triggered) {
94
		if (blockEntity instanceof CrafterBlockEntity crafterBlockEntity) {
95
			crafterBlockEntity.setTriggered(triggered);
96
		}
97
	}
98
99
	@Override
100
	public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
101
		CrafterBlockEntity crafterBlockEntity = new CrafterBlockEntity(worldPosition, blockState);
102
		crafterBlockEntity.setTriggered(blockState.hasProperty(TRIGGERED) && blockState.getValue(TRIGGERED));
103
		return crafterBlockEntity;
104
	}
105
106
	@Override
107
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
108
		Direction nearestLookingDirection = context.getNearestLookingDirection().getOpposite();
109
110
		Direction verticalDirection = switch (nearestLookingDirection) {
111
			case DOWN -> context.getHorizontalDirection().getOpposite();
112
			case UP -> context.getHorizontalDirection();
113
			case NORTH, SOUTH, WEST, EAST -> Direction.UP;
114
		};
115
		return this.defaultBlockState()
116
			.setValue(ORIENTATION, FrontAndTop.fromFrontAndTop(nearestLookingDirection, verticalDirection))
117
			.setValue(TRIGGERED, context.getLevel().hasNeighborSignal(context.getClickedPos()));
118
	}
119
120
	@Override
121
	public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
122
		if (state.getValue(TRIGGERED)) {
123
			level.scheduleTick(pos, this, 4);
124
		}
125
	}
126
127
	@Override
128
	protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
129
		Containers.updateNeighboursAfterDestroy(state, level, pos);
130
	}
131
132
	@Override
133
	protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
134
		if (!level.isClientSide() && level.getBlockEntity(pos) instanceof CrafterBlockEntity crafter) {
135
			player.openMenu(crafter);
136
		}
137
138
		return InteractionResult.SUCCESS;
139
	}
140
141
	protected void dispenseFrom(final BlockState state, final ServerLevel level, final BlockPos pos) {
142
		if (level.getBlockEntity(pos) instanceof CrafterBlockEntity blockEntity) {
143
			CraftingInput var11 = blockEntity.asCraftInput();
144
			Optional<RecipeHolder<CraftingRecipe>> recipe = getPotentialResults(level, var11);
145
			if (recipe.isEmpty()) {
146
				level.levelEvent(1050, pos, 0);
147
			} else {
148
				RecipeHolder<CraftingRecipe> pickedRecipe = recipe.get();
149
				ItemStack results = pickedRecipe.value().assemble(var11);
150
				if (results.isEmpty()) {
151
					level.levelEvent(1050, pos, 0);
152
				} else {
153
					blockEntity.setCraftingTicksRemaining(6);
154
					level.setBlock(pos, state.setValue(CRAFTING, true), 2);
155
					results.onCraftedBySystem(level);
156
					this.dispenseItem(level, pos, blockEntity, results, state, pickedRecipe);
157
158
					for (ItemStack remainingItem : pickedRecipe.value().getRemainingItems(var11)) {
159
						if (!remainingItem.isEmpty()) {
160
							this.dispenseItem(level, pos, blockEntity, remainingItem, state, pickedRecipe);
161
						}
162
					}
163
164
					blockEntity.getItems().forEach(it -> {
165
						if (!it.isEmpty()) {
166
							it.shrink(1);
167
						}
168
					});
169
					blockEntity.setChanged();
170
				}
171
			}
172
		}
173
	}
174
175
	public static Optional<RecipeHolder<CraftingRecipe>> getPotentialResults(final ServerLevel level, final CraftingInput input) {
176
		return RECIPE_CACHE.get(level, input);
177
	}
178
179
	private void dispenseItem(
180
		final ServerLevel level,
181
		final BlockPos pos,
182
		final CrafterBlockEntity blockEntity,
183
		final ItemStack results,
184
		final BlockState blockState,
185
		final RecipeHolder<?> recipe
186
	) {
187
		Direction direction = blockState.getValue(ORIENTATION).front();
188
		Container into = HopperBlockEntity.getContainerAt(level, pos.relative(direction));
189
		ItemStack remaining = results.copy();
190
		if (into != null && (into instanceof CrafterBlockEntity || results.getCount() > into.getMaxStackSize(results))) {
191
			while (!remaining.isEmpty()) {
192
				ItemStack copy = remaining.copyWithCount(1);
193
				ItemStack itemStack = HopperBlockEntity.addItem(blockEntity, into, copy, direction.getOpposite());
194
				if (!itemStack.isEmpty()) {
195
					break;
196
				}
197
198
				remaining.shrink(1);
199
			}
200
		} else if (into != null) {
201
			while (!remaining.isEmpty()) {
202
				int oldSize = remaining.getCount();
203
				remaining = HopperBlockEntity.addItem(blockEntity, into, remaining, direction.getOpposite());
204
				if (oldSize == remaining.getCount()) {
205
					break;
206
				}
207
			}
208
		}
209
210
		if (!remaining.isEmpty()) {
211
			Vec3 centerPos = Vec3.atCenterOf(pos);
212
			Vec3 itemSpawnOffset = centerPos.relative(direction, 0.7);
213
			DefaultDispenseItemBehavior.spawnItem(level, remaining, 6, direction, itemSpawnOffset);
214
215
			for (ServerPlayer player : level.getEntitiesOfClass(ServerPlayer.class, AABB.ofSize(centerPos, 17.0, 17.0, 17.0))) {
216
				CriteriaTriggers.CRAFTER_RECIPE_CRAFTED.trigger(player, recipe.id(), blockEntity.getItems());
217
			}
218
219
			level.levelEvent(1049, pos, 0);
220
			level.levelEvent(2010, pos, direction.get3DDataValue());
221
		}
222
	}
223
224
	@Override
225
	protected BlockState rotate(final BlockState state, final Rotation rotation) {
226
		return state.setValue(ORIENTATION, rotation.rotation().rotate(state.getValue(ORIENTATION)));
227
	}
228
229
	@Override
230
	protected BlockState mirror(final BlockState state, final Mirror mirror) {
231
		return state.setValue(ORIENTATION, mirror.rotation().rotate(state.getValue(ORIENTATION)));
232
	}
233
234
	@Override
235
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
236
		builder.add(ORIENTATION, TRIGGERED, CRAFTING);
237
	}
238
}
239

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