ClearInventoryCommands.java

net.minecraft.server.commands.ClearInventoryCommands

信息

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

    TODO

字段/常量

  • ERROR_SINGLE

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

      TODO

  • ERROR_MULTIPLE

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

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher
  • context: CommandBuildContext

说明:

TODO

private static int clearUnlimited(CommandSourceStack source, Collection<ServerPlayer> players, Predicate<ItemStack> predicate) @ L58

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

参数:

  • source: CommandSourceStack
  • players: Collection
  • predicate: Predicate

说明:

TODO

private static int clearInventory(CommandSourceStack source, Collection<ServerPlayer> players, Predicate<ItemStack> predicate, int maxCount) @ L62

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

参数:

  • source: CommandSourceStack
  • players: Collection
  • predicate: Predicate
  • maxCount: int

说明:

TODO

代码

public class ClearInventoryCommands {
    private static final DynamicCommandExceptionType ERROR_SINGLE = new DynamicCommandExceptionType(
        name -> Component.translatableEscape("clear.failed.single", name)
    );
    private static final DynamicCommandExceptionType ERROR_MULTIPLE = new DynamicCommandExceptionType(
        count -> Component.translatableEscape("clear.failed.multiple", count)
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
        dispatcher.register(
            Commands.literal("clear")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .executes(c -> clearUnlimited(c.getSource(), Collections.singleton(c.getSource().getPlayerOrException()), i -> true))
                .then(
                    Commands.argument("targets", EntityArgument.players())
                        .executes(c -> clearUnlimited(c.getSource(), EntityArgument.getPlayers(c, "targets"), i -> true))
                        .then(
                            Commands.argument("item", ItemPredicateArgument.itemPredicate(context))
                                .executes(
                                    c -> clearUnlimited(
                                        c.getSource(), EntityArgument.getPlayers(c, "targets"), ItemPredicateArgument.getItemPredicate(c, "item")
                                    )
                                )
                                .then(
                                    Commands.argument("maxCount", IntegerArgumentType.integer(0))
                                        .executes(
                                            c -> clearInventory(
                                                c.getSource(),
                                                EntityArgument.getPlayers(c, "targets"),
                                                ItemPredicateArgument.getItemPredicate(c, "item"),
                                                IntegerArgumentType.getInteger(c, "maxCount")
                                            )
                                        )
                                )
                        )
                )
        );
    }
 
    private static int clearUnlimited(CommandSourceStack source, Collection<ServerPlayer> players, Predicate<ItemStack> predicate) throws CommandSyntaxException {
        return clearInventory(source, players, predicate, -1);
    }
 
    private static int clearInventory(CommandSourceStack source, Collection<ServerPlayer> players, Predicate<ItemStack> predicate, int maxCount) throws CommandSyntaxException {
        int count = 0;
 
        for (ServerPlayer player : players) {
            count += player.getInventory().clearOrCountMatchingItems(predicate, maxCount, player.inventoryMenu.getCraftSlots());
            player.containerMenu.broadcastChanges();
            player.inventoryMenu.slotsChanged(player.getInventory());
        }
 
        if (count == 0) {
            if (players.size() == 1) {
                throw ERROR_SINGLE.create(players.iterator().next().getName());
            } else {
                throw ERROR_MULTIPLE.create(players.size());
            }
        } else {
            int finalCount = count;
            if (maxCount == 0) {
                if (players.size() == 1) {
                    source.sendSuccess(() -> Component.translatable("commands.clear.test.single", finalCount, players.iterator().next().getDisplayName()), true);
                } else {
                    source.sendSuccess(() -> Component.translatable("commands.clear.test.multiple", finalCount, players.size()), true);
                }
            } else if (players.size() == 1) {
                source.sendSuccess(() -> Component.translatable("commands.clear.success.single", finalCount, players.iterator().next().getDisplayName()), true);
            } else {
                source.sendSuccess(() -> Component.translatable("commands.clear.success.multiple", finalCount, players.size()), true);
            }
 
            return count;
        }
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

    • 引用位置: 方法调用
    • 关联成员: EntityArgument.getPlayers(), EntityArgument.players()
  • ItemPredicateArgument

    • 引用位置: 方法调用
    • 关联成员: ItemPredicateArgument.getItemPredicate(), ItemPredicateArgument.itemPredicate()
  • Component

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

    • 引用位置: 参数
  • ItemStack

    • 引用位置: 参数