Add PlayerItemGroupCooldownEvent (#11625)

This commit is contained in:
Nassim Jahnke 2024-11-19 11:33:06 +01:00
parent 5ada2514df
commit f13d2012fe
2 changed files with 107 additions and 11 deletions

View File

@ -12,8 +12,48 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@@ -0,0 +0,0 @@ @@ -0,0 +0,0 @@
+package io.papermc.paper.event.player; +package io.papermc.paper.event.player;
+ +
+import com.google.common.base.Preconditions;
+import org.bukkit.Material; +import org.bukkit.Material;
+import org.bukkit.NamespacedKey;
+import org.bukkit.entity.Player;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
+
+/**
+ * Fired when a player receives an item cooldown when using an item.
+ *
+ * @see PlayerItemGroupCooldownEvent for a more general event when applied to a group of items
+ */
+@NullMarked
+public class PlayerItemCooldownEvent extends PlayerItemGroupCooldownEvent {
+
+ private final Material type;
+
+ @ApiStatus.Internal
+ public PlayerItemCooldownEvent(final Player player, final Material type, final NamespacedKey cooldownGroup, final int cooldown) {
+ super(player, cooldownGroup, cooldown);
+ this.type = type;
+ }
+
+ /**
+ * Get the material of the item affected by the cooldown.
+ *
+ * @return material affected by the cooldown
+ */
+ public Material getType() {
+ return this.type;
+ }
+}
diff --git a/src/main/java/io/papermc/paper/event/player/PlayerItemGroupCooldownEvent.java b/src/main/java/io/papermc/paper/event/player/PlayerItemGroupCooldownEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/PlayerItemGroupCooldownEvent.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.event.player;
+
+import com.google.common.base.Preconditions;
+import org.bukkit.NamespacedKey;
+import org.bukkit.entity.Player; +import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable; +import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList; +import org.bukkit.event.HandlerList;
@ -23,31 +63,33 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ +
+/** +/**
+ * Fired when a player receives an item cooldown. + * Fired when a player receives an item cooldown.
+ *
+ * @see PlayerItemCooldownEvent for a more specific event when applied to a specific item.
+ */ + */
+@NullMarked +@NullMarked
+public class PlayerItemCooldownEvent extends PlayerEvent implements Cancellable { +public class PlayerItemGroupCooldownEvent extends PlayerEvent implements Cancellable {
+ +
+ private static final HandlerList HANDLER_LIST = new HandlerList(); + private static final HandlerList HANDLER_LIST = new HandlerList();
+ +
+ private final Material type; + private final NamespacedKey cooldownGroup;
+ private int cooldown; + private int cooldown;
+ +
+ private boolean cancelled; + private boolean cancelled;
+ +
+ @ApiStatus.Internal + @ApiStatus.Internal
+ public PlayerItemCooldownEvent(final Player player, final Material type, final int cooldown) { + public PlayerItemGroupCooldownEvent(final Player player, final NamespacedKey cooldownGroup, final int cooldown) {
+ super(player); + super(player);
+ this.type = type; + this.cooldownGroup = cooldownGroup;
+ this.cooldown = cooldown; + this.cooldown = cooldown;
+ } + }
+ +
+ /** + /**
+ * Get the material affected by the cooldown. + * Get the cooldown group as defined by an item's {@link org.bukkit.inventory.meta.components.UseCooldownComponent}.
+ * + *
+ * @return material affected by the cooldown + * @return cooldown group
+ */ + */
+ public Material getType() { + public NamespacedKey getCooldownGroup() {
+ return this.type; + return this.cooldownGroup;
+ } + }
+ +
+ /** + /**

View File

@ -4,6 +4,24 @@ Date: Tue, 25 Aug 2020 13:48:33 +0200
Subject: [PATCH] Add PlayerItemCooldownEvent Subject: [PATCH] Add PlayerItemCooldownEvent
diff --git a/src/main/java/net/minecraft/world/item/ItemCooldowns.java b/src/main/java/net/minecraft/world/item/ItemCooldowns.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/item/ItemCooldowns.java
+++ b/src/main/java/net/minecraft/world/item/ItemCooldowns.java
@@ -0,0 +0,0 @@ public class ItemCooldowns {
}
public void addCooldown(ResourceLocation groupId, int duration) {
+ // Paper start - Item cooldown events
+ this.addCooldown(groupId, duration, true);
+ }
+
+ public void addCooldown(ResourceLocation groupId, int duration, boolean callEvent) {
+ // Event called in server override
+ // Paper end - Item cooldown events
this.cooldowns.put(groupId, new ItemCooldowns.CooldownInstance(this.tickCount, this.tickCount + duration));
this.onCooldownStarted(groupId, duration);
}
diff --git a/src/main/java/net/minecraft/world/item/ServerItemCooldowns.java b/src/main/java/net/minecraft/world/item/ServerItemCooldowns.java diff --git a/src/main/java/net/minecraft/world/item/ServerItemCooldowns.java b/src/main/java/net/minecraft/world/item/ServerItemCooldowns.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/item/ServerItemCooldowns.java --- a/src/main/java/net/minecraft/world/item/ServerItemCooldowns.java
@ -15,13 +33,49 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ // Paper start - Add PlayerItemCooldownEvent + // Paper start - Add PlayerItemCooldownEvent
+ @Override + @Override
+ public void addCooldown(ItemStack item, int duration) { + public void addCooldown(ItemStack item, int duration) {
+ io.papermc.paper.event.player.PlayerItemCooldownEvent event = new io.papermc.paper.event.player.PlayerItemCooldownEvent(this.player.getBukkitEntity(), org.bukkit.craftbukkit.inventory.CraftItemType.minecraftToBukkit(item.getItem()), duration); + final ResourceLocation cooldownGroup = this.getCooldownGroup(item);
+ final io.papermc.paper.event.player.PlayerItemCooldownEvent event = new io.papermc.paper.event.player.PlayerItemCooldownEvent(
+ this.player.getBukkitEntity(),
+ org.bukkit.craftbukkit.inventory.CraftItemType.minecraftToBukkit(item.getItem()),
+ org.bukkit.craftbukkit.util.CraftNamespacedKey.fromMinecraft(cooldownGroup),
+ duration
+ );
+ if (event.callEvent()) { + if (event.callEvent()) {
+ super.addCooldown(item, event.getCooldown()); + super.addCooldown(cooldownGroup, event.getCooldown(), false);
+ } + }
+ } + }
+
+ @Override
+ public void addCooldown(ResourceLocation groupId, int duration, boolean callEvent) {
+ if (callEvent) {
+ final io.papermc.paper.event.player.PlayerItemGroupCooldownEvent event = new io.papermc.paper.event.player.PlayerItemGroupCooldownEvent(
+ this.player.getBukkitEntity(),
+ org.bukkit.craftbukkit.util.CraftNamespacedKey.fromMinecraft(groupId),
+ duration
+ );
+ if (!event.callEvent()) {
+ return;
+ }
+
+ duration = event.getCooldown();
+ }
+ super.addCooldown(groupId, duration, false);
+ }
+ // Paper end - Add PlayerItemCooldownEvent + // Paper end - Add PlayerItemCooldownEvent
+ +
@Override @Override
protected void onCooldownStarted(ResourceLocation groupId, int duration) { protected void onCooldownStarted(ResourceLocation groupId, int duration) {
super.onCooldownStarted(groupId, duration); super.onCooldownStarted(groupId, duration);
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
@@ -0,0 +0,0 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
}
ItemCooldowns.CooldownInstance cooldown = this.getHandle().getCooldowns().cooldowns.get(group);
- return (cooldown == null) ? 0 : Math.max(0, cooldown.endTime - this.getHandle().getCooldowns().tickCount);
+ return (cooldown == null) ? 0 : Math.max(0, cooldown.endTime() - this.getHandle().getCooldowns().tickCount);
}
@Override