| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.Iterator;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.Map.Entry;
|
| 7 | import net.minecraft.core.component.DataComponents;
|
| 8 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 | import net.minecraft.util.Mth;
|
| 11 | import net.minecraft.world.item.component.UseCooldown;
|
| 12 |
|
| 13 | public class ItemCooldowns {
|
| 14 | private final Map<Identifier, ItemCooldowns.CooldownInstance> cooldowns = Maps.newHashMap();
|
| 15 | private int tickCount;
|
| 16 |
|
| 17 | public boolean isOnCooldown(final ItemStack item) {
|
| 18 | return this.getCooldownPercent(item, 0.0F) > 0.0F;
|
| 19 | }
|
| 20 |
|
| 21 | public float getCooldownPercent(final ItemStack item, final float a) {
|
| 22 | Identifier group = this.getCooldownGroup(item);
|
| 23 | ItemCooldowns.CooldownInstance cooldown = this.cooldowns.get(group);
|
| 24 | if (cooldown != null) {
|
| 25 | float duration = cooldown.endTime - cooldown.startTime;
|
| 26 | float remaining = cooldown.endTime - (this.tickCount + a);
|
| 27 | return Mth.clamp(remaining / duration, 0.0F, 1.0F);
|
| 28 | } else {
|
| 29 | return 0.0F;
|
| 30 | }
|
| 31 | }
|
| 32 |
|
| 33 | public void tick() {
|
| 34 | this.tickCount++;
|
| 35 | if (!this.cooldowns.isEmpty()) {
|
| 36 | Iterator<Entry<Identifier, ItemCooldowns.CooldownInstance>> iterator = this.cooldowns.entrySet().iterator();
|
| 37 |
|
| 38 | while (iterator.hasNext()) {
|
| 39 | Entry<Identifier, ItemCooldowns.CooldownInstance> entry = iterator.next();
|
| 40 | if (entry.getValue().endTime <= this.tickCount) {
|
| 41 | iterator.remove();
|
| 42 | this.onCooldownEnded(entry.getKey());
|
| 43 | }
|
| 44 | }
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | public Identifier getCooldownGroup(final ItemStack item) {
|
| 49 | UseCooldown useCooldown = item.get(DataComponents.USE_COOLDOWN);
|
| 50 | Identifier defaultItemGroup = BuiltInRegistries.ITEM.getKey(item.getItem());
|
| 51 | return useCooldown == null ? defaultItemGroup : useCooldown.cooldownGroup().orElse(defaultItemGroup);
|
| 52 | }
|
| 53 |
|
| 54 | public void addCooldown(final ItemStack item, final int time) {
|
| 55 | this.addCooldown(this.getCooldownGroup(item), time);
|
| 56 | }
|
| 57 |
|
| 58 | public void addCooldown(final Identifier cooldownGroup, final int time) {
|
| 59 | this.cooldowns.put(cooldownGroup, new ItemCooldowns.CooldownInstance(this.tickCount, this.tickCount + time));
|
| 60 | this.onCooldownStarted(cooldownGroup, time);
|
| 61 | }
|
| 62 |
|
| 63 | public void removeCooldown(final Identifier cooldownGroup) {
|
| 64 | this.cooldowns.remove(cooldownGroup);
|
| 65 | this.onCooldownEnded(cooldownGroup);
|
| 66 | }
|
| 67 |
|
| 68 | protected void onCooldownStarted(final Identifier cooldownGroup, final int duration) {
|
| 69 | }
|
| 70 |
|
| 71 | protected void onCooldownEnded(final Identifier cooldownGroup) {
|
| 72 | }
|
| 73 |
|
| 74 | private record CooldownInstance(int startTime, int endTime) {
|
| 75 | }
|
| 76 | }
|
| 77 |
|