| 1 | package net.minecraft.world.entity.ai.attributes;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.ImmutableSet;
|
| 5 | import com.google.common.collect.Maps;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
| 9 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
| 10 | import java.util.Collection;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Set;
|
| 14 | import java.util.function.Consumer;
|
| 15 | import net.minecraft.core.Holder;
|
| 16 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 17 | import net.minecraft.resources.Identifier;
|
| 18 | import net.minecraft.util.ExtraCodecs;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | public class AttributeInstance {
|
| 22 | private final Holder<Attribute> attribute;
|
| 23 | private final Map<AttributeModifier.Operation, Map<Identifier, AttributeModifier>> modifiersByOperation = Maps.newEnumMap(AttributeModifier.Operation.class);
|
| 24 | private final Map<Identifier, AttributeModifier> modifierById = new Object2ObjectArrayMap<>();
|
| 25 | private final Map<Identifier, AttributeModifier> permanentModifiers = new Object2ObjectArrayMap<>();
|
| 26 | private double baseValue;
|
| 27 | private boolean dirty = true;
|
| 28 | private double cachedValue;
|
| 29 | private final Consumer<AttributeInstance> onDirty;
|
| 30 |
|
| 31 | public AttributeInstance(final Holder<Attribute> attribute, final Consumer<AttributeInstance> onDirty) {
|
| 32 | this.attribute = attribute;
|
| 33 | this.onDirty = onDirty;
|
| 34 | this.baseValue = attribute.value().getDefaultValue();
|
| 35 | }
|
| 36 |
|
| 37 | public Holder<Attribute> getAttribute() {
|
| 38 | return this.attribute;
|
| 39 | }
|
| 40 |
|
| 41 | public double getBaseValue() {
|
| 42 | return this.baseValue;
|
| 43 | }
|
| 44 |
|
| 45 | public void setBaseValue(final double baseValue) {
|
| 46 | if (baseValue != this.baseValue) {
|
| 47 | this.baseValue = baseValue;
|
| 48 | this.setDirty();
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | @VisibleForTesting
|
| 53 | Map<Identifier, AttributeModifier> getModifiers(final AttributeModifier.Operation operation) {
|
| 54 | return this.modifiersByOperation.computeIfAbsent(operation, key -> new Object2ObjectOpenHashMap<>());
|
| 55 | }
|
| 56 |
|
| 57 | public Set<AttributeModifier> getModifiers() {
|
| 58 | return ImmutableSet.copyOf(this.modifierById.values());
|
| 59 | }
|
| 60 |
|
| 61 | public Set<AttributeModifier> getPermanentModifiers() {
|
| 62 | return ImmutableSet.copyOf(this.permanentModifiers.values());
|
| 63 | }
|
| 64 |
|
| 65 | public @Nullable AttributeModifier getModifier(final Identifier id) {
|
| 66 | return this.modifierById.get(id);
|
| 67 | }
|
| 68 |
|
| 69 | public boolean hasModifier(final Identifier modifier) {
|
| 70 | return this.modifierById.get(modifier) != null;
|
| 71 | }
|
| 72 |
|
| 73 | private void addModifier(final AttributeModifier modifier) {
|
| 74 | AttributeModifier previous = this.modifierById.putIfAbsent(modifier.id(), modifier);
|
| 75 | if (previous != null) {
|
| 76 | throw new IllegalArgumentException("Modifier is already applied on this attribute!");
|
| 77 | }
|
| 78 |
|
| 79 | this.getModifiers(modifier.operation()).put(modifier.id(), modifier);
|
| 80 | this.setDirty();
|
| 81 | }
|
| 82 |
|
| 83 | public void addOrUpdateTransientModifier(final AttributeModifier modifier) {
|
| 84 | AttributeModifier oldModifier = this.modifierById.put(modifier.id(), modifier);
|
| 85 | if (modifier != oldModifier) {
|
| 86 | this.getModifiers(modifier.operation()).put(modifier.id(), modifier);
|
| 87 | this.setDirty();
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | public void addTransientModifier(final AttributeModifier modifier) {
|
| 92 | this.addModifier(modifier);
|
| 93 | }
|
| 94 |
|
| 95 | public void addOrReplacePermanentModifier(final AttributeModifier modifier) {
|
| 96 | this.removeModifier(modifier.id());
|
| 97 | this.addModifier(modifier);
|
| 98 | this.permanentModifiers.put(modifier.id(), modifier);
|
| 99 | }
|
| 100 |
|
| 101 | public void addPermanentModifier(final AttributeModifier modifier) {
|
| 102 | this.addModifier(modifier);
|
| 103 | this.permanentModifiers.put(modifier.id(), modifier);
|
| 104 | }
|
| 105 |
|
| 106 | public void addPermanentModifiers(final Collection<AttributeModifier> modifiers) {
|
| 107 | for (AttributeModifier modifier : modifiers) {
|
| 108 | this.addPermanentModifier(modifier);
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | protected void setDirty() {
|
| 113 | this.dirty = true;
|
| 114 | this.onDirty.accept(this);
|
| 115 | }
|
| 116 |
|
| 117 | public void removeModifier(final AttributeModifier modifier) {
|
| 118 | this.removeModifier(modifier.id());
|
| 119 | }
|
| 120 |
|
| 121 | public boolean removeModifier(final Identifier id) {
|
| 122 | AttributeModifier modifier = this.modifierById.remove(id);
|
| 123 | if (modifier == null) {
|
| 124 | return false;
|
| 125 | }
|
| 126 |
|
| 127 | this.getModifiers(modifier.operation()).remove(id);
|
| 128 | this.permanentModifiers.remove(id);
|
| 129 | this.setDirty();
|
| 130 | return true;
|
| 131 | }
|
| 132 |
|
| 133 | public void removeModifiers() {
|
| 134 | for (AttributeModifier modifier : this.getModifiers()) {
|
| 135 | this.removeModifier(modifier);
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | public double getValue() {
|
| 140 | if (this.dirty) {
|
| 141 | this.cachedValue = this.calculateValue();
|
| 142 | this.dirty = false;
|
| 143 | }
|
| 144 |
|
| 145 | return this.cachedValue;
|
| 146 | }
|
| 147 |
|
| 148 | private double calculateValue() {
|
| 149 | double base = this.getBaseValue();
|
| 150 |
|
| 151 | for (AttributeModifier modifier : this.getModifiersOrEmpty(AttributeModifier.Operation.ADD_VALUE)) {
|
| 152 | base += modifier.amount();
|
| 153 | }
|
| 154 |
|
| 155 | double result = base;
|
| 156 |
|
| 157 | for (AttributeModifier modifier : this.getModifiersOrEmpty(AttributeModifier.Operation.ADD_MULTIPLIED_BASE)) {
|
| 158 | result += base * modifier.amount();
|
| 159 | }
|
| 160 |
|
| 161 | for (AttributeModifier modifier : this.getModifiersOrEmpty(AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL)) {
|
| 162 | result *= 1.0 + modifier.amount();
|
| 163 | }
|
| 164 |
|
| 165 | return this.attribute.value().sanitizeValue(result);
|
| 166 | }
|
| 167 |
|
| 168 | private Collection<AttributeModifier> getModifiersOrEmpty(final AttributeModifier.Operation operation) {
|
| 169 | return this.modifiersByOperation.getOrDefault(operation, Map.of()).values();
|
| 170 | }
|
| 171 |
|
| 172 | public void replaceFrom(final AttributeInstance other) {
|
| 173 | this.baseValue = other.baseValue;
|
| 174 | this.modifierById.clear();
|
| 175 | this.modifierById.putAll(other.modifierById);
|
| 176 | this.permanentModifiers.clear();
|
| 177 | this.permanentModifiers.putAll(other.permanentModifiers);
|
| 178 | this.modifiersByOperation.clear();
|
| 179 | other.modifiersByOperation
|
| 180 | .forEach((operation, attributeModifiers) -> this.getModifiers(operation).putAll((Map<? extends Identifier, ? extends AttributeModifier>)attributeModifiers));
|
| 181 | this.setDirty();
|
| 182 | }
|
| 183 |
|
| 184 | public AttributeInstance.Packed pack() {
|
| 185 | return new AttributeInstance.Packed(this.attribute, this.baseValue, List.copyOf(this.permanentModifiers.values()));
|
| 186 | }
|
| 187 |
|
| 188 | public void apply(final AttributeInstance.Packed packed) {
|
| 189 | this.baseValue = packed.baseValue;
|
| 190 |
|
| 191 | for (AttributeModifier modifier : packed.modifiers) {
|
| 192 | this.modifierById.put(modifier.id(), modifier);
|
| 193 | this.getModifiers(modifier.operation()).put(modifier.id(), modifier);
|
| 194 | this.permanentModifiers.put(modifier.id(), modifier);
|
| 195 | }
|
| 196 |
|
| 197 | this.setDirty();
|
| 198 | }
|
| 199 |
|
| 200 | public record Packed(Holder<Attribute> attribute, double baseValue, List<AttributeModifier> modifiers) {
|
| 201 | public static final Codec<AttributeInstance.Packed> CODEC = RecordCodecBuilder.create(
|
| 202 | i -> i.group(
|
| 203 | BuiltInRegistries.ATTRIBUTE.holderByNameCodec().fieldOf("id").forGetter(AttributeInstance.Packed::attribute),
|
| 204 | ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.DOUBLE, "base", 0.0).forGetter(AttributeInstance.Packed::baseValue),
|
| 205 | AttributeModifier.CODEC.listOf().optionalFieldOf("modifiers", List.of()).forGetter(AttributeInstance.Packed::modifiers)
|
| 206 | )
|
| 207 | .apply(i, AttributeInstance.Packed::new)
|
| 208 | );
|
| 209 | public static final Codec<List<AttributeInstance.Packed>> LIST_CODEC = CODEC.listOf();
|
| 210 | }
|
| 211 | }
|
| 212 |
|