| 1 | package net.minecraft.world.entity.ai.village.poi;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.google.common.collect.Sets;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import com.mojang.serialization.Codec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
|
| 10 | import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.Set;
|
| 15 | import java.util.function.BiConsumer;
|
| 16 | import java.util.function.Consumer;
|
| 17 | import java.util.function.Predicate;
|
| 18 | import java.util.stream.Stream;
|
| 19 | import net.minecraft.core.BlockPos;
|
| 20 | import net.minecraft.core.Holder;
|
| 21 | import net.minecraft.core.SectionPos;
|
| 22 | import net.minecraft.util.Util;
|
| 23 | import net.minecraft.util.VisibleForDebug;
|
| 24 | import net.minecraft.util.debug.DebugPoiInfo;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 | import org.slf4j.Logger;
|
| 27 |
|
| 28 | public class PoiSection {
|
| 29 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 30 | private final Short2ObjectMap<PoiRecord> records = new Short2ObjectOpenHashMap<>();
|
| 31 | private final Map<Holder<PoiType>, Set<PoiRecord>> byType = Maps.newHashMap();
|
| 32 | private final Runnable setDirty;
|
| 33 | private boolean isValid;
|
| 34 |
|
| 35 | public PoiSection(final Runnable setDirty) {
|
| 36 | this(setDirty, true, ImmutableList.of());
|
| 37 | }
|
| 38 |
|
| 39 | private PoiSection(final Runnable setDirty, final boolean isValid, final List<PoiRecord> records) {
|
| 40 | this.setDirty = setDirty;
|
| 41 | this.isValid = isValid;
|
| 42 | records.forEach(this::add);
|
| 43 | }
|
| 44 |
|
| 45 | public PoiSection.Packed pack() {
|
| 46 | return new PoiSection.Packed(this.isValid, this.records.values().stream().map(PoiRecord::pack).toList());
|
| 47 | }
|
| 48 |
|
| 49 | public Stream<PoiRecord> getRecords(final Predicate<Holder<PoiType>> predicate, final PoiManager.Occupancy occupancy) {
|
| 50 | return this.byType.entrySet().stream().filter(e -> predicate.test(e.getKey())).flatMap(e -> e.getValue().stream()).filter(occupancy.getTest());
|
| 51 | }
|
| 52 |
|
| 53 | public @Nullable PoiRecord add(final BlockPos blockPos, final Holder<PoiType> type) {
|
| 54 | PoiRecord record = new PoiRecord(blockPos, type, this.setDirty);
|
| 55 | if (this.add(record)) {
|
| 56 | LOGGER.debug("Added POI of type {} @ {}", type.getRegisteredName(), blockPos);
|
| 57 | this.setDirty.run();
|
| 58 | return record;
|
| 59 | } else {
|
| 60 | return null;
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | private boolean add(final PoiRecord record) {
|
| 65 | BlockPos blockPos = record.getPos();
|
| 66 | Holder<PoiType> poiType = record.getPoiType();
|
| 67 | short key = SectionPos.sectionRelativePos(blockPos);
|
| 68 | PoiRecord oldRecord = this.records.get(key);
|
| 69 | if (oldRecord != null) {
|
| 70 | if (poiType.equals(oldRecord.getPoiType())) {
|
| 71 | return false;
|
| 72 | }
|
| 73 |
|
| 74 | Util.logAndPauseIfInIde("POI data mismatch: already registered at " + blockPos);
|
| 75 | }
|
| 76 |
|
| 77 | this.records.put(key, record);
|
| 78 | this.byType.computeIfAbsent(poiType, k -> Sets.newHashSet()).add(record);
|
| 79 | return true;
|
| 80 | }
|
| 81 |
|
| 82 | public void remove(final BlockPos pos) {
|
| 83 | PoiRecord poiRecord = this.records.remove(SectionPos.sectionRelativePos(pos));
|
| 84 | if (poiRecord == null) {
|
| 85 | LOGGER.error("POI data mismatch: never registered at {}", pos);
|
| 86 | } else {
|
| 87 | this.byType.get(poiRecord.getPoiType()).remove(poiRecord);
|
| 88 | LOGGER.debug("Removed POI of type {} @ {}", LogUtils.defer(poiRecord::getPoiType), LogUtils.defer(poiRecord::getPos));
|
| 89 | this.setDirty.run();
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | @Deprecated
|
| 94 | @VisibleForDebug
|
| 95 | public int getFreeTickets(final BlockPos pos) {
|
| 96 | return this.getPoiRecord(pos).map(PoiRecord::getFreeTickets).orElse(0);
|
| 97 | }
|
| 98 |
|
| 99 | public boolean release(final BlockPos pos) {
|
| 100 | PoiRecord record = this.records.get(SectionPos.sectionRelativePos(pos));
|
| 101 | if (record == null) {
|
| 102 | throw (IllegalStateException)Util.pauseInIde(new IllegalStateException("POI never registered at " + pos));
|
| 103 | }
|
| 104 |
|
| 105 | boolean success = record.releaseTicket();
|
| 106 | this.setDirty.run();
|
| 107 | return success;
|
| 108 | }
|
| 109 |
|
| 110 | public boolean exists(final BlockPos pos, final Predicate<Holder<PoiType>> predicate) {
|
| 111 | return this.getType(pos).filter(predicate).isPresent();
|
| 112 | }
|
| 113 |
|
| 114 | public Optional<Holder<PoiType>> getType(final BlockPos pos) {
|
| 115 | return this.getPoiRecord(pos).map(PoiRecord::getPoiType);
|
| 116 | }
|
| 117 |
|
| 118 | private Optional<PoiRecord> getPoiRecord(final BlockPos pos) {
|
| 119 | return Optional.ofNullable(this.records.get(SectionPos.sectionRelativePos(pos)));
|
| 120 | }
|
| 121 |
|
| 122 | public Optional<DebugPoiInfo> getDebugPoiInfo(final BlockPos pos) {
|
| 123 | return this.getPoiRecord(pos).map(DebugPoiInfo::new);
|
| 124 | }
|
| 125 |
|
| 126 | public void refresh(final Consumer<BiConsumer<BlockPos, Holder<PoiType>>> updater) {
|
| 127 | if (!this.isValid) {
|
| 128 | Short2ObjectMap<PoiRecord> oldRecords = new Short2ObjectOpenHashMap<>(this.records);
|
| 129 | this.clear();
|
| 130 | updater.accept((blockPos, poiType) -> {
|
| 131 | short key = SectionPos.sectionRelativePos(blockPos);
|
| 132 | PoiRecord newRecord = oldRecords.computeIfAbsent(key, k -> new PoiRecord(blockPos, poiType, this.setDirty));
|
| 133 | this.add(newRecord);
|
| 134 | });
|
| 135 | this.isValid = true;
|
| 136 | this.setDirty.run();
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | private void clear() {
|
| 141 | this.records.clear();
|
| 142 | this.byType.clear();
|
| 143 | }
|
| 144 |
|
| 145 | public boolean isValid() {
|
| 146 | return this.isValid;
|
| 147 | }
|
| 148 |
|
| 149 | public record Packed(boolean isValid, List<PoiRecord.Packed> records) {
|
| 150 | public static final Codec<PoiSection.Packed> CODEC = RecordCodecBuilder.create(
|
| 151 | i -> i.group(
|
| 152 | Codec.BOOL.lenientOptionalFieldOf("Valid", false).forGetter(PoiSection.Packed::isValid),
|
| 153 | PoiRecord.Packed.CODEC.listOf().fieldOf("Records").forGetter(PoiSection.Packed::records)
|
| 154 | )
|
| 155 | .apply(i, PoiSection.Packed::new)
|
| 156 | );
|
| 157 |
|
| 158 | public PoiSection unpack(final Runnable setDirty) {
|
| 159 | return new PoiSection(setDirty, this.isValid, this.records.stream().map(record -> record.unpack(setDirty)).toList());
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 |
|