ItemCommands.java

net.minecraft.server.commands.ItemCommands

信息

  • 全限定名:net.minecraft.server.commands.ItemCommands
  • 类型:public class
  • 包:net.minecraft.server.commands
  • 源码路径:src/main/java/net/minecraft/server/commands/ItemCommands.java
  • 起始行号:L40
  • 职责:

    TODO

字段/常量

  • ERROR_TARGET_NOT_A_CONTAINER

    • 类型: Dynamic3CommandExceptionType
    • 修饰符: static final
    • 源码定位: L41
    • 说明:

      TODO

  • ERROR_SOURCE_NOT_A_CONTAINER

    • 类型: Dynamic3CommandExceptionType
    • 修饰符: static final
    • 源码定位: L44
    • 说明:

      TODO

  • ERROR_TARGET_INAPPLICABLE_SLOT

    • 类型: DynamicCommandExceptionType
    • 修饰符: static final
    • 源码定位: L47
    • 说明:

      TODO

  • ERROR_SOURCE_INAPPLICABLE_SLOT

    • 类型: DynamicCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L50
    • 说明:

      TODO

  • ERROR_TARGET_NO_CHANGES

    • 类型: DynamicCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L53
    • 说明:

      TODO

  • ERROR_TARGET_NO_CHANGES_KNOWN_ITEM

    • 类型: Dynamic2CommandExceptionType
    • 修饰符: private static final
    • 源码定位: L56
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) @ L60

  • 方法名:register
  • 源码定位:L60
  • 返回类型:void
  • 修饰符:public static

参数:

  • dispatcher: CommandDispatcher
  • context: CommandBuildContext

说明:

TODO

private static int modifyBlockItem(CommandSourceStack source, BlockPos pos, int slot, Holder<LootItemFunction> modifier) @ L313

  • 方法名:modifyBlockItem
  • 源码定位:L313
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • pos: BlockPos
  • slot: int
  • modifier: Holder

说明:

TODO

private static int modifyEntityItem(CommandSourceStack source, Collection<?extends Entity> entities, int slot, Holder<LootItemFunction> modifier) @ L327

  • 方法名:modifyEntityItem
  • 源码定位:L327
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • entities: Collection<?extends Entity>
  • slot: int
  • modifier: Holder

说明:

TODO

private static int setBlockItem(CommandSourceStack source, BlockPos pos, int slot, ItemStack itemStack) @ L359

  • 方法名:setBlockItem
  • 源码定位:L359
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • pos: BlockPos
  • slot: int
  • itemStack: ItemStack

说明:

TODO

static Container getContainer(CommandSourceStack source, BlockPos pos, Dynamic3CommandExceptionType exceptionType) @ L372

  • 方法名:getContainer
  • 源码定位:L372
  • 返回类型:Container
  • 修饰符:static

参数:

  • source: CommandSourceStack
  • pos: BlockPos
  • exceptionType: Dynamic3CommandExceptionType

说明:

TODO

private static int setEntityItem(CommandSourceStack source, Collection<?extends Entity> entities, int slot, ItemStack itemStack) @ L380

  • 方法名:setEntityItem
  • 源码定位:L380
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • entities: Collection<?extends Entity>
  • slot: int
  • itemStack: ItemStack

说明:

TODO

private static int blockToEntities(CommandSourceStack source, BlockPos sourcePos, int sourceSlot, Collection<?extends Entity> targetEntities, int targetSlot) @ L413

  • 方法名:blockToEntities
  • 源码定位:L413
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • sourcePos: BlockPos
  • sourceSlot: int
  • targetEntities: Collection<?extends Entity>
  • targetSlot: int

说明:

TODO

private static int blockToEntities(CommandSourceStack source, BlockPos sourcePos, int sourceSlot, Collection<?extends Entity> targetEntities, int targetSlot, Holder<LootItemFunction> modifier) @ L419

  • 方法名:blockToEntities
  • 源码定位:L419
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • sourcePos: BlockPos
  • sourceSlot: int
  • targetEntities: Collection<?extends Entity>
  • targetSlot: int
  • modifier: Holder

