minecraft-src/net/minecraft/world/item/ItemCooldowns.java
2025-07-04 01:41:11 +03:00

68 lines
2 KiB
Java

package net.minecraft.world.item;
import com.google.common.collect.Maps;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.util.Mth;
public class ItemCooldowns {
private final Map<Item, ItemCooldowns.CooldownInstance> cooldowns = Maps.<Item, ItemCooldowns.CooldownInstance>newHashMap();
private int tickCount;
public boolean isOnCooldown(Item item) {
return this.getCooldownPercent(item, 0.0F) > 0.0F;
}
public float getCooldownPercent(Item item, float partialTicks) {
ItemCooldowns.CooldownInstance cooldownInstance = (ItemCooldowns.CooldownInstance)this.cooldowns.get(item);
if (cooldownInstance != null) {
float f = cooldownInstance.endTime - cooldownInstance.startTime;
float g = cooldownInstance.endTime - (this.tickCount + partialTicks);
return Mth.clamp(g / f, 0.0F, 1.0F);
} else {
return 0.0F;
}
}
public void tick() {
this.tickCount++;
if (!this.cooldowns.isEmpty()) {
Iterator<Entry<Item, ItemCooldowns.CooldownInstance>> iterator = this.cooldowns.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Item, ItemCooldowns.CooldownInstance> entry = (Entry<Item, ItemCooldowns.CooldownInstance>)iterator.next();
if (((ItemCooldowns.CooldownInstance)entry.getValue()).endTime <= this.tickCount) {
iterator.remove();
this.onCooldownEnded((Item)entry.getKey());
}
}
}
}
public void addCooldown(Item item, int ticks) {
this.cooldowns.put(item, new ItemCooldowns.CooldownInstance(this.tickCount, this.tickCount + ticks));
this.onCooldownStarted(item, ticks);
}
public void removeCooldown(Item item) {
this.cooldowns.remove(item);
this.onCooldownEnded(item);
}
protected void onCooldownStarted(Item item, int ticks) {
}
protected void onCooldownEnded(Item item) {
}
static class CooldownInstance {
final int startTime;
final int endTime;
CooldownInstance(int startTime, int endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
}
}