From 3b02552381f25e713967272fd917397c5db78290 Mon Sep 17 00:00:00 2001 From: ewoudje <7384674+ewoudje@users.noreply.github.com> Date: Fri, 8 Nov 2024 01:18:44 +0100 Subject: [PATCH 1/5] Destroys items if damaged enough in crafting #1585 --- src/main/java/net/neoforged/neoforge/common/CommonHooks.java | 2 +- .../neoforge/common/extensions/IItemStackExtension.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/neoforged/neoforge/common/CommonHooks.java b/src/main/java/net/neoforged/neoforge/common/CommonHooks.java index d81c78914c5..d774909204d 100644 --- a/src/main/java/net/neoforged/neoforge/common/CommonHooks.java +++ b/src/main/java/net/neoforged/neoforge/common/CommonHooks.java @@ -739,7 +739,7 @@ public static Player getCraftingPlayer() { } public static ItemStack getCraftingRemainder(ItemStack stack) { - stack = stack.getCraftingRemainder(); + stack = stack.getItem().getCraftingRemainder(stack); if (!stack.isEmpty() && stack.isDamageableItem() && stack.getDamageValue() > stack.getMaxDamage()) { EventHooks.onPlayerDestroyItem(craftingPlayer.get(), stack, null); return ItemStack.EMPTY; diff --git a/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java b/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java index 0b18ba462f3..3f66b32211e 100644 --- a/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java +++ b/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java @@ -61,7 +61,7 @@ private ItemStack self() { * @return The resulting ItemStack */ default ItemStack getCraftingRemainder() { - return self().getItem().getCraftingRemainder(self()); + return CommonHooks.getCraftingRemainder(self()); } /** From 31e82c376ecd31b56d5e09b30ab489c4f750c88c Mon Sep 17 00:00:00 2001 From: ewoudje <7384674+ewoudje@users.noreply.github.com> Date: Fri, 8 Nov 2024 01:19:25 +0100 Subject: [PATCH 2/5] Use CommonHooks.getCraftingRemaining instead of IItemStackExtension --- .../world/item/crafting/BannerDuplicateRecipe.java.patch | 2 +- .../minecraft/world/item/crafting/BookCloningRecipe.java.patch | 2 +- .../minecraft/world/item/crafting/CraftingRecipe.java.patch | 3 ++- .../level/block/entity/AbstractFurnaceBlockEntity.java.patch | 2 +- .../level/block/entity/BrewingStandBlockEntity.java.patch | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/patches/net/minecraft/world/item/crafting/BannerDuplicateRecipe.java.patch b/patches/net/minecraft/world/item/crafting/BannerDuplicateRecipe.java.patch index 2a92e339c34..34d48ec2fc4 100644 --- a/patches/net/minecraft/world/item/crafting/BannerDuplicateRecipe.java.patch +++ b/patches/net/minecraft/world/item/crafting/BannerDuplicateRecipe.java.patch @@ -5,7 +5,7 @@ ItemStack itemstack = p_345377_.getItem(i); if (!itemstack.isEmpty()) { - ItemStack itemstack1 = itemstack.getItem().getCraftingRemainder(); -+ ItemStack itemstack1 = itemstack.getCraftingRemainder(); ++ ItemStack itemstack1 = net.neoforged.neoforge.common.CommonHooks.getCraftingRemainder(itemstack); if (!itemstack1.isEmpty()) { nonnulllist.set(i, itemstack1); } else if (!itemstack.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY).layers().isEmpty()) { diff --git a/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch b/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch index ccfed826ab5..0f6a15d8f94 100644 --- a/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch +++ b/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch @@ -5,7 +5,7 @@ for (int i = 0; i < nonnulllist.size(); i++) { ItemStack itemstack = p_344763_.getItem(i); - ItemStack itemstack1 = itemstack.getItem().getCraftingRemainder(); -+ ItemStack itemstack1 = itemstack.getCraftingRemainder(); ++ ItemStack itemstack1 = net.neoforged.neoforge.common.CommonHooks.getCraftingRemainder(itemstack); if (!itemstack1.isEmpty()) { nonnulllist.set(i, itemstack1); } else if (itemstack.getItem() instanceof WrittenBookItem) { diff --git a/patches/net/minecraft/world/item/crafting/CraftingRecipe.java.patch b/patches/net/minecraft/world/item/crafting/CraftingRecipe.java.patch index 7ba401edc67..a72aa9276c9 100644 --- a/patches/net/minecraft/world/item/crafting/CraftingRecipe.java.patch +++ b/patches/net/minecraft/world/item/crafting/CraftingRecipe.java.patch @@ -6,6 +6,7 @@ for (int i = 0; i < nonnulllist.size(); i++) { - Item item = p_380223_.getItem(i).getItem(); + ItemStack item = p_380223_.getItem(i); - nonnulllist.set(i, item.getCraftingRemainder()); +- nonnulllist.set(i, item.getCraftingRemainder()); ++ nonnulllist.set(i, net.neoforged.neoforge.common.CommonHooks.getCraftingRemainder(item)); } diff --git a/patches/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java.patch b/patches/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java.patch index 03bd036ae79..a0ace893bb6 100644 --- a/patches/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java.patch +++ b/patches/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java.patch @@ -62,7 +62,7 @@ p_155017_.litDuration = p_155017_.litTime; if (p_155017_.isLit()) { flag1 = true; -+ var remainder = itemstack.getCraftingRemainder(); ++ var remainder = net.neoforged.neoforge.common.CommonHooks.getCraftingRemainder(itemstack); + if (!remainder.isEmpty()) + p_155017_.items.set(1, remainder); + else diff --git a/patches/net/minecraft/world/level/block/entity/BrewingStandBlockEntity.java.patch b/patches/net/minecraft/world/level/block/entity/BrewingStandBlockEntity.java.patch index 12d0923b2fc..6c265da6883 100644 --- a/patches/net/minecraft/world/level/block/entity/BrewingStandBlockEntity.java.patch +++ b/patches/net/minecraft/world/level/block/entity/BrewingStandBlockEntity.java.patch @@ -13,7 +13,7 @@ } + net.neoforged.neoforge.event.EventHooks.onPotionBrewed(p_155293_); -+ ItemStack itemstack1 = itemstack.getCraftingRemainder(); ++ ItemStack itemstack1 = net.neoforged.neoforge.common.CommonHooks.getCraftingRemainder(itemstack); itemstack.shrink(1); - ItemStack itemstack1 = itemstack.getItem().getCraftingRemainder(); if (!itemstack1.isEmpty()) { From 4e951cac0ad7880576e6e0e136835e96825ea710 Mon Sep 17 00:00:00 2001 From: ewoudje <7384674+ewoudje@users.noreply.github.com> Date: Fri, 8 Nov 2024 15:33:32 +0100 Subject: [PATCH 3/5] Revert "Use CommonHooks.getCraftingRemaining instead of IItemStackExtension" This reverts commit 31e82c376ecd31b56d5e09b30ab489c4f750c88c. --- .../world/item/crafting/BannerDuplicateRecipe.java.patch | 2 +- .../minecraft/world/item/crafting/BookCloningRecipe.java.patch | 2 +- .../minecraft/world/item/crafting/CraftingRecipe.java.patch | 3 +-- .../level/block/entity/AbstractFurnaceBlockEntity.java.patch | 2 +- .../level/block/entity/BrewingStandBlockEntity.java.patch | 2 +- 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/patches/net/minecraft/world/item/crafting/BannerDuplicateRecipe.java.patch b/patches/net/minecraft/world/item/crafting/BannerDuplicateRecipe.java.patch index 34d48ec2fc4..2a92e339c34 100644 --- a/patches/net/minecraft/world/item/crafting/BannerDuplicateRecipe.java.patch +++ b/patches/net/minecraft/world/item/crafting/BannerDuplicateRecipe.java.patch @@ -5,7 +5,7 @@ ItemStack itemstack = p_345377_.getItem(i); if (!itemstack.isEmpty()) { - ItemStack itemstack1 = itemstack.getItem().getCraftingRemainder(); -+ ItemStack itemstack1 = net.neoforged.neoforge.common.CommonHooks.getCraftingRemainder(itemstack); ++ ItemStack itemstack1 = itemstack.getCraftingRemainder(); if (!itemstack1.isEmpty()) { nonnulllist.set(i, itemstack1); } else if (!itemstack.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY).layers().isEmpty()) { diff --git a/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch b/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch index 0f6a15d8f94..ccfed826ab5 100644 --- a/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch +++ b/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch @@ -5,7 +5,7 @@ for (int i = 0; i < nonnulllist.size(); i++) { ItemStack itemstack = p_344763_.getItem(i); - ItemStack itemstack1 = itemstack.getItem().getCraftingRemainder(); -+ ItemStack itemstack1 = net.neoforged.neoforge.common.CommonHooks.getCraftingRemainder(itemstack); ++ ItemStack itemstack1 = itemstack.getCraftingRemainder(); if (!itemstack1.isEmpty()) { nonnulllist.set(i, itemstack1); } else if (itemstack.getItem() instanceof WrittenBookItem) { diff --git a/patches/net/minecraft/world/item/crafting/CraftingRecipe.java.patch b/patches/net/minecraft/world/item/crafting/CraftingRecipe.java.patch index a72aa9276c9..7ba401edc67 100644 --- a/patches/net/minecraft/world/item/crafting/CraftingRecipe.java.patch +++ b/patches/net/minecraft/world/item/crafting/CraftingRecipe.java.patch @@ -6,7 +6,6 @@ for (int i = 0; i < nonnulllist.size(); i++) { - Item item = p_380223_.getItem(i).getItem(); + ItemStack item = p_380223_.getItem(i); -- nonnulllist.set(i, item.getCraftingRemainder()); -+ nonnulllist.set(i, net.neoforged.neoforge.common.CommonHooks.getCraftingRemainder(item)); + nonnulllist.set(i, item.getCraftingRemainder()); } diff --git a/patches/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java.patch b/patches/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java.patch index a0ace893bb6..03bd036ae79 100644 --- a/patches/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java.patch +++ b/patches/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java.patch @@ -62,7 +62,7 @@ p_155017_.litDuration = p_155017_.litTime; if (p_155017_.isLit()) { flag1 = true; -+ var remainder = net.neoforged.neoforge.common.CommonHooks.getCraftingRemainder(itemstack); ++ var remainder = itemstack.getCraftingRemainder(); + if (!remainder.isEmpty()) + p_155017_.items.set(1, remainder); + else diff --git a/patches/net/minecraft/world/level/block/entity/BrewingStandBlockEntity.java.patch b/patches/net/minecraft/world/level/block/entity/BrewingStandBlockEntity.java.patch index 6c265da6883..12d0923b2fc 100644 --- a/patches/net/minecraft/world/level/block/entity/BrewingStandBlockEntity.java.patch +++ b/patches/net/minecraft/world/level/block/entity/BrewingStandBlockEntity.java.patch @@ -13,7 +13,7 @@ } + net.neoforged.neoforge.event.EventHooks.onPotionBrewed(p_155293_); -+ ItemStack itemstack1 = net.neoforged.neoforge.common.CommonHooks.getCraftingRemainder(itemstack); ++ ItemStack itemstack1 = itemstack.getCraftingRemainder(); itemstack.shrink(1); - ItemStack itemstack1 = itemstack.getItem().getCraftingRemainder(); if (!itemstack1.isEmpty()) { From 7f22bccf87e7eaca00a13d3510c0e39771a9e02a Mon Sep 17 00:00:00 2001 From: ewoudje <7384674+ewoudje@users.noreply.github.com> Date: Sat, 9 Nov 2024 15:41:12 +0100 Subject: [PATCH 4/5] Inline CommonHooks call --- .../java/net/neoforged/neoforge/common/CommonHooks.java | 9 --------- .../neoforge/common/extensions/IItemStackExtension.java | 7 ++++++- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/neoforged/neoforge/common/CommonHooks.java b/src/main/java/net/neoforged/neoforge/common/CommonHooks.java index d774909204d..34b843d7371 100644 --- a/src/main/java/net/neoforged/neoforge/common/CommonHooks.java +++ b/src/main/java/net/neoforged/neoforge/common/CommonHooks.java @@ -738,15 +738,6 @@ public static Player getCraftingPlayer() { return craftingPlayer.get(); } - public static ItemStack getCraftingRemainder(ItemStack stack) { - stack = stack.getItem().getCraftingRemainder(stack); - if (!stack.isEmpty() && stack.isDamageableItem() && stack.getDamageValue() > stack.getMaxDamage()) { - EventHooks.onPlayerDestroyItem(craftingPlayer.get(), stack, null); - return ItemStack.EMPTY; - } - return stack; - } - public static boolean onPlayerAttackTarget(Player player, Entity target) { if (NeoForge.EVENT_BUS.post(new AttackEntityEvent(player, target)).isCanceled()) return false; diff --git a/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java b/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java index 3f66b32211e..4237f37617e 100644 --- a/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java +++ b/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java @@ -61,7 +61,12 @@ private ItemStack self() { * @return The resulting ItemStack */ default ItemStack getCraftingRemainder() { - return CommonHooks.getCraftingRemainder(self()); + ItemStack stack = self().getItem().getCraftingRemainder(self()); + if (!stack.isEmpty() && stack.isDamageableItem() && stack.getDamageValue() > stack.getMaxDamage()) { + EventHooks.onPlayerDestroyItem(craftingPlayer.get(), stack, null); + return ItemStack.EMPTY; + } + return stack; } /** From 95dd711ebc9d514e00c540393885a2279cb1e8d3 Mon Sep 17 00:00:00 2001 From: ewoudje <7384674+ewoudje@users.noreply.github.com> Date: Sat, 9 Nov 2024 16:32:33 +0100 Subject: [PATCH 5/5] Small fix --- .../neoforge/common/extensions/IItemStackExtension.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java b/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java index 4237f37617e..4fa15f8d8bc 100644 --- a/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java +++ b/src/main/java/net/neoforged/neoforge/common/extensions/IItemStackExtension.java @@ -63,7 +63,7 @@ private ItemStack self() { default ItemStack getCraftingRemainder() { ItemStack stack = self().getItem().getCraftingRemainder(self()); if (!stack.isEmpty() && stack.isDamageableItem() && stack.getDamageValue() > stack.getMaxDamage()) { - EventHooks.onPlayerDestroyItem(craftingPlayer.get(), stack, null); + EventHooks.onPlayerDestroyItem(CommonHooks.getCraftingPlayer(), stack, null); return ItemStack.EMPTY; } return stack;