| 1 | package net.minecraft.world.entity.ai.village.poi;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Objects;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.registries.Registries;
|
| 9 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 10 | import net.minecraft.util.VisibleForDebug;
|
| 11 |
|
| 12 | public class PoiRecord {
|
| 13 | private final BlockPos pos;
|
| 14 | private final Holder<PoiType> poiType;
|
| 15 | private int freeTickets;
|
| 16 | private final Runnable setDirty;
|
| 17 |
|
| 18 | private PoiRecord(final BlockPos pos, final Holder<PoiType> poiType, final int freeTickets, final Runnable setDirty) {
|
| 19 | this.pos = pos.immutable();
|
| 20 | this.poiType = poiType;
|
| 21 | this.freeTickets = freeTickets;
|
| 22 | this.setDirty = setDirty;
|
| 23 | }
|
| 24 |
|
| 25 | public PoiRecord(final BlockPos pos, final Holder<PoiType> poiType, final Runnable setDirty) {
|
| 26 | this(pos, poiType, poiType.value().maxTickets(), setDirty);
|
| 27 | }
|
| 28 |
|
| 29 | public PoiRecord.Packed pack() {
|
| 30 | return new PoiRecord.Packed(this.pos, this.poiType, this.freeTickets);
|
| 31 | }
|
| 32 |
|
| 33 | @Deprecated
|
| 34 | @VisibleForDebug
|
| 35 | public int getFreeTickets() {
|
| 36 | return this.freeTickets;
|
| 37 | }
|
| 38 |
|
| 39 | protected boolean acquireTicket() {
|
| 40 | if (this.freeTickets <= 0) {
|
| 41 | return false;
|
| 42 | }
|
| 43 |
|
| 44 | this.freeTickets--;
|
| 45 | this.setDirty.run();
|
| 46 | return true;
|
| 47 | }
|
| 48 |
|
| 49 | protected boolean releaseTicket() {
|
| 50 | if (this.freeTickets >= this.poiType.value().maxTickets()) {
|
| 51 | return false;
|
| 52 | }
|
| 53 |
|
| 54 | this.freeTickets++;
|
| 55 | this.setDirty.run();
|
| 56 | return true;
|
| 57 | }
|
| 58 |
|
| 59 | public boolean hasSpace() {
|
| 60 | return this.freeTickets > 0;
|
| 61 | }
|
| 62 |
|
| 63 | public boolean isOccupied() {
|
| 64 | return this.freeTickets != this.poiType.value().maxTickets();
|
| 65 | }
|
| 66 |
|
| 67 | public BlockPos getPos() {
|
| 68 | return this.pos;
|
| 69 | }
|
| 70 |
|
| 71 | public Holder<PoiType> getPoiType() {
|
| 72 | return this.poiType;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public boolean equals(final Object o) {
|
| 77 | if (this == o) {
|
| 78 | return true;
|
| 79 | } else {
|
| 80 | return o != null && this.getClass() == o.getClass() ? Objects.equals(this.pos, ((PoiRecord)o).pos) : false;
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public int hashCode() {
|
| 86 | return this.pos.hashCode();
|
| 87 | }
|
| 88 |
|
| 89 | public record Packed(BlockPos pos, Holder<PoiType> poiType, int freeTickets) {
|
| 90 | public static final Codec<PoiRecord.Packed> CODEC = RecordCodecBuilder.create(
|
| 91 | i -> i.group(
|
| 92 | BlockPos.CODEC.fieldOf("pos").forGetter(PoiRecord.Packed::pos),
|
| 93 | RegistryCodecs.holder(Registries.POINT_OF_INTEREST_TYPE).fieldOf("type").forGetter(PoiRecord.Packed::poiType),
|
| 94 | Codec.INT.optionalFieldOf("free_tickets", 0).forGetter(PoiRecord.Packed::freeTickets)
|
| 95 | )
|
| 96 | .apply(i, PoiRecord.Packed::new)
|
| 97 | );
|
| 98 |
|
| 99 | public PoiRecord unpack(final Runnable setDirty) {
|
| 100 | return new PoiRecord(this.pos, this.poiType, this.freeTickets, setDirty);
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|