| 1 | package net.minecraft.world.entity.raid;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 6 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 7 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap.Entry;
|
| 8 | import java.util.Iterator;
|
| 9 | import java.util.List;
|
| 10 | import java.util.OptionalInt;
|
| 11 | import net.minecraft.core.BlockPos;
|
| 12 | import net.minecraft.nbt.CompoundTag;
|
| 13 | import net.minecraft.nbt.NbtOps;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.server.level.ServerPlayer;
|
| 17 | import net.minecraft.tags.PoiTypeTags;
|
| 18 | import net.minecraft.util.VisibleForDebug;
|
| 19 | import net.minecraft.util.datafix.DataFixTypes;
|
| 20 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 21 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 22 | import net.minecraft.world.entity.ai.village.poi.PoiRecord;
|
| 23 | import net.minecraft.world.level.ChunkPos;
|
| 24 | import net.minecraft.world.level.gamerules.GameRules;
|
| 25 | import net.minecraft.world.level.saveddata.SavedData;
|
| 26 | import net.minecraft.world.level.saveddata.SavedDataType;
|
| 27 | import net.minecraft.world.phys.Vec3;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | public class Raids extends SavedData {
|
| 31 | private static final Identifier RAID_FILE_ID = Identifier.withDefaultNamespace("raids");
|
| 32 | public static final Codec<Raids> CODEC = RecordCodecBuilder.create(
|
| 33 | i -> i.group(
|
| 34 | Raids.RaidWithId.CODEC
|
| 35 | .listOf()
|
| 36 | .optionalFieldOf("raids", List.of())
|
| 37 | .forGetter(r -> r.raidMap.int2ObjectEntrySet().stream().map(Raids.RaidWithId::from).toList()),
|
| 38 | Codec.INT.fieldOf("next_id").forGetter(r -> r.nextId),
|
| 39 | Codec.INT.fieldOf("tick").forGetter(r -> r.tick)
|
| 40 | )
|
| 41 | .apply(i, Raids::new)
|
| 42 | );
|
| 43 | public static final SavedDataType<Raids> TYPE = new SavedDataType<>(RAID_FILE_ID, Raids::new, CODEC, DataFixTypes.SAVED_DATA_RAIDS);
|
| 44 | private final Int2ObjectMap<Raid> raidMap = new Int2ObjectOpenHashMap<>();
|
| 45 | private int nextId = 1;
|
| 46 | private int tick;
|
| 47 |
|
| 48 | public Raids() {
|
| 49 | this.setDirty();
|
| 50 | }
|
| 51 |
|
| 52 | private Raids(final List<Raids.RaidWithId> raids, final int nextId, final int tick) {
|
| 53 | for (Raids.RaidWithId raid : raids) {
|
| 54 | this.raidMap.put(raid.id, raid.raid);
|
| 55 | }
|
| 56 |
|
| 57 | this.nextId = nextId;
|
| 58 | this.tick = tick;
|
| 59 | }
|
| 60 |
|
| 61 | public @Nullable Raid get(final int raidId) {
|
| 62 | return this.raidMap.get(raidId);
|
| 63 | }
|
| 64 |
|
| 65 | public OptionalInt getId(final Raid raid) {
|
| 66 | for (Entry<Raid> entry : this.raidMap.int2ObjectEntrySet()) {
|
| 67 | if (entry.getValue() == raid) {
|
| 68 | return OptionalInt.of(entry.getIntKey());
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | return OptionalInt.empty();
|
| 73 | }
|
| 74 |
|
| 75 | public void tick(final ServerLevel level) {
|
| 76 | this.tick++;
|
| 77 | Iterator<Raid> raidIterator = this.raidMap.values().iterator();
|
| 78 |
|
| 79 | while (raidIterator.hasNext()) {
|
| 80 | Raid raid = raidIterator.next();
|
| 81 | if (!level.getGameRules().get(GameRules.RAIDS)) {
|
| 82 | raid.stop();
|
| 83 | }
|
| 84 |
|
| 85 | if (raid.isStopped()) {
|
| 86 | raidIterator.remove();
|
| 87 | this.setDirty();
|
| 88 | } else {
|
| 89 | raid.tick(level);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | if (this.tick % 200 == 0) {
|
| 94 | this.setDirty();
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | public static boolean canJoinRaid(final Raider raider) {
|
| 99 | return raider.isAlive() && raider.canJoinRaid() && raider.getNoActionTime() <= 2400;
|
| 100 | }
|
| 101 |
|
| 102 | public @Nullable Raid createOrExtendRaid(final ServerPlayer player, final BlockPos raidPosition) {
|
| 103 | if (player.isSpectator()) {
|
| 104 | return null;
|
| 105 | }
|
| 106 |
|
| 107 | ServerLevel level = player.level();
|
| 108 | if (!level.getGameRules().get(GameRules.RAIDS)) {
|
| 109 | return null;
|
| 110 | }
|
| 111 |
|
| 112 | if (!level.environmentAttributes().getValue(EnvironmentAttributes.CAN_START_RAID, raidPosition)) {
|
| 113 | return null;
|
| 114 | }
|
| 115 |
|
| 116 | List<PoiRecord> posses = level.getPoiManager().getInRange(e -> e.is(PoiTypeTags.VILLAGE), raidPosition, 64, PoiManager.Occupancy.IS_OCCUPIED).toList();
|
| 117 | int count = 0;
|
| 118 | Vec3 posTotals = Vec3.ZERO;
|
| 119 |
|
| 120 | for (PoiRecord p : posses) {
|
| 121 | BlockPos pos = p.getPos();
|
| 122 | posTotals = posTotals.add(pos.getX(), pos.getY(), pos.getZ());
|
| 123 | count++;
|
| 124 | }
|
| 125 |
|
| 126 | BlockPos raidCenterPos;
|
| 127 | if (count > 0) {
|
| 128 | posTotals = posTotals.scale(1.0 / count);
|
| 129 | raidCenterPos = BlockPos.containing(posTotals);
|
| 130 | } else {
|
| 131 | raidCenterPos = raidPosition;
|
| 132 | }
|
| 133 |
|
| 134 | Raid raid = this.getOrCreateRaid(level, raidCenterPos);
|
| 135 | if (!raid.isStarted() && !this.raidMap.containsValue(raid)) {
|
| 136 | this.raidMap.put(this.getUniqueId(), raid);
|
| 137 | }
|
| 138 |
|
| 139 | if (!raid.isStarted() || raid.getRaidOmenLevel() < raid.getMaxRaidOmenLevel()) {
|
| 140 | raid.absorbRaidOmen(player);
|
| 141 | }
|
| 142 |
|
| 143 | this.setDirty();
|
| 144 | return raid;
|
| 145 | }
|
| 146 |
|
| 147 | private Raid getOrCreateRaid(final ServerLevel level, final BlockPos pos) {
|
| 148 | Raid raid = level.getRaidAt(pos);
|
| 149 | return raid != null ? raid : new Raid(pos, level.getDifficulty());
|
| 150 | }
|
| 151 |
|
| 152 | public static Raids load(final CompoundTag tag) {
|
| 153 | return CODEC.parse(NbtOps.INSTANCE, tag).resultOrPartial().orElseGet(Raids::new);
|
| 154 | }
|
| 155 |
|
| 156 | private int getUniqueId() {
|
| 157 | return ++this.nextId;
|
| 158 | }
|
| 159 |
|
| 160 | public @Nullable Raid getNearbyRaid(final BlockPos pos, final int maxDistSqr) {
|
| 161 | Raid closest = null;
|
| 162 | double closestDistanceSqr = maxDistSqr;
|
| 163 |
|
| 164 | for (Raid raid : this.raidMap.values()) {
|
| 165 | double distance = raid.getCenter().distSqr(pos);
|
| 166 | if (raid.isActive() && distance < closestDistanceSqr) {
|
| 167 | closest = raid;
|
| 168 | closestDistanceSqr = distance;
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | return closest;
|
| 173 | }
|
| 174 |
|
| 175 | @VisibleForDebug
|
| 176 | public List<BlockPos> getRaidCentersInChunk(final ChunkPos chunkPos) {
|
| 177 | return this.raidMap.values().stream().map(Raid::getCenter).filter(chunkPos::contains).toList();
|
| 178 | }
|
| 179 |
|
| 180 | private record RaidWithId(int id, Raid raid) {
|
| 181 | public static final Codec<Raids.RaidWithId> CODEC = RecordCodecBuilder.create(
|
| 182 | i -> i.group(Codec.INT.fieldOf("id").forGetter(Raids.RaidWithId::id), Raid.MAP_CODEC.forGetter(Raids.RaidWithId::raid)).apply(i, Raids.RaidWithId::new)
|
| 183 | );
|
| 184 |
|
| 185 | public static Raids.RaidWithId from(final Entry<Raid> entry) {
|
| 186 | return new Raids.RaidWithId(entry.getIntKey(), entry.getValue());
|
| 187 | }
|
| 188 | }
|
| 189 | }
|
| 190 |
|