| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import net.minecraft.commands.CommandBuildContext;
|
| 7 | import net.minecraft.commands.CommandSourceStack;
|
| 8 | import net.minecraft.commands.Commands;
|
| 9 | import net.minecraft.commands.arguments.ComponentArgument;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.registries.Registries;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.server.level.ServerLevel;
|
| 14 | import net.minecraft.server.level.ServerPlayer;
|
| 15 | import net.minecraft.sounds.SoundEvents;
|
| 16 | import net.minecraft.sounds.SoundSource;
|
| 17 | import net.minecraft.world.effect.MobEffectInstance;
|
| 18 | import net.minecraft.world.effect.MobEffects;
|
| 19 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 20 | import net.minecraft.world.entity.EntityTypes;
|
| 21 | import net.minecraft.world.entity.EquipmentSlot;
|
| 22 | import net.minecraft.world.entity.raid.Raid;
|
| 23 | import net.minecraft.world.entity.raid.Raider;
|
| 24 | import net.minecraft.world.entity.raid.Raids;
|
| 25 | import net.minecraft.world.phys.Vec3;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public class RaidCommand {
|
| 29 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 30 | dispatcher.register(
|
| 31 | Commands.literal("raid")
|
| 32 | .requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
|
| 33 | .then(
|
| 34 | Commands.literal("start")
|
| 35 | .then(Commands.argument("omenlvl", IntegerArgumentType.integer(0)).executes(c -> start(c.getSource(), IntegerArgumentType.getInteger(c, "omenlvl"))))
|
| 36 | )
|
| 37 | .then(Commands.literal("stop").executes(c -> stop(c.getSource())))
|
| 38 | .then(Commands.literal("check").executes(c -> check(c.getSource())))
|
| 39 | .then(
|
| 40 | Commands.literal("sound")
|
| 41 | .then(
|
| 42 | Commands.argument("type", ComponentArgument.textComponent(context))
|
| 43 | .executes(c -> playSound(c.getSource(), ComponentArgument.getResolvedComponent(c, "type")))
|
| 44 | )
|
| 45 | )
|
| 46 | .then(Commands.literal("spawnleader").executes(c -> spawnLeader(c.getSource())))
|
| 47 | .then(
|
| 48 | Commands.literal("setomen")
|
| 49 | .then(
|
| 50 | Commands.argument("level", IntegerArgumentType.integer(0)).executes(c -> setRaidOmenLevel(c.getSource(), IntegerArgumentType.getInteger(c, "level")))
|
| 51 | )
|
| 52 | )
|
| 53 | .then(Commands.literal("glow").executes(c -> glow(c.getSource())))
|
| 54 | );
|
| 55 | }
|
| 56 |
|
| 57 | private static int glow(final CommandSourceStack source) throws CommandSyntaxException {
|
| 58 | Raid raid = getRaid(source.getPlayerOrException());
|
| 59 | if (raid != null) {
|
| 60 | for (Raider raider : raid.getAllRaiders()) {
|
| 61 | raider.addEffect(new MobEffectInstance(MobEffects.GLOWING, 1000, 1));
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | return 1;
|
| 66 | }
|
| 67 |
|
| 68 | private static int setRaidOmenLevel(final CommandSourceStack source, final int level) throws CommandSyntaxException {
|
| 69 | Raid raid = getRaid(source.getPlayerOrException());
|
| 70 | if (raid != null) {
|
| 71 | int max = raid.getMaxRaidOmenLevel();
|
| 72 | if (level > max) {
|
| 73 | source.sendFailure(Component.literal("Sorry, the max raid omen level you can set is " + max));
|
| 74 | } else {
|
| 75 | int before = raid.getRaidOmenLevel();
|
| 76 | raid.setRaidOmenLevel(level);
|
| 77 | source.sendSuccess(() -> Component.literal("Changed village's raid omen level from " + before + " to " + level), false);
|
| 78 | }
|
| 79 | } else {
|
| 80 | source.sendFailure(Component.literal("No raid found here"));
|
| 81 | }
|
| 82 |
|
| 83 | return 1;
|
| 84 | }
|
| 85 |
|
| 86 | private static int spawnLeader(final CommandSourceStack source) {
|
| 87 | source.sendSuccess(() -> Component.literal("Spawned a raid captain"), false);
|
| 88 | Raider raider = EntityTypes.PILLAGER.create(source.getLevel(), EntitySpawnReason.COMMAND);
|
| 89 | if (raider == null) {
|
| 90 | source.sendFailure(Component.literal("Pillager failed to spawn"));
|
| 91 | return 0;
|
| 92 | } else {
|
| 93 | raider.setPatrolLeader(true);
|
| 94 | raider.setItemSlot(EquipmentSlot.HEAD, Raid.getOminousBannerInstance(source.registryAccess().lookupOrThrow(Registries.BANNER_PATTERN)));
|
| 95 | raider.setPos(source.getPosition().x, source.getPosition().y, source.getPosition().z);
|
| 96 | raider.finalizeSpawn(source.getLevel(), source.getLevel().getCurrentDifficultyAt(BlockPos.containing(source.getPosition())), EntitySpawnReason.COMMAND, null);
|
| 97 | source.getLevel().addFreshEntityWithPassengers(raider);
|
| 98 | return 1;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | private static int playSound(final CommandSourceStack source, final @Nullable Component type) {
|
| 103 | if (type != null && type.getString().equals("local")) {
|
| 104 | ServerLevel level = source.getLevel();
|
| 105 | Vec3 pos = source.getPosition().add(5.0, 0.0, 0.0);
|
| 106 | level.playSeededSound(null, pos.x, pos.y, pos.z, SoundEvents.RAID_HORN, SoundSource.NEUTRAL, 2.0F, 1.0F, level.getRandom().nextLong());
|
| 107 | }
|
| 108 |
|
| 109 | return 1;
|
| 110 | }
|
| 111 |
|
| 112 | private static int start(final CommandSourceStack source, final int raidOmenLevel) throws CommandSyntaxException {
|
| 113 | ServerPlayer player = source.getPlayerOrException();
|
| 114 | BlockPos pos = player.blockPosition();
|
| 115 | if (player.level().isRaided(pos)) {
|
| 116 | source.sendFailure(Component.literal("Raid already started close by"));
|
| 117 | return -1;
|
| 118 | }
|
| 119 |
|
| 120 | Raids raids = player.level().getRaids();
|
| 121 | Raid raid = raids.createOrExtendRaid(player, player.blockPosition());
|
| 122 | if (raid != null) {
|
| 123 | raid.setRaidOmenLevel(raidOmenLevel);
|
| 124 | raids.setDirty();
|
| 125 | source.sendSuccess(() -> Component.literal("Created a raid in your local village"), false);
|
| 126 | } else {
|
| 127 | source.sendFailure(Component.literal("Failed to create a raid in your local village"));
|
| 128 | }
|
| 129 |
|
| 130 | return 1;
|
| 131 | }
|
| 132 |
|
| 133 | private static int stop(final CommandSourceStack source) throws CommandSyntaxException {
|
| 134 | ServerPlayer player = source.getPlayerOrException();
|
| 135 | BlockPos pos = player.blockPosition();
|
| 136 | Raid raid = player.level().getRaidAt(pos);
|
| 137 | if (raid != null) {
|
| 138 | raid.stop();
|
| 139 | source.sendSuccess(() -> Component.literal("Stopped raid"), false);
|
| 140 | return 1;
|
| 141 | } else {
|
| 142 | source.sendFailure(Component.literal("No raid here"));
|
| 143 | return -1;
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | private static int check(final CommandSourceStack source) throws CommandSyntaxException {
|
| 148 | Raid raid = getRaid(source.getPlayerOrException());
|
| 149 | if (raid != null) {
|
| 150 | source.sendSuccess(() -> Component.literal("Found a started raid!"), false);
|
| 151 | String status = "Num groups spawned: "
|
| 152 | + raid.getGroupsSpawned()
|
| 153 | + " Raid omen level: "
|
| 154 | + raid.getRaidOmenLevel()
|
| 155 | + " Num mobs: "
|
| 156 | + raid.getTotalRaidersAlive()
|
| 157 | + " Raid health: "
|
| 158 | + raid.getHealthOfLivingRaiders()
|
| 159 | + " / "
|
| 160 | + raid.getTotalHealth();
|
| 161 | source.sendSuccess(() -> Component.literal(status), false);
|
| 162 | return 1;
|
| 163 | } else {
|
| 164 | source.sendFailure(Component.literal("Found no started raids"));
|
| 165 | return 0;
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | private static @Nullable Raid getRaid(final ServerPlayer player) {
|
| 170 | return player.level().getRaidAt(player.blockPosition());
|
| 171 | }
|
| 172 | }
|
| 173 |
|