From 1f8b087dff2268de188011dbb66bf2d89abc68fd Mon Sep 17 00:00:00 2001 From: MrPowerGamerBR Date: Tue, 25 Jun 2024 02:59:17 -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 --- .../api/0003-Add-CraftItemRecipeEvent.patch | 83 +++++++++++++++++++ ...atch => 0004-Parallel-world-ticking.patch} | 0 ...0005-SPARKLYPOWER-Add-custom-blocks.patch} | 2 +- .../0022-Add-CraftItemRecipeEvent.patch | 75 +++++++++++++++++ ... 0023-Allow-item-version-downgrades.patch} | 0 ...atch => 0024-Parallel-world-ticking.patch} | 4 +- ...p-SparklyPower-hacky-legacy-NBT-tag.patch} | 0 ...0026-SPARKLYPOWER-Add-custom-blocks.patch} | 0 8 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 patches/api/0003-Add-CraftItemRecipeEvent.patch rename patches/api/{0003-Parallel-world-ticking.patch => 0004-Parallel-world-ticking.patch} (100%) rename patches/api/{0004-SPARKLYPOWER-Add-custom-blocks.patch => 0005-SPARKLYPOWER-Add-custom-blocks.patch} (96%) create mode 100644 patches/server/0022-Add-CraftItemRecipeEvent.patch rename patches/server/{0025-Allow-item-version-downgrades.patch => 0023-Allow-item-version-downgrades.patch} (100%) rename patches/server/{0022-Parallel-world-ticking.patch => 0024-Parallel-world-ticking.patch} (99%) rename patches/server/{0023-SPARKLYPOWER-Remap-SparklyPower-hacky-legacy-NBT-tag.patch => 0025-SPARKLYPOWER-Remap-SparklyPower-hacky-legacy-NBT-tag.patch} (100%) rename patches/server/{0024-SPARKLYPOWER-Add-custom-blocks.patch => 0026-SPARKLYPOWER-Add-custom-blocks.patch} (100%) diff --git a/patches/api/0003-Add-CraftItemRecipeEvent.patch b/patches/api/0003-Add-CraftItemRecipeEvent.patch new file mode 100644 index 0000000..e25f248 --- /dev/null +++ b/patches/api/0003-Add-CraftItemRecipeEvent.patch @@ -0,0 +1,83 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: MrPowerGamerBR +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; ++ } ++} ++ diff --git a/patches/api/0003-Parallel-world-ticking.patch b/patches/api/0004-Parallel-world-ticking.patch similarity index 100% rename from patches/api/0003-Parallel-world-ticking.patch rename to patches/api/0004-Parallel-world-ticking.patch diff --git a/patches/api/0004-SPARKLYPOWER-Add-custom-blocks.patch b/patches/api/0005-SPARKLYPOWER-Add-custom-blocks.patch similarity index 96% rename from patches/api/0004-SPARKLYPOWER-Add-custom-blocks.patch rename to patches/api/0005-SPARKLYPOWER-Add-custom-blocks.patch index 2337e6c..4c08de0 100644 --- a/patches/api/0004-SPARKLYPOWER-Add-custom-blocks.patch +++ b/patches/api/0005-SPARKLYPOWER-Add-custom-blocks.patch @@ -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 diff --git a/patches/server/0022-Add-CraftItemRecipeEvent.patch b/patches/server/0022-Add-CraftItemRecipeEvent.patch new file mode 100644 index 0000000..975b46d --- /dev/null +++ b/patches/server/0022-Add-CraftItemRecipeEvent.patch @@ -0,0 +1,75 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: MrPowerGamerBR +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 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) { diff --git a/patches/server/0025-Allow-item-version-downgrades.patch b/patches/server/0023-Allow-item-version-downgrades.patch similarity index 100% rename from patches/server/0025-Allow-item-version-downgrades.patch rename to patches/server/0023-Allow-item-version-downgrades.patch diff --git a/patches/server/0022-Parallel-world-ticking.patch b/patches/server/0024-Parallel-world-ticking.patch similarity index 99% rename from patches/server/0022-Parallel-world-ticking.patch rename to patches/server/0024-Parallel-world-ticking.patch index 16b37b4..65e3efb 100644 --- a/patches/server/0022-Parallel-world-ticking.patch +++ b/patches/server/0024-Parallel-world-ticking.patch @@ -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 { @@ -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)); diff --git a/patches/server/0023-SPARKLYPOWER-Remap-SparklyPower-hacky-legacy-NBT-tag.patch b/patches/server/0025-SPARKLYPOWER-Remap-SparklyPower-hacky-legacy-NBT-tag.patch similarity index 100% rename from patches/server/0023-SPARKLYPOWER-Remap-SparklyPower-hacky-legacy-NBT-tag.patch rename to patches/server/0025-SPARKLYPOWER-Remap-SparklyPower-hacky-legacy-NBT-tag.patch diff --git a/patches/server/0024-SPARKLYPOWER-Add-custom-blocks.patch b/patches/server/0026-SPARKLYPOWER-Add-custom-blocks.patch similarity index 100% rename from patches/server/0024-SPARKLYPOWER-Add-custom-blocks.patch rename to patches/server/0026-SPARKLYPOWER-Add-custom-blocks.patch