/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.item.inventory.custom;

import com.google.common.collect.ImmutableList;
import org.spongepowered.api.event.EventListener;
import org.spongepowered.api.event.item.inventory.InteractInventoryEvent;
import org.spongepowered.api.item.inventory.Inventory;

import java.util.List;
import java.util.function.Consumer;

public class CustomInventoryListener implements EventListener<InteractInventoryEvent> {

    private Inventory inventory;
    List<Consumer<InteractInventoryEvent>> consumers;

    @SuppressWarnings({"unchecked", "rawtypes"})
    public CustomInventoryListener(Inventory inventory, List<Consumer<? extends InteractInventoryEvent>> consumers) {
        this.inventory = inventory;
        this.consumers = (List) ImmutableList.copyOf(consumers);
    }

    @Override
    public void handle(InteractInventoryEvent event) throws Exception {
        if (!(event.getTargetInventory() == this.inventory || (event.getTargetInventory() instanceof CustomContainer && (((CustomContainer) event.getTargetInventory()).inv == this.inventory)))) {
            return;
        }
        for (Consumer<InteractInventoryEvent> consumer: this.consumers) {
            consumer.accept(event);
        }
    }
}

@@ -1524,16 +1524,83 @@
 ventory;
+%0Aimport org.spongepowered.common.item.inventory.util.ContainerUtil;
 %0A%0Aimport
@@ -2203,129 +2203,167 @@
     
-if (!(event.getTargetInventory() == this.inventory %7C%7C (event.getTargetInventory() instanceof CustomContainer && (((Custom
+net.minecraft.inventory.Container nativeContainer = ContainerUtil.toNative(event.getTargetInventory());%0A        for (net.minecraft.inventory.Slot slot : native
 Cont
@@ -2371,34 +2371,57 @@
 iner
-) e
+.in
 vent
-.getTargetI
+orySlots) %7B%0A            if (slot.i
 nventory
 ()).
@@ -2420,15 +2420,8 @@
 tory
-()).inv
  == 
@@ -2435,19 +2435,16 @@
 ventory)
-)))
  %7B%0A     
@@ -2454,15 +2454,56 @@
     
+ 
-return;
+   // This container does contain our inventory
 %0A   
@@ -2503,26 +2503,24 @@
 ory%0A        
-%7D%0A
         for 
@@ -2593,16 +2593,24 @@
         
+        
 consumer
@@ -2625,16 +2625,71 @@
 event);%0A
+                %7D%0A                break;%0A            %7D%0A
         

Fix a bug where a CustomInventory cannot handle inventory events