File size: 8,765 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package net.minecraft.server.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import javax.annotation.Nullable;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.ComponentArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.raid.Raid;
import net.minecraft.world.entity.raid.Raider;
import net.minecraft.world.entity.raid.Raids;
import net.minecraft.world.phys.Vec3;

public class RaidCommand {
    public static void register(CommandDispatcher<CommandSourceStack> p_180469_, CommandBuildContext p_334392_) {
        p_180469_.register(
            Commands.literal("raid")
                .requires(p_180498_ -> p_180498_.hasPermission(3))
                .then(
                    Commands.literal("start")
                        .then(
                            Commands.argument("omenlvl", IntegerArgumentType.integer(0))
                                .executes(p_180502_ -> start(p_180502_.getSource(), IntegerArgumentType.getInteger(p_180502_, "omenlvl")))
                        )
                )
                .then(Commands.literal("stop").executes(p_180500_ -> stop(p_180500_.getSource())))
                .then(Commands.literal("check").executes(p_180496_ -> check(p_180496_.getSource())))
                .then(
                    Commands.literal("sound")
                        .then(
                            Commands.argument("type", ComponentArgument.textComponent(p_334392_))
                                .executes(p_180492_ -> playSound(p_180492_.getSource(), ComponentArgument.getComponent(p_180492_, "type")))
                        )
                )
                .then(Commands.literal("spawnleader").executes(p_180488_ -> spawnLeader(p_180488_.getSource())))
                .then(
                    Commands.literal("setomen")
                        .then(
                            Commands.argument("level", IntegerArgumentType.integer(0))
                                .executes(p_326325_ -> setRaidOmenLevel(p_326325_.getSource(), IntegerArgumentType.getInteger(p_326325_, "level")))
                        )
                )
                .then(Commands.literal("glow").executes(p_180471_ -> glow(p_180471_.getSource())))
        );
    }

    private static int glow(CommandSourceStack p_180473_) throws CommandSyntaxException {
        Raid raid = getRaid(p_180473_.getPlayerOrException());
        if (raid != null) {
            for (Raider raider : raid.getAllRaiders()) {
                raider.addEffect(new MobEffectInstance(MobEffects.GLOWING, 1000, 1));
            }
        }

        return 1;
    }

    private static int setRaidOmenLevel(CommandSourceStack p_180475_, int p_180476_) throws CommandSyntaxException {
        Raid raid = getRaid(p_180475_.getPlayerOrException());
        if (raid != null) {
            int i = raid.getMaxRaidOmenLevel();
            if (p_180476_ > i) {
                p_180475_.sendFailure(Component.literal("Sorry, the max raid omen level you can set is " + i));
            } else {
                int j = raid.getRaidOmenLevel();
                raid.setRaidOmenLevel(p_180476_);
                p_180475_.sendSuccess(() -> Component.literal("Changed village's raid omen level from " + j + " to " + p_180476_), false);
            }
        } else {
            p_180475_.sendFailure(Component.literal("No raid found here"));
        }

        return 1;
    }

    private static int spawnLeader(CommandSourceStack p_180483_) {
        p_180483_.sendSuccess(() -> Component.literal("Spawned a raid captain"), false);
        Raider raider = EntityType.PILLAGER.create(p_180483_.getLevel(), EntitySpawnReason.COMMAND);
        if (raider == null) {
            p_180483_.sendFailure(Component.literal("Pillager failed to spawn"));
            return 0;
        } else {
            raider.setPatrolLeader(true);
            raider.setItemSlot(EquipmentSlot.HEAD, Raid.getOminousBannerInstance(p_180483_.registryAccess().lookupOrThrow(Registries.BANNER_PATTERN)));
            raider.setPos(p_180483_.getPosition().x, p_180483_.getPosition().y, p_180483_.getPosition().z);
            raider.finalizeSpawn(p_180483_.getLevel(), p_180483_.getLevel().getCurrentDifficultyAt(BlockPos.containing(p_180483_.getPosition())), EntitySpawnReason.COMMAND, null);
            p_180483_.getLevel().addFreshEntityWithPassengers(raider);
            return 1;
        }
    }

    private static int playSound(CommandSourceStack p_180478_, @Nullable Component p_180479_) {
        if (p_180479_ != null && p_180479_.getString().equals("local")) {
            ServerLevel serverlevel = p_180478_.getLevel();
            Vec3 vec3 = p_180478_.getPosition().add(5.0, 0.0, 0.0);
            serverlevel.playSeededSound(
                null, vec3.x, vec3.y, vec3.z, SoundEvents.RAID_HORN, SoundSource.NEUTRAL, 2.0F, 1.0F, serverlevel.random.nextLong()
            );
        }

        return 1;
    }

    private static int start(CommandSourceStack p_180485_, int p_180486_) throws CommandSyntaxException {
        ServerPlayer serverplayer = p_180485_.getPlayerOrException();
        BlockPos blockpos = serverplayer.blockPosition();
        if (serverplayer.serverLevel().isRaided(blockpos)) {
            p_180485_.sendFailure(Component.literal("Raid already started close by"));
            return -1;
        } else {
            Raids raids = serverplayer.serverLevel().getRaids();
            Raid raid = raids.createOrExtendRaid(serverplayer, serverplayer.blockPosition());
            if (raid != null) {
                raid.setRaidOmenLevel(p_180486_);
                raids.setDirty();
                p_180485_.sendSuccess(() -> Component.literal("Created a raid in your local village"), false);
            } else {
                p_180485_.sendFailure(Component.literal("Failed to create a raid in your local village"));
            }

            return 1;
        }
    }

    private static int stop(CommandSourceStack p_180490_) throws CommandSyntaxException {
        ServerPlayer serverplayer = p_180490_.getPlayerOrException();
        BlockPos blockpos = serverplayer.blockPosition();
        Raid raid = serverplayer.serverLevel().getRaidAt(blockpos);
        if (raid != null) {
            raid.stop();
            p_180490_.sendSuccess(() -> Component.literal("Stopped raid"), false);
            return 1;
        } else {
            p_180490_.sendFailure(Component.literal("No raid here"));
            return -1;
        }
    }

    private static int check(CommandSourceStack p_180494_) throws CommandSyntaxException {
        Raid raid = getRaid(p_180494_.getPlayerOrException());
        if (raid != null) {
            StringBuilder stringbuilder = new StringBuilder();
            stringbuilder.append("Found a started raid! ");
            p_180494_.sendSuccess(() -> Component.literal(stringbuilder.toString()), false);
            StringBuilder stringbuilder1 = new StringBuilder();
            stringbuilder1.append("Num groups spawned: ");
            stringbuilder1.append(raid.getGroupsSpawned());
            stringbuilder1.append(" Raid omen level: ");
            stringbuilder1.append(raid.getRaidOmenLevel());
            stringbuilder1.append(" Num mobs: ");
            stringbuilder1.append(raid.getTotalRaidersAlive());
            stringbuilder1.append(" Raid health: ");
            stringbuilder1.append(raid.getHealthOfLivingRaiders());
            stringbuilder1.append(" / ");
            stringbuilder1.append(raid.getTotalHealth());
            p_180494_.sendSuccess(() -> Component.literal(stringbuilder1.toString()), false);
            return 1;
        } else {
            p_180494_.sendFailure(Component.literal("Found no started raids"));
            return 0;
        }
    }

    @Nullable
    private static Raid getRaid(ServerPlayer p_180467_) {
        return p_180467_.serverLevel().getRaidAt(p_180467_.blockPosition());
    }
}