| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.component.DataComponents;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.util.Util;
|
| 11 | import net.minecraft.world.entity.npc.villager.Villager;
|
| 12 | import net.minecraft.world.entity.projectile.FireworkRocketEntity;
|
| 13 | import net.minecraft.world.entity.projectile.Projectile;
|
| 14 | import net.minecraft.world.entity.raid.Raid;
|
| 15 | import net.minecraft.world.item.DyeColor;
|
| 16 | import net.minecraft.world.item.ItemStack;
|
| 17 | import net.minecraft.world.item.Items;
|
| 18 | import net.minecraft.world.item.component.FireworkExplosion;
|
| 19 | import net.minecraft.world.item.component.Fireworks;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | public class CelebrateVillagersSurvivedRaid extends Behavior<Villager> {
|
| 23 | private @Nullable Raid currentRaid;
|
| 24 |
|
| 25 | public CelebrateVillagersSurvivedRaid(final int minDuration, final int maxDuration) {
|
| 26 | super(ImmutableMap.of(), minDuration, maxDuration);
|
| 27 | }
|
| 28 |
|
| 29 | protected boolean checkExtraStartConditions(final ServerLevel level, final Villager body) {
|
| 30 | BlockPos testPos = body.blockPosition();
|
| 31 | this.currentRaid = level.getRaidAt(testPos);
|
| 32 | return this.currentRaid != null && this.currentRaid.isVictory() && MoveToSkySeeingSpot.hasNoBlocksAbove(level, body, testPos);
|
| 33 | }
|
| 34 |
|
| 35 | protected boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp) {
|
| 36 | return this.currentRaid != null && !this.currentRaid.isStopped();
|
| 37 | }
|
| 38 |
|
| 39 | protected void stop(final ServerLevel level, final Villager body, final long timestamp) {
|
| 40 | this.currentRaid = null;
|
| 41 | body.getBrain().updateActivityFromSchedule(level.environmentAttributes(), level.getGameTime(), body.position());
|
| 42 | }
|
| 43 |
|
| 44 | protected void tick(final ServerLevel level, final Villager body, final long timestamp) {
|
| 45 | RandomSource random = body.getRandom();
|
| 46 | if (random.nextInt(100) == 0) {
|
| 47 | body.playCelebrateSound();
|
| 48 | }
|
| 49 |
|
| 50 | if (random.nextInt(200) == 0 && MoveToSkySeeingSpot.hasNoBlocksAbove(level, body, body.blockPosition())) {
|
| 51 | DyeColor color = Util.getRandom(DyeColor.values(), random);
|
| 52 | int flightDuration = random.nextInt(3);
|
| 53 | ItemStack firework = this.getFirework(color, flightDuration);
|
| 54 | Projectile.spawnProjectile(new FireworkRocketEntity(body.level(), body, body.getX(), body.getEyeY(), body.getZ(), firework), level, firework);
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | private ItemStack getFirework(final DyeColor color, final int flightDuration) {
|
| 59 | ItemStack rocket = new ItemStack(Items.FIREWORK_ROCKET);
|
| 60 | rocket.set(
|
| 61 | DataComponents.FIREWORKS,
|
| 62 | new Fireworks(
|
| 63 | (byte)flightDuration, List.of(new FireworkExplosion(FireworkExplosion.Shape.BURST, IntList.of(color.getFireworkColor()), IntList.of(), false, false))
|
| 64 | )
|
| 65 | );
|
| 66 | return rocket;
|
| 67 | }
|
| 68 | }
|
| 69 |
|