说明:

TODO

private static int blockToBlock(CommandSourceStack source, BlockPos sourcePos, int sourceSlot, BlockPos targetPos, int targetSlot) @ L430

  • 方法名:blockToBlock
  • 源码定位:L430
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • sourcePos: BlockPos
  • sourceSlot: int
  • targetPos: BlockPos
  • targetSlot: int

说明:

TODO

private static int blockToBlock(CommandSourceStack source, BlockPos sourcePos, int sourceSlot, BlockPos targetPos, int targetSlot, Holder<LootItemFunction> modifier) @ L434

  • 方法名:blockToBlock
  • 源码定位:L434
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • sourcePos: BlockPos
  • sourceSlot: int
  • targetPos: BlockPos
  • targetSlot: int
  • modifier: Holder

说明:

TODO

private static int entityToBlock(CommandSourceStack source, Entity sourceEntity, int sourceSlot, BlockPos targetPos, int targetSlot) @ L440

  • 方法名:entityToBlock
  • 源码定位:L440
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • sourceEntity: Entity
  • sourceSlot: int
  • targetPos: BlockPos
  • targetSlot: int

说明:

TODO

private static int entityToBlock(CommandSourceStack source, Entity sourceEntity, int sourceSlot, BlockPos targetPos, int targetSlot, Holder<LootItemFunction> modifier) @ L444

  • 方法名:entityToBlock
  • 源码定位:L444
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • sourceEntity: Entity
  • sourceSlot: int
  • targetPos: BlockPos
  • targetSlot: int
  • modifier: Holder

说明:

TODO

private static int entityToEntities(CommandSourceStack source, Entity sourceEntity, int sourceSlot, Collection<?extends Entity> targetEntities, int targetSlot) @ L450

  • 方法名:entityToEntities
  • 源码定位:L450
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • sourceEntity: Entity
  • sourceSlot: int
  • targetEntities: Collection<?extends Entity>
  • targetSlot: int

说明:

TODO

private static int entityToEntities(CommandSourceStack source, Entity sourceEntity, int sourceSlot, Collection<?extends Entity> targetEntities, int targetSlot, Holder<LootItemFunction> modifier) @ L456

  • 方法名:entityToEntities
  • 源码定位:L456
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • sourceEntity: Entity
  • sourceSlot: int
  • targetEntities: Collection<?extends Entity>
  • targetSlot: int
  • modifier: Holder

说明:

TODO

private static ItemStack applyModifier(CommandSourceStack source, Holder<LootItemFunction> modifier, ItemStack item) @ L467

  • 方法名:applyModifier
  • 源码定位:L467
  • 返回类型:ItemStack
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • modifier: Holder
  • item: ItemStack

说明:

TODO

private static ItemStack getItemInSlot(SlotProvider slotProvider, int slot) @ L480

  • 方法名:getItemInSlot
  • 源码定位:L480
  • 返回类型:ItemStack
  • 修饰符:private static

参数:

  • slotProvider: SlotProvider
  • slot: int

说明:

TODO

private static ItemStack getBlockItem(CommandSourceStack source, BlockPos pos, int slot) @ L489

  • 方法名:getBlockItem
  • 源码定位:L489
  • 返回类型:ItemStack
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • pos: BlockPos
  • slot: int

说明:

TODO

代码

