-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
1b3fb6b
commit 1f8b087
Showing
8 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
+ } | ||
+} | ||
+ |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.