DamageCommand.java

net.minecraft.server.commands.DamageCommand

信息

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

    TODO

字段/常量

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

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher
  • context: CommandBuildContext

说明:

TODO

private static int damage(CommandSourceStack stack, Entity target, float amount, DamageSource source) @ L105

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

参数:

  • stack: CommandSourceStack
  • target: Entity
  • amount: float
  • source: DamageSource

说明:

TODO

代码

public class DamageCommand {
    private static final SimpleCommandExceptionType ERROR_INVULNERABLE = new SimpleCommandExceptionType(Component.translatable("commands.damage.invulnerable"));
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
        dispatcher.register(
            Commands.literal("damage")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .then(
                    Commands.argument("target", EntityArgument.entity())
                        .then(
                            Commands.argument("amount", FloatArgumentType.floatArg(0.0F))
                                .executes(
                                    c -> damage(
                                        c.getSource(),
                                        EntityArgument.getEntity(c, "target"),
                                        FloatArgumentType.getFloat(c, "amount"),
                                        c.getSource().getLevel().damageSources().generic()
                                    )
                                )
                                .then(
                                    Commands.argument("damageType", ResourceArgument.resource(context, Registries.DAMAGE_TYPE))
                                        .executes(
                                            c -> damage(
                                                c.getSource(),
                                                EntityArgument.getEntity(c, "target"),
                                                FloatArgumentType.getFloat(c, "amount"),
                                                new DamageSource(ResourceArgument.getResource(c, "damageType", Registries.DAMAGE_TYPE))
                                            )
                                        )
                                        .then(
                                            Commands.literal("at")
                                                .then(
                                                    Commands.argument("location", Vec3Argument.vec3())
                                                        .executes(
                                                            c -> damage(
                                                                c.getSource(),
                                                                EntityArgument.getEntity(c, "target"),
                                                                FloatArgumentType.getFloat(c, "amount"),
                                                                new DamageSource(
                                                                    ResourceArgument.getResource(c, "damageType", Registries.DAMAGE_TYPE),
                                                                    Vec3Argument.getVec3(c, "location")
                                                                )
                                                            )
                                                        )
                                                )
                                        )
                                        .then(
                                            Commands.literal("by")
                                                .then(
                                                    Commands.argument("entity", EntityArgument.entity())
                                                        .executes(
                                                            c -> damage(
                                                                c.getSource(),
                                                                EntityArgument.getEntity(c, "target"),
                                                                FloatArgumentType.getFloat(c, "amount"),
                                                                new DamageSource(
                                                                    ResourceArgument.getResource(c, "damageType", Registries.DAMAGE_TYPE),
                                                                    EntityArgument.getEntity(c, "entity")
                                                                )
                                                            )
                                                        )
                                                        .then(
                                                            Commands.literal("from")
                                                                .then(
                                                                    Commands.argument("cause", EntityArgument.entity())
                                                                        .executes(
                                                                            c -> damage(
                                                                                c.getSource(),
                                                                                EntityArgument.getEntity(c, "target"),
                                                                                FloatArgumentType.getFloat(c, "amount"),
                                                                                new DamageSource(
                                                                                    ResourceArgument.getResource(c, "damageType", Registries.DAMAGE_TYPE),
                                                                                    EntityArgument.getEntity(c, "entity"),
                                                                                    EntityArgument.getEntity(c, "cause")
                                                                                )
                                                                            )
                                                                        )
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        );
    }
 
    private static int damage(CommandSourceStack stack, Entity target, float amount, DamageSource source) throws CommandSyntaxException {
        if (target.hurtServer(stack.getLevel(), source, amount)) {
            stack.sendSuccess(() -> Component.translatable("commands.damage.success", amount, target.getDisplayName()), true);
            return 1;
        } else {
            throw ERROR_INVULNERABLE.create();
        }
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

    • 引用位置: 方法调用
    • 关联成员: EntityArgument.entity(), EntityArgument.getEntity()
  • ResourceArgument

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

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

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

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

    • 引用位置: 参数