/*
 * 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 org.spongepowered.common.item.inventory.util.ContainerUtil;

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 {
        net.minecraft.inventory.Container nativeContainer = ContainerUtil.toNative(event.getTargetInventory());
        for (net.minecraft.inventory.Slot slot : nativeContainer.inventorySlots) {
            if (slot.inventory == this.inventory) {
                // This container does contain our inventory
                for (Consumer<InteractInventoryEvent> consumer: this.consumers) {
                    consumer.accept(event);
                }
                break;
            }
        }
    }

    public Inventory getInventory() {
        return inventory;
    }
}

@@ -1342,24 +1342,61 @@
 utableList;%0A
+import org.spongepowered.api.Sponge;%0A
 import org.s
@@ -1630,16 +1630,52 @@
 rUtil;%0A%0A
+import java.lang.ref.WeakReference;%0A
 import j
@@ -1827,16 +1827,30 @@
 private 
+WeakReference%3C
 Inventor
@@ -1842,32 +1842,33 @@
 erence%3CInventory
+%3E
  inventory;%0A    
@@ -2110,16 +2110,36 @@
 ntory = 
+new WeakReference%3C%3E(
 inventor
@@ -2135,24 +2135,25 @@
 %3C%3E(inventory
+)
 ;%0A        th
@@ -2491,24 +2491,121 @@
 orySlots) %7B%0A
+            Inventory inventory = this.inventory.get();%0A            if (inventory != null) %7B%0A    
             
@@ -2626,21 +2626,16 @@
 tory == 
-this.
 inventor
@@ -2635,24 +2635,28 @@
 nventory) %7B%0A
+    
             
@@ -2716,24 +2716,28 @@
             
+    
 for (Consume
@@ -2810,16 +2810,20 @@
         
+    
 consumer
@@ -2846,34 +2846,42 @@
                 
+ 
-%7D%0A
+   %7D%0A    
                 
@@ -2887,16 +2887,200 @@
  break;%0A
+                %7D%0A            %7D else %7B%0A                // Remove the listeners if the inventory is no longer in use%0A                Sponge.getEventManager().unregisterListeners(this);%0A
         
@@ -3077,32 +3077,33 @@
 ;%0A            %7D%0A
+%0A
         %7D%0A    %7D%0A
@@ -3156,16 +3156,21 @@
  return 
+this.
 inventor
@@ -3166,22 +3166,28 @@
 is.inventory
+.get()
 ;%0A    %7D%0A%7D%0A

remove custom inventory listeners when inventory is no longer used