| 1 | package net.minecraft.world.entity.monster.warden;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.Streams;
|
| 5 | import com.mojang.datafixers.util.Pair;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
| 9 | import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
| 10 | import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
| 11 | import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry;
|
| 12 | import java.util.ArrayList;
|
| 13 | import java.util.Comparator;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Optional;
|
| 16 | import java.util.UUID;
|
| 17 | import java.util.function.Predicate;
|
| 18 | import java.util.stream.Collectors;
|
| 19 | import net.minecraft.core.UUIDUtil;
|
| 20 | import net.minecraft.server.level.ServerLevel;
|
| 21 | import net.minecraft.util.ExtraCodecs;
|
| 22 | import net.minecraft.util.Mth;
|
| 23 | import net.minecraft.util.RandomSource;
|
| 24 | import net.minecraft.world.entity.Entity;
|
| 25 | import net.minecraft.world.entity.LivingEntity;
|
| 26 | import net.minecraft.world.entity.player.Player;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | public class AngerManagement {
|
| 30 | @VisibleForTesting
|
| 31 | protected static final int CONVERSION_DELAY = 2;
|
| 32 | @VisibleForTesting
|
| 33 | protected static final int MAX_ANGER = 150;
|
| 34 | private static final int DEFAULT_ANGER_DECREASE = 1;
|
| 35 | private int conversionDelay = Mth.randomBetweenInclusive(RandomSource.createThreadLocalInstance(), 0, 2);
|
| 36 | private int highestAnger;
|
| 37 | private static final Codec<Pair<UUID, Integer>> SUSPECT_ANGER_PAIR = RecordCodecBuilder.create(
|
| 38 | i -> i.group(UUIDUtil.CODEC.fieldOf("uuid").forGetter(Pair::getFirst), ExtraCodecs.NON_NEGATIVE_INT.fieldOf("anger").forGetter(Pair::getSecond))
|
| 39 | .apply(i, Pair::of)
|
| 40 | );
|
| 41 | private final Predicate<Entity> filter;
|
| 42 | @VisibleForTesting
|
| 43 | protected final ArrayList<Entity> suspects;
|
| 44 | private final AngerManagement.Sorter suspectSorter;
|
| 45 | @VisibleForTesting
|
| 46 | protected final Object2IntMap<Entity> angerBySuspect;
|
| 47 | @VisibleForTesting
|
| 48 | protected final Object2IntMap<UUID> angerByUuid;
|
| 49 |
|
| 50 | public static Codec<AngerManagement> codec(final Predicate<Entity> filter) {
|
| 51 | return RecordCodecBuilder.create(
|
| 52 | i -> i.group(SUSPECT_ANGER_PAIR.listOf().lenientOptionalFieldOf("suspects", List.of()).forGetter(AngerManagement::createUuidAngerPairs))
|
| 53 | .apply(i, list -> new AngerManagement(filter, list))
|
| 54 | );
|
| 55 | }
|
| 56 |
|
| 57 | public AngerManagement(final Predicate<Entity> filter, final List<Pair<UUID, Integer>> angerByUuid) {
|
| 58 | this.filter = filter;
|
| 59 | this.suspects = new ArrayList<>();
|
| 60 | this.suspectSorter = new AngerManagement.Sorter(this);
|
| 61 | this.angerBySuspect = new Object2IntOpenHashMap<>();
|
| 62 | this.angerByUuid = new Object2IntOpenHashMap<>(angerByUuid.size());
|
| 63 | angerByUuid.forEach(pair -> this.angerByUuid.put(pair.getFirst(), pair.getSecond()));
|
| 64 | }
|
| 65 |
|
| 66 | private List<Pair<UUID, Integer>> createUuidAngerPairs() {
|
| 67 | return Streams.<Pair<UUID, Integer>>concat(
|
| 68 | this.suspects.stream().map(e -> Pair.of(e.getUUID(), this.angerBySuspect.getInt(e))),
|
| 69 | this.angerByUuid.object2IntEntrySet().stream().map(e -> Pair.of(e.getKey(), e.getIntValue()))
|
| 70 | )
|
| 71 | .collect(Collectors.toList());
|
| 72 | }
|
| 73 |
|
| 74 | public void tick(final ServerLevel level, final Predicate<Entity> validEntity) {
|
| 75 | this.conversionDelay--;
|
| 76 | if (this.conversionDelay <= 0) {
|
| 77 | this.convertFromUuids(level);
|
| 78 | this.conversionDelay = 2;
|
| 79 | }
|
| 80 |
|
| 81 | ObjectIterator<Entry<UUID>> serializedIterator = this.angerByUuid.object2IntEntrySet().iterator();
|
| 82 |
|
| 83 | while (serializedIterator.hasNext()) {
|
| 84 | Entry<UUID> entry = serializedIterator.next();
|
| 85 | int anger = entry.getIntValue();
|
| 86 | if (anger <= 1) {
|
| 87 | serializedIterator.remove();
|
| 88 | } else {
|
| 89 | entry.setValue(anger - 1);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | ObjectIterator<Entry<Entity>> iterator = this.angerBySuspect.object2IntEntrySet().iterator();
|
| 94 |
|
| 95 | while (iterator.hasNext()) {
|
| 96 | Entry<Entity> entry = iterator.next();
|
| 97 | int anger = entry.getIntValue();
|
| 98 | Entity entity = entry.getKey();
|
| 99 | Entity.RemovalReason removalReason = entity.getRemovalReason();
|
| 100 | if (anger > 1 && validEntity.test(entity) && removalReason == null) {
|
| 101 | entry.setValue(anger - 1);
|
| 102 | } else {
|
| 103 | this.suspects.remove(entity);
|
| 104 | iterator.remove();
|
| 105 | if (anger > 1 && removalReason != null) {
|
| 106 | switch (removalReason) {
|
| 107 | case CHANGED_DIMENSION:
|
| 108 | case UNLOADED_TO_CHUNK:
|
| 109 | case UNLOADED_WITH_PLAYER:
|
| 110 | this.angerByUuid.put(entity.getUUID(), anger - 1);
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | this.sortAndUpdateHighestAnger();
|
| 117 | }
|
| 118 |
|
| 119 | private void sortAndUpdateHighestAnger() {
|
| 120 | this.highestAnger = 0;
|
| 121 | this.suspects.sort(this.suspectSorter);
|
| 122 | if (this.suspects.size() == 1) {
|
| 123 | this.highestAnger = this.angerBySuspect.getInt(this.suspects.get(0));
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | private void convertFromUuids(final ServerLevel level) {
|
| 128 | ObjectIterator<Entry<UUID>> iterator = this.angerByUuid.object2IntEntrySet().iterator();
|
| 129 |
|
| 130 | while (iterator.hasNext()) {
|
| 131 | Entry<UUID> entry = iterator.next();
|
| 132 | int anger = entry.getIntValue();
|
| 133 | Entity entity = level.getEntity(entry.getKey());
|
| 134 | if (entity != null) {
|
| 135 | this.angerBySuspect.put(entity, anger);
|
| 136 | this.suspects.add(entity);
|
| 137 | iterator.remove();
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | public int increaseAnger(final Entity entity, final int increment) {
|
| 143 | boolean newSuspect = !this.angerBySuspect.containsKey(entity);
|
| 144 | int currentAnger = this.angerBySuspect.computeInt(entity, (k, anger) -> Math.min(150, (anger == null ? 0 : anger) + increment));
|
| 145 | if (newSuspect) {
|
| 146 | int serializedAnger = this.angerByUuid.removeInt(entity.getUUID());
|
| 147 | currentAnger += serializedAnger;
|
| 148 | this.angerBySuspect.put(entity, currentAnger);
|
| 149 | this.suspects.add(entity);
|
| 150 | }
|
| 151 |
|
| 152 | this.sortAndUpdateHighestAnger();
|
| 153 | return currentAnger;
|
| 154 | }
|
| 155 |
|
| 156 | public void clearAnger(final Entity entity) {
|
| 157 | this.angerBySuspect.removeInt(entity);
|
| 158 | this.suspects.remove(entity);
|
| 159 | this.sortAndUpdateHighestAnger();
|
| 160 | }
|
| 161 |
|
| 162 | private @Nullable Entity getTopSuspect() {
|
| 163 | return this.suspects.stream().filter(this.filter).findFirst().orElse(null);
|
| 164 | }
|
| 165 |
|
| 166 | public int getActiveAnger(final @Nullable Entity currentTarget) {
|
| 167 | return currentTarget == null ? this.highestAnger : this.angerBySuspect.getInt(currentTarget);
|
| 168 | }
|
| 169 |
|
| 170 | public Optional<LivingEntity> getActiveEntity() {
|
| 171 | return Optional.ofNullable(this.getTopSuspect()).filter(e -> e instanceof LivingEntity).map(e -> (LivingEntity)e);
|
| 172 | }
|
| 173 |
|
| 174 | @VisibleForTesting
|
| 175 | protected record Sorter(AngerManagement angerManagement) implements Comparator<Entity> {
|
| 176 | public int compare(final Entity entity1, final Entity entity2) {
|
| 177 | if (entity1.equals(entity2)) {
|
| 178 | return 0;
|
| 179 | } else {
|
| 180 | int anger1 = this.angerManagement.angerBySuspect.getOrDefault(entity1, 0);
|
| 181 | int anger2 = this.angerManagement.angerBySuspect.getOrDefault(entity2, 0);
|
| 182 | this.angerManagement.highestAnger = Math.max(this.angerManagement.highestAnger, Math.max(anger1, anger2));
|
| 183 | boolean angryAt1 = AngerLevel.byAnger(anger1).isAngry();
|
| 184 | boolean angryAt2 = AngerLevel.byAnger(anger2).isAngry();
|
| 185 | if (angryAt1 != angryAt2) {
|
| 186 | return angryAt1 ? -1 : 1;
|
| 187 | } else {
|
| 188 | boolean isPlayer1 = entity1 instanceof Player;
|
| 189 | boolean isPlayer2 = entity2 instanceof Player;
|
| 190 | if (isPlayer1 != isPlayer2) {
|
| 191 | return isPlayer1 ? -1 : 1;
|
| 192 | } else {
|
| 193 | return Integer.compare(anger2, anger1);
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 |
|