Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CrafterCraftItemEvent #11082

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions patches/api/0481-Add-CrafterCraftEvent.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
Date: Thu, 18 Jul 2024 16:37:58 +0200
Subject: [PATCH] Add CrafterCraftEvent


diff --git a/src/main/java/org/bukkit/event/block/CrafterCraftEvent.java b/src/main/java/org/bukkit/event/block/CrafterCraftEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..8dec6d16aa3c3579eadd77f637c9afa54ee9a90f
--- /dev/null
+++ b/src/main/java/org/bukkit/event/block/CrafterCraftEvent.java
@@ -0,0 +1,77 @@
+package org.bukkit.event.block;
+
+import org.bukkit.block.Block;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.inventory.CraftingRecipe;
+import org.bukkit.inventory.ItemStack;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Event called when a Crafter is about to craft an item
+ * @apiNote Currently still experimental as it is ported from an open, not merged, spigot PR.
+ * The event was pulled to allow protection plugins and the likes to properly manage crafters.
+ * The type remains experimental as upstream *may* change the event before pulling it, resulting in a breaking change.
+ */
+@org.jetbrains.annotations.ApiStatus.Experimental
+public class CrafterCraftEvent extends BlockEvent implements Cancellable {
+
+ private static final HandlerList handlers = new HandlerList();
+ private boolean cancelled;
+ private ItemStack result;
+ private final CraftingRecipe recipe;
+
+ @org.jetbrains.annotations.ApiStatus.Internal // Paper - internal constructor.
+ public CrafterCraftEvent(@NotNull Block theBlock, @NotNull CraftingRecipe recipe, @NotNull ItemStack result) {
+ super(theBlock);
+ this.result = result;
+ this.recipe = recipe;
+ }
+
+ /**
+ * Gets the result for the craft
+ * @return the result for the craft
+ */
+ @NotNull
+ public ItemStack getResult() {
+ return result.clone();
+ }
+
+ /**
+ * Sets the result of the craft
+ * @param result the result of the craft
+ */
+ public void setResult(@NotNull ItemStack result) {
+ this.result = result.clone();
+ }
+
+ /**
+ * The recipe that was used to craft this item
+ * @return the recipe that was used to craft this item
+ */
+ @NotNull
+ public CraftingRecipe getRecipe() {
+ return recipe;
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancelled = cancel;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
27 changes: 27 additions & 0 deletions patches/server/1041-Add-CrafterCraftEvent.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
Date: Thu, 18 Jul 2024 16:38:48 +0200
Subject: [PATCH] Add CrafterCraftEvent


diff --git a/src/main/java/net/minecraft/world/level/block/CrafterBlock.java b/src/main/java/net/minecraft/world/level/block/CrafterBlock.java
index 6c24b25a7c8ce6e34aceb5702f1a0a6732ebca44..391a6af36d4c27a04000b31583297a25b89125d3 100644
--- a/src/main/java/net/minecraft/world/level/block/CrafterBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/CrafterBlock.java
@@ -163,6 +163,16 @@ public class CrafterBlock extends BaseEntityBlock {
} else {
RecipeHolder<CraftingRecipe> recipeHolder = optional.get();
ItemStack itemStack = recipeHolder.value().assemble(var11, world.registryAccess());
+ // Paper start - call CrafterCraftEvent
+ final org.bukkit.event.block.CrafterCraftEvent event = new org.bukkit.event.block.CrafterCraftEvent(
+ org.bukkit.craftbukkit.block.CraftBlock.at(world, pos),
+ (org.bukkit.inventory.CraftingRecipe) recipeHolder.toBukkitRecipe(),
+ org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(itemStack)
+ );
+ if (!event.callEvent()) return;
+
+ itemStack = org.bukkit.craftbukkit.inventory.CraftItemStack.unwrap(event.getResult());
+ // Paper end - call CrafterCraftEvent
if (itemStack.isEmpty()) {
world.levelEvent(1050, pos, 0);
} else {
Loading