Skip to content

Commit

Permalink
Fix hopper not triggering InventoryMoveItemEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Mgazul committed Aug 3, 2023
1 parent 5f980e4 commit f52aeee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
return this.field_194138_c;
}

@@ -47,17 +_,51 @@
@@ -47,17 +_,49 @@

public boolean func_77569_a(CraftingInventory p_77569_1_, World p_77569_2_) {
RecipeItemHelper recipeitemhelper = new RecipeItemHelper();
Expand All @@ -83,7 +83,6 @@
-
- return i == this.field_77579_b.size() && recipeitemhelper.func_194116_a(this, (IntList)null);
+ if (isSimple) {
+ // Paper start
+ itemstack = itemstack.func_77946_l();
+ providedItems.add(itemstack);
+ for (Ingredient ingredient : field_77579_b) {
Expand All @@ -92,7 +91,6 @@
+ matchedIngredients.increment(ingredient);
+ }
+ }
+ // Paper end
+ } else {
+ inputs.add(itemstack);
+ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

+ // CraftBukkit start - add fields and methods
+ private int maxStack = MAX_STACK;
+ public List<HumanEntity> transaction = new java.util.ArrayList<HumanEntity>();
+ public List<HumanEntity> transaction = new java.util.ArrayList<>();
+
+ @Override
+ public int func_70297_j_() {
Expand Down Expand Up @@ -107,45 +107,6 @@
IInventory iinventory = this.func_145895_l();
if (iinventory == null) {
return false;
@@ -144,12 +_,37 @@
for(int i = 0; i < this.func_70302_i_(); ++i) {
if (!this.func_70301_a(i).func_190926_b()) {
ItemStack itemstack = this.func_70301_a(i).func_77946_l();
- ItemStack itemstack1 = func_174918_a(this, iinventory, this.func_70298_a(i, 1), direction);
+
+ // CraftBukkit start - Call event when pushing items into other inventories
+ CraftItemStack oitemstack = CraftItemStack.asCraftMirror(this.func_70298_a(i, field_145850_b.spigotConfig.hopperAmount)); // Spigot
+ Inventory destinationInventory;
+ // Have to special case large chests as they work oddly
+ if (iinventory instanceof DoubleSidedInventory) {
+ destinationInventory = new org.bukkit.craftbukkit.v1_16_R3.inventory.CraftInventoryDoubleChest((DoubleSidedInventory) iinventory);
+ } else {
+ InventoryHolder owner = InventoryOwner.get(iinventory);
+ destinationInventory = (owner != null ? owner.getInventory() : new CraftCustomInventory(iinventory).getInventory());
+ }
+
+ InventoryHolder owner = InventoryOwner.get((TileEntity) this);
+ Inventory hopperOwner = (owner != null ? owner.getInventory() : new CraftCustomInventory(this).getInventory());
+ InventoryMoveItemEvent event = new InventoryMoveItemEvent(hopperOwner, oitemstack.clone(), destinationInventory, true);
+ this.func_145831_w().getCBServer().getPluginManager().callEvent(event);
+ if (event.isCancelled()) {
+ this.func_70299_a(i, itemstack);
+ this.func_145896_c(field_145850_b.spigotConfig.hopperTransfer); // Spigot
+ return false;
+ }
+ int origCount = event.getItem().getAmount(); // Spigot
+ ItemStack itemstack1 = func_174918_a(this, iinventory, CraftItemStack.asNMSCopy(event.getItem()), direction);
+ // CraftBukkit end
+
if (itemstack1.func_190926_b()) {
iinventory.func_70296_d();
return true;
}

+ itemstack.func_190918_g(origCount - itemstack1.func_190916_E()); // Spigot
this.func_70299_a(i, itemstack);
}
}
@@ -177,6 +_,8 @@
}

Expand Down Expand Up @@ -223,6 +184,15 @@
}
}

@@ -296,7 +_,7 @@
}

@Nullable
- private IInventory func_145895_l() {
+ public IInventory func_145895_l() {
Direction direction = this.func_195044_w().func_177229_b(HopperBlock.field_176430_a);
return func_195484_a(this.func_145831_w(), this.field_174879_c.func_177972_a(direction));
}
@@ -322,10 +_,11 @@
IInventory iinventory = null;
BlockPos blockpos = new BlockPos(p_145893_1_, p_145893_3_, p_145893_5_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@

package net.minecraftforge.items;

import com.mohistmc.inventory.CraftCustomInventory;
import com.mohistmc.inventory.InventoryOwner;
import net.minecraft.block.Block;
import net.minecraft.block.DropperBlock;
import net.minecraft.block.HopperBlock;
import net.minecraft.inventory.DoubleSidedInventory;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.IHopper;
import net.minecraft.tileentity.TileEntity;
Expand All @@ -21,6 +25,11 @@

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -120,14 +129,37 @@ public static boolean insertHook(HopperTileEntity hopper)
if (!hopper.getItem(i).isEmpty())
{
ItemStack originalSlotContents = hopper.getItem(i).copy();
ItemStack insertStack = hopper.removeItem(i, 1);
ItemStack remainder = putStackInInventoryAllSlots(hopper, destination, itemHandler, insertStack);

// CraftBukkit start - Call event when pushing items into other inventories
CraftItemStack oitemstack = CraftItemStack.asCraftMirror(hopper.removeItem(i, hopper.level.spigotConfig.hopperAmount)); // Spigot
IInventory iinventory = hopper.getAttachedContainer();
Inventory destinationInventory;
// Have to special case large chests as they work oddly
if (iinventory instanceof DoubleSidedInventory) {
destinationInventory = new org.bukkit.craftbukkit.v1_16_R3.inventory.CraftInventoryDoubleChest((DoubleSidedInventory) iinventory);
} else {
InventoryHolder owner = InventoryOwner.get(iinventory);
destinationInventory = (owner != null ? owner.getInventory() : new CraftCustomInventory(iinventory).getInventory());
}

InventoryHolder owner = InventoryOwner.get((TileEntity) hopper);
Inventory hopperOwner = (owner != null ? owner.getInventory() : new CraftCustomInventory(hopper).getInventory());
InventoryMoveItemEvent event = new InventoryMoveItemEvent(hopperOwner, oitemstack.clone(), destinationInventory, true);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
hopper.setItem(i, originalSlotContents);
hopper.setCooldown(hopper.level.spigotConfig.hopperTransfer); // Spigot
return false;
}
int origCount = event.getItem().getAmount(); // Spigot

ItemStack remainder = putStackInInventoryAllSlots(hopper, destination, itemHandler, CraftItemStack.asNMSCopy(event.getItem()));

if (remainder.isEmpty())
{
return true;
}

originalSlotContents.shrink(origCount - remainder.getCount()); // Spigot
hopper.setItem(i, originalSlotContents);
}
}
Expand Down

0 comments on commit f52aeee

Please sign in to comment.