SummonCommand.java

net.minecraft.server.commands.SummonCommand

信息

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

    TODO

字段/常量

  • ERROR_FAILED

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

      TODO

  • ERROR_FAILED_PEACEFUL

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

      TODO

  • ERROR_DUPLICATE_UUID

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

      TODO

  • INVALID_POSITION

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

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher
  • context: CommandBuildContext

说明:

TODO

public static Entity createEntity(CommandSourceStack source, Holder.Reference<EntityType<?>> type, Vec3 pos, CompoundTag nbt, boolean finalize) @ L75

  • 方法名:createEntity
  • 源码定位:L75
  • 返回类型:Entity
  • 修饰符:public static

参数:

  • source: CommandSourceStack
  • type: Holder.Reference<EntityType<?>>
  • pos: Vec3
  • nbt: CompoundTag
  • finalize: boolean

说明:

TODO

private static int spawnEntity(CommandSourceStack source, Holder.Reference<EntityType<?>> type, Vec3 pos, CompoundTag nbt, boolean finalize) @ L105

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

参数:

  • source: CommandSourceStack
  • type: Holder.Reference<EntityType<?>>
  • pos: Vec3
  • nbt: CompoundTag
  • finalize: boolean

说明:

TODO

代码

public class SummonCommand {
    private static final SimpleCommandExceptionType ERROR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.summon.failed"));
    private static final SimpleCommandExceptionType ERROR_FAILED_PEACEFUL = new SimpleCommandExceptionType(
        Component.translatable("commands.summon.failed.peaceful")
    );
    private static final SimpleCommandExceptionType ERROR_DUPLICATE_UUID = new SimpleCommandExceptionType(Component.translatable("commands.summon.failed.uuid"));
    private static final SimpleCommandExceptionType INVALID_POSITION = new SimpleCommandExceptionType(Component.translatable("commands.summon.invalidPosition"));
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
        dispatcher.register(
            Commands.literal("summon")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .then(
                    Commands.argument("entity", ResourceArgument.resource(context, Registries.ENTITY_TYPE))
                        .suggests(SuggestionProviders.cast(SuggestionProviders.SUMMONABLE_ENTITIES))
                        .executes(
                            c -> spawnEntity(
                                c.getSource(), ResourceArgument.getSummonableEntityType(c, "entity"), c.getSource().getPosition(), new CompoundTag(), true
                            )
                        )
                        .then(
                            Commands.argument("pos", Vec3Argument.vec3())
                                .executes(
                                    c -> spawnEntity(
                                        c.getSource(),
                                        ResourceArgument.getSummonableEntityType(c, "entity"),
                                        Vec3Argument.getVec3(c, "pos"),
                                        new CompoundTag(),
                                        true
                                    )
                                )
                                .then(
                                    Commands.argument("nbt", CompoundTagArgument.compoundTag())
                                        .executes(
                                            c -> spawnEntity(
                                                c.getSource(),
                                                ResourceArgument.getSummonableEntityType(c, "entity"),
                                                Vec3Argument.getVec3(c, "pos"),
                                                CompoundTagArgument.getCompoundTag(c, "nbt"),
                                                false
                                            )
                                        )
                                )
                        )
                )
        );
    }
 
    public static Entity createEntity(CommandSourceStack source, Holder.Reference<EntityType<?>> type, Vec3 pos, CompoundTag nbt, boolean finalize) throws CommandSyntaxException {
        BlockPos blockPos = BlockPos.containing(pos);
        if (!Level.isInSpawnableBounds(blockPos)) {
            throw INVALID_POSITION.create();
        } else if (source.getLevel().getDifficulty() == Difficulty.PEACEFUL && !type.value().isAllowedInPeaceful()) {
            throw ERROR_FAILED_PEACEFUL.create();
        } else {
            CompoundTag entityTag = nbt.copy();
            entityTag.putString("id", type.key().identifier().toString());
            ServerLevel level = source.getLevel();
            Entity entity = EntityType.loadEntityRecursive(entityTag, level, EntitySpawnReason.COMMAND, e -> {
                e.snapTo(pos.x, pos.y, pos.z, e.getYRot(), e.getXRot());
                return e;
            });
            if (entity == null) {
                throw ERROR_FAILED.create();
            } else {
                if (finalize && entity instanceof Mob mob) {
                    mob.finalizeSpawn(source.getLevel(), source.getLevel().getCurrentDifficultyAt(entity.blockPosition()), EntitySpawnReason.COMMAND, null);
                }
 
                if (!level.tryAddFreshEntityWithPassengers(entity)) {
                    throw ERROR_DUPLICATE_UUID.create();
                } else {
                    return entity;
                }
            }
        }
    }
 
    private static int spawnEntity(CommandSourceStack source, Holder.Reference<EntityType<?>> type, Vec3 pos, CompoundTag nbt, boolean finalize) throws CommandSyntaxException {
        Entity entity = createEntity(source, type, pos, nbt, finalize);
        source.sendSuccess(() -> Component.translatable("commands.summon.success", entity.getDisplayName()), true);
        return 1;
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

    • 引用位置: 方法调用
    • 关联成员: CompoundTagArgument.compoundTag(), CompoundTagArgument.getCompoundTag()
  • ResourceArgument

    • 引用位置: 方法调用
    • 关联成员: ResourceArgument.getSummonableEntityType(), ResourceArgument.resource()
  • Vec3Argument

    • 引用位置: 方法调用
    • 关联成员: Vec3Argument.getVec3(), Vec3Argument.vec3()
  • SuggestionProviders

    • 引用位置: 方法调用
    • 关联成员: SuggestionProviders.cast()
  • BlockPos

    • 引用位置: 方法调用
    • 关联成员: BlockPos.containing()
  • Holder

    • 引用位置: 参数
  • CompoundTag

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

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

    • 引用位置: 返回值
  • EntityType

    • 引用位置: 参数/方法调用
    • 关联成员: EntityType.loadEntityRecursive()
  • Level

    • 引用位置: 方法调用
    • 关联成员: Level.isInSpawnableBounds()
  • Vec3

    • 引用位置: 参数