RaidCommand.java

net.minecraft.server.commands.RaidCommand

信息

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

    TODO

字段/常量

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher
  • context: CommandBuildContext

说明:

TODO

private static int glow(CommandSourceStack source) @ L61

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static int setRaidOmenLevel(CommandSourceStack source, int level) @ L72

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

参数:

  • source: CommandSourceStack
  • level: int

说明:

TODO

private static int spawnLeader(CommandSourceStack source) @ L90

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static int playSound(CommandSourceStack source, Component type) @ L108

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

参数:

  • source: CommandSourceStack
  • type: Component

说明:

TODO

private static int start(CommandSourceStack source, int raidOmenLevel) @ L118

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

参数:

  • source: CommandSourceStack
  • raidOmenLevel: int

说明:

TODO

private static int stop(CommandSourceStack source) @ L139

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static int check(CommandSourceStack source) @ L153

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static Raid getRaid(ServerPlayer player) @ L178

  • 方法名:getRaid
  • 源码定位:L178
  • 返回类型:Raid
  • 修饰符:private static

参数:

  • player: ServerPlayer

说明:

TODO

代码

public class RaidCommand {
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
        dispatcher.register(
            Commands.literal("raid")
                .requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
                .then(
                    Commands.literal("start")
                        .then(
                            Commands.argument("omenlvl", IntegerArgumentType.integer(0))
                                .executes(c -> start(c.getSource(), IntegerArgumentType.getInteger(c, "omenlvl")))
                        )
                )
                .then(Commands.literal("stop").executes(c -> stop(c.getSource())))
                .then(Commands.literal("check").executes(c -> check(c.getSource())))
                .then(
                    Commands.literal("sound")
                        .then(
                            Commands.argument("type", ComponentArgument.textComponent(context))
                                .executes(c -> playSound(c.getSource(), ComponentArgument.getResolvedComponent(c, "type")))
                        )
                )
                .then(Commands.literal("spawnleader").executes(c -> spawnLeader(c.getSource())))
                .then(
                    Commands.literal("setomen")
                        .then(
                            Commands.argument("level", IntegerArgumentType.integer(0))
                                .executes(c -> setRaidOmenLevel(c.getSource(), IntegerArgumentType.getInteger(c, "level")))
                        )
                )
                .then(Commands.literal("glow").executes(c -> glow(c.getSource())))
        );
    }
 
    private static int glow(CommandSourceStack source) throws CommandSyntaxException {
        Raid raid = getRaid(source.getPlayerOrException());
        if (raid != null) {
            for (Raider raider : raid.getAllRaiders()) {
                raider.addEffect(new MobEffectInstance(MobEffects.GLOWING, 1000, 1));
            }
        }
 
        return 1;
    }
 
    private static int setRaidOmenLevel(CommandSourceStack source, int level) throws CommandSyntaxException {
        Raid raid = getRaid(source.getPlayerOrException());
        if (raid != null) {
            int max = raid.getMaxRaidOmenLevel();
            if (level > max) {
                source.sendFailure(Component.literal("Sorry, the max raid omen level you can set is " + max));
            } else {
                int before = raid.getRaidOmenLevel();
                raid.setRaidOmenLevel(level);
                source.sendSuccess(() -> Component.literal("Changed village's raid omen level from " + before + " to " + level), false);
            }
        } else {
            source.sendFailure(Component.literal("No raid found here"));
        }
 
        return 1;
    }
 
    private static int spawnLeader(CommandSourceStack source) {
        source.sendSuccess(() -> Component.literal("Spawned a raid captain"), false);
        Raider raider = EntityType.PILLAGER.create(source.getLevel(), EntitySpawnReason.COMMAND);
        if (raider == null) {
            source.sendFailure(Component.literal("Pillager failed to spawn"));
            return 0;
        } else {
            raider.setPatrolLeader(true);
            raider.setItemSlot(EquipmentSlot.HEAD, Raid.getOminousBannerInstance(source.registryAccess().lookupOrThrow(Registries.BANNER_PATTERN)));
            raider.setPos(source.getPosition().x, source.getPosition().y, source.getPosition().z);
            raider.finalizeSpawn(
                source.getLevel(), source.getLevel().getCurrentDifficultyAt(BlockPos.containing(source.getPosition())), EntitySpawnReason.COMMAND, null
            );
            source.getLevel().addFreshEntityWithPassengers(raider);
            return 1;
        }
    }
 
    private static int playSound(CommandSourceStack source, @Nullable Component type) {
        if (type != null && type.getString().equals("local")) {
            ServerLevel level = source.getLevel();
            Vec3 pos = source.getPosition().add(5.0, 0.0, 0.0);
            level.playSeededSound(null, pos.x, pos.y, pos.z, SoundEvents.RAID_HORN, SoundSource.NEUTRAL, 2.0F, 1.0F, level.getRandom().nextLong());
        }
 
        return 1;
    }
 
    private static int start(CommandSourceStack source, int raidOmenLevel) throws CommandSyntaxException {
        ServerPlayer player = source.getPlayerOrException();
        BlockPos pos = player.blockPosition();
        if (player.level().isRaided(pos)) {
            source.sendFailure(Component.literal("Raid already started close by"));
            return -1;
        } else {
            Raids raids = player.level().getRaids();
            Raid raid = raids.createOrExtendRaid(player, player.blockPosition());
            if (raid != null) {
                raid.setRaidOmenLevel(raidOmenLevel);
                raids.setDirty();
                source.sendSuccess(() -> Component.literal("Created a raid in your local village"), false);
            } else {
                source.sendFailure(Component.literal("Failed to create a raid in your local village"));
            }
 
            return 1;
        }
    }
 
    private static int stop(CommandSourceStack source) throws CommandSyntaxException {
        ServerPlayer player = source.getPlayerOrException();
        BlockPos pos = player.blockPosition();
        Raid raid = player.level().getRaidAt(pos);
        if (raid != null) {
            raid.stop();
            source.sendSuccess(() -> Component.literal("Stopped raid"), false);
            return 1;
        } else {
            source.sendFailure(Component.literal("No raid here"));
            return -1;
        }
    }
 
    private static int check(CommandSourceStack source) throws CommandSyntaxException {
        Raid raid = getRaid(source.getPlayerOrException());
        if (raid != null) {
            StringBuilder status = new StringBuilder();
            status.append("Found a started raid! ");
            source.sendSuccess(() -> Component.literal(status.toString()), false);
            StringBuilder status2 = new StringBuilder();
            status2.append("Num groups spawned: ");
            status2.append(raid.getGroupsSpawned());
            status2.append(" Raid omen level: ");
            status2.append(raid.getRaidOmenLevel());
            status2.append(" Num mobs: ");
            status2.append(raid.getTotalRaidersAlive());
            status2.append(" Raid health: ");
            status2.append(raid.getHealthOfLivingRaiders());
            status2.append(" / ");
            status2.append(raid.getTotalHealth());
            source.sendSuccess(() -> Component.literal(status2.toString()), false);
            return 1;
        } else {
            source.sendFailure(Component.literal("Found no started raids"));
            return 0;
        }
    }
 
    private static @Nullable Raid getRaid(ServerPlayer player) {
        return player.level().getRaidAt(player.blockPosition());
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

    • 引用位置: 方法调用
    • 关联成员: ComponentArgument.getResolvedComponent(), ComponentArgument.textComponent()
  • BlockPos

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

    • 引用位置: 参数/方法调用
    • 关联成员: Component.literal()
  • ServerPlayer

    • 引用位置: 参数
  • MobEffectInstance

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

    • 引用位置: 方法调用/返回值
    • 关联成员: Raid.getOminousBannerInstance()