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
This commit is contained in:
KnightMiner committed 2022-10-15 04:16:40 -04:00
1 parent 3baee80485
commit 59a6f08184
4 files changed
+74 -2

No files matched your search

@@ -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_);
@@ -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());
@@ -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);
}
}
}
+2
View File
@@ -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