Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.21.3] Add remove count context to ItemSmeltedEvent and fix firing multiple times with shift-click #1440

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
--- a/net/minecraft/world/inventory/FurnaceResultSlot.java
+++ b/net/minecraft/world/inventory/FurnaceResultSlot.java
@@ -49,5 +_,6 @@
@@ -48,6 +_,9 @@
abstractfurnaceblockentity.awardUsedRecipesAndPopExperience(serverplayer);
}

+ if (this.removeCount != 0) {
+ net.neoforged.neoforge.event.EventHooks.firePlayerSmeltedEvent(this.player, p_39558_, this.removeCount);
+ }
this.removeCount = 0;
+ net.neoforged.neoforge.event.EventHooks.firePlayerSmeltedEvent(this.player, p_39558_);
}
}
4 changes: 2 additions & 2 deletions src/main/java/net/neoforged/neoforge/event/EventHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,8 @@ public static void firePlayerCraftingEvent(Player player, ItemStack crafted, Con
NeoForge.EVENT_BUS.post(new PlayerEvent.ItemCraftedEvent(player, crafted, craftMatrix));
}

public static void firePlayerSmeltedEvent(Player player, ItemStack smelted) {
NeoForge.EVENT_BUS.post(new PlayerEvent.ItemSmeltedEvent(player, smelted));
public static void firePlayerSmeltedEvent(Player player, ItemStack smelted, int amountRemoved) {
NeoForge.EVENT_BUS.post(new PlayerEvent.ItemSmeltedEvent(player, smelted, amountRemoved));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,15 +408,21 @@ public Container getInventory() {

public static class ItemSmeltedEvent extends PlayerEvent {
private final ItemStack smelting;
private final int amountRemoved;

public ItemSmeltedEvent(Player player, ItemStack crafting) {
public ItemSmeltedEvent(Player player, ItemStack crafting, int amountRemoved) {
super(player);
this.smelting = crafting;
this.amountRemoved = amountRemoved;
}

public ItemStack getSmelting() {
return this.smelting;
}

public int getAmountRemoved() {
return this.amountRemoved;
}
}

public static class PlayerLoggedInEvent extends PlayerEvent {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.debug.crafting;

import com.mojang.blaze3d.platform.InputConstants;
import java.util.concurrent.atomic.AtomicInteger;
import net.minecraft.core.BlockPos;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.world.inventory.ClickType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.FurnaceBlockEntity;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.testframework.DynamicTest;
import net.neoforged.testframework.annotation.ForEachTest;
import net.neoforged.testframework.annotation.TestHolder;
import net.neoforged.testframework.gametest.EmptyTemplate;

@ForEachTest(groups = "crafting.event")
public class CraftingEventTests {
@GameTest
@EmptyTemplate
@TestHolder(description = "Tests that ItemSmeltedEvent is fired correctly")
static void itemSmeltedEventTest(final DynamicTest test) {
AtomicInteger timesFired = new AtomicInteger(0);
test.whenEnabled(listeners -> {
listeners.forge().addListener((final PlayerEvent.ItemSmeltedEvent event) -> {
timesFired.incrementAndGet();
var removed = event.getAmountRemoved();
if (removed != 32) {
test.fail("Test should be removing half of a stack, yet extracted a different amount");
}
});
});
test.onGameTest(helper -> {
helper.setBlock(BlockPos.ZERO, Blocks.FURNACE);
var be = helper.getBlockEntity(BlockPos.ZERO, FurnaceBlockEntity.class);
helper.assertFalse(be == null, "FurnaceBlockEntity was not found for furnace position");
// Slot 2 is the result slot
be.setItem(2, new ItemStack(Items.IRON_INGOT, 64));
var player = helper.makeTickingMockServerPlayerInLevel(GameType.CREATIVE);
player.openMenu(be);
// Test that right-clicking half of the stack out of the FurnaceResultSlot functions as expected
player.containerMenu.clicked(2, InputConstants.MOUSE_BUTTON_RIGHT, ClickType.PICKUP, player);
helper.assertTrue(timesFired.getPlain() == 1, "Event was not fired the expected number of times for right-click pickup. Fired: " + timesFired.getPlain());
player.containerMenu.setCarried(ItemStack.EMPTY);
// Test that shift-left-clicking the rest of the stack out works (should only fire once, not twice)
player.containerMenu.clicked(2, InputConstants.MOUSE_BUTTON_LEFT, ClickType.QUICK_MOVE, player);
helper.assertTrue(timesFired.getPlain() == 2, "Event was not fired the expected number of times for shift-left-click quick-move. Fired: " + timesFired.getPlain());
// The slot is now empty, this should not fire the event
player.containerMenu.clicked(2, InputConstants.MOUSE_BUTTON_LEFT, ClickType.QUICK_MOVE, player);
helper.assertTrue(timesFired.getPlain() == 2, "Event fired for an empty slot, which should not happen.");
helper.succeed();
});
}
}