public class ItemCommands {
    static final Dynamic3CommandExceptionType ERROR_TARGET_NOT_A_CONTAINER = new Dynamic3CommandExceptionType(
        (x, y, z) -> Component.translatableEscape("commands.item.target.not_a_container", x, y, z)
    );
    static final Dynamic3CommandExceptionType ERROR_SOURCE_NOT_A_CONTAINER = new Dynamic3CommandExceptionType(
        (x, y, z) -> Component.translatableEscape("commands.item.source.not_a_container", x, y, z)
    );
    static final DynamicCommandExceptionType ERROR_TARGET_INAPPLICABLE_SLOT = new DynamicCommandExceptionType(
        slot -> Component.translatableEscape("commands.item.target.no_such_slot", slot)
    );
    private static final DynamicCommandExceptionType ERROR_SOURCE_INAPPLICABLE_SLOT = new DynamicCommandExceptionType(
        slot -> Component.translatableEscape("commands.item.source.no_such_slot", slot)
    );
    private static final DynamicCommandExceptionType ERROR_TARGET_NO_CHANGES = new DynamicCommandExceptionType(
        slot -> Component.translatableEscape("commands.item.target.no_changes", slot)
    );
    private static final Dynamic2CommandExceptionType ERROR_TARGET_NO_CHANGES_KNOWN_ITEM = new Dynamic2CommandExceptionType(
        (item, slot) -> Component.translatableEscape("commands.item.target.no_changed.known_item", item, slot)
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
        dispatcher.register(
            Commands.literal("item")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .then(
                    Commands.literal("replace")
                        .then(
                            Commands.literal("block")
                                .then(
                                    Commands.argument("pos", BlockPosArgument.blockPos())
                                        .then(
                                            Commands.argument("slot", SlotArgument.slot())
                                                .then(
                                                    Commands.literal("with")
                                                        .then(
                                                            Commands.argument("item", ItemArgument.item(context))
                                                                .executes(
                                                                    c -> setBlockItem(
                                                                        c.getSource(),
                                                                        BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                                                        SlotArgument.getSlot(c, "slot"),
                                                                        ItemArgument.getItem(c, "item").createItemStack(1)
                                                                    )
                                                                )
                                                                .then(
                                                                    Commands.argument("count", IntegerArgumentType.integer(1, 99))
                                                                        .executes(
                                                                            c -> setBlockItem(
                                                                                c.getSource(),
                                                                                BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                                                                SlotArgument.getSlot(c, "slot"),
                                                                                ItemArgument.getItem(c, "item")
                                                                                    .createItemStack(IntegerArgumentType.getInteger(c, "count"))
                                                                            )
                                                                        )
                                                                )
                                                        )
                                                )
                                                .then(
                                                    Commands.literal("from")
                                                        .then(
                                                            Commands.literal("block")
                                                                .then(
                                                                    Commands.argument("source", BlockPosArgument.blockPos())
                                                                        .then(
                                                                            Commands.argument("sourceSlot", SlotArgument.slot())
                                                                                .executes(
                                                                                    c -> blockToBlock(
                                                                                        c.getSource(),
                                                                                        BlockPosArgument.getLoadedBlockPos(c, "source"),
                                                                                        SlotArgument.getSlot(c, "sourceSlot"),
                                                                                        BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                                                                        SlotArgument.getSlot(c, "slot")
                                                                                    )
                                                                                )
                                                                                .then(
                                                                                    Commands.argument("modifier", ResourceOrIdArgument.lootModifier(context))
                                                                                        .executes(
                                                                                            c -> blockToBlock(
                                                                                                (CommandSourceStack)c.getSource(),
                                                                                                BlockPosArgument.getLoadedBlockPos(c, "source"),
                                                                                                SlotArgument.getSlot(c, "sourceSlot"),
                                                                                                BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                                                                                SlotArgument.getSlot(c, "slot"),
                                                                                                ResourceOrIdArgument.getLootModifier(c, "modifier")
                                                                                            )
                                                                                        )
                                                                                )
                                                                        )
                                                                )
                                                        )
                                                        .then(
                                                            Commands.literal("entity")
                                                                .then(
                                                                    Commands.argument("source", EntityArgument.entity())
                                                                        .then(
                                                                            Commands.argument("sourceSlot", SlotArgument.slot())
                                                                                .executes(
                                                                                    c -> entityToBlock(
                                                                                        c.getSource(),
                                                                                        EntityArgument.getEntity(c, "source"),
                                                                                        SlotArgument.getSlot(c, "sourceSlot"),
                                                                                        BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                                                                        SlotArgument.getSlot(c, "slot")
                                                                                    )
                                                                                )
                                                                                .then(
                                                                                    Commands.argument("modifier", ResourceOrIdArgument.lootModifier(context))
                                                                                        .executes(
                                                                                            c -> entityToBlock(
                                                                                                (CommandSourceStack)c.getSource(),
                                                                                                EntityArgument.getEntity(c, "source"),
                                                                                                SlotArgument.getSlot(c, "sourceSlot"),
                                                                                                BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                                                                                SlotArgument.getSlot(c, "slot"),
                                                                                                ResourceOrIdArgument.getLootModifier(c, "modifier")
                                                                                            )
                                                                                        )
                                                                                )
                                                                        )
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                        .then(
                            Commands.literal("entity")
                                .then(
                                    Commands.argument("targets", EntityArgument.entities())
                                        .then(
                                            Commands.argument("slot", SlotArgument.slot())
                                                .then(
                                                    Commands.literal("with")
                                                        .then(
                                                            Commands.argument("item", ItemArgument.item(context))
                                                                .executes(
                                                                    c -> setEntityItem(
                                                                        c.getSource(),
                                                                        EntityArgument.getEntities(c, "targets"),
                                                                        SlotArgument.getSlot(c, "slot"),
                                                                        ItemArgument.getItem(c, "item").createItemStack(1)
                                                                    )
                                                                )
                                                                .then(
                                                                    Commands.argument("count", IntegerArgumentType.integer(1, 99))
                                                                        .executes(
                                                                            c -> setEntityItem(
                                                                                c.getSource(),
                                                                                EntityArgument.getEntities(c, "targets"),
                                                                                SlotArgument.getSlot(c, "slot"),
                                                                                ItemArgument.getItem(c, "item")
                                                                                    .createItemStack(IntegerArgumentType.getInteger(c, "count"))
                                                                            )
                                                                        )
                                                                )
                                                        )
                                                )
                                                .then(
                                                    Commands.literal("from")
                                                        .then(
                                                            Commands.literal("block")
                                                                .then(
                                                                    Commands.argument("source", BlockPosArgument.blockPos())
                                                                        .then(
                                                                            Commands.argument("sourceSlot", SlotArgument.slot())
                                                                                .executes(
                                                                                    c -> blockToEntities(
                                                                                        c.getSource(),
                                                                                        BlockPosArgument.getLoadedBlockPos(c, "source"),
                                                                                        SlotArgument.getSlot(c, "sourceSlot"),
                                                                                        EntityArgument.getEntities(c, "targets"),
                                                                                        SlotArgument.getSlot(c, "slot")
                                                                                    )
                                                                                )
                                                                                .then(
                                                                                    Commands.argument("modifier", ResourceOrIdArgument.lootModifier(context))
                                                                                        .executes(
                                                                                            c -> blockToEntities(
                                                                                                (CommandSourceStack)c.getSource(),
                                                                                                BlockPosArgument.getLoadedBlockPos(c, "source"),
                                                                                                SlotArgument.getSlot(c, "sourceSlot"),
                                                                                                EntityArgument.getEntities(c, "targets"),
                                                                                                SlotArgument.getSlot(c, "slot"),
                                                                                                ResourceOrIdArgument.getLootModifier(c, "modifier")
                                                                                            )
                                                                                        )
                                                                                )
                                                                        )
                                                                )
                                                        )
                                                        .then(
                                                            Commands.literal("entity")
                                                                .then(
                                                                    Commands.argument("source", EntityArgument.entity())
                                                                        .then(
                                                                            Commands.argument("sourceSlot", SlotArgument.slot())
                                                                                .executes(
                                                                                    c -> entityToEntities(
                                                                                        c.getSource(),
                                                                                        EntityArgument.getEntity(c, "source"),
                                                                                        SlotArgument.getSlot(c, "sourceSlot"),
                                                                                        EntityArgument.getEntities(c, "targets"),
                                                                                        SlotArgument.getSlot(c, "slot")
                                                                                    )
                                                                                )
                                                                                .then(
                                                                                    Commands.argument("modifier", ResourceOrIdArgument.lootModifier(context))
                                                                                        .executes(
                                                                                            c -> entityToEntities(
                                                                                                (CommandSourceStack)c.getSource(),
                                                                                                EntityArgument.getEntity(c, "source"),
                                                                                                SlotArgument.getSlot(c, "sourceSlot"),
                                                                                                EntityArgument.getEntities(c, "targets"),
                                                                                                SlotArgument.getSlot(c, "slot"),
                                                                                                ResourceOrIdArgument.getLootModifier(c, "modifier")
                                                                                            )
                                                                                        )
                                                                                )
                                                                        )
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                )
                .then(
                    Commands.literal("modify")
                        .then(
                            Commands.literal("block")
                                .then(
                                    Commands.argument("pos", BlockPosArgument.blockPos())
                                        .then(
                                            Commands.argument("slot", SlotArgument.slot())
                                                .then(
                                                    Commands.argument("modifier", ResourceOrIdArgument.lootModifier(context))
                                                        .executes(
                                                            c -> modifyBlockItem(
                                                                (CommandSourceStack)c.getSource(),
                                                                BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                                                SlotArgument.getSlot(c, "slot"),
                                                                ResourceOrIdArgument.getLootModifier(c, "modifier")
                                                            )
                                                        )
                                                )
                                        )
                                )
                        )
                        .then(
                            Commands.literal("entity")
                                .then(
                                    Commands.argument("targets", EntityArgument.entities())
                                        .then(
                                            Commands.argument("slot", SlotArgument.slot())
                                                .then(
                                                    Commands.argument("modifier", ResourceOrIdArgument.lootModifier(context))
                                                        .executes(
                                                            c -> modifyEntityItem(
                                                                (CommandSourceStack)c.getSource(),
                                                                EntityArgument.getEntities(c, "targets"),
                                                                SlotArgument.getSlot(c, "slot"),
                                                                ResourceOrIdArgument.getLootModifier(c, "modifier")
                                                            )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        );
    }
 
    private static int modifyBlockItem(CommandSourceStack source, BlockPos pos, int slot, Holder<LootItemFunction> modifier) throws CommandSyntaxException {
        Container container = getContainer(source, pos, ERROR_TARGET_NOT_A_CONTAINER);
        if (slot >= 0 && slot < container.getContainerSize()) {
            ItemStack itemStack = applyModifier(source, modifier, container.getItem(slot));
            container.setItem(slot, itemStack);
            source.sendSuccess(
                () -> Component.translatable("commands.item.block.set.success", pos.getX(), pos.getY(), pos.getZ(), itemStack.getDisplayName()), true
            );
            return 1;
        } else {
            throw ERROR_TARGET_INAPPLICABLE_SLOT.create(slot);
        }
    }
 
    private static int modifyEntityItem(CommandSourceStack source, Collection<? extends Entity> entities, int slot, Holder<LootItemFunction> modifier) throws CommandSyntaxException {
        Map<Entity, ItemStack> changedEntities = Maps.newHashMapWithExpectedSize(entities.size());
 
        for (Entity entity : entities) {
            SlotAccess slotAccess = entity.getSlot(slot);
            if (slotAccess != null) {
                ItemStack itemStack = applyModifier(source, modifier, slotAccess.get().copy());
                if (slotAccess.set(itemStack)) {
                    changedEntities.put(entity, itemStack);
                    if (entity instanceof ServerPlayer serverPlayer) {
                        serverPlayer.containerMenu.broadcastChanges();
                    }
                }
            }
        }
 
        if (changedEntities.isEmpty()) {
            throw ERROR_TARGET_NO_CHANGES.create(slot);
        } else {
            if (changedEntities.size() == 1) {
                Entry<Entity, ItemStack> e = changedEntities.entrySet().iterator().next();
                source.sendSuccess(
                    () -> Component.translatable("commands.item.entity.set.success.single", e.getKey().getDisplayName(), e.getValue().getDisplayName()), true
                );
            } else {
                source.sendSuccess(() -> Component.translatable("commands.item.entity.set.success.multiple", changedEntities.size()), true);
            }
 
            return changedEntities.size();
        }
    }
 
    private static int setBlockItem(CommandSourceStack source, BlockPos pos, int slot, ItemStack itemStack) throws CommandSyntaxException {
        Container container = getContainer(source, pos, ERROR_TARGET_NOT_A_CONTAINER);
        if (slot >= 0 && slot < container.getContainerSize()) {
            container.setItem(slot, itemStack);
            source.sendSuccess(
                () -> Component.translatable("commands.item.block.set.success", pos.getX(), pos.getY(), pos.getZ(), itemStack.getDisplayName()), true
            );
            return 1;
        } else {
            throw ERROR_TARGET_INAPPLICABLE_SLOT.create(slot);
        }
    }
 
    static Container getContainer(CommandSourceStack source, BlockPos pos, Dynamic3CommandExceptionType exceptionType) throws CommandSyntaxException {
        if (source.getLevel().getBlockEntity(pos) instanceof Container container) {
            return container;
        } else {
            throw exceptionType.create(pos.getX(), pos.getY(), pos.getZ());
        }
    }
 
    private static int setEntityItem(CommandSourceStack source, Collection<? extends Entity> entities, int slot, ItemStack itemStack) throws CommandSyntaxException {
        List<Entity> changedEntities = Lists.newArrayListWithCapacity(entities.size());
 
        for (Entity entity : entities) {
            SlotAccess slotAccess = entity.getSlot(slot);
            if (slotAccess != null && slotAccess.set(itemStack.copy())) {
                changedEntities.add(entity);
                if (entity instanceof ServerPlayer serverPlayer) {
                    serverPlayer.containerMenu.broadcastChanges();
                }
            }
        }
 
        if (changedEntities.isEmpty()) {
            throw ERROR_TARGET_NO_CHANGES_KNOWN_ITEM.create(itemStack.getDisplayName(), slot);
        } else {
            if (changedEntities.size() == 1) {
                source.sendSuccess(
                    () -> Component.translatable(
                        "commands.item.entity.set.success.single", changedEntities.getFirst().getDisplayName(), itemStack.getDisplayName()
                    ),
                    true
                );
            } else {
                source.sendSuccess(
                    () -> Component.translatable("commands.item.entity.set.success.multiple", changedEntities.size(), itemStack.getDisplayName()), true
                );
            }
 
            return changedEntities.size();
        }
    }
 
    private static int blockToEntities(
        CommandSourceStack source, BlockPos sourcePos, int sourceSlot, Collection<? extends Entity> targetEntities, int targetSlot
    ) throws CommandSyntaxException {
        return setEntityItem(source, targetEntities, targetSlot, getBlockItem(source, sourcePos, sourceSlot));
    }
 
    private static int blockToEntities(
        CommandSourceStack source,
        BlockPos sourcePos,
        int sourceSlot,
        Collection<? extends Entity> targetEntities,
        int targetSlot,
        Holder<LootItemFunction> modifier
    ) throws CommandSyntaxException {
        return setEntityItem(source, targetEntities, targetSlot, applyModifier(source, modifier, getBlockItem(source, sourcePos, sourceSlot)));
    }
 
    private static int blockToBlock(CommandSourceStack source, BlockPos sourcePos, int sourceSlot, BlockPos targetPos, int targetSlot) throws CommandSyntaxException {
        return setBlockItem(source, targetPos, targetSlot, getBlockItem(source, sourcePos, sourceSlot));
    }
 
    private static int blockToBlock(
        CommandSourceStack source, BlockPos sourcePos, int sourceSlot, BlockPos targetPos, int targetSlot, Holder<LootItemFunction> modifier
    ) throws CommandSyntaxException {
        return setBlockItem(source, targetPos, targetSlot, applyModifier(source, modifier, getBlockItem(source, sourcePos, sourceSlot)));
    }
 
    private static int entityToBlock(CommandSourceStack source, Entity sourceEntity, int sourceSlot, BlockPos targetPos, int targetSlot) throws CommandSyntaxException {
        return setBlockItem(source, targetPos, targetSlot, getItemInSlot(sourceEntity, sourceSlot));
    }
 
    private static int entityToBlock(
        CommandSourceStack source, Entity sourceEntity, int sourceSlot, BlockPos targetPos, int targetSlot, Holder<LootItemFunction> modifier
    ) throws CommandSyntaxException {
        return setBlockItem(source, targetPos, targetSlot, applyModifier(source, modifier, getItemInSlot(sourceEntity, sourceSlot)));
    }
 
    private static int entityToEntities(
        CommandSourceStack source, Entity sourceEntity, int sourceSlot, Collection<? extends Entity> targetEntities, int targetSlot
    ) throws CommandSyntaxException {
        return setEntityItem(source, targetEntities, targetSlot, getItemInSlot(sourceEntity, sourceSlot));
    }
 
    private static int entityToEntities(
        CommandSourceStack source,
        Entity sourceEntity,
        int sourceSlot,
        Collection<? extends Entity> targetEntities,
        int targetSlot,
        Holder<LootItemFunction> modifier
    ) throws CommandSyntaxException {
        return setEntityItem(source, targetEntities, targetSlot, applyModifier(source, modifier, getItemInSlot(sourceEntity, sourceSlot)));
    }
 
    private static ItemStack applyModifier(CommandSourceStack source, Holder<LootItemFunction> modifier, ItemStack item) {
        ServerLevel level = source.getLevel();
        LootParams lootParams = new LootParams.Builder(level)
            .withParameter(LootContextParams.ORIGIN, source.getPosition())
            .withOptionalParameter(LootContextParams.THIS_ENTITY, source.getEntity())
            .create(LootContextParamSets.COMMAND);
        LootContext context = new LootContext.Builder(lootParams).create(Optional.empty());
        context.pushVisitedElement(LootContext.createVisitedEntry(modifier.value()));
        ItemStack newItem = modifier.value().apply(item, context);
        newItem.limitSize(newItem.getMaxStackSize());
        return newItem;
    }
 
    private static ItemStack getItemInSlot(SlotProvider slotProvider, int slot) throws CommandSyntaxException {
        SlotAccess slotAccess = slotProvider.getSlot(slot);
        if (slotAccess == null) {
            throw ERROR_SOURCE_INAPPLICABLE_SLOT.create(slot);
        } else {
            return slotAccess.get().copy();
        }
    }
 
    private static ItemStack getBlockItem(CommandSourceStack source, BlockPos pos, int slot) throws CommandSyntaxException {
        Container container = getContainer(source, pos, ERROR_SOURCE_NOT_A_CONTAINER);
        return getItemInSlot(container, slot);
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数
  • Commands

    • 引用位置: 方法调用
    • 关联成员: Commands.argument(), Commands.hasPermission(), Commands.literal()
  • EntityArgument

    • 引用位置: 方法调用
    • 关联成员: EntityArgument.entities(), EntityArgument.entity(), EntityArgument.getEntities(), EntityArgument.getEntity()
  • ResourceOrIdArgument

    • 引用位置: 方法调用
    • 关联成员: ResourceOrIdArgument.getLootModifier(), ResourceOrIdArgument.lootModifier()
  • SlotArgument

    • 引用位置: 方法调用
    • 关联成员: SlotArgument.getSlot(), SlotArgument.slot()
  • BlockPosArgument

    • 引用位置: 方法调用
    • 关联成员: BlockPosArgument.blockPos(), BlockPosArgument.getLoadedBlockPos()
  • ItemArgument

    • 引用位置: 方法调用
    • 关联成员: ItemArgument.getItem(), ItemArgument.item()
  • BlockPos

    • 引用位置: 参数
  • Holder

    • 引用位置: 参数
  • Component

    • 引用位置: 方法调用
    • 关联成员: Component.translatable(), Component.translatableEscape()
  • Container

    • 引用位置: 返回值
  • Entity

    • 引用位置: 参数
  • SlotProvider

    • 引用位置: 参数
  • ItemStack

    • 引用位置: 参数/返回值
  • LootContext

    • 引用位置: 方法调用/构造调用
    • 关联成员: Builder(), LootContext.Builder(), LootContext.createVisitedEntry()
  • LootParams

    • 引用位置: 方法调用/构造调用
    • 关联成员: Builder(), LootParams.Builder()
  • LootItemFunction

    • 引用位置: 参数