Commit 888ca8ab authored by Vladislav's avatar Vladislav
Browse files

[FIX] When player put item in Item Hopper that conected to mod container (ic2...

[FIX] When player put item in Item Hopper that conected to mod container (ic2 mechanisms, etc) and it's inventory already full server will crash.
parent 7a90edec
Loading
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
package net.minecraft.tileentity;

import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;

import net.minecraft.block.Block;
@@ -201,7 +202,10 @@ public class TileEntityHopper extends TileEntityLockableLoot implements IHopper,
                        if (iinventory instanceof InventoryLargeChest) {
                            destinationInventory = new org.bukkit.craftbukkit.inventory.CraftInventoryDoubleChest((InventoryLargeChest) iinventory);
                        } else {
                            destinationInventory = InventoryUtils.getInventoryOwner(iinventory).getInventory();
                            if (InventoryUtils.getInventoryOwner(iinventory) != null)
                                destinationInventory = Objects.requireNonNull(InventoryUtils.getInventoryOwner(iinventory)).getInventory();
                            else
                                destinationInventory = null;
                        }

                        InventoryMoveItemEvent event = new InventoryMoveItemEvent(InventoryUtils.getInventoryOwner(this).getInventory(), oitemstack.clone(), destinationInventory, true);
+4 −0
Original line number Diff line number Diff line
@@ -191,4 +191,8 @@ public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, I
    protected void onContentsChanged(int slot) {

    }

    public NonNullList<ItemStack> getStacksList() {
        return this.stacks;
    }
}
+5 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

package net.minecraftforge.items.wrapper;

import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
@@ -202,4 +203,8 @@ public class SidedInvWrapper implements IItemHandlerModifiable {
    public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
        return inv.isItemValidForSlot(slot, stack);
    }//https://github.com/MinecraftForge/MinecraftForge/commit/c7c2921b2fa05b6d0fa9e6c2934c08fc84dcb71f

    public IInventory getInv() {
        return this.inv;
    }
}
+61 −0
Original line number Diff line number Diff line
package org.atom.inventory;

import net.minecraft.inventory.IInventory;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.SlotItemHandler;
import net.minecraftforge.items.wrapper.InvWrapper;
import net.minecraftforge.items.wrapper.SidedInvWrapper;
import org.bukkit.craftbukkit.inventory.CraftInventory;
import org.bukkit.craftbukkit.inventory.CraftInventoryCustom;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

import javax.annotation.Nullable;

public class CustomInventory implements InventoryHolder {

    private final IInventory inventory;
    private final CraftInventory container;

    public CustomInventory(final IInventory inventory) {
        this.container = new CraftInventory(inventory);
        this.inventory = inventory;
    }

    public CustomInventory(final ItemStackHandler handler) {
        this.container = new CraftInventoryCustom(this, handler.getStacksList());
        this.inventory = this.container.getInventory();
    }

    @Override
    public Inventory getInventory() {
        return this.container;
    }

    @Nullable
    public static InventoryHolder holderFromForge(final IItemHandler handler) {
        if (handler == null) {
            return null;
        }
        if (handler instanceof ItemStackHandler) {
            return new CustomInventory((ItemStackHandler) handler);
        }
        if (handler instanceof SlotItemHandler) {
            return new CustomInventory(((SlotItemHandler) handler).inventory);
        }
        if (handler instanceof InvWrapper) {
            return new CustomInventory(((InvWrapper) handler).getInv());
        }
        if (handler instanceof SidedInvWrapper) {
            return new CustomInventory(((SidedInvWrapper) handler).getInv());
        }
        return null;
    }

    @Nullable
    public static Inventory inventoryFromForge(final IItemHandler handler) {
        final InventoryHolder holder = holderFromForge(handler);
        return (holder != null) ? holder.getInventory() : null;
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.InventoryEnderChest;
import net.minecraft.inventory.InventoryMerchant;
import net.minecraft.tileentity.TileEntity;
import org.atom.inventory.CustomInventory;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.InventoryHolder;
@@ -41,6 +42,8 @@ public class InventoryUtils {
            return inventory.getOwner();
        } else if (inventory instanceof InventoryMerchant) {
            return ((InventoryMerchant) inventory).getPlayer().getBukkitEntity();
        } else {
            return new CustomInventory(inventory).getInventory().getHolder();
        }
        return null;
    }
Loading