Expand cooldown API (#12435)

This commit is contained in:
Jakub Zacek 2025-04-22 11:30:00 +02:00 committed by GitHub
parent def0532ffc
commit d22644aada
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 5 deletions

View File

@ -3,6 +3,8 @@ package org.bukkit.entity;
import java.util.Collection;
import java.util.Set;
import java.util.function.Consumer;
import io.papermc.paper.datacomponent.item.UseCooldown;
import net.kyori.adventure.key.Key;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
@ -368,7 +370,7 @@ public interface HumanEntity extends LivingEntity, AnimalTamer, InventoryHolder
/**
* Set a cooldown on the specified material for a certain amount of ticks.
* ticks. 0 ticks will result in the removal of the cooldown.
* 0 ticks will result in the removal of the cooldown.
* <p>
* Cooldowns are used by the server for items such as ender pearls and
* shields to prevent them from being used repeatedly.
@ -415,7 +417,7 @@ public interface HumanEntity extends LivingEntity, AnimalTamer, InventoryHolder
/**
* Set a cooldown on the specified item for a certain amount of ticks.
* ticks. 0 ticks will result in the removal of the cooldown.
* 0 ticks will result in the removal of the cooldown.
* <p>
* Cooldowns are used by the server for items such as ender pearls and
* shields to prevent them from being used repeatedly.
@ -428,6 +430,31 @@ public interface HumanEntity extends LivingEntity, AnimalTamer, InventoryHolder
*/
public void setCooldown(ItemStack item, int ticks);
/**
* Get the cooldown time in ticks remaining for the specified cooldown group.
*
* @param key the cooldown group to check
* @return the remaining cooldown time in ticks
* @see UseCooldown#cooldownGroup()
*/
public int getCooldown(Key key);
/**
* Set a cooldown on items with the specified cooldown group for a certain amount of ticks.
* 0 ticks will result in the removal of the cooldown.
* <p>
* Cooldowns are used by the server for items such as ender pearls and
* shields to prevent them from being used repeatedly.
* <p>
* Note that cooldowns will not by themselves stop an item from being used
* for attacking.
*
* @param key cooldown group to set the cooldown for
* @param ticks the amount of ticks to set or 0 to remove
* @see UseCooldown#cooldownGroup()
*/
public void setCooldown(Key key, int ticks);
/**
* Get the sleep ticks of the player. This value may be capped.
*

View File

@ -8,6 +8,8 @@ import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import io.papermc.paper.adventure.PaperAdventure;
import net.kyori.adventure.key.Key;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
@ -651,14 +653,14 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
@Override
public boolean hasCooldown(ItemStack item) {
Preconditions.checkArgument(item != null, "Material cannot be null");
Preconditions.checkArgument(item != null, "Item cannot be null");
return this.getHandle().getCooldowns().isOnCooldown(CraftItemStack.asNMSCopy(item));
}
@Override
public int getCooldown(ItemStack item) {
Preconditions.checkArgument(item != null, "Material cannot be null");
Preconditions.checkArgument(item != null, "Item cannot be null");
ResourceLocation group = this.getHandle().getCooldowns().getCooldownGroup(CraftItemStack.asNMSCopy(item));
if (group == null) {
@ -671,12 +673,28 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
@Override
public void setCooldown(ItemStack item, int ticks) {
Preconditions.checkArgument(item != null, "Material cannot be null");
Preconditions.checkArgument(item != null, "Item cannot be null");
Preconditions.checkArgument(ticks >= 0, "Cannot have negative cooldown");
this.getHandle().getCooldowns().addCooldown(CraftItemStack.asNMSCopy(item), ticks);
}
@Override
public int getCooldown(Key key) {
Preconditions.checkArgument(key != null, "Key cannot be null");
ItemCooldowns.CooldownInstance cooldown = this.getHandle().getCooldowns().cooldowns.get(PaperAdventure.asVanilla(key));
return (cooldown == null) ? 0 : Math.max(0, cooldown.endTime() - this.getHandle().getCooldowns().tickCount);
}
@Override
public void setCooldown(Key key, int ticks) {
Preconditions.checkArgument(key != null, "Key cannot be null");
Preconditions.checkArgument(ticks >= 0, "Cannot have negative cooldown");
this.getHandle().getCooldowns().addCooldown(PaperAdventure.asVanilla(key), ticks);
}
@Override
public org.bukkit.entity.Entity releaseLeftShoulderEntity() {
if (!getHandle().getShoulderEntityLeft().isEmpty()) {