| 1 | package net.minecraft.world.entity.raid;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.MapCodec;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import java.util.Collection;
|
| 9 | import java.util.Comparator;
|
| 10 | import java.util.Iterator;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.Set;
|
| 15 | import java.util.UUID;
|
| 16 | import java.util.function.Predicate;
|
| 17 | import java.util.stream.Stream;
|
| 18 | import net.minecraft.SharedConstants;
|
| 19 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 20 | import net.minecraft.core.BlockPos;
|
| 21 | import net.minecraft.core.HolderGetter;
|
| 22 | import net.minecraft.core.SectionPos;
|
| 23 | import net.minecraft.core.UUIDUtil;
|
| 24 | import net.minecraft.core.component.DataComponentPatch;
|
| 25 | import net.minecraft.core.component.DataComponents;
|
| 26 | import net.minecraft.core.registries.Registries;
|
| 27 | import net.minecraft.network.chat.CommonComponents;
|
| 28 | import net.minecraft.network.chat.Component;
|
| 29 | import net.minecraft.network.protocol.game.ClientboundSoundPacket;
|
| 30 | import net.minecraft.server.level.ServerBossEvent;
|
| 31 | import net.minecraft.server.level.ServerLevel;
|
| 32 | import net.minecraft.server.level.ServerPlayer;
|
| 33 | import net.minecraft.sounds.SoundEvents;
|
| 34 | import net.minecraft.sounds.SoundSource;
|
| 35 | import net.minecraft.stats.Stats;
|
| 36 | import net.minecraft.util.Mth;
|
| 37 | import net.minecraft.util.RandomSource;
|
| 38 | import net.minecraft.util.StringRepresentable;
|
| 39 | import net.minecraft.world.BossEvent;
|
| 40 | import net.minecraft.world.Difficulty;
|
| 41 | import net.minecraft.world.DifficultyInstance;
|
| 42 | import net.minecraft.world.effect.MobEffectInstance;
|
| 43 | import net.minecraft.world.effect.MobEffects;
|
| 44 | import net.minecraft.world.entity.Entity;
|
| 45 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 46 | import net.minecraft.world.entity.EntityType;
|
| 47 | import net.minecraft.world.entity.EntityTypes;
|
| 48 | import net.minecraft.world.entity.EquipmentSlot;
|
| 49 | import net.minecraft.world.entity.LivingEntity;
|
| 50 | import net.minecraft.world.entity.SpawnPlacementType;
|
| 51 | import net.minecraft.world.entity.SpawnPlacements;
|
| 52 | import net.minecraft.world.item.DyeColor;
|
| 53 | import net.minecraft.world.item.ItemStack;
|
| 54 | import net.minecraft.world.item.ItemStackTemplate;
|
| 55 | import net.minecraft.world.item.Items;
|
| 56 | import net.minecraft.world.item.Rarity;
|
| 57 | import net.minecraft.world.item.component.TooltipDisplay;
|
| 58 | import net.minecraft.world.level.block.Blocks;
|
| 59 | import net.minecraft.world.level.block.entity.BannerPattern;
|
| 60 | import net.minecraft.world.level.block.entity.BannerPatternLayers;
|
| 61 | import net.minecraft.world.level.block.entity.BannerPatterns;
|
| 62 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 63 | import net.minecraft.world.phys.Vec3;
|
| 64 | import org.jspecify.annotations.Nullable;
|
| 65 |
|
| 66 | public class Raid {
|
| 67 | public static final SpawnPlacementType RAVAGER_SPAWN_PLACEMENT_TYPE = SpawnPlacements.getPlacementType(EntityTypes.RAVAGER);
|
| 68 | public static final MapCodec<Raid> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 69 | i -> i.group(
|
| 70 | Codec.BOOL.fieldOf("started").forGetter(r -> r.started),
|
| 71 | Codec.BOOL.fieldOf("active").forGetter(r -> r.active),
|
| 72 | Codec.LONG.fieldOf("ticks_active").forGetter(r -> r.ticksActive),
|
| 73 | Codec.INT.fieldOf("raid_omen_level").forGetter(r -> r.raidOmenLevel),
|
| 74 | Codec.INT.fieldOf("groups_spawned").forGetter(r -> r.groupsSpawned),
|
| 75 | Codec.INT.fieldOf("cooldown_ticks").forGetter(r -> r.raidCooldownTicks),
|
| 76 | Codec.INT.fieldOf("post_raid_ticks").forGetter(r -> r.postRaidTicks),
|
| 77 | Codec.FLOAT.fieldOf("total_health").forGetter(r -> r.totalHealth),
|
| 78 | Codec.INT.fieldOf("group_count").forGetter(r -> r.numGroups),
|
| 79 | Raid.RaidStatus.CODEC.fieldOf("status").forGetter(r -> r.status),
|
| 80 | BlockPos.CODEC.fieldOf("center").forGetter(r -> r.center),
|
| 81 | UUIDUtil.CODEC_SET.fieldOf("heroes_of_the_village").forGetter(r -> r.heroesOfTheVillage)
|
| 82 | )
|
| 83 | .apply(i, Raid::new)
|
| 84 | );
|
| 85 | private static final int ALLOW_SPAWNING_WITHIN_VILLAGE_SECONDS_THRESHOLD = 7;
|
| 86 | private static final int SECTION_RADIUS_FOR_FINDING_NEW_VILLAGE_CENTER = 2;
|
| 87 | private static final int VILLAGE_SEARCH_RADIUS = 32;
|
| 88 | private static final int RAID_TIMEOUT_TICKS = 48000;
|
| 89 | private static final int NUM_SPAWN_ATTEMPTS = 5;
|
| 90 | private static final Component OMINOUS_BANNER_PATTERN_NAME = Component.translatable("block.minecraft.ominous_banner");
|
| 91 | private static final String RAIDERS_REMAINING = "event.minecraft.raid.raiders_remaining";
|
| 92 | public static final int VILLAGE_RADIUS_BUFFER = 16;
|
| 93 | private static final int POST_RAID_TICK_LIMIT = 40;
|
| 94 | private static final int DEFAULT_PRE_RAID_TICKS = 300;
|
| 95 | public static final int MAX_NO_ACTION_TIME = 2400;
|
| 96 | public static final int MAX_CELEBRATION_TICKS = 600;
|
| 97 | private static final int OUTSIDE_RAID_BOUNDS_TIMEOUT = 30;
|
| 98 | public static final int DEFAULT_MAX_RAID_OMEN_LEVEL = 5;
|
| 99 | private static final int LOW_MOB_THRESHOLD = 2;
|
| 100 | private static final Component RAID_NAME_COMPONENT = Component.translatable("event.minecraft.raid");
|
| 101 | private static final Component RAID_BAR_VICTORY_COMPONENT = Component.translatable("event.minecraft.raid.victory.full");
|
| 102 | private static final Component RAID_BAR_DEFEAT_COMPONENT = Component.translatable("event.minecraft.raid.defeat.full");
|
| 103 | private static final int HERO_OF_THE_VILLAGE_DURATION = 48000;
|
| 104 | private static final int VALID_RAID_RADIUS = 96;
|
| 105 | public static final int VALID_RAID_RADIUS_SQR = 9216;
|
| 106 | public static final int RAID_REMOVAL_THRESHOLD_SQR = 12544;
|
| 107 | private final Map<Integer, Raider> groupToLeaderMap = Maps.newHashMap();
|
| 108 | private final Map<Integer, Set<Raider>> groupRaiderMap = Maps.newHashMap();
|
| 109 | private final Set<UUID> heroesOfTheVillage = Sets.newHashSet();
|
| 110 | private long ticksActive;
|
| 111 | private BlockPos center;
|
| 112 | private boolean started;
|
| 113 | private float totalHealth;
|
| 114 | private int raidOmenLevel;
|
| 115 | private boolean active;
|
| 116 | private int groupsSpawned;
|
| 117 | private int postRaidTicks;
|
| 118 | private int raidCooldownTicks;
|
| 119 | private final RandomSource random = RandomSource.create();
|
| 120 | private final ServerBossEvent raidEvent = new ServerBossEvent(
|
| 121 | Mth.createInsecureUUID(this.random), RAID_NAME_COMPONENT, BossEvent.BossBarColor.RED, BossEvent.BossBarOverlay.NOTCHED_10
|
| 122 | );
|
| 123 | private final int numGroups;
|
| 124 | private Raid.RaidStatus status;
|
| 125 | private int celebrationTicks;
|
| 126 | private Optional<BlockPos> waveSpawnPos = Optional.empty();
|
| 127 |
|
| 128 | public Raid(final BlockPos center, final Difficulty difficulty) {
|
| 129 | this.active = true;
|
| 130 | this.raidCooldownTicks = 300;
|
| 131 | this.raidEvent.setProgress(0.0F);
|
| 132 | this.center = center;
|
| 133 | this.numGroups = this.getNumGroups(difficulty);
|
| 134 | this.status = Raid.RaidStatus.ONGOING;
|
| 135 | }
|
| 136 |
|
| 137 | private Raid(
|
| 138 | final boolean started,
|
| 139 | final boolean active,
|
| 140 | final long ticksActive,
|
| 141 | final int raidOmenLevel,
|
| 142 | final int groupsSpawned,
|
| 143 | final int raidCooldownTicks,
|
| 144 | final int postRaidTicks,
|
| 145 | final float totalHealth,
|
| 146 | final int numGroups,
|
| 147 | final Raid.RaidStatus status,
|
| 148 | final BlockPos center,
|
| 149 | final Set<UUID> heroesOfTheVillage
|
| 150 | ) {
|
| 151 | this.started = started;
|
| 152 | this.active = active;
|
| 153 | this.ticksActive = ticksActive;
|
| 154 | this.raidOmenLevel = raidOmenLevel;
|
| 155 | this.groupsSpawned = groupsSpawned;
|
| 156 | this.raidCooldownTicks = raidCooldownTicks;
|
| 157 | this.postRaidTicks = postRaidTicks;
|
| 158 | this.totalHealth = totalHealth;
|
| 159 | this.center = center;
|
| 160 | this.numGroups = numGroups;
|
| 161 | this.status = status;
|
| 162 | this.heroesOfTheVillage.addAll(heroesOfTheVillage);
|
| 163 | }
|
| 164 |
|
| 165 | public boolean isOver() {
|
| 166 | return this.isVictory() || this.isLoss();
|
| 167 | }
|
| 168 |
|
| 169 | public boolean isBetweenWaves() {
|
| 170 | return this.hasFirstWaveSpawned() && this.getTotalRaidersAlive() == 0 && this.raidCooldownTicks > 0;
|
| 171 | }
|
| 172 |
|
| 173 | public boolean hasFirstWaveSpawned() {
|
| 174 | return this.groupsSpawned > 0;
|
| 175 | }
|
| 176 |
|
| 177 | public boolean isStopped() {
|
| 178 | return this.status == Raid.RaidStatus.STOPPED;
|
| 179 | }
|
| 180 |
|
| 181 | public boolean isVictory() {
|
| 182 | return this.status == Raid.RaidStatus.VICTORY;
|
| 183 | }
|
| 184 |
|
| 185 | public boolean isLoss() {
|
| 186 | return this.status == Raid.RaidStatus.LOSS;
|
| 187 | }
|
| 188 |
|
| 189 | public float getTotalHealth() {
|
| 190 | return this.totalHealth;
|
| 191 | }
|
| 192 |
|
| 193 | public Set<Raider> getAllRaiders() {
|
| 194 | Set<Raider> raiders = Sets.newHashSet();
|
| 195 |
|
| 196 | for (Set<Raider> raiderSet : this.groupRaiderMap.values()) {
|
| 197 | raiders.addAll(raiderSet);
|
| 198 | }
|
| 199 |
|
| 200 | return raiders;
|
| 201 | }
|
| 202 |
|
| 203 | public boolean isStarted() {
|
| 204 | return this.started;
|
| 205 | }
|
| 206 |
|
| 207 | public int getGroupsSpawned() {
|
| 208 | return this.groupsSpawned;
|
| 209 | }
|
| 210 |
|
| 211 | private Predicate<ServerPlayer> validPlayer() {
|
| 212 | return player -> {
|
| 213 | BlockPos pos = player.blockPosition();
|
| 214 | return player.isAlive() && player.level().getRaidAt(pos) == this;
|
| 215 | };
|
| 216 | }
|
| 217 |
|
| 218 | private void updatePlayers(final ServerLevel level) {
|
| 219 | Set<ServerPlayer> currentPlayersInRaid = Sets.newHashSet(this.raidEvent.getPlayers());
|
| 220 | List<ServerPlayer> newPlayersInRaid = level.getPlayers(this.validPlayer());
|
| 221 |
|
| 222 | for (ServerPlayer player : newPlayersInRaid) {
|
| 223 | if (!currentPlayersInRaid.contains(player)) {
|
| 224 | this.raidEvent.addPlayer(player);
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | for (ServerPlayer player : currentPlayersInRaid) {
|
| 229 | if (!newPlayersInRaid.contains(player)) {
|
| 230 | this.raidEvent.removePlayer(player);
|
| 231 | }
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | public int getMaxRaidOmenLevel() {
|
| 236 | return 5;
|
| 237 | }
|
| 238 |
|
| 239 | public int getRaidOmenLevel() {
|
| 240 | return this.raidOmenLevel;
|
| 241 | }
|
| 242 |
|
| 243 | public void setRaidOmenLevel(final int raidOmenLevel) {
|
| 244 | this.raidOmenLevel = raidOmenLevel;
|
| 245 | }
|
| 246 |
|
| 247 | public boolean absorbRaidOmen(final ServerPlayer player) {
|
| 248 | MobEffectInstance effect = player.getEffect(MobEffects.RAID_OMEN);
|
| 249 | if (effect == null) {
|
| 250 | return false;
|
| 251 | }
|
| 252 |
|
| 253 | this.raidOmenLevel = this.raidOmenLevel + effect.getAmplifier() + 1;
|
| 254 | this.raidOmenLevel = Mth.clamp(this.raidOmenLevel, 0, this.getMaxRaidOmenLevel());
|
| 255 | if (!this.hasFirstWaveSpawned()) {
|
| 256 | player.awardStat(Stats.RAID_TRIGGER);
|
| 257 | CriteriaTriggers.RAID_OMEN.trigger(player);
|
| 258 | }
|
| 259 |
|
| 260 | return true;
|
| 261 | }
|
| 262 |
|
| 263 | public void stop() {
|
| 264 | this.active = false;
|
| 265 | this.raidEvent.removeAllPlayers();
|
| 266 | this.status = Raid.RaidStatus.STOPPED;
|
| 267 | }
|
| 268 |
|
| 269 | public void tick(final ServerLevel level) {
|
| 270 | if (!this.isStopped()) {
|
| 271 | if (this.status == Raid.RaidStatus.ONGOING) {
|
| 272 | boolean oldActive = this.active;
|
| 273 | this.active = level.hasChunkAt(this.center);
|
| 274 | if (level.getDifficulty() == Difficulty.PEACEFUL) {
|
| 275 | this.stop();
|
| 276 | return;
|
| 277 | }
|
| 278 |
|
| 279 | if (oldActive != this.active) {
|
| 280 | this.raidEvent.setVisible(this.active);
|
| 281 | }
|
| 282 |
|
| 283 | if (!this.active) {
|
| 284 | return;
|
| 285 | }
|
| 286 |
|
| 287 | if (!level.isVillage(this.center)) {
|
| 288 | this.moveRaidCenterToNearbyVillageSection(level);
|
| 289 | }
|
| 290 |
|
| 291 | if (!level.isVillage(this.center)) {
|
| 292 | if (this.groupsSpawned > 0) {
|
| 293 | this.status = Raid.RaidStatus.LOSS;
|
| 294 | } else {
|
| 295 | this.stop();
|
| 296 | }
|
| 297 | }
|
| 298 |
|
| 299 | this.ticksActive++;
|
| 300 | if (this.ticksActive >= 48000L) {
|
| 301 | this.stop();
|
| 302 | return;
|
| 303 | }
|
| 304 |
|
| 305 | int raidersAlive = this.getTotalRaidersAlive();
|
| 306 | if (raidersAlive == 0 && this.hasMoreWaves()) {
|
| 307 | if (this.raidCooldownTicks <= 0) {
|
| 308 | if (this.raidCooldownTicks == 0 && this.groupsSpawned > 0) {
|
| 309 | this.raidCooldownTicks = 300;
|
| 310 | this.raidEvent.setName(RAID_NAME_COMPONENT);
|
| 311 | return;
|
| 312 | }
|
| 313 | } else {
|
| 314 | boolean hasCachedWaveSpawnPos = this.waveSpawnPos.isPresent();
|
| 315 | boolean shouldTryToFindSpawnPos = !hasCachedWaveSpawnPos && this.raidCooldownTicks % 5 == 0;
|
| 316 | if (hasCachedWaveSpawnPos && !level.isPositionEntityTicking(this.waveSpawnPos.get())) {
|
| 317 | shouldTryToFindSpawnPos = true;
|
| 318 | }
|
| 319 |
|
| 320 | if (shouldTryToFindSpawnPos) {
|
| 321 | this.waveSpawnPos = this.getValidSpawnPos(level);
|
| 322 | }
|
| 323 |
|
| 324 | if (this.raidCooldownTicks == 300 || this.raidCooldownTicks % 20 == 0) {
|
| 325 | this.updatePlayers(level);
|
| 326 | }
|
| 327 |
|
| 328 | this.raidCooldownTicks--;
|
| 329 | this.raidEvent.setProgress(Mth.clamp((300 - this.raidCooldownTicks) / 300.0F, 0.0F, 1.0F));
|
| 330 | }
|
| 331 | }
|
| 332 |
|
| 333 | if (this.ticksActive % 20L == 0L) {
|
| 334 | this.updatePlayers(level);
|
| 335 | this.updateRaiders(level);
|
| 336 | if (raidersAlive > 0) {
|
| 337 | if (raidersAlive <= 2) {
|
| 338 | this.raidEvent.setName(RAID_NAME_COMPONENT.copy().append(" - ").append(Component.translatable("event.minecraft.raid.raiders_remaining", raidersAlive)));
|
| 339 | } else {
|
| 340 | this.raidEvent.setName(RAID_NAME_COMPONENT);
|
| 341 | }
|
| 342 | } else {
|
| 343 | this.raidEvent.setName(RAID_NAME_COMPONENT);
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | if (SharedConstants.DEBUG_RAIDS) {
|
| 348 | this.raidEvent
|
| 349 | .setName(
|
| 350 | RAID_NAME_COMPONENT.copy()
|
| 351 | .append(" wave: ")
|
| 352 | .append(this.groupsSpawned + "")
|
| 353 | .append(CommonComponents.SPACE)
|
| 354 | .append("Raiders alive: ")
|
| 355 | .append(this.getTotalRaidersAlive() + "")
|
| 356 | .append(CommonComponents.SPACE)
|
| 357 | .append(this.getHealthOfLivingRaiders() + "")
|
| 358 | .append(" / ")
|
| 359 | .append(this.totalHealth + "")
|
| 360 | .append(" Is bonus? ")
|
| 361 | .append((this.hasBonusWave() && this.hasSpawnedBonusWave()) + "")
|
| 362 | .append(" Status: ")
|
| 363 | .append(this.status.getSerializedName())
|
| 364 | );
|
| 365 | }
|
| 366 |
|
| 367 | boolean soundPlayed = false;
|
| 368 | int attempt = 0;
|
| 369 |
|
| 370 | while (this.shouldSpawnGroup()) {
|
| 371 | BlockPos spawnPos = this.waveSpawnPos.orElseGet(() -> this.findRandomSpawnPos(level, 20));
|
| 372 | if (spawnPos != null) {
|
| 373 | this.started = true;
|
| 374 | this.spawnGroup(level, spawnPos);
|
| 375 | if (!soundPlayed) {
|
| 376 | this.playSound(level, spawnPos);
|
| 377 | soundPlayed = true;
|
| 378 | }
|
| 379 | } else {
|
| 380 | attempt++;
|
| 381 | }
|
| 382 |
|
| 383 | if (attempt > 5) {
|
| 384 | this.stop();
|
| 385 | break;
|
| 386 | }
|
| 387 | }
|
| 388 |
|
| 389 | if (this.isStarted() && !this.hasMoreWaves() && raidersAlive == 0) {
|
| 390 | if (this.postRaidTicks < 40) {
|
| 391 | this.postRaidTicks++;
|
| 392 | } else {
|
| 393 | this.status = Raid.RaidStatus.VICTORY;
|
| 394 |
|
| 395 | for (UUID heroUUID : this.heroesOfTheVillage) {
|
| 396 | Entity entity = level.getEntity(heroUUID);
|
| 397 | if (entity instanceof LivingEntity hero && !entity.isSpectator()) {
|
| 398 | hero.addEffect(new MobEffectInstance(MobEffects.HERO_OF_THE_VILLAGE, 48000, this.raidOmenLevel - 1, false, false, true));
|
| 399 | if (hero instanceof ServerPlayer playerHero) {
|
| 400 | playerHero.awardStat(Stats.RAID_WIN);
|
| 401 | CriteriaTriggers.RAID_WIN.trigger(playerHero);
|
| 402 | }
|
| 403 | }
|
| 404 | }
|
| 405 | }
|
| 406 | }
|
| 407 |
|
| 408 | this.setDirty(level);
|
| 409 | } else if (this.isOver()) {
|
| 410 | this.celebrationTicks++;
|
| 411 | if (this.celebrationTicks >= 600) {
|
| 412 | this.stop();
|
| 413 | return;
|
| 414 | }
|
| 415 |
|
| 416 | if (this.celebrationTicks % 20 == 0) {
|
| 417 | this.updatePlayers(level);
|
| 418 | this.raidEvent.setVisible(true);
|
| 419 | if (this.isVictory()) {
|
| 420 | this.raidEvent.setProgress(0.0F);
|
| 421 | this.raidEvent.setName(RAID_BAR_VICTORY_COMPONENT);
|
| 422 | } else {
|
| 423 | this.raidEvent.setName(RAID_BAR_DEFEAT_COMPONENT);
|
| 424 | }
|
| 425 | }
|
| 426 | }
|
| 427 | }
|
| 428 | }
|
| 429 |
|
| 430 | private void moveRaidCenterToNearbyVillageSection(final ServerLevel level) {
|
| 431 | Stream<SectionPos> sectionsToSearchForVillage = SectionPos.cube(SectionPos.of(this.center), 2);
|
| 432 | sectionsToSearchForVillage.filter(level::isVillage)
|
| 433 | .map(SectionPos::center)
|
| 434 | .min(Comparator.comparingDouble(pos -> pos.distSqr(this.center)))
|
| 435 | .ifPresent(this::setCenter);
|
| 436 | }
|
| 437 |
|
| 438 | private Optional<BlockPos> getValidSpawnPos(final ServerLevel level) {
|
| 439 | BlockPos spawnPos = this.findRandomSpawnPos(level, 8);
|
| 440 | return spawnPos != null ? Optional.of(spawnPos) : Optional.empty();
|
| 441 | }
|
| 442 |
|
| 443 | private boolean hasMoreWaves() {
|
| 444 | return this.hasBonusWave() ? !this.hasSpawnedBonusWave() : !this.isFinalWave();
|
| 445 | }
|
| 446 |
|
| 447 | private boolean isFinalWave() {
|
| 448 | return this.getGroupsSpawned() == this.numGroups;
|
| 449 | }
|
| 450 |
|
| 451 | private boolean hasBonusWave() {
|
| 452 | return this.raidOmenLevel > 1;
|
| 453 | }
|
| 454 |
|
| 455 | private boolean hasSpawnedBonusWave() {
|
| 456 | return this.getGroupsSpawned() > this.numGroups;
|
| 457 | }
|
| 458 |
|
| 459 | private boolean shouldSpawnBonusGroup() {
|
| 460 | return this.isFinalWave() && this.getTotalRaidersAlive() == 0 && this.hasBonusWave();
|
| 461 | }
|
| 462 |
|
| 463 | private void updateRaiders(final ServerLevel level) {
|
| 464 | Iterator<Set<Raider>> raiders = this.groupRaiderMap.values().iterator();
|
| 465 | Set<Raider> toRemove = Sets.newHashSet();
|
| 466 |
|
| 467 | while (raiders.hasNext()) {
|
| 468 | Set<Raider> raiderSet = raiders.next();
|
| 469 |
|
| 470 | for (Raider raider : raiderSet) {
|
| 471 | BlockPos raiderPos = raider.blockPosition();
|
| 472 | if (raider.isRemoved() || raider.level().dimension() != level.dimension() || this.center.distSqr(raiderPos) >= 12544.0) {
|
| 473 | toRemove.add(raider);
|
| 474 | } else if (raider.tickCount > 600) {
|
| 475 | if (level.getEntity(raider.getUUID()) == null) {
|
| 476 | toRemove.add(raider);
|
| 477 | }
|
| 478 |
|
| 479 | if (!level.isVillage(raiderPos) && raider.getNoActionTime() > 2400) {
|
| 480 | raider.setTicksOutsideRaid(raider.getTicksOutsideRaid() + 1);
|
| 481 | }
|
| 482 |
|
| 483 | if (raider.getTicksOutsideRaid() >= 30) {
|
| 484 | toRemove.add(raider);
|
| 485 | }
|
| 486 | }
|
| 487 | }
|
| 488 | }
|
| 489 |
|
| 490 | for (Raider raider : toRemove) {
|
| 491 | this.removeFromRaid(level, raider, true);
|
| 492 | if (raider.isPatrolLeader()) {
|
| 493 | this.removeLeader(raider.getWave());
|
| 494 | }
|
| 495 | }
|
| 496 | }
|
| 497 |
|
| 498 | private void playSound(final ServerLevel level, final BlockPos soundOrigin) {
|
| 499 | float distAway = 13.0F;
|
| 500 | int range = 64;
|
| 501 | Collection<ServerPlayer> playersInRaid = this.raidEvent.getPlayers();
|
| 502 | long seed = this.random.nextLong();
|
| 503 |
|
| 504 | for (ServerPlayer player : level.players()) {
|
| 505 | Vec3 playerLoc = player.position();
|
| 506 | Vec3 raidLoc = Vec3.atCenterOf(soundOrigin);
|
| 507 | double distBtwn = Math.sqrt((raidLoc.x - playerLoc.x) * (raidLoc.x - playerLoc.x) + (raidLoc.z - playerLoc.z) * (raidLoc.z - playerLoc.z));
|
| 508 | double x3 = playerLoc.x + 13.0 / distBtwn * (raidLoc.x - playerLoc.x);
|
| 509 | double z3 = playerLoc.z + 13.0 / distBtwn * (raidLoc.z - playerLoc.z);
|
| 510 | if (distBtwn <= 64.0 || playersInRaid.contains(player)) {
|
| 511 | player.connection.send(new ClientboundSoundPacket(SoundEvents.RAID_HORN, SoundSource.NEUTRAL, x3, player.getY(), z3, 64.0F, 1.0F, seed));
|
| 512 | }
|
| 513 | }
|
| 514 | }
|
| 515 |
|
| 516 | private void spawnGroup(final ServerLevel level, final BlockPos pos) {
|
| 517 | boolean leaderSet = false;
|
| 518 | int groupNumber = this.groupsSpawned + 1;
|
| 519 | this.totalHealth = 0.0F;
|
| 520 | DifficultyInstance difficulty = level.getCurrentDifficultyAt(pos);
|
| 521 | boolean isBonusGroup = this.shouldSpawnBonusGroup();
|
| 522 |
|
| 523 | for (Raid.RaiderType raiderType : Raid.RaiderType.VALUES) {
|
| 524 | int numSpawns = this.getDefaultNumSpawns(raiderType, groupNumber, isBonusGroup)
|
| 525 | + this.getPotentialBonusSpawns(raiderType, this.random, groupNumber, difficulty, isBonusGroup);
|
| 526 | int ravagersSpawned = 0;
|
| 527 |
|
| 528 | for (int i = 0; i < numSpawns; i++) {
|
| 529 | Raider raider = raiderType.entityType.create(level, EntitySpawnReason.EVENT);
|
| 530 | if (raider == null) {
|
| 531 | break;
|
| 532 | }
|
| 533 |
|
| 534 | if (!leaderSet && raider.canBeLeader()) {
|
| 535 | raider.setPatrolLeader(true);
|
| 536 | this.setLeader(groupNumber, raider);
|
| 537 | leaderSet = true;
|
| 538 | }
|
| 539 |
|
| 540 | this.joinRaid(level, groupNumber, raider, pos, false);
|
| 541 | if (raiderType.entityType == EntityTypes.RAVAGER) {
|
| 542 | Raider ridingRaider = null;
|
| 543 | if (groupNumber == this.getNumGroups(Difficulty.NORMAL)) {
|
| 544 | ridingRaider = EntityTypes.PILLAGER.create(level, EntitySpawnReason.EVENT);
|
| 545 | } else if (groupNumber >= this.getNumGroups(Difficulty.HARD)) {
|
| 546 | if (ravagersSpawned == 0) {
|
| 547 | ridingRaider = EntityTypes.EVOKER.create(level, EntitySpawnReason.EVENT);
|
| 548 | } else {
|
| 549 | ridingRaider = EntityTypes.VINDICATOR.create(level, EntitySpawnReason.EVENT);
|
| 550 | }
|
| 551 | }
|
| 552 |
|
| 553 | ravagersSpawned++;
|
| 554 | if (ridingRaider != null) {
|
| 555 | this.joinRaid(level, groupNumber, ridingRaider, pos, false);
|
| 556 | ridingRaider.snapTo(pos, 0.0F, 0.0F);
|
| 557 | ridingRaider.startRiding(raider, false, false);
|
| 558 | }
|
| 559 | }
|
| 560 | }
|
| 561 | }
|
| 562 |
|
| 563 | this.waveSpawnPos = Optional.empty();
|
| 564 | this.groupsSpawned++;
|
| 565 | this.updateBossbar();
|
| 566 | this.setDirty(level);
|
| 567 | }
|
| 568 |
|
| 569 | public void joinRaid(final ServerLevel level, final int groupNumber, final Raider raider, final @Nullable BlockPos pos, final boolean exists) {
|
| 570 | boolean added = this.addWaveMob(level, groupNumber, raider);
|
| 571 | if (added) {
|
| 572 | raider.setCurrentRaid(this);
|
| 573 | raider.setWave(groupNumber);
|
| 574 | raider.setCanJoinRaid(true);
|
| 575 | raider.setTicksOutsideRaid(0);
|
| 576 | if (!exists && pos != null) {
|
| 577 | raider.setPos(pos.getX() + 0.5, pos.getY() + 1.0, pos.getZ() + 0.5);
|
| 578 | raider.finalizeSpawn(level, level.getCurrentDifficultyAt(pos), EntitySpawnReason.EVENT, null);
|
| 579 | raider.applyRaidBuffs(level, groupNumber, false);
|
| 580 | raider.setOnGround(true);
|
| 581 | level.addFreshEntityWithPassengers(raider);
|
| 582 | }
|
| 583 | }
|
| 584 | }
|
| 585 |
|
| 586 | public void updateBossbar() {
|
| 587 | this.raidEvent.setProgress(Mth.clamp(this.getHealthOfLivingRaiders() / this.totalHealth, 0.0F, 1.0F));
|
| 588 | }
|
| 589 |
|
| 590 | public float getHealthOfLivingRaiders() {
|
| 591 | float health = 0.0F;
|
| 592 |
|
| 593 | for (Set<Raider> raiders : this.groupRaiderMap.values()) {
|
| 594 | for (Raider raider : raiders) {
|
| 595 | health += raider.getHealth();
|
| 596 | }
|
| 597 | }
|
| 598 |
|
| 599 | return health;
|
| 600 | }
|
| 601 |
|
| 602 | private boolean shouldSpawnGroup() {
|
| 603 | return this.raidCooldownTicks == 0 && (this.groupsSpawned < this.numGroups || this.shouldSpawnBonusGroup()) && this.getTotalRaidersAlive() == 0;
|
| 604 | }
|
| 605 |
|
| 606 | public int getTotalRaidersAlive() {
|
| 607 | return this.groupRaiderMap.values().stream().mapToInt(Set::size).sum();
|
| 608 | }
|
| 609 |
|
| 610 | public void removeFromRaid(final ServerLevel level, final Raider raider, final boolean removeFromTotalHealth) {
|
| 611 | Set<Raider> raiders = this.groupRaiderMap.get(raider.getWave());
|
| 612 | if (raiders != null) {
|
| 613 | boolean couldRemove = raiders.remove(raider);
|
| 614 | if (couldRemove) {
|
| 615 | if (removeFromTotalHealth) {
|
| 616 | this.totalHealth = this.totalHealth - raider.getHealth();
|
| 617 | }
|
| 618 |
|
| 619 | raider.setCurrentRaid(null);
|
| 620 | this.updateBossbar();
|
| 621 | this.setDirty(level);
|
| 622 | }
|
| 623 | }
|
| 624 | }
|
| 625 |
|
| 626 | private void setDirty(final ServerLevel level) {
|
| 627 | level.getRaids().setDirty();
|
| 628 | }
|
| 629 |
|
| 630 | public static DataComponentPatch getBannerComponentPatch(final HolderGetter<BannerPattern> patternGetter) {
|
| 631 | DataComponentPatch.Builder builder = DataComponentPatch.builder();
|
| 632 | BannerPatternLayers patterns = new BannerPatternLayers.Builder()
|
| 633 | .addIfRegistered(patternGetter, BannerPatterns.RHOMBUS_MIDDLE, DyeColor.CYAN)
|
| 634 | .addIfRegistered(patternGetter, BannerPatterns.STRIPE_BOTTOM, DyeColor.LIGHT_GRAY)
|
| 635 | .addIfRegistered(patternGetter, BannerPatterns.STRIPE_CENTER, DyeColor.GRAY)
|
| 636 | .addIfRegistered(patternGetter, BannerPatterns.BORDER, DyeColor.LIGHT_GRAY)
|
| 637 | .addIfRegistered(patternGetter, BannerPatterns.STRIPE_MIDDLE, DyeColor.BLACK)
|
| 638 | .addIfRegistered(patternGetter, BannerPatterns.HALF_HORIZONTAL, DyeColor.LIGHT_GRAY)
|
| 639 | .addIfRegistered(patternGetter, BannerPatterns.CIRCLE_MIDDLE, DyeColor.LIGHT_GRAY)
|
| 640 | .addIfRegistered(patternGetter, BannerPatterns.BORDER, DyeColor.BLACK)
|
| 641 | .build();
|
| 642 | builder.set(DataComponents.BANNER_PATTERNS, patterns);
|
| 643 | builder.set(DataComponents.TOOLTIP_DISPLAY, TooltipDisplay.DEFAULT.withHidden(DataComponents.BANNER_PATTERNS, true));
|
| 644 | builder.set(DataComponents.ITEM_NAME, OMINOUS_BANNER_PATTERN_NAME);
|
| 645 | builder.set(DataComponents.RARITY, Rarity.UNCOMMON);
|
| 646 | return builder.build();
|
| 647 | }
|
| 648 |
|
| 649 | public static ItemStackTemplate getOminousBannerTemplate(final HolderGetter<BannerPattern> patternGetter) {
|
| 650 | return new ItemStackTemplate(Items.BANNER.white(), getBannerComponentPatch(patternGetter));
|
| 651 | }
|
| 652 |
|
| 653 | public static ItemStack getOminousBannerInstance(final HolderGetter<BannerPattern> patternGetter) {
|
| 654 | return getOminousBannerTemplate(patternGetter).create();
|
| 655 | }
|
| 656 |
|
| 657 | public @Nullable Raider getLeader(final int wave) {
|
| 658 | return this.groupToLeaderMap.get(wave);
|
| 659 | }
|
| 660 |
|
| 661 | private @Nullable BlockPos findRandomSpawnPos(final ServerLevel level, final int maxTries) {
|
| 662 | int secondsRemaining = this.raidCooldownTicks / 20;
|
| 663 | float howFar = 0.22F * secondsRemaining - 0.24F;
|
| 664 | BlockPos.MutableBlockPos spawnPos = new BlockPos.MutableBlockPos();
|
| 665 | float startAngle = this.random.nextFloat() * (float) (Math.PI * 2);
|
| 666 |
|
| 667 | for (int i = 0; i < maxTries; i++) {
|
| 668 | float angle = startAngle + (float) Math.PI * i / 8.0F;
|
| 669 | int spawnX = this.center.getX() + Mth.floor(Mth.cos(angle) * 32.0F * howFar) + this.random.nextInt(3) * Mth.floor(howFar);
|
| 670 | int spawnZ = this.center.getZ() + Mth.floor(Mth.sin(angle) * 32.0F * howFar) + this.random.nextInt(3) * Mth.floor(howFar);
|
| 671 | int spawnY = level.getHeight(Heightmap.Types.WORLD_SURFACE, spawnX, spawnZ);
|
| 672 | if (Mth.abs(spawnY - this.center.getY()) <= 96) {
|
| 673 | spawnPos.set(spawnX, spawnY, spawnZ);
|
| 674 | if (!level.isVillage(spawnPos) || secondsRemaining <= 7) {
|
| 675 | int delta = 10;
|
| 676 | if (level.hasChunksAt(spawnPos.getX() - 10, spawnPos.getZ() - 10, spawnPos.getX() + 10, spawnPos.getZ() + 10)
|
| 677 | && level.isPositionEntityTicking(spawnPos)
|
| 678 | && (
|
| 679 | RAVAGER_SPAWN_PLACEMENT_TYPE.isSpawnPositionOk(level, spawnPos, EntityTypes.RAVAGER)
|
| 680 | || level.getBlockState(spawnPos.below()).is(Blocks.SNOW) && level.getBlockState(spawnPos).isAir()
|
| 681 | )) {
|
| 682 | return spawnPos;
|
| 683 | }
|
| 684 | }
|
| 685 | }
|
| 686 | }
|
| 687 |
|
| 688 | return null;
|
| 689 | }
|
| 690 |
|
| 691 | private boolean addWaveMob(final ServerLevel level, final int wave, final Raider raider) {
|
| 692 | return this.addWaveMob(level, wave, raider, true);
|
| 693 | }
|
| 694 |
|
| 695 | public boolean addWaveMob(final ServerLevel level, final int wave, final Raider raider, final boolean updateHealth) {
|
| 696 | this.groupRaiderMap.computeIfAbsent(wave, v -> Sets.newHashSet());
|
| 697 | Set<Raider> raiders = this.groupRaiderMap.get(wave);
|
| 698 | Raider existingCopy = null;
|
| 699 |
|
| 700 | for (Raider r : raiders) {
|
| 701 | if (r.getUUID().equals(raider.getUUID())) {
|
| 702 | existingCopy = r;
|
| 703 | break;
|
| 704 | }
|
| 705 | }
|
| 706 |
|
| 707 | if (existingCopy != null) {
|
| 708 | raiders.remove(existingCopy);
|
| 709 | raiders.add(raider);
|
| 710 | }
|
| 711 |
|
| 712 | raiders.add(raider);
|
| 713 | if (updateHealth) {
|
| 714 | this.totalHealth = this.totalHealth + raider.getHealth();
|
| 715 | }
|
| 716 |
|
| 717 | this.updateBossbar();
|
| 718 | this.setDirty(level);
|
| 719 | return true;
|
| 720 | }
|
| 721 |
|
| 722 | public void setLeader(final int wave, final Raider raider) {
|
| 723 | this.groupToLeaderMap.put(wave, raider);
|
| 724 | raider.setItemSlot(EquipmentSlot.HEAD, getOminousBannerInstance(raider.registryAccess().lookupOrThrow(Registries.BANNER_PATTERN)));
|
| 725 | raider.setDropChance(EquipmentSlot.HEAD, 2.0F);
|
| 726 | }
|
| 727 |
|
| 728 | public void removeLeader(final int wave) {
|
| 729 | this.groupToLeaderMap.remove(wave);
|
| 730 | }
|
| 731 |
|
| 732 | public BlockPos getCenter() {
|
| 733 | return this.center;
|
| 734 | }
|
| 735 |
|
| 736 | private void setCenter(final BlockPos center) {
|
| 737 | this.center = center;
|
| 738 | }
|
| 739 |
|
| 740 | private int getDefaultNumSpawns(final Raid.RaiderType type, final int wav, final boolean isBonusWave) {
|
| 741 | return isBonusWave ? type.spawnsPerWaveBeforeBonus[this.numGroups] : type.spawnsPerWaveBeforeBonus[wav];
|
| 742 | }
|
| 743 |
|
| 744 | private int getPotentialBonusSpawns(
|
| 745 | final Raid.RaiderType type, final RandomSource random, final int wav, final DifficultyInstance difficultyInstance, final boolean isBonusWave
|
| 746 | ) {
|
| 747 | Difficulty difficulty = difficultyInstance.getDifficulty();
|
| 748 | boolean isEasy = difficulty == Difficulty.EASY;
|
| 749 | boolean isNormal = difficulty == Difficulty.NORMAL;
|
| 750 | int bonusSpawns;
|
| 751 | switch (type) {
|
| 752 | case VINDICATOR:
|
| 753 | case PILLAGER:
|
| 754 | if (isEasy) {
|
| 755 | bonusSpawns = random.nextInt(2);
|
| 756 | } else if (isNormal) {
|
| 757 | bonusSpawns = 1;
|
| 758 | } else {
|
| 759 | bonusSpawns = 2;
|
| 760 | }
|
| 761 | break;
|
| 762 | case EVOKER:
|
| 763 | default:
|
| 764 | return 0;
|
| 765 | case WITCH:
|
| 766 | if (isEasy || wav <= 2 || wav == 4) {
|
| 767 | return 0;
|
| 768 | }
|
| 769 |
|
| 770 | bonusSpawns = 1;
|
| 771 | break;
|
| 772 | case RAVAGER:
|
| 773 | bonusSpawns = !isEasy && isBonusWave ? 1 : 0;
|
| 774 | }
|
| 775 |
|
| 776 | return bonusSpawns > 0 ? random.nextInt(bonusSpawns + 1) : 0;
|
| 777 | }
|
| 778 |
|
| 779 | public boolean isActive() {
|
| 780 | return this.active;
|
| 781 | }
|
| 782 |
|
| 783 | public int getNumGroups(final Difficulty difficulty) {
|
| 784 | return switch (difficulty) {
|
| 785 | case PEACEFUL -> 0;
|
| 786 | case EASY -> 3;
|
| 787 | case NORMAL -> 5;
|
| 788 | case HARD -> 7;
|
| 789 | };
|
| 790 | }
|
| 791 |
|
| 792 | public float getEnchantOdds() {
|
| 793 | int raidOmenLvl = this.getRaidOmenLevel();
|
| 794 | if (raidOmenLvl == 2) {
|
| 795 | return 0.1F;
|
| 796 | } else if (raidOmenLvl == 3) {
|
| 797 | return 0.25F;
|
| 798 | } else if (raidOmenLvl == 4) {
|
| 799 | return 0.5F;
|
| 800 | } else {
|
| 801 | return raidOmenLvl == 5 ? 0.75F : 0.0F;
|
| 802 | }
|
| 803 | }
|
| 804 |
|
| 805 | public void addHeroOfTheVillage(final Entity killer) {
|
| 806 | this.heroesOfTheVillage.add(killer.getUUID());
|
| 807 | }
|
| 808 |
|
| 809 | private enum RaidStatus implements StringRepresentable {
|
| 810 | ONGOING("ongoing"),
|
| 811 | VICTORY("victory"),
|
| 812 | LOSS("loss"),
|
| 813 | STOPPED("stopped");
|
| 814 |
|
| 815 | public static final Codec<Raid.RaidStatus> CODEC = StringRepresentable.fromEnum(Raid.RaidStatus::values);
|
| 816 | private final String name;
|
| 817 |
|
| 818 | RaidStatus(final String name) {
|
| 819 | this.name = name;
|
| 820 | }
|
| 821 |
|
| 822 | @Override
|
| 823 | public String getSerializedName() {
|
| 824 | return this.name;
|
| 825 | }
|
| 826 | }
|
| 827 |
|
| 828 | private enum RaiderType {
|
| 829 | VINDICATOR(EntityTypes.VINDICATOR, new int[]{0, 0, 2, 0, 1, 4, 2, 5}),
|
| 830 | EVOKER(EntityTypes.EVOKER, new int[]{0, 0, 0, 0, 0, 1, 1, 2}),
|
| 831 | PILLAGER(EntityTypes.PILLAGER, new int[]{0, 4, 3, 3, 4, 4, 4, 2}),
|
| 832 | WITCH(EntityTypes.WITCH, new int[]{0, 0, 0, 0, 3, 0, 0, 1}),
|
| 833 | RAVAGER(EntityTypes.RAVAGER, new int[]{0, 0, 0, 1, 0, 1, 0, 2});
|
| 834 |
|
| 835 | private static final Raid.RaiderType[] VALUES = values();
|
| 836 | private final EntityType<? extends Raider> entityType;
|
| 837 | private final int[] spawnsPerWaveBeforeBonus;
|
| 838 |
|
| 839 | RaiderType(final EntityType<? extends Raider> entityType, final int[] spawnsPerWaveBeforeBonus) {
|
| 840 | this.entityType = entityType;
|
| 841 | this.spawnsPerWaveBeforeBonus = spawnsPerWaveBeforeBonus;
|
| 842 | }
|
| 843 | }
|
| 844 | }
|
| 845 |
|