EntityItemAccessor .java

net.minecraft.server.commands.item.EntityItemAccessor (record): members: entities, RESPONSE_SET_KNOWN_ITEM, RESPONSE_MODIFY, getReplaceSuccess, PROVIDER.

net.minecraft.server.commands.itemrecord75 linesserverextends Entity>3.8 KB

Implements

API Surface (4)

SlotCollection getSlots(final CommandSourceStack source, final SlotSource slotSource)
void setItems(final CommandSourceStack source, final SlotSource slotSource, final ItemAccessor.SetterFunction<Entity> function)
int getReplaceSuccess(final CommandSourceStack source, final CommandResponseTracker<Entity> tracker, final @Nullable ItemStack knownItem)
int getModifySuccess(final CommandSourceStack source, final CommandResponseTracker<Entity> tracker)

Fields & Constants (4)

PROVIDER
RESPONSE_SET
RESPONSE_SET_KNOWN_ITEM
RESPONSE_MODIFY

String literals (5)

"entity"
"commands.item.entity.replace.success.single"
"commands.item.entity.replace.success.multiple"
"commands.item.entity.modify.success.single"
"commands.item.entity.modify.success.multiple"

Full Source

EntityItemAccessor.java75 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands.item;
2
3
import com.mojang.brigadier.exceptions.CommandSyntaxException;
4
import java.util.ArrayList;
5
import java.util.Collection;
6
import java.util.List;
7
import net.minecraft.commands.CommandSourceStack;
8
import net.minecraft.commands.Commands;
9
import net.minecraft.commands.arguments.EntityArgument;
10
import net.minecraft.network.chat.Component;
11
import net.minecraft.server.commands.ArgProvider;
12
import net.minecraft.server.commands.CommandResponseTracker;
13
import net.minecraft.server.level.ServerPlayer;
14
import net.minecraft.world.entity.Entity;
15
import net.minecraft.world.item.ItemStack;
16
import net.minecraft.world.item.slot.SlotCollection;
17
import net.minecraft.world.item.slot.SlotSource;
18
import org.jspecify.annotations.Nullable;
19
20
public record EntityItemAccessor(Collection<? extends Entity> entities) implements ItemAccessor<Entity> {
21
	public static final ArgProvider.Factory<ItemAccessor<?>> PROVIDER = arg -> ArgProvider.create(
22
		"entity", () -> Commands.argument(arg, EntityArgument.entities()), c -> new EntityItemAccessor(EntityArgument.getEntities(c, arg))
23
	);
24
	private static final CommandResponseTracker.Messages<Entity> RESPONSE_SET = CommandResponseTracker.messages(
25
		ItemCommands.ERROR_TARGET_NO_CHANGES,
26
		(entity, slotCount) -> Component.translatable("commands.item.entity.replace.success.single", slotCount, entity.getDisplayName()),
27
		(entityCount, var1) -> Component.translatable("commands.item.entity.replace.success.multiple", entityCount)
28
	);
29
	private static final CommandResponseTracker.MessagesWithArg<Entity, ItemStack> RESPONSE_SET_KNOWN_ITEM = CommandResponseTracker.messages(
30
		ItemCommands.ERROR_TARGET_NO_CHANGES_KNOWN_ITEM::create,
31
		(entity, slotCount, itemStack) -> Component.translatable(
32
			"commands.item.entity.replace.success.single.known_item", slotCount, entity.getDisplayName(), itemStack.getDisplayName()
33
		),
34
		(entityCount, var1, itemStack) -> Component.translatable("commands.item.entity.replace.success.multiple.known_item", entityCount, itemStack.getDisplayName())
35
	);
36
	private static final CommandResponseTracker.Messages<Entity> RESPONSE_MODIFY = CommandResponseTracker.messages(
37
		ItemCommands.ERROR_TARGET_NO_CHANGES,
38
		(entity, slotCount) -> Component.translatable("commands.item.entity.modify.success.single", slotCount, entity.getDisplayName()),
39
		(entityCount, var1) -> Component.translatable("commands.item.entity.modify.success.multiple", entityCount)
40
	);
41
42
	@Override
43
	public SlotCollection getSlots(final CommandSourceStack source, final SlotSource slotSource) {
44
		List<SlotCollection> slotCollections = new ArrayList<>();
45
46
		for (Entity entity : this.entities) {
47
			SlotCollection slots = ItemCommands.getSlotsFromProvider(source, entity, slotSource);
48
			slotCollections.add(slots);
49
		}
50
51
		return SlotCollection.concat(slotCollections);
52
	}
53
54
	@Override
55
	public void setItems(final CommandSourceStack source, final SlotSource slotSource, final ItemAccessor.SetterFunction<Entity> function) {
56
		for (Entity entity : this.entities) {
57
			SlotCollection targetSlots = ItemCommands.getSlotsFromProvider(source, entity, slotSource);
58
			int updatedSlots = function.apply(entity, targetSlots);
59
			if (updatedSlots > 0 && entity instanceof ServerPlayer serverPlayer) {
60
				serverPlayer.containerMenu.broadcastChanges();
61
			}
62
		}
63
	}
64
65
	@Override
66
	public int getReplaceSuccess(final CommandSourceStack source, final CommandResponseTracker<Entity> tracker, final @Nullable ItemStack knownItem) throws CommandSyntaxException {
67
		return knownItem != null ? tracker.sendFeedback(source, true, RESPONSE_SET_KNOWN_ITEM, knownItem) : tracker.sendFeedback(source, true, RESPONSE_SET);
68
	}
69
70
	@Override
71
	public int getModifySuccess(final CommandSourceStack source, final CommandResponseTracker<Entity> tracker) throws CommandSyntaxException {
72
		return tracker.sendFeedback(source, true, RESPONSE_MODIFY);
73
	}
74
}
75

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