SetBlockCommand.java

net.minecraft.server.commands.SetBlockCommand

信息

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

    TODO

字段/常量

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

      TODO

内部类/嵌套类型

  • net.minecraft.server.commands.SetBlockCommand.Mode
    • 类型: enum
    • 修饰符: public static
    • 源码定位: L130
    • 说明:

      TODO

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher
  • context: CommandBuildContext

说明:

TODO

private static int setBlock(CommandSourceStack source, BlockPos pos, BlockInput block, SetBlockCommand.Mode mode, Predicate<BlockInWorld> predicate, boolean strict) @ L99

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

参数:

  • source: CommandSourceStack
  • pos: BlockPos
  • block: BlockInput
  • mode: SetBlockCommand.Mode
  • predicate: Predicate
  • strict: boolean

说明:

TODO

代码

public class SetBlockCommand {
    private static final SimpleCommandExceptionType ERROR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.setblock.failed"));
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
        Predicate<BlockInWorld> filter = b -> b.getLevel().isEmptyBlock(b.getPos());
        dispatcher.register(
            Commands.literal("setblock")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .then(
                    Commands.argument("pos", BlockPosArgument.blockPos())
                        .then(
                            Commands.argument("block", BlockStateArgument.block(context))
                                .executes(
                                    c -> setBlock(
                                        c.getSource(),
                                        BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                        BlockStateArgument.getBlock(c, "block"),
                                        SetBlockCommand.Mode.REPLACE,
                                        null,
                                        false
                                    )
                                )
                                .then(
                                    Commands.literal("destroy")
                                        .executes(
                                            c -> setBlock(
                                                c.getSource(),
                                                BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                                BlockStateArgument.getBlock(c, "block"),
                                                SetBlockCommand.Mode.DESTROY,
                                                null,
                                                false
                                            )
                                        )
                                )
                                .then(
                                    Commands.literal("keep")
                                        .executes(
                                            c -> setBlock(
                                                c.getSource(),
                                                BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                                BlockStateArgument.getBlock(c, "block"),
                                                SetBlockCommand.Mode.REPLACE,
                                                filter,
                                                false
                                            )
                                        )
                                )
                                .then(
                                    Commands.literal("replace")
                                        .executes(
                                            c -> setBlock(
                                                c.getSource(),
                                                BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                                BlockStateArgument.getBlock(c, "block"),
                                                SetBlockCommand.Mode.REPLACE,
                                                null,
                                                false
                                            )
                                        )
                                )
                                .then(
                                    Commands.literal("strict")
                                        .executes(
                                            c -> setBlock(
                                                c.getSource(),
                                                BlockPosArgument.getLoadedBlockPos(c, "pos"),
                                                BlockStateArgument.getBlock(c, "block"),
                                                SetBlockCommand.Mode.REPLACE,
                                                null,
                                                true
                                            )
                                        )
                                )
                        )
                )
        );
    }
 
    private static int setBlock(
        CommandSourceStack source, BlockPos pos, BlockInput block, SetBlockCommand.Mode mode, @Nullable Predicate<BlockInWorld> predicate, boolean strict
    ) throws CommandSyntaxException {
        ServerLevel level = source.getLevel();
        if (level.isDebug()) {
            throw ERROR_FAILED.create();
        } else if (predicate != null && !predicate.test(new BlockInWorld(level, pos, true))) {
            throw ERROR_FAILED.create();
        } else {
            boolean placeNeeded;
            if (mode == SetBlockCommand.Mode.DESTROY) {
                level.destroyBlock(pos, true);
                placeNeeded = !block.getState().isAir() || !level.getBlockState(pos).isAir();
            } else {
                placeNeeded = true;
            }
 
            BlockState oldState = level.getBlockState(pos);
            if (placeNeeded && !block.place(level, pos, 2 | (strict ? 816 : 256))) {
                throw ERROR_FAILED.create();
            } else {
                if (!strict) {
                    level.updateNeighboursOnBlockSet(pos, oldState);
                }
 
                source.sendSuccess(() -> Component.translatable("commands.setblock.success", pos.getX(), pos.getY(), pos.getZ()), true);
                return 1;
            }
        }
    }
 
    public static enum Mode {
        REPLACE,
        DESTROY;
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

    • 引用位置: 参数
  • BlockStateArgument

    • 引用位置: 方法调用
    • 关联成员: BlockStateArgument.block(), BlockStateArgument.getBlock()
  • BlockPosArgument

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

    • 引用位置: 参数
  • Component

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

    • 引用位置: 参数/构造调用
    • 关联成员: BlockInWorld()