[1.19.x] Add event for items being stacked or swapped in a GUI. (#9050)

This commit is contained in:
eerussianguy authored and GitHub committed 2022-10-14 17:30:12 +01:00
1 parent bdba5ef2d3
commit 051db7907a
6 files changed
+172 -1

No files matched your search

@@ -60,7 +60,10 @@ 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.monster.EnderMan;
import net.minecraft.world.entity.SlotAccess;
import net.minecraft.world.inventory.ClickAction;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.*;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeType;
@@ -115,6 +118,7 @@ import net.minecraftforge.event.DifficultyChangeEvent;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.GrindstoneEvent;
import net.minecraftforge.event.ItemAttributeModifierEvent;
import net.minecraftforge.event.ItemStackedOnOtherEvent;
import net.minecraftforge.event.ModMismatchEvent;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.event.RegisterStructureConversionsEvent;
@@ -266,6 +270,11 @@ public class ForgeHooks
return false;
}
public static boolean onItemStackedOn(ItemStack carriedItem, ItemStack stackedOnItem, Slot slot, ClickAction action, Player player, SlotAccess carriedSlotAccess)
{
return MinecraftForge.EVENT_BUS.post(new ItemStackedOnOtherEvent(carriedItem, stackedOnItem, slot, action, player, carriedSlotAccess));
}
public static void onDifficultyChange(Difficulty difficulty, Difficulty oldDifficulty)
{
MinecraftForge.EVENT_BUS.post(new DifficultyChangeEvent(difficulty, oldDifficulty));
@@ -0,0 +1,104 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.event;
import net.minecraft.world.entity.SlotAccess;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ClickAction;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.fml.LogicalSide;
import org.jetbrains.annotations.ApiStatus;
/**
* This event provides the functionality of the pair of functions used for the Bundle, in one event:
* <ul>
* <li>{@link Item#overrideOtherStackedOnMe(ItemStack, ItemStack, Slot, ClickAction, Player, SlotAccess)}</li>
* <li>{@link Item#overrideStackedOnOther(ItemStack, Slot, ClickAction, Player)}</li>
* </ul>
*
* This event is fired before either of the above are called, when a carried item is clicked on top of another in a GUI slot.
* This event (and items stacking on others in general) is fired on both {@linkplain LogicalSide sides}, but only on {@linkplain LogicalSide#CLIENT the client} in the creative menu.
* Practically, that means that listeners of this event should require the player to be in survival mode if using capabilities that are not synced.
* <p>
* This event is {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.
* If the event is cancelled, the container's logic halts, the carried item and the slot will not be swapped, and handling is assumed to have been done by the mod.
* This also means that the two vanilla checks described above will not be called.
*/
@Cancelable
public class ItemStackedOnOtherEvent extends Event
{
private final ItemStack carriedItem;
private final ItemStack stackedOnItem;
private final Slot slot;
private final ClickAction action;
private final Player player;
private final SlotAccess carriedSlotAccess;
@ApiStatus.Internal
public ItemStackedOnOtherEvent(ItemStack carriedItem, ItemStack stackedOnItem, Slot slot, ClickAction action, Player player, SlotAccess carriedSlotAccess)
{
this.carriedItem = carriedItem;
this.stackedOnItem = stackedOnItem;
this.slot = slot;
this.action = action;
this.player = player;
this.carriedSlotAccess = carriedSlotAccess;
}
/**
* {@return the stack being carried by the mouse} This may be empty!
*/
public ItemStack getCarriedItem()
{
return carriedItem;
}
/**
* {@return the stack currently in the slot being clicked on} This may be empty!
*/
public ItemStack getStackedOnItem()
{
return stackedOnItem;
}
/**
* {@return the slot being clicked on}
*/
public Slot getSlot()
{
return slot;
}
/**
* {@return the click action being used} By default {@linkplain ClickAction#PRIMARY} corresponds to left-click, and {@linkplain ClickAction#SECONDARY} is right-click.
*/
public ClickAction getClickAction()
{
return action;
}
/**
* {@return the player doing the item swap attempt}
*/
public Player getPlayer()
{
return player;
}
/**
* {@return a fake slot allowing the listener to see and change what item is being carried}
*/
public SlotAccess getCarriedSlotAccess()
{
return carriedSlotAccess;
}
}