package net.minecraft.world.entity.ai.attributes;

import java.util.Collection;
import java.util.Iterator;
import net.minecraft.util.InsensitiveStringMap;
import com.google.common.collect.Sets;
import java.util.Map;
import java.util.Set;

public class ModifiableAttributeMap extends BaseAttributeMap {
    private final Set<AttributeInstance> dirtyAttributes;
    protected final Map<String, AttributeInstance> attributesByLegacy;
    
    public ModifiableAttributeMap() {
        this.dirtyAttributes = Sets.newHashSet();
        this.attributesByLegacy = new InsensitiveStringMap<AttributeInstance>();
    }
    
    @Override
    public ModifiableAttributeInstance getInstance(final Attribute alq) {
        return (ModifiableAttributeInstance)super.getInstance(alq);
    }
    
    @Override
    public ModifiableAttributeInstance getInstance(final String string) {
        AttributeInstance alr3 = super.getInstance(string);
        if (alr3 == null) {
            alr3 = this.attributesByLegacy.get(string);
        }
        return (ModifiableAttributeInstance)alr3;
    }
    
    @Override
    public AttributeInstance registerAttribute(final Attribute alq) {
        final AttributeInstance alr3 = super.registerAttribute(alq);
        if (alq instanceof RangedAttribute && ((RangedAttribute)alq).getImportLegacyName() != null) {
            this.attributesByLegacy.put(((RangedAttribute)alq).getImportLegacyName(), alr3);
        }
        return alr3;
    }
    
    @Override
    protected AttributeInstance createAttributeInstance(final Attribute alq) {
        return new ModifiableAttributeInstance(this, alq);
    }
    
    @Override
    public void onAttributeModified(final AttributeInstance alr) {
        if (alr.getAttribute().isClientSyncable()) {
            this.dirtyAttributes.add(alr);
        }
        for (final Attribute alq4 : this.descendantsByParent.get(alr.getAttribute())) {
            final ModifiableAttributeInstance alv5 = this.getInstance(alq4);
            if (alv5 != null) {
                alv5.setDirty();
            }
        }
    }
    
    public Set<AttributeInstance> getDirtyAttributes() {
        return this.dirtyAttributes;
    }
    
    public Collection<AttributeInstance> getSyncableAttributes() {
        final Set<AttributeInstance> set2 = Sets.newHashSet();
        for (final AttributeInstance alr4 : this.getAttributes()) {
            if (alr4.getAttribute().isClientSyncable()) {
                set2.add(alr4);
            }
        }
        return set2;
    }
}
