package net.minecraft.world.item;

import java.util.Iterator;
import net.minecraft.util.Mth;
import com.google.common.collect.Maps;
import java.util.Map;

public class ItemCooldowns {
    private final Map<Item, CooldownInstance> cooldowns;
    private int tickCount;
    
    public ItemCooldowns() {
        this.cooldowns = Maps.newHashMap();
    }
    
    public boolean isOnCooldown(final Item bke) {
        return this.getCooldownPercent(bke, 0.0f) > 0.0f;
    }
    
    public float getCooldownPercent(final Item bke, final float float2) {
        final CooldownInstance a4 = this.cooldowns.get(bke);
        if (a4 != null) {
            final float float3 = (float)(a4.endTime - a4.startTime);
            final float float4 = a4.endTime - (this.tickCount + float2);
            return Mth.clamp(float4 / float3, 0.0f, 1.0f);
        }
        return 0.0f;
    }
    
    public void tick() {
        ++this.tickCount;
        if (!this.cooldowns.isEmpty()) {
            final Iterator<Map.Entry<Item, CooldownInstance>> iterator2 = this.cooldowns.entrySet().iterator();
            while (iterator2.hasNext()) {
                final Map.Entry<Item, CooldownInstance> entry3 = iterator2.next();
                if (entry3.getValue().endTime <= this.tickCount) {
                    iterator2.remove();
                    this.onCooldownEnded(entry3.getKey());
                }
            }
        }
    }
    
    public void addCooldown(final Item bke, final int integer) {
        this.cooldowns.put(bke, new CooldownInstance(this.tickCount, this.tickCount + integer));
        this.onCooldownStarted(bke, integer);
    }
    
    public void removeCooldown(final Item bke) {
        this.cooldowns.remove(bke);
        this.onCooldownEnded(bke);
    }
    
    protected void onCooldownStarted(final Item bke, final int integer) {
    }
    
    protected void onCooldownEnded(final Item bke) {
    }
    
    class CooldownInstance {
        private final int startTime;
        private final int endTime;
        
        private CooldownInstance(final int integer2, final int integer3) {
            this.startTime = integer2;
            this.endTime = integer3;
        }
    }
}
