[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

@@ -1,5 +1,53 @@
--- a/net/minecraft/world/inventory/GrindstoneMenu.java
+++ b/net/minecraft/world/inventory/GrindstoneMenu.java
@@ -35,6 +_,7 @@
}
};
private final ContainerLevelAccess f_39561_;
+ private int xp = -1;
public GrindstoneMenu(int p_39563_, Inventory p_39564_) {
this(p_39563_, p_39564_, ContainerLevelAccess.f_39287_);
@@ -45,12 +_,12 @@
this.f_39561_ = p_39568_;
this.m_38897_(new Slot(this.f_39560_, 0, 49, 19) {
public boolean m_5857_(ItemStack p_39607_) {
- return p_39607_.m_41763_() || p_39607_.m_150930_(Items.f_42690_) || p_39607_.m_41793_();
+ return true; //Allow all items in the slot, not just repairable
}
});
this.m_38897_(new Slot(this.f_39560_, 1, 49, 40) {
public boolean m_5857_(ItemStack p_39616_) {
- return p_39616_.m_41763_() || p_39616_.m_150930_(Items.f_42690_) || p_39616_.m_41793_();
+ return true; //Allow all items in the slot, not just repairable
}
});
this.m_38897_(new Slot(this.f_39559_, 2, 129, 34) {
@@ -59,6 +_,7 @@
}
public void m_142406_(Player p_150574_, ItemStack p_150575_) {
+ if (net.minecraftforge.common.ForgeHooks.onGrindstoneTake(GrindstoneMenu.this.f_39560_, p_39568_, this::m_39631_)) return;
p_39568_.m_39292_((p_39634_, p_39635_) -> {
if (p_39634_ instanceof ServerLevel) {
ExperienceOrb.m_147082_((ServerLevel)p_39634_, Vec3.m_82512_(p_39635_), this.m_39631_(p_39634_));
@@ -71,6 +_,7 @@
}
private int m_39631_(Level p_39632_) {
+ if (xp > -1) return xp;
int l = 0;
l += this.m_39636_(GrindstoneMenu.this.f_39560_.m_8020_(0));
l += this.m_39636_(GrindstoneMenu.this.f_39560_.m_8020_(1));
@@ -123,6 +_,8 @@
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_, -1);
+ if (this.xp == Integer.MIN_VALUE)
if (!flag) {
this.f_39559_.m_6836_(0, ItemStack.f_41583_);
} else {
@@ -144,12 +_,13 @@
}
@@ -50,17 +50,20 @@ import it.unimi.dsi.fastutil.longs.LongSet;
import net.minecraft.ResourceLocationException;
import net.minecraft.advancements.Advancement;
import net.minecraft.core.*;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.packs.PackType;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.TagKey;
import net.minecraft.util.datafix.fixes.StructuresBecomeConfiguredFix;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.entity.ExperienceOrb;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.DefaultAttributes;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.monster.EnderMan;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.item.*;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeType;
@@ -128,6 +131,7 @@ import net.minecraftforge.common.world.MobSpawnSettingsBuilder;
import net.minecraftforge.event.AnvilUpdateEvent;
import net.minecraftforge.event.DifficultyChangeEvent;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.GrindstoneEvent;
import net.minecraftforge.event.ItemAttributeModifierEvent;
import net.minecraftforge.event.ModMismatchEvent;
import net.minecraftforge.event.ServerChatEvent;
@@ -710,6 +714,41 @@ public class ForgeHooks
return e.getBreakChance();
}
public static int onGrindstoneChange(@Nonnull ItemStack top, @Nonnull ItemStack bottom, Container outputSlot, int xp)
{
GrindstoneEvent.OnPlaceItem e = new GrindstoneEvent.OnPlaceItem(top, bottom, xp);
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());
return e.getXp();
}
public static boolean onGrindstoneTake(Container inputSlots, ContainerLevelAccess access, Function<Level, Integer> xpFunction)
{
access.execute((l,p) -> {
int xp = xpFunction.apply(l);
GrindstoneEvent.OnTakeItem e = new GrindstoneEvent.OnTakeItem(inputSlots.getItem(0), inputSlots.getItem(1), xp);
if (MinecraftForge.EVENT_BUS.post(e))
{
return;
}
if (l instanceof ServerLevel serverLevel)
{
ExperienceOrb.award(serverLevel, Vec3.atCenterOf(p), e.getXp());
}
l.levelEvent(1042, p, 0);
inputSlots.setItem(0, e.getNewTopItem());
inputSlots.setItem(1, e.getNewBottomItem());
inputSlots.setChanged();
});
return true;
}
private static ThreadLocal<Player> craftingPlayer = new ThreadLocal<Player>();
public static void setCraftingPlayer(Player player)
{
@@ -0,0 +1,160 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.event;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.GrindstoneMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
/**
* Shared logic for the two events fired from grindstones.
* @see OnPlaceItem
* @see OnTakeItem
*/
public abstract class GrindstoneEvent extends Event
{
private final ItemStack top;
private final ItemStack bottom;
private int xp;
protected GrindstoneEvent(ItemStack top, ItemStack bottom, int xp)
{
this.top = top;
this.bottom = bottom;
this.xp = xp;
}
/** {@return the item in the top input grindstone slot} */
public ItemStack getTopItem()
{
return top;
}
/** {@return the item in the bottom input grindstone slot} */
public ItemStack getBottomItem()
{
return bottom;
}
/** {@return the experience amount given to the player} It will be {@code -1} unless {@link #setXp(int)} is called. */
public int getXp()
{
return xp;
}
/**
* Sets the experience amount. <br>
* @param xp The experience amount given to the player. <br>
*/
public void setXp(int xp)
{
this.xp = xp;
}
/**
* This event is {@link Cancelable} <br>
* {@link OnPlaceItem} is fired when the inputs to a grindstone are changed. <br>
*
* The following rules apply:
* <ul>
* <li>If the event is canceled, vanilla behavior will not run, and the output will be {@linkplain ItemStack#EMPTY empty}.</li>
* <li>If the event is not canceled</li>
* <ul>
* <li>and the output is empty, the output will be determined by vanilla.</li>
* <li>and the output is not empty, the output will be set, without running vanilla behavior.</li>
* </ul>
* <li>Vanilla XP calculation logic will be used unless all of the following criterias are met:</li>
* <ul>
* <li>the amount of experience is greater than or equal to {@code 0};</li>
* <li>the event is not {@linkplain #isCanceled() canceled};</li>
* <li>the {@linkplain #getOutput() output} is not empty.</li>
* </ul>
* </ul>
*/
@Cancelable
public static class OnPlaceItem extends GrindstoneEvent
{
private ItemStack output;
public OnPlaceItem(ItemStack top, ItemStack bottom, int xp)
{
super(top, bottom, xp);
this.output = ItemStack.EMPTY;
}
/**
* This is the output as determined by the event, not by the vanilla behavior between these two items. <br>
* If you are the first receiver of this event, it is guaranteed to be empty. <br>
* It will only be non-empty if changed by an event handler. <br>
* If this event is cancelled, this output stack is discarded. <br>
* @return The item to set in the output grindstone slot. <br>
*/
public ItemStack getOutput()
{
return output;
}
/**
* Sets the output slot to a specific itemstack.
* @param output The stack to change the output to.
*/
public void setOutput(ItemStack output)
{
this.output = output;
}
}
/**
* This event is {@link Cancelable}.<br>
* {@link OnTakeItem} is fired when the output of a grindstone is taken. <br>
* The event is called from {@link GrindstoneMenu#GrindstoneMenu(int, Inventory)}. <br>
* If the event is canceled, vanilla behavior will not run, and no inputs will be consumed. <br>
* If the amount of experience is larger than or equal to 0, the vanilla behavior for calculating experience will not be used. <br>
*/
@Cancelable
public static class OnTakeItem extends GrindstoneEvent
{
private ItemStack newTop = ItemStack.EMPTY;
private ItemStack newBottom = ItemStack.EMPTY;
public OnTakeItem(ItemStack top, ItemStack bottom, int xp)
{
super(top, bottom, xp);
}
/** {@return the item in that will be in the top input grindstone slot after the event.} */
public ItemStack getNewTopItem()
{
return newTop;
}
/** {@return the item in that will be in the bottom input grindstone slot after the event} */
public ItemStack getNewBottomItem()
{
return newBottom;
}
/** Sets the itemstack in the top slot. */
public void setNewTopItem(ItemStack newTop)
{
this.newTop = newTop;
}
/** Sets the itemstack in the bottom slot. */
public void setNewBottomItem(ItemStack newBottom)
{
this.newBottom = newBottom;
}
/** {@return the experience amount given to the player, will be the value from vanilla calculations unless modified by the event} */
public int getXp()
{
return super.getXp();
}
}
}
@@ -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