| 1 | package net.minecraft.entity.ai.attributes;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.google.common.collect.Sets;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.UUID;
|
| 10 |
|
| 11 | public class ModifiableAttributeInstance implements IAttributeInstance
|
| 12 | {
|
| 13 | /** The BaseAttributeMap this attributeInstance can be found in */
|
| 14 | private final BaseAttributeMap attributeMap;
|
| 15 |
|
| 16 | /** The Attribute this is an instance of */
|
| 17 | private final IAttribute genericAttribute;
|
| 18 | private final Map<Integer, Set<AttributeModifier>> mapByOperation = Maps.<Integer, Set<AttributeModifier>>newHashMap();
|
| 19 | private final Map<String, Set<AttributeModifier>> mapByName = Maps.<String, Set<AttributeModifier>>newHashMap();
|
| 20 | private final Map<UUID, AttributeModifier> mapByUUID = Maps.<UUID, AttributeModifier>newHashMap();
|
| 21 | private double baseValue;
|
| 22 | private boolean needsUpdate = true;
|
| 23 | private double cachedValue;
|
| 24 |
|
| 25 | public ModifiableAttributeInstance(BaseAttributeMap attributeMapIn, IAttribute genericAttributeIn)
|
| 26 | {
|
| 27 | this.attributeMap = attributeMapIn;
|
| 28 | this.genericAttribute = genericAttributeIn;
|
| 29 | this.baseValue = genericAttributeIn.getDefaultValue();
|
| 30 |
|
| 31 | for (int i = 0; i < 3; ++i)
|
| 32 | {
|
| 33 | this.mapByOperation.put(Integer.valueOf(i), Sets.<AttributeModifier>newHashSet());
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | /**
|
| 38 | * Get the Attribute this is an instance of
|
| 39 | */
|
| 40 | public IAttribute getAttribute()
|
| 41 | {
|
| 42 | return this.genericAttribute;
|
| 43 | }
|
| 44 |
|
| 45 | public double getBaseValue()
|
| 46 | {
|
| 47 | return this.baseValue;
|
| 48 | }
|
| 49 |
|
| 50 | public void setBaseValue(double baseValue)
|
| 51 | {
|
| 52 | if (baseValue != this.getBaseValue())
|
| 53 | {
|
| 54 | this.baseValue = baseValue;
|
| 55 | this.flagForUpdate();
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | public Collection<AttributeModifier> getModifiersByOperation(int operation)
|
| 60 | {
|
| 61 | return (Collection)this.mapByOperation.get(Integer.valueOf(operation));
|
| 62 | }
|
| 63 |
|
| 64 | public Collection<AttributeModifier> func_111122_c()
|
| 65 | {
|
| 66 | Set<AttributeModifier> set = Sets.<AttributeModifier>newHashSet();
|
| 67 |
|
| 68 | for (int i = 0; i < 3; ++i)
|
| 69 | {
|
| 70 | set.addAll(this.getModifiersByOperation(i));
|
| 71 | }
|
| 72 |
|
| 73 | return set;
|
| 74 | }
|
| 75 |
|
| 76 | /**
|
| 77 | * Returns attribute modifier, if any, by the given UUID
|
| 78 | */
|
| 79 | public AttributeModifier getModifier(UUID uuid)
|
| 80 | {
|
| 81 | return (AttributeModifier)this.mapByUUID.get(uuid);
|
| 82 | }
|
| 83 |
|
| 84 | public boolean hasModifier(AttributeModifier modifier)
|
| 85 | {
|
| 86 | return this.mapByUUID.get(modifier.getID()) != null;
|
| 87 | }
|
| 88 |
|
| 89 | public void applyModifier(AttributeModifier modifier)
|
| 90 | {
|
| 91 | if (this.getModifier(modifier.getID()) != null)
|
| 92 | {
|
| 93 | throw new IllegalArgumentException("Modifier is already applied on this attribute!");
|
| 94 | }
|
| 95 | else
|
| 96 | {
|
| 97 | Set<AttributeModifier> set = (Set)this.mapByName.get(modifier.getName());
|
| 98 |
|
| 99 | if (set == null)
|
| 100 | {
|
| 101 | set = Sets.<AttributeModifier>newHashSet();
|
| 102 | this.mapByName.put(modifier.getName(), set);
|
| 103 | }
|
| 104 |
|
| 105 | ((Set)this.mapByOperation.get(Integer.valueOf(modifier.getOperation()))).add(modifier);
|
| 106 | set.add(modifier);
|
| 107 | this.mapByUUID.put(modifier.getID(), modifier);
|
| 108 | this.flagForUpdate();
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | protected void flagForUpdate()
|
| 113 | {
|
| 114 | this.needsUpdate = true;
|
| 115 | this.attributeMap.func_180794_a(this);
|
| 116 | }
|
| 117 |
|
| 118 | public void removeModifier(AttributeModifier modifier)
|
| 119 | {
|
| 120 | for (int i = 0; i < 3; ++i)
|
| 121 | {
|
| 122 | Set<AttributeModifier> set = (Set)this.mapByOperation.get(Integer.valueOf(i));
|
| 123 | set.remove(modifier);
|
| 124 | }
|
| 125 |
|
| 126 | Set<AttributeModifier> set1 = (Set)this.mapByName.get(modifier.getName());
|
| 127 |
|
| 128 | if (set1 != null)
|
| 129 | {
|
| 130 | set1.remove(modifier);
|
| 131 |
|
| 132 | if (set1.isEmpty())
|
| 133 | {
|
| 134 | this.mapByName.remove(modifier.getName());
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | this.mapByUUID.remove(modifier.getID());
|
| 139 | this.flagForUpdate();
|
| 140 | }
|
| 141 |
|
| 142 | public void removeAllModifiers()
|
| 143 | {
|
| 144 | Collection<AttributeModifier> collection = this.func_111122_c();
|
| 145 |
|
| 146 | if (collection != null)
|
| 147 | {
|
| 148 | for (AttributeModifier attributemodifier : Lists.newArrayList(collection))
|
| 149 | {
|
| 150 | this.removeModifier(attributemodifier);
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | public double getAttributeValue()
|
| 156 | {
|
| 157 | if (this.needsUpdate)
|
| 158 | {
|
| 159 | this.cachedValue = this.computeValue();
|
| 160 | this.needsUpdate = false;
|
| 161 | }
|
| 162 |
|
| 163 | return this.cachedValue;
|
| 164 | }
|
| 165 |
|
| 166 | private double computeValue()
|
| 167 | {
|
| 168 | double d0 = this.getBaseValue();
|
| 169 |
|
| 170 | for (AttributeModifier attributemodifier : this.func_180375_b(0))
|
| 171 | {
|
| 172 | d0 += attributemodifier.getAmount();
|
| 173 | }
|
| 174 |
|
| 175 | double d1 = d0;
|
| 176 |
|
| 177 | for (AttributeModifier attributemodifier1 : this.func_180375_b(1))
|
| 178 | {
|
| 179 | d1 += d0 * attributemodifier1.getAmount();
|
| 180 | }
|
| 181 |
|
| 182 | for (AttributeModifier attributemodifier2 : this.func_180375_b(2))
|
| 183 | {
|
| 184 | d1 *= 1.0D + attributemodifier2.getAmount();
|
| 185 | }
|
| 186 |
|
| 187 | return this.genericAttribute.clampValue(d1);
|
| 188 | }
|
| 189 |
|
| 190 | private Collection<AttributeModifier> func_180375_b(int operation)
|
| 191 | {
|
| 192 | Set<AttributeModifier> set = Sets.newHashSet(this.getModifiersByOperation(operation));
|
| 193 |
|
| 194 | for (IAttribute iattribute = this.genericAttribute.func_180372_d(); iattribute != null; iattribute = iattribute.func_180372_d())
|
| 195 | {
|
| 196 | IAttributeInstance iattributeinstance = this.attributeMap.getAttributeInstance(iattribute);
|
| 197 |
|
| 198 | if (iattributeinstance != null)
|
| 199 | {
|
| 200 | set.addAll(iattributeinstance.getModifiersByOperation(operation));
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | return set;
|
| 205 | }
|
| 206 | }
|
| 207 |
|