From 4496d3662fa689837fd8df4e2313ffb194fec925 Mon Sep 17 00:00:00 2001 From: Mgazul Date: Wed, 18 Oct 2023 07:07:28 +0800 Subject: [PATCH] Sync 1.20.1 InvWrapperMoveItemEvent --- .../api/event/InvWrapperMoveItemEvent.java | 123 ++++++++++++++++++ .../mohistmc/inventory/InventoryOwner.java | 5 + .../items/VanillaInventoryCodeHooks.java | 32 +++-- .../items/wrapper/InvWrapper.java | 16 +++ 4 files changed, 163 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/mohistmc/api/event/InvWrapperMoveItemEvent.java diff --git a/src/main/java/com/mohistmc/api/event/InvWrapperMoveItemEvent.java b/src/main/java/com/mohistmc/api/event/InvWrapperMoveItemEvent.java new file mode 100644 index 0000000000..6ab3c5af3c --- /dev/null +++ b/src/main/java/com/mohistmc/api/event/InvWrapperMoveItemEvent.java @@ -0,0 +1,123 @@ +package com.mohistmc.api.event; + +import com.google.common.base.Preconditions; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +/** + * @author Mgazul by MohistMC + * @date 2023/10/10 3:29:10 + */ +public class InvWrapperMoveItemEvent extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + + private final Inventory inventory; + private final ItemStack itemStack; + + public InvWrapperMoveItemEvent(@NotNull final Inventory inventory, @NotNull final ItemStack itemStack) { + Preconditions.checkArgument(itemStack != null, "ItemStack cannot be null"); + this.inventory = inventory; + this.itemStack = itemStack; + } + + @NotNull + public Inventory getInventory() { + return inventory; + } + + @NotNull + public ItemStack getItem() { + return itemStack.clone(); + } + + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } + + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + + public static class Extract extends InvWrapperMoveItemEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + + public Extract(@NotNull Inventory inventory, @NotNull ItemStack itemStack) { + super(inventory, itemStack); + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } + + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + } + + public static class Insert extends InvWrapperMoveItemEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + + public Insert(@NotNull Inventory inventory, @NotNull ItemStack itemStack) { + super(inventory, itemStack); + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } + + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + } +} diff --git a/src/main/java/com/mohistmc/inventory/InventoryOwner.java b/src/main/java/com/mohistmc/inventory/InventoryOwner.java index d71683f2a0..3b3de52861 100644 --- a/src/main/java/com/mohistmc/inventory/InventoryOwner.java +++ b/src/main/java/com/mohistmc/inventory/InventoryOwner.java @@ -44,6 +44,11 @@ */ public class InventoryOwner { + public static Inventory getInventory(IInventory inventory) { + InventoryHolder owner = get(inventory); + return (owner == null ? new CraftCustomInventory(inventory).getInventory() : owner.getInventory()); + } + public static InventoryHolder get(TileEntity te) { return get(te.level, te.getBlockPos(), true); } diff --git a/src/main/java/net/minecraftforge/items/VanillaInventoryCodeHooks.java b/src/main/java/net/minecraftforge/items/VanillaInventoryCodeHooks.java index 8a98343fe8..4af3ce2e43 100644 --- a/src/main/java/net/minecraftforge/items/VanillaInventoryCodeHooks.java +++ b/src/main/java/net/minecraftforge/items/VanillaInventoryCodeHooks.java @@ -93,7 +93,7 @@ public static boolean dropperInsertHook(World world, BlockPos pos, DispenserTile Object destination = destinationResult.getValue(); // CraftBukkit start - Fire event when pushing items into other inventories CraftItemStack oitemstack = CraftItemStack.asCraftMirror(stack.copy().split(1)); - org.bukkit.inventory.InventoryHolder owner = InventoryOwner.get((TileEntity) dropper); + org.bukkit.inventory.InventoryHolder owner = InventoryOwner.get((TileEntity) destination); org.bukkit.inventory.Inventory destinationInventory = owner != null ? owner.getInventory() : InventoryOwner.inventoryFromForge(itemHandler); InventoryMoveItemEvent event = new InventoryMoveItemEvent(dropper.getOwner().getInventory(), oitemstack.clone(), destinationInventory, true); Bukkit.getPluginManager().callEvent(event); @@ -142,19 +142,25 @@ public static boolean insertHook(HopperTileEntity hopper) ItemStack originalSlotContents = hopper.getItem(i).copy(); // CraftBukkit start - Call event when pushing items into other inventories - CraftItemStack oitemstack = CraftItemStack.asCraftMirror(hopper.removeItem(i, hopper.level.spigotConfig.hopperAmount)); // Spigot - org.bukkit.inventory.InventoryHolder owner = InventoryOwner.get((TileEntity) hopper); - org.bukkit.inventory.Inventory destinationInventory = owner != null ? owner.getInventory() : InventoryOwner.inventoryFromForge(itemHandler); - InventoryMoveItemEvent event = new InventoryMoveItemEvent(hopper.getOwner().getInventory(), oitemstack.clone(), destinationInventory, true); - Bukkit.getPluginManager().callEvent(event); - if (event.isCancelled()) { - hopper.setItem(i, originalSlotContents); - hopper.setCooldown(hopper.level.spigotConfig.hopperTransfer); // Spigot - return false; + ItemStack insertStack = hopper.removeItem(i, hopper.getLevel().spigotConfig.hopperAmount); + ItemStack stack = insertStack; + if (!insertStack.isEmpty()) { + CraftItemStack oitemstack = CraftItemStack.asCraftMirror(insertStack); + org.bukkit.inventory.InventoryHolder owner = InventoryOwner.get((TileEntity) destination); + org.bukkit.inventory.Inventory destinationInventory = owner != null ? owner.getInventory() : InventoryOwner.inventoryFromForge(itemHandler); + if (destinationInventory != null) { + InventoryMoveItemEvent event = new InventoryMoveItemEvent(InventoryOwner.getInventory(hopper), oitemstack.clone(), destinationInventory, true); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + hopper.setItem(i, originalSlotContents); + hopper.setCooldown(hopper.getLevel().spigotConfig.hopperTransfer); // Spigot + return true; + } + stack = CraftItemStack.asNMSCopy(event.getItem()); + } } - int origCount = event.getItem().getAmount(); // Spigot - - ItemStack remainder = putStackInInventoryAllSlots(hopper, destination, itemHandler, CraftItemStack.asNMSCopy(event.getItem())); + int origCount = insertStack.getCount(); + ItemStack remainder = putStackInInventoryAllSlots(hopper, destination, itemHandler, stack); if (remainder.isEmpty()) { diff --git a/src/main/java/net/minecraftforge/items/wrapper/InvWrapper.java b/src/main/java/net/minecraftforge/items/wrapper/InvWrapper.java index 075028cb64..608a596a89 100644 --- a/src/main/java/net/minecraftforge/items/wrapper/InvWrapper.java +++ b/src/main/java/net/minecraftforge/items/wrapper/InvWrapper.java @@ -5,12 +5,16 @@ package net.minecraftforge.items.wrapper; +import com.mohistmc.api.event.InvWrapperMoveItemEvent; +import com.mohistmc.inventory.InventoryOwner; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraftforge.items.IItemHandlerModifiable; import net.minecraftforge.items.ItemHandlerHelper; import javax.annotation.Nonnull; +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack; public class InvWrapper implements IItemHandlerModifiable { @@ -62,6 +66,12 @@ public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate return ItemStack.EMPTY; ItemStack stackInSlot = getInv().getItem(slot); + CraftItemStack oitemstack = CraftItemStack.asCraftMirror(stackInSlot); + InvWrapperMoveItemEvent.Insert event = new InvWrapperMoveItemEvent.Insert(InventoryOwner.getInventory(getInv()), oitemstack); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return stack; + } int m; if (!stackInSlot.isEmpty()) @@ -155,6 +165,12 @@ public ItemStack extractItem(int slot, int amount, boolean simulate) if (stackInSlot.isEmpty()) return ItemStack.EMPTY; + CraftItemStack oitemstack = CraftItemStack.asCraftMirror(stackInSlot); + InvWrapperMoveItemEvent.Extract event = new InvWrapperMoveItemEvent.Extract(InventoryOwner.getInventory(getInv()), oitemstack); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + return ItemStack.EMPTY; + } if (simulate) { if (stackInSlot.getCount() < amount)