0
0
mirror of https://github.com/PaperMC/Paper.git synced 2025-01-31 12:01:22 +00:00
Paper/paper-server/patches/sources/net/minecraft/world/item/ServerItemCooldowns.java.patch

54 lines
2.3 KiB
Diff

--- a/net/minecraft/world/item/ServerItemCooldowns.java
+++ b/net/minecraft/world/item/ServerItemCooldowns.java
@@ -11,6 +_,50 @@
this.player = player;
}
+ // Paper start - Add PlayerItemCooldownEvent
+ private int getCurrentCooldown(final ResourceLocation groupId) {
+ final net.minecraft.world.item.ItemCooldowns.CooldownInstance cooldownInstance = this.cooldowns.get(groupId);
+ if (cooldownInstance == null) {
+ return 0;
+ }
+ return Math.max(0, cooldownInstance.endTime() - this.tickCount);
+ }
+
+ @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()) {
+ this.addCooldown(cooldownGroup, event.getCooldown(), false);
+ } else {
+ this.player.connection.send(new ClientboundCooldownPacket(cooldownGroup, this.getCurrentCooldown(cooldownGroup)));
+ }
+ }
+
+ @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()) {
+ this.player.connection.send(new ClientboundCooldownPacket(groupId, this.getCurrentCooldown(groupId)));
+ return;
+ }
+
+ duration = event.getCooldown();
+ }
+ super.addCooldown(groupId, duration, false);
+ }
+ // Paper end - Add PlayerItemCooldownEvent
+
@Override
protected void onCooldownStarted(ResourceLocation group, int cooldown) {
super.onCooldownStarted(group, cooldown);