From d1d54d35bf8f829942d0d09d6590ab961ba8c497 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Tue, 25 Aug 2020 13:48:33 +0200 Subject: [PATCH] Add PlayerItemCooldownEvent --- .../world/item/ItemCooldowns.java.patch | 16 +++++++ .../world/item/ServerItemCooldowns.java.patch | 43 +++++++++++++++++++ .../craftbukkit/entity/CraftHumanEntity.java | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 paper-server/patches/sources/net/minecraft/world/item/ItemCooldowns.java.patch create mode 100644 paper-server/patches/sources/net/minecraft/world/item/ServerItemCooldowns.java.patch diff --git a/paper-server/patches/sources/net/minecraft/world/item/ItemCooldowns.java.patch b/paper-server/patches/sources/net/minecraft/world/item/ItemCooldowns.java.patch new file mode 100644 index 0000000000..f8d61e11c7 --- /dev/null +++ b/paper-server/patches/sources/net/minecraft/world/item/ItemCooldowns.java.patch @@ -0,0 +1,16 @@ +--- a/net/minecraft/world/item/ItemCooldowns.java ++++ b/net/minecraft/world/item/ItemCooldowns.java +@@ -56,6 +56,13 @@ + } + + 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/paper-server/patches/sources/net/minecraft/world/item/ServerItemCooldowns.java.patch b/paper-server/patches/sources/net/minecraft/world/item/ServerItemCooldowns.java.patch new file mode 100644 index 0000000000..b9e17d595b --- /dev/null +++ b/paper-server/patches/sources/net/minecraft/world/item/ServerItemCooldowns.java.patch @@ -0,0 +1,43 @@ +--- a/net/minecraft/world/item/ServerItemCooldowns.java ++++ b/net/minecraft/world/item/ServerItemCooldowns.java +@@ -11,7 +11,40 @@ + this.player = player; + } + ++ // Paper start - Add PlayerItemCooldownEvent + @Override ++ public void addCooldown(ItemStack item, int 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(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); + this.player.connection.send(new ClientboundCooldownPacket(groupId, duration)); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java index 9022555db0..8012ee71e1 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java +++ b/paper-server/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