Add PlayerItemGroupCooldownEvent (#11625)

This commit is contained in:
Nassim Jahnke 2024-11-19 11:33:06 +01:00 committed by GitHub
parent 21cc763352
commit 57eab3e312
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 111 additions and 15 deletions

View File

@ -6,14 +6,54 @@ Subject: [PATCH] Add PlayerItemCooldownEvent
diff --git a/src/main/java/io/papermc/paper/event/player/PlayerItemCooldownEvent.java b/src/main/java/io/papermc/paper/event/player/PlayerItemCooldownEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..07b3a93ea09f0ae7d0e7a5af3633a0c669d36fcf
index 0000000000000000000000000000000000000000..165dcf8b68da5dc01eaa83b3bffd7a2bbf0df526
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/PlayerItemCooldownEvent.java
@@ -0,0 +1,79 @@
@@ -0,0 +1,34 @@
+package io.papermc.paper.event.player;
+
+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..e66aed33b165b86cc5e51eb5c29159232be4a9ef
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/PlayerItemGroupCooldownEvent.java
@@ -0,0 +1,81 @@
+package io.papermc.paper.event.player;
+
+import com.google.common.base.Preconditions;
+import org.bukkit.Material;
+import org.bukkit.NamespacedKey;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
@ -23,31 +63,33 @@ index 0000000000000000000000000000000000000000..07b3a93ea09f0ae7d0e7a5af3633a0c6
+
+/**
+ * Fired when a player receives an item cooldown.
+ *
+ * @see PlayerItemCooldownEvent for a more specific event when applied to a specific item.
+ */
+@NullMarked
+public class PlayerItemCooldownEvent extends PlayerEvent implements Cancellable {
+public class PlayerItemGroupCooldownEvent extends PlayerEvent implements Cancellable {
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
+ private final Material type;
+ private final NamespacedKey cooldownGroup;
+ private int cooldown;
+
+ private boolean cancelled;
+
+ @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);
+ this.type = type;
+ this.cooldownGroup = cooldownGroup;
+ 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() {
+ return this.type;
+ public NamespacedKey getCooldownGroup() {
+ return this.cooldownGroup;
+ }
+
+ /**

View File

@ -4,24 +4,78 @@ Date: Tue, 25 Aug 2020 13:48:33 +0200
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 2add88d2543b2e1143bd4b1c53946d7ba3f399da..c7a21b33db802fa6b64865ff2b4f0941279b72cb 100644
--- a/src/main/java/net/minecraft/world/item/ItemCooldowns.java
+++ b/src/main/java/net/minecraft/world/item/ItemCooldowns.java
@@ -56,6 +56,13 @@ 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
index 3a45a149ec4a28f25ea9e45803ecbb7392b63f86..2a80b8e962bf9d01e720b5967f0feac7d5dcaa28 100644
index 3a45a149ec4a28f25ea9e45803ecbb7392b63f86..7b634a0a8639524a276cd6c5d6535e28a580b20a 100644
--- a/src/main/java/net/minecraft/world/item/ServerItemCooldowns.java
+++ b/src/main/java/net/minecraft/world/item/ServerItemCooldowns.java
@@ -11,6 +11,16 @@ public class ServerItemCooldowns extends ItemCooldowns {
@@ -11,6 +11,39 @@ public class ServerItemCooldowns extends ItemCooldowns {
this.player = player;
}
+ // Paper start - Add PlayerItemCooldownEvent
+ @Override
+ 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()) {
+ 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
+
@Override
protected void onCooldownStarted(ResourceLocation groupId, int 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 9022555db0df8c269fc039c895422cf36c08097e..8012ee71e1ce9f174eb5c4ac9eb8372b81e0a78c 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
@@ -619,7 +619,7 @@ 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