EffectCommands.java

net.minecraft.server.commands.EffectCommands

信息

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

    TODO

字段/常量

  • ERROR_GIVE_FAILED

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

      TODO

  • ERROR_CLEAR_EVERYTHING_FAILED

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

      TODO

  • ERROR_CLEAR_SPECIFIC_FAILED

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

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher
  • context: CommandBuildContext

说明:

TODO

private static int giveEffect(CommandSourceStack source, Collection<?extends Entity> entities, Holder<MobEffect> effectHolder, Integer seconds, int amplifier, boolean particles) @ L153

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

参数:

  • source: CommandSourceStack
  • entities: Collection<?extends Entity>
  • effectHolder: Holder
  • seconds: Integer
  • amplifier: int
  • particles: boolean

说明:

TODO

private static int clearEffects(CommandSourceStack source, Collection<?extends Entity> entities) @ L207

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

参数:

  • source: CommandSourceStack
  • entities: Collection<?extends Entity>

说明:

TODO

private static int clearEffect(CommandSourceStack source, Collection<?extends Entity> entities, Holder<MobEffect> effectHolder) @ L231

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

参数:

  • source: CommandSourceStack
  • entities: Collection<?extends Entity>
  • effectHolder: Holder

说明:

TODO

代码

public class EffectCommands {
    private static final SimpleCommandExceptionType ERROR_GIVE_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.effect.give.failed"));
    private static final SimpleCommandExceptionType ERROR_CLEAR_EVERYTHING_FAILED = new SimpleCommandExceptionType(
        Component.translatable("commands.effect.clear.everything.failed")
    );
    private static final SimpleCommandExceptionType ERROR_CLEAR_SPECIFIC_FAILED = new SimpleCommandExceptionType(
        Component.translatable("commands.effect.clear.specific.failed")
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
        dispatcher.register(
            Commands.literal("effect")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .then(
                    Commands.literal("clear")
                        .executes(c -> clearEffects(c.getSource(), ImmutableList.of(c.getSource().getEntityOrException())))
                        .then(
                            Commands.argument("targets", EntityArgument.entities())
                                .executes(c -> clearEffects(c.getSource(), EntityArgument.getEntities(c, "targets")))
                                .then(
                                    Commands.argument("effect", ResourceArgument.resource(context, Registries.MOB_EFFECT))
                                        .executes(
                                            c -> clearEffect(
                                                c.getSource(), EntityArgument.getEntities(c, "targets"), ResourceArgument.getMobEffect(c, "effect")
                                            )
                                        )
                                )
                        )
                )
                .then(
                    Commands.literal("give")
                        .then(
                            Commands.argument("targets", EntityArgument.entities())
                                .then(
                                    Commands.argument("effect", ResourceArgument.resource(context, Registries.MOB_EFFECT))
                                        .executes(
                                            c -> giveEffect(
                                                c.getSource(),
                                                EntityArgument.getEntities(c, "targets"),
                                                ResourceArgument.getMobEffect(c, "effect"),
                                                null,
                                                0,
                                                true
                                            )
                                        )
                                        .then(
                                            Commands.argument("seconds", IntegerArgumentType.integer(1, 1000000))
                                                .executes(
                                                    c -> giveEffect(
                                                        c.getSource(),
                                                        EntityArgument.getEntities(c, "targets"),
                                                        ResourceArgument.getMobEffect(c, "effect"),
                                                        IntegerArgumentType.getInteger(c, "seconds"),
                                                        0,
                                                        true
                                                    )
                                                )
                                                .then(
                                                    Commands.argument("amplifier", IntegerArgumentType.integer(0, 255))
                                                        .executes(
                                                            c -> giveEffect(
                                                                c.getSource(),
                                                                EntityArgument.getEntities(c, "targets"),
                                                                ResourceArgument.getMobEffect(c, "effect"),
                                                                IntegerArgumentType.getInteger(c, "seconds"),
                                                                IntegerArgumentType.getInteger(c, "amplifier"),
                                                                true
                                                            )
                                                        )
                                                        .then(
                                                            Commands.argument("hideParticles", BoolArgumentType.bool())
                                                                .executes(
                                                                    c -> giveEffect(
                                                                        c.getSource(),
                                                                        EntityArgument.getEntities(c, "targets"),
                                                                        ResourceArgument.getMobEffect(c, "effect"),
                                                                        IntegerArgumentType.getInteger(c, "seconds"),
                                                                        IntegerArgumentType.getInteger(c, "amplifier"),
                                                                        !BoolArgumentType.getBool(c, "hideParticles")
                                                                    )
                                                                )
                                                        )
                                                )
                                        )
                                        .then(
                                            Commands.literal("infinite")
                                                .executes(
                                                    c -> giveEffect(
                                                        c.getSource(),
                                                        EntityArgument.getEntities(c, "targets"),
                                                        ResourceArgument.getMobEffect(c, "effect"),
                                                        -1,
                                                        0,
                                                        true
                                                    )
                                                )
                                                .then(
                                                    Commands.argument("amplifier", IntegerArgumentType.integer(0, 255))
                                                        .executes(
                                                            c -> giveEffect(
                                                                c.getSource(),
                                                                EntityArgument.getEntities(c, "targets"),
                                                                ResourceArgument.getMobEffect(c, "effect"),
                                                                -1,
                                                                IntegerArgumentType.getInteger(c, "amplifier"),
                                                                true
                                                            )
                                                        )
                                                        .then(
                                                            Commands.argument("hideParticles", BoolArgumentType.bool())
                                                                .executes(
                                                                    c -> giveEffect(
                                                                        c.getSource(),
                                                                        EntityArgument.getEntities(c, "targets"),
                                                                        ResourceArgument.getMobEffect(c, "effect"),
                                                                        -1,
                                                                        IntegerArgumentType.getInteger(c, "amplifier"),
                                                                        !BoolArgumentType.getBool(c, "hideParticles")
                                                                    )
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        );
    }
 
    private static int giveEffect(
        CommandSourceStack source,
        Collection<? extends Entity> entities,
        Holder<MobEffect> effectHolder,
        @Nullable Integer seconds,
        int amplifier,
        boolean particles
    ) throws CommandSyntaxException {
        MobEffect effect = effectHolder.value();
        int count = 0;
        int duration;
        if (seconds != null) {
            if (effect.isInstantenous()) {
                duration = seconds;
            } else if (seconds == -1) {
                duration = -1;
            } else {
                duration = seconds * 20;
            }
        } else if (effect.isInstantenous()) {
            duration = 1;
        } else {
            duration = 600;
        }
 
        for (Entity entity : entities) {
            if (entity instanceof LivingEntity) {
                MobEffectInstance instance = new MobEffectInstance(effectHolder, duration, amplifier, false, particles);
                if (((LivingEntity)entity).addEffect(instance, source.getEntity())) {
                    count++;
                }
            }
        }
 
        if (count == 0) {
            throw ERROR_GIVE_FAILED.create();
        } else {
            if (entities.size() == 1) {
                source.sendSuccess(
                    () -> Component.translatable(
                        "commands.effect.give.success.single", effect.getDisplayName(), entities.iterator().next().getDisplayName(), duration / 20
                    ),
                    true
                );
            } else {
                source.sendSuccess(
                    () -> Component.translatable("commands.effect.give.success.multiple", effect.getDisplayName(), entities.size(), duration / 20), true
                );
            }
 
            return count;
        }
    }
 
    private static int clearEffects(CommandSourceStack source, Collection<? extends Entity> entities) throws CommandSyntaxException {
        int count = 0;
 
        for (Entity entity : entities) {
            if (entity instanceof LivingEntity && ((LivingEntity)entity).removeAllEffects()) {
                count++;
            }
        }
 
        if (count == 0) {
            throw ERROR_CLEAR_EVERYTHING_FAILED.create();
        } else {
            if (entities.size() == 1) {
                source.sendSuccess(
                    () -> Component.translatable("commands.effect.clear.everything.success.single", entities.iterator().next().getDisplayName()), true
                );
            } else {
                source.sendSuccess(() -> Component.translatable("commands.effect.clear.everything.success.multiple", entities.size()), true);
            }
 
            return count;
        }
    }
 
    private static int clearEffect(CommandSourceStack source, Collection<? extends Entity> entities, Holder<MobEffect> effectHolder) throws CommandSyntaxException {
        MobEffect effect = effectHolder.value();
        int count = 0;
 
        for (Entity entity : entities) {
            if (entity instanceof LivingEntity && ((LivingEntity)entity).removeEffect(effectHolder)) {
                count++;
            }
        }
 
        if (count == 0) {
            throw ERROR_CLEAR_SPECIFIC_FAILED.create();
        } else {
            if (entities.size() == 1) {
                source.sendSuccess(
                    () -> Component.translatable(
                        "commands.effect.clear.specific.success.single", effect.getDisplayName(), entities.iterator().next().getDisplayName()
                    ),
                    true
                );
            } else {
                source.sendSuccess(
                    () -> Component.translatable("commands.effect.clear.specific.success.multiple", effect.getDisplayName(), entities.size()), true
                );
            }
 
            return count;
        }
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

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

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

    • 引用位置: 参数
  • Component

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

    • 引用位置: 参数
  • MobEffectInstance

    • 引用位置: 构造调用
    • 关联成员: MobEffectInstance()
  • Entity

    • 引用位置: 参数