[1.18.x] Backport grindstone event from 1.19 (#9090)

* Backport grindstone event from 1.19

* 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

* Add comments to the test mod explaining the expected behavior

* Clarify docs on behavior of the XP

Makes it clear XP is ignored in on place if the output is empty or the event is canceled

* Fix broadcast changes not being called if the event overrides grindstone behavior

Not sure its strictly needed, but every vanilla branch in the function calls it and its a trivial change.

* Add enabled flag to the test mod

* Improve javadocs

(cherry picked from commit e12c849cc0)

* Formatting fixes

* Apply javadoc suggestions from review

Co-authored-by: matyrobbrt <[email protected]>

* Correct capitalization in event name

* Javadoc improvements

Remove some redundancy in certain javadoc comments
Add @see to the header
Inline single line javadocs

* Add some funny curly brackets next to returns

Seems the kids these days have invented a new way to write return statements in javadocs

* Remove extra dots and capital letters

The return tag apparently puts more dots at the end, and having two dots on each sentence is not in the budget. Have also reached the capital letter quota

* Apply suggestions from code review

Co-authored-by: matyrobbrt <[email protected]>

Co-authored-by: ferriarnus <[email protected]>
Co-authored-by: matyrobbrt <[email protected]>
This commit is contained in:
authored and GitHub committed 2022-12-20 21:47:49 +10:30
1 parent 0ca10b5569
commit 2c3cb2025e
5 files changed
+337

No files matched your search

@@ -0,0 +1,88 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.GrindstoneEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@Mod("grindstone_event_test")
public class GrindstoneEventTest
{
private static final boolean ENABLED = false;
public GrindstoneEventTest()
{
if (ENABLED)
{
MinecraftForge.EVENT_BUS.register(this);
}
}
@SubscribeEvent
public void onGrindestonePlace(GrindstoneEvent.OnPlaceItem event)
{
// all of these "recipes" are slot sensitive, the top and bottom must match exactly for the behavior to change
// switching the order will cause the "recipe" to fail
ItemStack topItem = event.getTopItem();
ItemStack bottomItem = event.getBottomItem();
// craft lapis + netherite to get diamond and 5 XP
// this "recipe" is handled in OnTakeItem so the inputs only shrink by 1 each time
if (topItem.is(Items.LAPIS_LAZULI) && bottomItem.is(Items.NETHERITE_INGOT))
{
event.setOutput(new ItemStack(Items.DIAMOND, 1));
event.setXp(5);
}
// craft iron ore and flint to make raw iron ore, no XP is rewarded
// this "recipe" is *not* handled in OnTakeItem, so the inputs will always be set to empty regardless of stack size
if (topItem.is(Items.IRON_ORE) && bottomItem.is(Items.FLINT))
{
event.setOutput(new ItemStack(Items.RAW_IRON, 3));
event.setXp(0);
}
// when placing an iron axe in the top slot, simply copy it to the output without change (do not remove enchants)
// still grant XP equivalent to all enchantments on the axe
if (topItem.is(Items.IRON_AXE) && bottomItem.is(Items.AIR))
{
event.setOutput(topItem.copy());
event.setXp(-1);
}
// setting the output to empty will run default behavior, effectively ignoring all previous overrides to the event
// note this will ignore any XP value you set, effectively for this test mod this has no impact
if (topItem.is(Items.IRON_SHOVEL) && bottomItem.is(Items.AIR))
{
event.setOutput(ItemStack.EMPTY);
}
// canceling the event will prevent disenchanting an iron sword in the top slot
if (topItem.is(Items.IRON_SWORD) && bottomItem.is(Items.AIR))
{
event.setCanceled(true);
}
}
@SubscribeEvent
public void onGrindstoneTake(GrindstoneEvent.OnTakeItem event)
{
ItemStack topItem = event.getTopItem();
ItemStack bottomItem = event.getBottomItem();
// only shrink stacks by 1 for the lapis + netherite "recipe"
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
@@ -240,5 +240,7 @@ license="LGPL v2.1"
modId="potentialspawnsevent_test"
[[mods]]
modId="block_grow_feature_test"
[[mods]]
modId="grindstone_event_test"
# ADD ABOVE THIS LINE