minecraft-source/src/net/minecraft/world/item/ItemCooldowns.java

70 lines
2.2 KiB
Java

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 bef) {
return this.getCooldownPercent(bef, 0.0f) > 0.0f;
}
public float getCooldownPercent(final Item bef, final float float2) {
final CooldownInstance a4 = this.cooldowns.get(bef);
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 bef, final int integer) {
this.cooldowns.put(bef, new CooldownInstance(this.tickCount, this.tickCount + integer));
this.onCooldownStarted(bef, integer);
}
public void removeCooldown(final Item bef) {
this.cooldowns.remove(bef);
this.onCooldownEnded(bef);
}
protected void onCooldownStarted(final Item bef, final int integer) {
}
protected void onCooldownEnded(final Item bef) {
}
class CooldownInstance {
private final int startTime;
private final int endTime;
private CooldownInstance(final int integer2, final int integer3) {
this.startTime = integer2;
this.endTime = integer3;
}
}
}