From 59a6f08184391c600f1713ff191487cd70c6dd2d Mon Sep 17 00:00:00 2001 From: KnightMiner Date: Sat, 15 Oct 2022 02:31:21 -0400 Subject: [PATCH] Fix several bugs in the original PR related to canceling the event Fix canceling the event not clearing the result item, causing it to dupe whatever the result with the previous item was Fix canceling the event running vanilla behavior if vanilla behavior ran at all since the menu was opened and no other listener changed the XP --- .../world/inventory/GrindstoneMenu.java.patch | 2 +- .../net/minecraftforge/common/ForgeHooks.java | 6 +- .../debug/misc/GrindstoneEventTest.java | 66 +++++++++++++++++++ src/test/resources/META-INF/mods.toml | 2 + 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/test/java/net/minecraftforge/debug/misc/GrindstoneEventTest.java diff --git a/patches/minecraft/net/minecraft/world/inventory/GrindstoneMenu.java.patch b/patches/minecraft/net/minecraft/world/inventory/GrindstoneMenu.java.patch index 8f31fefb4a..fa89a33968 100644 --- a/patches/minecraft/net/minecraft/world/inventory/GrindstoneMenu.java.patch +++ b/patches/minecraft/net/minecraft/world/inventory/GrindstoneMenu.java.patch @@ -43,7 +43,7 @@ ItemStack itemstack1 = this.f_39560_.m_8020_(1); boolean flag = !itemstack.m_41619_() || !itemstack1.m_41619_(); boolean flag1 = !itemstack.m_41619_() && !itemstack1.m_41619_(); -+ this.xp = net.minecraftforge.common.ForgeHooks.onGrindstoneChange(itemstack, itemstack1, this.f_39559_, this.xp); ++ this.xp = net.minecraftforge.common.ForgeHooks.onGrindstoneChange(itemstack, itemstack1, this.f_39559_, -1); + if (this.xp != Integer.MIN_VALUE) return; if (!flag) { this.f_39559_.m_6836_(0, ItemStack.f_41583_); diff --git a/src/main/java/net/minecraftforge/common/ForgeHooks.java b/src/main/java/net/minecraftforge/common/ForgeHooks.java index ed57b0a316..c301c72a49 100644 --- a/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -759,7 +759,11 @@ public class ForgeHooks public static int onGrindstoneChange(@NotNull ItemStack top, @NotNull ItemStack bottom, Container outputSlot, int xp) { GrindstoneEvent.OnplaceItem e = new GrindstoneEvent.OnplaceItem(top, bottom, xp); - if (MinecraftForge.EVENT_BUS.post(e)) return e.getXp(); + if (MinecraftForge.EVENT_BUS.post(e)) + { + outputSlot.setItem(0, ItemStack.EMPTY); + return -1; + } if (e.getOutput().isEmpty()) return Integer.MIN_VALUE; outputSlot.setItem(0, e.getOutput()); diff --git a/src/test/java/net/minecraftforge/debug/misc/GrindstoneEventTest.java b/src/test/java/net/minecraftforge/debug/misc/GrindstoneEventTest.java new file mode 100644 index 0000000000..b1100b5fc4 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/misc/GrindstoneEventTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.debug.misc; + +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraftforge.event.GrindstoneEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +@Mod("grindstone_event_test") +@Mod.EventBusSubscriber +public class GrindstoneEventTest { + + @SubscribeEvent + public static void onGrindestonePlace(GrindstoneEvent.OnplaceItem event) + { + ItemStack topItem = event.getTopItem(); + ItemStack bottomItem = event.getBottomItem(); + if (topItem.is(Items.LAPIS_LAZULI) && bottomItem.is(Items.NETHERITE_INGOT)) + { + event.setOutput(new ItemStack(Items.DIAMOND, 1)); + event.setXp(5); + } + + if (topItem.is(Items.IRON_ORE) && bottomItem.is(Items.FLINT)) + { + event.setOutput(new ItemStack(Items.RAW_IRON, 3)); + event.setXp(0); + } + + if (topItem.is(Items.IRON_AXE) && bottomItem.is(Items.AIR)) + { + event.setOutput(topItem.copy()); + event.setXp(-1); + } + + if (topItem.is(Items.IRON_SHOVEL) && bottomItem.is(Items.AIR)) + { + event.setOutput(ItemStack.EMPTY); + } + + if (topItem.is(Items.IRON_SWORD) && bottomItem.is(Items.AIR)) + { + event.setCanceled(true); + } + } + + @SubscribeEvent + public static void onGrindstoneTake(GrindstoneEvent.OnTakeItem event) { + ItemStack topItem = event.getTopItem(); + ItemStack bottomItem = event.getBottomItem(); + if (topItem.is(Items.LAPIS_LAZULI) && bottomItem.is(Items.NETHERITE_INGOT)) + { + ItemStack top = topItem.copy(); + ItemStack bottom = bottomItem.copy(); + bottom.shrink(1); + top.shrink(1); + event.setNewBottomItem(bottom); + event.setNewTopItem(top); + } + } +} \ No newline at end of file diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index a0fb47688f..5d5bfe507a 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -268,6 +268,8 @@ modId="custom_item_decorations_test" modId="item_stacked_on_other_test" [[mods]] modId="ambient_occlusion_elements_test" +[[mods]] +modId="grindstone_event_test" # ADD ABOVE THIS LINE