BlockItemAccessor .java

net.minecraft.server.commands.item.BlockItemAccessor (record): members: getBlockSlots, RESPONSE_SET_KNOWN_ITEM, RESPONSE_MODIFY, getReplaceSuccess, pos.

net.minecraft.server.commands.itemrecord74 linesserver3.9 KB

Implements

API Surface (5)

SlotCollection getSlots(final CommandSourceStack source, final SlotSource slotSource)
void setItems(final CommandSourceStack source, final SlotSource slotSource, final ItemAccessor.SetterFunction<BlockPos> function)
int getReplaceSuccess(final CommandSourceStack source, final CommandResponseTracker<BlockPos> tracker, final @Nullable ItemStack knownItem)
int getModifySuccess(final CommandSourceStack source, final CommandResponseTracker<BlockPos> tracker)
Container getContainer(final CommandSourceStack source, final BlockPos pos, final Dynamic3CommandExceptionType exceptionType)

Fields & Constants (4)

PROVIDER
RESPONSE_SET
RESPONSE_SET_KNOWN_ITEM
RESPONSE_MODIFY

String literals (4)

"block"
"commands.item.block.replace.success"
"commands.item.block.replace.success.known_item"
"commands.item.block.modify.success"

Full Source

BlockItemAccessor.java74 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands.item;
2
3
import com.mojang.brigadier.exceptions.CommandSyntaxException;
4
import com.mojang.brigadier.exceptions.Dynamic3CommandExceptionType;
5
import net.minecraft.commands.CommandSourceStack;
6
import net.minecraft.commands.Commands;
7
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
8
import net.minecraft.core.BlockPos;
9
import net.minecraft.network.chat.Component;
10
import net.minecraft.server.commands.ArgProvider;
11
import net.minecraft.server.commands.CommandResponseTracker;
12
import net.minecraft.world.Container;
13
import net.minecraft.world.item.ItemStack;
14
import net.minecraft.world.item.slot.SlotCollection;
15
import net.minecraft.world.item.slot.SlotSource;
16
import org.jspecify.annotations.Nullable;
17
18
public record BlockItemAccessor(BlockPos pos) implements ItemAccessor<BlockPos> {
19
	public static final ArgProvider.Factory<ItemAccessor<?>> PROVIDER = arg -> ArgProvider.create(
20
		"block", () -> Commands.argument(arg, BlockPosArgument.blockPos()), c -> new BlockItemAccessor(BlockPosArgument.getLoadedBlockPos(c, arg))
21
	);
22
	private static final CommandResponseTracker.Messages<BlockPos> RESPONSE_SET = CommandResponseTracker.messages(
23
		ItemCommands.ERROR_TARGET_NO_CHANGES,
24
		(pos, slotCount) -> Component.translatable("commands.item.block.replace.success", slotCount, pos.getX(), pos.getY(), pos.getZ()),
25
		(var0, var1) -> Component.empty()
26
	);
27
	private static final CommandResponseTracker.MessagesWithArg<BlockPos, ItemStack> RESPONSE_SET_KNOWN_ITEM = CommandResponseTracker.messages(
28
		ItemCommands.ERROR_TARGET_NO_CHANGES_KNOWN_ITEM::create,
29
		(pos, slotCount, itemStack) -> Component.translatable(
30
			"commands.item.block.replace.success.known_item", slotCount, pos.getX(), pos.getY(), pos.getZ(), itemStack.getDisplayName()
31
		),
32
		(var0, var1, var2) -> Component.empty()
33
	);
34
	private static final CommandResponseTracker.Messages<BlockPos> RESPONSE_MODIFY = CommandResponseTracker.messages(
35
		ItemCommands.ERROR_TARGET_NO_CHANGES,
36
		(pos, slotCount) -> Component.translatable("commands.item.block.modify.success", slotCount, pos.getX(), pos.getY(), pos.getZ()),
37
		(var0, var1) -> Component.empty()
38
	);
39
40
	@Override
41
	public SlotCollection getSlots(final CommandSourceStack source, final SlotSource slotSource) throws CommandSyntaxException {
42
		return this.getBlockSlots(source, slotSource, ItemCommands.ERROR_SOURCE_NOT_A_CONTAINER);
43
	}
44
45
	private SlotCollection getBlockSlots(final CommandSourceStack source, final SlotSource slotSource, final Dynamic3CommandExceptionType exceptionType) throws CommandSyntaxException {
46
		Container container = getContainer(source, this.pos, exceptionType);
47
		return ItemCommands.getSlotsFromProvider(source, container, slotSource);
48
	}
49
50
	@Override
51
	public void setItems(final CommandSourceStack source, final SlotSource slotSource, final ItemAccessor.SetterFunction<BlockPos> function) throws CommandSyntaxException {
52
		SlotCollection targetSlots = this.getBlockSlots(source, slotSource, ItemCommands.ERROR_TARGET_NOT_A_CONTAINER);
53
		function.apply(this.pos, targetSlots);
54
	}
55
56
	@Override
57
	public int getReplaceSuccess(final CommandSourceStack source, final CommandResponseTracker<BlockPos> tracker, final @Nullable ItemStack knownItem) throws CommandSyntaxException {
58
		return knownItem != null ? tracker.sendFeedback(source, true, RESPONSE_SET_KNOWN_ITEM, knownItem) : tracker.sendFeedback(source, true, RESPONSE_SET);
59
	}
60
61
	@Override
62
	public int getModifySuccess(final CommandSourceStack source, final CommandResponseTracker<BlockPos> tracker) throws CommandSyntaxException {
63
		return tracker.sendFeedback(source, true, RESPONSE_MODIFY);
64
	}
65
66
	public static Container getContainer(final CommandSourceStack source, final BlockPos pos, final Dynamic3CommandExceptionType exceptionType) throws CommandSyntaxException {
67
		if (source.getLevel().getBlockEntity(pos) instanceof Container container) {
68
			return container;
69
		} else {
70
			throw exceptionType.create(pos.getX(), pos.getY(), pos.getZ());
71
		}
72
	}
73
}
74

Decompiled Minecraft 26.3 source of BlockItemAccessor (net/minecraft/server/commands/item/BlockItemAccessor.java) with Mojang official mappings, stored verbatim on the site.