Skip to content

Commit

Permalink
Add CraftItemRecipeEvent
Browse files Browse the repository at this point in the history
Used when a player OR a crafter block crafts an item, as an alternative to PrepareItemCraftEvent and CraftItemEvent, because both events are not triggered when a item is crafted from a crafter
  • Loading branch information
MrPowerGamerBR committed Jun 25, 2024
1 parent 1b3fb6b commit 1f8b087
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 3 deletions.
83 changes: 83 additions & 0 deletions patches/api/0003-Add-CraftItemRecipeEvent.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrPowerGamerBR <[email protected]>
Date: Tue, 25 Jun 2024 02:54:47 -0300
Subject: [PATCH] Add CraftItemRecipeEvent

Used when a player OR a crafter block crafts an item, as an alternative to PrepareItemCraftEvent and CraftItemEvent, because both events are not triggered when a item is crafted from a crafter

diff --git a/src/main/java/net/sparklypower/sparklypaper/event/inventory/CraftItemRecipeEvent.java b/src/main/java/net/sparklypower/sparklypaper/event/inventory/CraftItemRecipeEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..129e5244fd0928fc21d9aa7d4bc28e89c1408be0
--- /dev/null
+++ b/src/main/java/net/sparklypower/sparklypaper/event/inventory/CraftItemRecipeEvent.java
@@ -0,0 +1,70 @@
+package net.sparklypower.sparklypaper.event.inventory;
+
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+import org.bukkit.inventory.*;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Called when the recipe of an Item is completed inside a crafting matrix.
+ *
+ * This is an alternate version of [org.bukkit.event.inventory.CraftItemEvent], where this one is called for player crafting items and crafters.
+ */
+public class CraftItemRecipeEvent extends Event implements Cancellable {
+ private static final HandlerList handlers = new HandlerList();
+ private final Recipe recipe;
+ private final ItemStack @Nullable [] matrix;
+ private ItemStack result;
+ private boolean isCancelled = false;
+
+ public CraftItemRecipeEvent(@NotNull ItemStack @Nullable [] matrix, @NotNull Recipe recipe, @Nullable ItemStack result) {
+ this.matrix = matrix;
+ this.recipe = recipe;
+ this.result = result;
+ }
+
+ public void setResult(@Nullable ItemStack result) {
+ this.result = result;
+ }
+
+ @Nullable
+ public ItemStack getResult() {
+ return result;
+ }
+
+ /**
+ * @return A copy of the current recipe on the crafting matrix.
+ */
+ @NotNull
+ public Recipe getRecipe() {
+ return recipe;
+ }
+
+ public @Nullable ItemStack[] getCraftingMatrix() {
+ return matrix;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return isCancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.isCancelled = cancel;
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
+
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ index be212b4fbeabab32a4dab6ae554768c368efaa88..fc885e551b1e8393569ec1391ee3decd

/**
diff --git a/src/main/java/org/bukkit/Material.java b/src/main/java/org/bukkit/Material.java
index 7509b61dfdc0a6675256970cb850b08f9e814580..04ec0c27b37d7e732382815dafc52a4726c098a7 100644
index 762216a117145676d3df2b74036799b024461fb7..5e7b8972f4a2bb21458049a552ebbfae2697f82b 100644
--- a/src/main/java/org/bukkit/Material.java
+++ b/src/main/java/org/bukkit/Material.java
@@ -3818,6 +3818,22 @@ public enum Material implements Keyed, Translatable, net.kyori.adventure.transla
Expand Down
75 changes: 75 additions & 0 deletions patches/server/0022-Add-CraftItemRecipeEvent.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrPowerGamerBR <[email protected]>
Date: Tue, 25 Jun 2024 02:52:32 -0300
Subject: [PATCH] Add CraftItemRecipeEvent

Used when a player OR a crafter block crafts an item, as an alternative to PrepareItemCraftEvent and CraftItemEvent, because both events are not triggered when a item is crafted from a crafter

diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 4ae88bfcead40cd05f9514a48a922a37767cb3cf..1093fbbeb534d2639571c6b7814ba3254e290d73 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -3145,6 +3145,21 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
} else {
event = new CraftItemEvent(recipe, inventory, type, packet.getSlotNum(), click, action);
}
+ // SparklyPaper start - add CraftItemRecipeEvent
+ // We will pigback a bit on the current implementation
+ net.sparklypower.sparklypaper.event.inventory.CraftItemRecipeEvent craftItemRecipeEvent = new net.sparklypower.sparklypaper.event.inventory.CraftItemRecipeEvent(
+ ((CraftingInventory) top).getMatrix(), // We cannot use the top inventory directly because (it seems) that the first slot is the "result" slot
+ recipe,
+ event.getCurrentItem()
+ );
+ if (craftItemRecipeEvent.callEvent()) {
+ event.setCurrentItem(craftItemRecipeEvent.getResult());
+ } else {
+ event.setCancelled(true);
+ cancelled = true;
+ event.setCurrentItem(craftItemRecipeEvent.getResult());
+ }
+ // SparklyPaper end
}
}

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 4d315bd1df9f4647814500135195375166c578b3..1c69cd23b7f9dd1aacc0e26fe7181b99bb94d3ec 100644
--- a/src/main/java/net/minecraft/world/level/block/CrafterBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/CrafterBlock.java
@@ -162,6 +162,7 @@ public class CrafterBlock extends BaseEntityBlock {
} else {
RecipeHolder<CraftingRecipe> recipeHolder = optional.get();
ItemStack itemStack = recipeHolder.value().assemble(crafterBlockEntity, world.registryAccess());
+ itemStack = org.bukkit.craftbukkit.event.CraftEventFactory.callCraftItemRecipeEvent(crafterBlockEntity, recipeHolder.toBukkitRecipe(), itemStack); // SparklyPaper - add CraftItemRecipeEvent
if (itemStack.isEmpty()) {
world.levelEvent(1050, pos, 0);
} else {
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 0cba343989d6d33026a8e94f2b58ca93571721a9..bbde12198b15ebbe7f34ec28b79ff0f96bbed38f 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -1442,6 +1442,25 @@ public class CraftEventFactory {
return CraftItemStack.asNMSCopy(bitem);
}

+ // SparklyPaper start - add CraftItemRecipeEvent
+ public static ItemStack callCraftItemRecipeEvent(net.minecraft.world.inventory.CraftingContainer container, Recipe recipe, ItemStack result) {
+ org.bukkit.inventory.ItemStack[] matrix = new org.bukkit.inventory.ItemStack[container.getItems().size()];
+ int i = 0;
+ for (ItemStack item : container.getItems()) {
+ matrix[i] = CraftItemStack.asCraftMirror(item);
+ i++;
+ }
+ org.bukkit.inventory.ItemStack bukkitResult = CraftItemStack.asCraftMirror(result);
+
+ net.sparklypower.sparklypaper.event.inventory.CraftItemRecipeEvent event = new net.sparklypower.sparklypaper.event.inventory.CraftItemRecipeEvent(matrix, recipe, bukkitResult);
+ Bukkit.getPluginManager().callEvent(event);
+
+ org.bukkit.inventory.ItemStack bitem = event.getResult();
+
+ return CraftItemStack.asNMSCopy(bitem);
+ }
+ // SparklyPaper end
+
// Paper start
@Deprecated
public static com.destroystokyo.paper.event.entity.ProjectileCollideEvent callProjectileCollideEvent(Entity entity, EntityHitResult position) {
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ index b7ff7af2513204b151340538d50a65c850bdb75f..45f9b2594e449926d7f00f64bf12fef2
// Paper end
}
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 0cba343989d6d33026a8e94f2b58ca93571721a9..779c235c1819a0ec5263c47ff217827d240acf0f 100644
index bbde12198b15ebbe7f34ec28b79ff0f96bbed38f..5b342cc7d6621b2e9302cd6298fdef1c4f9b39da 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -942,7 +942,7 @@ public class CraftEventFactory {
Expand All @@ -1786,7 +1786,7 @@ index 0cba343989d6d33026a8e94f2b58ca93571721a9..779c235c1819a0ec5263c47ff217827d
Bukkit.getPluginManager().callEvent(event);

if (!event.isCancelled()) {
@@ -2154,7 +2154,7 @@ public class CraftEventFactory {
@@ -2173,7 +2173,7 @@ public class CraftEventFactory {
CraftItemStack craftItem = CraftItemStack.asCraftMirror(itemStack.copyWithCount(1));

org.bukkit.event.block.BlockDispenseEvent event = new org.bukkit.event.block.BlockDispenseEvent(bukkitBlock, craftItem.clone(), CraftVector.toBukkit(to));
Expand Down

0 comments on commit 1f8b087

Please sign in to comment.