From c5a39ffa2ad39d484481ce0a6393b31cf7bc1149 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Wed, 20 Jan 2021 08:55:25 +1000 Subject: [PATCH] Add BlockPreDispenseEvent (#5075) --- .../0263-Add-BlockPreDispenseEvent.patch | 72 +++++++++++++++++++ ...0656-Implement-BlockPreDispenseEvent.patch | 42 +++++++++++ 2 files changed, 114 insertions(+) create mode 100644 Spigot-API-Patches/0263-Add-BlockPreDispenseEvent.patch create mode 100644 Spigot-Server-Patches/0656-Implement-BlockPreDispenseEvent.patch diff --git a/Spigot-API-Patches/0263-Add-BlockPreDispenseEvent.patch b/Spigot-API-Patches/0263-Add-BlockPreDispenseEvent.patch new file mode 100644 index 0000000000..5850391175 --- /dev/null +++ b/Spigot-API-Patches/0263-Add-BlockPreDispenseEvent.patch @@ -0,0 +1,72 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Matthew Miller +Date: Sun, 17 Jan 2021 13:15:54 +1000 +Subject: [PATCH] Add BlockPreDispenseEvent + + +diff --git a/src/main/java/io/papermc/paper/event/block/BlockPreDispenseEvent.java b/src/main/java/io/papermc/paper/event/block/BlockPreDispenseEvent.java +new file mode 100644 +index 0000000000000000000000000000000000000000..07aad3f4ff60a6a6de69634b0d31926e9c00e77b +--- /dev/null ++++ b/src/main/java/io/papermc/paper/event/block/BlockPreDispenseEvent.java +@@ -0,0 +1,60 @@ ++package io.papermc.paper.event.block; ++ ++import org.bukkit.block.Block; ++import org.bukkit.event.Cancellable; ++import org.bukkit.event.HandlerList; ++import org.bukkit.event.block.BlockEvent; ++import org.bukkit.inventory.ItemStack; ++import org.jetbrains.annotations.NotNull; ++ ++public class BlockPreDispenseEvent extends BlockEvent implements Cancellable { ++ private static final HandlerList handlers = new HandlerList(); ++ private boolean cancelled = false; ++ private final ItemStack itemStack; ++ private final int slot; ++ ++ public BlockPreDispenseEvent(@NotNull Block block, @NotNull ItemStack itemStack, int slot) { ++ super(block); ++ this.itemStack = itemStack; ++ this.slot = slot; ++ } ++ ++ /** ++ * Gets the {@link ItemStack} to be dispensed. ++ * ++ * @return The item to be dispensed ++ */ ++ @NotNull ++ public ItemStack getItemStack() { ++ return itemStack; ++ } ++ ++ /** ++ * Gets the inventory slot of the dispenser to dispense from. ++ * ++ * @return The inventory slot ++ */ ++ public int getSlot() { ++ return slot; ++ } ++ ++ @NotNull ++ public HandlerList getHandlers() { ++ return handlers; ++ } ++ ++ @NotNull ++ public static HandlerList getHandlerList() { ++ return handlers; ++ } ++ ++ @Override ++ public boolean isCancelled() { ++ return this.cancelled; ++ } ++ ++ @Override ++ public void setCancelled(boolean cancel) { ++ this.cancelled = cancel; ++ } ++} diff --git a/Spigot-Server-Patches/0656-Implement-BlockPreDispenseEvent.patch b/Spigot-Server-Patches/0656-Implement-BlockPreDispenseEvent.patch new file mode 100644 index 0000000000..84e1db2f66 --- /dev/null +++ b/Spigot-Server-Patches/0656-Implement-BlockPreDispenseEvent.patch @@ -0,0 +1,42 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Matthew Miller +Date: Sun, 17 Jan 2021 13:16:09 +1000 +Subject: [PATCH] Implement BlockPreDispenseEvent + + +diff --git a/src/main/java/net/minecraft/server/BlockDispenser.java b/src/main/java/net/minecraft/server/BlockDispenser.java +index 063453552b7b6c2395c8f9be27cdb71a15a56ca4..89325af931b5ca43729758e94e971b612f65eea2 100644 +--- a/src/main/java/net/minecraft/server/BlockDispenser.java ++++ b/src/main/java/net/minecraft/server/BlockDispenser.java +@@ -55,6 +55,7 @@ public class BlockDispenser extends BlockTileEntity { + IDispenseBehavior idispensebehavior = this.a(itemstack); + + if (idispensebehavior != IDispenseBehavior.NONE) { ++ if (!org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockPreDispenseEvent(worldserver, blockposition, itemstack, i)) return; // Paper - BlockPreDispenseEvent is called here + eventFired = false; // CraftBukkit - reset event status + tileentitydispenser.setItem(i, idispensebehavior.dispense(sourceblock, itemstack)); + } +diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +index 63d329dca12f902e2fb2a62ee4c86aea6453f1a3..696ded96e6018bc5289cc20f72d6dc9395d3b6a6 100644 +--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java ++++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +@@ -13,6 +13,7 @@ import java.util.List; + import java.util.Map; + import java.util.stream.Collectors; + import javax.annotation.Nullable; ++import io.papermc.paper.event.block.BlockPreDispenseEvent; + import net.minecraft.server.BlockPosition; + import net.minecraft.server.BlockPropertyInstrument; + import net.minecraft.server.Container; +@@ -1770,5 +1771,11 @@ public class CraftEventFactory { + BlockFailedDispenseEvent event = new BlockFailedDispenseEvent(block); + return event.callEvent(); + } ++ ++ public static boolean handleBlockPreDispenseEvent(WorldServer worldserver, BlockPosition blockposition, ItemStack itemStack, int slot) { ++ org.bukkit.block.Block block = worldserver.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ()); ++ BlockPreDispenseEvent event = new BlockPreDispenseEvent(block, org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(itemStack), slot); ++ return event.callEvent(); ++ } + // Paper end + }