| 1 | package net.minecraft.world.entity.ai.attributes;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.function.Consumer;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.resources.Identifier;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 |
|
| 10 | public class AttributeSupplier {
|
| 11 | private final Map<Holder<Attribute>, AttributeInstance> instances;
|
| 12 |
|
| 13 | private AttributeSupplier(final Map<Holder<Attribute>, AttributeInstance> instances) {
|
| 14 | this.instances = instances;
|
| 15 | }
|
| 16 |
|
| 17 | private AttributeInstance getAttributeInstance(final Holder<Attribute> attribute) {
|
| 18 | AttributeInstance instance = this.instances.get(attribute);
|
| 19 | if (instance == null) {
|
| 20 | throw new IllegalArgumentException("Can't find attribute " + attribute.getRegisteredName());
|
| 21 | } else {
|
| 22 | return instance;
|
| 23 | }
|
| 24 | }
|
| 25 |
|
| 26 | public double getValue(final Holder<Attribute> attribute) {
|
| 27 | return this.getAttributeInstance(attribute).getValue();
|
| 28 | }
|
| 29 |
|
| 30 | public double getBaseValue(final Holder<Attribute> attribute) {
|
| 31 | return this.getAttributeInstance(attribute).getBaseValue();
|
| 32 | }
|
| 33 |
|
| 34 | public double getModifierValue(final Holder<Attribute> attribute, final Identifier id) {
|
| 35 | AttributeModifier modifier = this.getAttributeInstance(attribute).getModifier(id);
|
| 36 | if (modifier == null) {
|
| 37 | throw new IllegalArgumentException("Can't find modifier " + id + " on attribute " + attribute.getRegisteredName());
|
| 38 | } else {
|
| 39 | return modifier.amount();
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | public @Nullable AttributeInstance createInstance(final Consumer<AttributeInstance> onDirty, final Holder<Attribute> attribute) {
|
| 44 | AttributeInstance template = this.instances.get(attribute);
|
| 45 | if (template == null) {
|
| 46 | return null;
|
| 47 | }
|
| 48 |
|
| 49 | AttributeInstance result = new AttributeInstance(attribute, onDirty);
|
| 50 | result.replaceFrom(template);
|
| 51 | return result;
|
| 52 | }
|
| 53 |
|
| 54 | public static AttributeSupplier.Builder builder() {
|
| 55 | return new AttributeSupplier.Builder();
|
| 56 | }
|
| 57 |
|
| 58 | public boolean hasAttribute(final Holder<Attribute> attribute) {
|
| 59 | return this.instances.containsKey(attribute);
|
| 60 | }
|
| 61 |
|
| 62 | public boolean hasModifier(final Holder<Attribute> attribute, final Identifier modifier) {
|
| 63 | AttributeInstance attributeInstance = this.instances.get(attribute);
|
| 64 | return attributeInstance != null && attributeInstance.getModifier(modifier) != null;
|
| 65 | }
|
| 66 |
|
| 67 | public static class Builder {
|
| 68 | private final ImmutableMap.Builder<Holder<Attribute>, AttributeInstance> builder = ImmutableMap.builder();
|
| 69 | private boolean instanceFrozen;
|
| 70 |
|
| 71 | private AttributeInstance create(final Holder<Attribute> attribute) {
|
| 72 | AttributeInstance result = new AttributeInstance(attribute, attributeInstance -> {
|
| 73 | if (this.instanceFrozen) {
|
| 74 | throw new UnsupportedOperationException("Tried to change value for default attribute instance: " + attribute.getRegisteredName());
|
| 75 | }
|
| 76 | });
|
| 77 | this.builder.put(attribute, result);
|
| 78 | return result;
|
| 79 | }
|
| 80 |
|
| 81 | public AttributeSupplier.Builder add(final Holder<Attribute> attribute) {
|
| 82 | this.create(attribute);
|
| 83 | return this;
|
| 84 | }
|
| 85 |
|
| 86 | public AttributeSupplier.Builder add(final Holder<Attribute> attribute, final double baseValue) {
|
| 87 | AttributeInstance result = this.create(attribute);
|
| 88 | result.setBaseValue(baseValue);
|
| 89 | return this;
|
| 90 | }
|
| 91 |
|
| 92 | public AttributeSupplier build() {
|
| 93 | this.instanceFrozen = true;
|
| 94 | return new AttributeSupplier(this.builder.buildKeepingLast());
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|