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;
}
}引用的其他类
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Commands.argument(), Commands.hasPermission(), Commands.literal()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
CompoundTagArgument.compoundTag(), CompoundTagArgument.getCompoundTag()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
ResourceArgument.getSummonableEntityType(), ResourceArgument.resource()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Vec3Argument.getVec3(), Vec3Argument.vec3()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
SuggestionProviders.cast()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
BlockPos.containing()
- 引用位置:
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
参数/构造调用 - 关联成员:
CompoundTag()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Component.translatable()
- 引用位置:
-
- 引用位置:
返回值
- 引用位置:
-
- 引用位置:
参数/方法调用 - 关联成员:
EntityType.loadEntityRecursive()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Level.isInSpawnableBounds()
- 引用位置:
-
- 引用位置:
参数
- 引用位置: