RecipeCommand.java

net.minecraft.server.commands.RecipeCommand

信息

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

    TODO

字段/常量

  • ERROR_GIVE_FAILED

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

      TODO

  • ERROR_TAKE_FAILED

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

      TODO

内部类/嵌套类型

构造器

方法

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

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) @ L21

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int giveRecipes(CommandSourceStack source, Collection<ServerPlayer> players, Collection<RecipeHolder<?>> recipes) @ L80

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

参数:

  • source: CommandSourceStack
  • players: Collection
  • recipes: Collection<RecipeHolder<?>>

说明:

TODO

private static int takeRecipes(CommandSourceStack source, Collection<ServerPlayer> players, Collection<RecipeHolder<?>> recipes) @ L102

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

参数:

  • source: CommandSourceStack
  • players: Collection
  • recipes: Collection<RecipeHolder<?>>

说明:

TODO

代码

public class RecipeCommand {
    private static final SimpleCommandExceptionType ERROR_GIVE_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.recipe.give.failed"));
    private static final SimpleCommandExceptionType ERROR_TAKE_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.recipe.take.failed"));
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("recipe")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .then(
                    Commands.literal("give")
                        .then(
                            Commands.argument("targets", EntityArgument.players())
                                .then(
                                    Commands.argument("recipe", ResourceKeyArgument.key(Registries.RECIPE))
                                        .executes(
                                            c -> giveRecipes(
                                                c.getSource(),
                                                EntityArgument.getPlayers(c, "targets"),
                                                Collections.singleton(ResourceKeyArgument.getRecipe(c, "recipe"))
                                            )
                                        )
                                )
                                .then(
                                    Commands.literal("*")
                                        .executes(
                                            c -> giveRecipes(
                                                c.getSource(),
                                                EntityArgument.getPlayers(c, "targets"),
                                                c.getSource().getServer().getRecipeManager().getRecipes()
                                            )
                                        )
                                )
                        )
                )
                .then(
                    Commands.literal("take")
                        .then(
                            Commands.argument("targets", EntityArgument.players())
                                .then(
                                    Commands.argument("recipe", ResourceKeyArgument.key(Registries.RECIPE))
                                        .executes(
                                            c -> takeRecipes(
                                                c.getSource(),
                                                EntityArgument.getPlayers(c, "targets"),
                                                Collections.singleton(ResourceKeyArgument.getRecipe(c, "recipe"))
                                            )
                                        )
                                )
                                .then(
                                    Commands.literal("*")
                                        .executes(
                                            c -> takeRecipes(
                                                c.getSource(),
                                                EntityArgument.getPlayers(c, "targets"),
                                                c.getSource().getServer().getRecipeManager().getRecipes()
                                            )
                                        )
                                )
                        )
                )
        );
    }
 
    private static int giveRecipes(CommandSourceStack source, Collection<ServerPlayer> players, Collection<RecipeHolder<?>> recipes) throws CommandSyntaxException {
        int success = 0;
 
        for (ServerPlayer player : players) {
            success += player.awardRecipes(recipes);
        }
 
        if (success == 0) {
            throw ERROR_GIVE_FAILED.create();
        } else {
            if (players.size() == 1) {
                source.sendSuccess(
                    () -> Component.translatable("commands.recipe.give.success.single", recipes.size(), players.iterator().next().getDisplayName()), true
                );
            } else {
                source.sendSuccess(() -> Component.translatable("commands.recipe.give.success.multiple", recipes.size(), players.size()), true);
            }
 
            return success;
        }
    }
 
    private static int takeRecipes(CommandSourceStack source, Collection<ServerPlayer> players, Collection<RecipeHolder<?>> recipes) throws CommandSyntaxException {
        int success = 0;
 
        for (ServerPlayer player : players) {
            success += player.resetRecipes(recipes);
        }
 
        if (success == 0) {
            throw ERROR_TAKE_FAILED.create();
        } else {
            if (players.size() == 1) {
                source.sendSuccess(
                    () -> Component.translatable("commands.recipe.take.success.single", recipes.size(), players.iterator().next().getDisplayName()), true
                );
            } else {
                source.sendSuccess(() -> Component.translatable("commands.recipe.take.success.multiple", recipes.size(), players.size()), true);
            }
 
            return success;
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

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

    • 引用位置: 方法调用
    • 关联成员: ResourceKeyArgument.getRecipe(), ResourceKeyArgument.key()
  • Component

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

    • 引用位置: 参数
  • RecipeHolder

    • 引用位置: 参数