From 9a7508c3abc78707fd62ae94d34f4b72732f76e5 Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Tue, 30 Jul 2024 07:07:10 +1000 Subject: [PATCH] #1450: Add CrafterCraftEvent By: ploppyperson Also-by: md_5 --- .../world/item/crafting/IRecipeComplex.patch | 16 +++++++++++--- .../world/level/block/CrafterBlock.patch | 22 ++++++++++++++++--- .../craftbukkit/event/CraftEventFactory.java | 14 ++++++++++++ .../inventory/CraftComplexRecipe.java | 21 +++++------------- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/paper-server/nms-patches/net/minecraft/world/item/crafting/IRecipeComplex.patch b/paper-server/nms-patches/net/minecraft/world/item/crafting/IRecipeComplex.patch index 60e3d123f5..4087f9ddb6 100644 --- a/paper-server/nms-patches/net/minecraft/world/item/crafting/IRecipeComplex.patch +++ b/paper-server/nms-patches/net/minecraft/world/item/crafting/IRecipeComplex.patch @@ -1,18 +1,22 @@ --- a/net/minecraft/world/item/crafting/IRecipeComplex.java +++ b/net/minecraft/world/item/crafting/IRecipeComplex.java -@@ -3,6 +3,11 @@ +@@ -3,6 +3,15 @@ import net.minecraft.core.HolderLookup; import net.minecraft.world.item.ItemStack; +// CraftBukkit start ++import net.minecraft.core.IRegistryCustom; +import org.bukkit.NamespacedKey; ++import org.bukkit.craftbukkit.inventory.CraftComplexRecipe; ++import org.bukkit.craftbukkit.inventory.CraftItemStack; ++import org.bukkit.craftbukkit.inventory.CraftRecipe; +import org.bukkit.inventory.Recipe; +// CraftBukkit end + public abstract class IRecipeComplex implements RecipeCrafting { private final CraftingBookCategory category; -@@ -25,4 +30,11 @@ +@@ -25,4 +34,17 @@ public CraftingBookCategory category() { return this.category; } @@ -20,7 +24,13 @@ + // CraftBukkit start + @Override + public Recipe toBukkitRecipe(NamespacedKey id) { -+ return new org.bukkit.craftbukkit.inventory.CraftComplexRecipe(id, this); ++ CraftItemStack result = CraftItemStack.asCraftMirror(getResultItem(IRegistryCustom.EMPTY)); ++ ++ CraftComplexRecipe recipe = new CraftComplexRecipe(id, result, this); ++ recipe.setGroup(this.getGroup()); ++ recipe.setCategory(CraftRecipe.getCategory(this.category())); ++ ++ return recipe; + } + // CraftBukkit end } diff --git a/paper-server/nms-patches/net/minecraft/world/level/block/CrafterBlock.patch b/paper-server/nms-patches/net/minecraft/world/level/block/CrafterBlock.patch index 01c47d9f38..d0ada7204f 100644 --- a/paper-server/nms-patches/net/minecraft/world/level/block/CrafterBlock.patch +++ b/paper-server/nms-patches/net/minecraft/world/level/block/CrafterBlock.patch @@ -1,12 +1,14 @@ --- a/net/minecraft/world/level/block/CrafterBlock.java +++ b/net/minecraft/world/level/block/CrafterBlock.java -@@ -39,6 +39,13 @@ +@@ -39,6 +39,15 @@ import net.minecraft.world.phys.MovingObjectPositionBlock; import net.minecraft.world.phys.Vec3D; +// CraftBukkit start +import net.minecraft.world.InventoryLargeChest; ++import org.bukkit.craftbukkit.event.CraftEventFactory; +import org.bukkit.craftbukkit.inventory.CraftItemStack; ++import org.bukkit.event.block.CrafterCraftEvent; +import org.bukkit.event.inventory.InventoryMoveItemEvent; +import org.bukkit.inventory.Inventory; +// CraftBukkit end @@ -14,7 +16,21 @@ public class CrafterBlock extends BlockTileEntity { public static final MapCodec CODEC = simpleCodec(CrafterBlock::new); -@@ -226,7 +233,25 @@ +@@ -188,6 +197,13 @@ + RecipeHolder recipeholder = (RecipeHolder) optional.get(); + ItemStack itemstack = ((RecipeCrafting) recipeholder.value()).assemble(craftinginput, worldserver.registryAccess()); + ++ // CraftBukkit start ++ CrafterCraftEvent event = CraftEventFactory.callCrafterCraftEvent(blockposition, worldserver, crafterblockentity, itemstack, recipeholder); ++ if (event.isCancelled()) { ++ return; ++ } ++ itemstack = CraftItemStack.asNMSCopy(event.getResult()); ++ // CraftBukkit end + if (itemstack.isEmpty()) { + worldserver.levelEvent(1050, blockposition, 0); + } else { +@@ -226,7 +242,25 @@ ItemStack itemstack1 = itemstack.copy(); if (iinventory != null && (iinventory instanceof CrafterBlockEntity || itemstack.getCount() > iinventory.getMaxStackSize(itemstack))) { @@ -40,7 +56,7 @@ ItemStack itemstack2 = itemstack1.copyWithCount(1); ItemStack itemstack3 = TileEntityHopper.addItem(crafterblockentity, iinventory, itemstack2, enumdirection.getOpposite()); -@@ -237,7 +262,25 @@ +@@ -237,7 +271,25 @@ itemstack1.shrink(1); } } else if (iinventory != null) { diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index 8171606d73..fe60bfb5b5 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -61,6 +61,8 @@ import net.minecraft.world.inventory.RecipeBookType; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.item.context.ItemActionContext; +import net.minecraft.world.item.crafting.RecipeCrafting; +import net.minecraft.world.item.crafting.RecipeHolder; import net.minecraft.world.level.ChunkCoordIntPair; import net.minecraft.world.level.Explosion; import net.minecraft.world.level.GeneratorAccess; @@ -156,6 +158,7 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockRedstoneEvent; import org.bukkit.event.block.BlockShearEntityEvent; import org.bukkit.event.block.BlockSpreadEvent; +import org.bukkit.event.block.CrafterCraftEvent; import org.bukkit.event.block.EntityBlockFormEvent; import org.bukkit.event.block.FluidLevelChangeEvent; import org.bukkit.event.block.MoistureChangeEvent; @@ -256,6 +259,7 @@ import org.bukkit.event.weather.LightningStrikeEvent; import org.bukkit.event.world.EntitiesLoadEvent; import org.bukkit.event.world.EntitiesUnloadEvent; import org.bukkit.event.world.LootGenerateEvent; +import org.bukkit.inventory.CraftingRecipe; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.Recipe; @@ -1286,6 +1290,16 @@ public class CraftEventFactory { return CraftItemStack.asNMSCopy(bitem); } + public static CrafterCraftEvent callCrafterCraftEvent(BlockPosition pos, World world, InventoryCrafting inventoryCrafting, ItemStack result, RecipeHolder holder) { + CraftBlock block = CraftBlock.at(world, pos); + CraftItemStack itemStack = CraftItemStack.asCraftMirror(result); + CraftingRecipe craftingRecipe = (CraftingRecipe) holder.toBukkitRecipe(); + + CrafterCraftEvent crafterCraftEvent = new CrafterCraftEvent(block, craftingRecipe, itemStack); + Bukkit.getPluginManager().callEvent(crafterCraftEvent); + return crafterCraftEvent; + } + public static ProjectileLaunchEvent callProjectileLaunchEvent(Entity entity) { Projectile bukkitEntity = (Projectile) entity.getBukkitEntity(); ProjectileLaunchEvent event = new ProjectileLaunchEvent(bukkitEntity); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftComplexRecipe.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftComplexRecipe.java index 3fd7db9b21..1427f478fb 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftComplexRecipe.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftComplexRecipe.java @@ -1,36 +1,25 @@ package org.bukkit.craftbukkit.inventory; -import net.minecraft.core.IRegistryCustom; import net.minecraft.server.MinecraftServer; import net.minecraft.world.item.crafting.IRecipeComplex; import net.minecraft.world.item.crafting.RecipeHolder; import org.bukkit.NamespacedKey; import org.bukkit.craftbukkit.util.CraftNamespacedKey; import org.bukkit.inventory.ComplexRecipe; +import org.bukkit.inventory.CraftingRecipe; import org.bukkit.inventory.ItemStack; -public class CraftComplexRecipe implements CraftRecipe, ComplexRecipe { +public class CraftComplexRecipe extends CraftingRecipe implements CraftRecipe, ComplexRecipe { - private final NamespacedKey key; private final IRecipeComplex recipe; - public CraftComplexRecipe(NamespacedKey key, IRecipeComplex recipe) { - this.key = key; + public CraftComplexRecipe(NamespacedKey key, ItemStack result, IRecipeComplex recipe) { + super(key, result); this.recipe = recipe; } - @Override - public ItemStack getResult() { - return CraftItemStack.asCraftMirror(recipe.getResultItem(IRegistryCustom.EMPTY)); - } - - @Override - public NamespacedKey getKey() { - return key; - } - @Override public void addToCraftingManager() { - MinecraftServer.getServer().getRecipeManager().addRecipe(new RecipeHolder<>(CraftNamespacedKey.toMinecraft(key), recipe)); + MinecraftServer.getServer().getRecipeManager().addRecipe(new RecipeHolder<>(CraftNamespacedKey.toMinecraft(this.getKey()), recipe)); } }