Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Sync 1.20.1 InvWrapperMoveItemEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Mgazul committed Oct 17, 2023
1 parent 0bf60ef commit 4496d36
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 13 deletions.
123 changes: 123 additions & 0 deletions src/main/java/com/mohistmc/api/event/InvWrapperMoveItemEvent.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/mohistmc/inventory/InventoryOwner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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())
{
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/net/minecraftforge/items/wrapper/InvWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4496d36

Please sign in to comment.