/*
 * Copyright (c) 2013 - 2015 xxyy (Philipp Nowak; devnull@nowak-at.net). All rights reserved.
 *
 * Any usage, including, but not limited to, compiling, running, redistributing, printing,
 *  copying and reverse-engineering is strictly prohibited without explicit written permission
 *  from the original author and may result in legal steps being taken.
 *
 * See the included LICENSE file (core/src/main/resources) or email xxyy98+xyclicense@gmail.com for details.
 */

package li.l1t.common.inventory.gui.holder;

import li.l1t.common.inventory.gui.element.MenuElement;

/**
 * An element holder that works as a template for creating inventory menus.
 *
 * @author <a href="http://xxyy.github.io/">xxyy</a>
 * @since 2016-06-24
 */
public class TemplateElementHolder extends SimpleElementHolder {
    /**
     * Copies all elements from this template into a holder, setting elements directly instead of
     * through the API, bypassing unnecessary bound-checks. If the handler has special handling when
     * an element gets added, this might break that handling.
     *
     * @param holder the holder to copy into
     * @param <T>    the type of the holder
     * @return {@code holder}, for easy chaining
     * @see #apply(ElementHolder) The API-only counterpart of this method
     */
    public <T extends SimpleElementHolder> T applyRaw(T holder) {
        MenuElement[] elements = getElements();
        for (int slotId = 0; slotId < elements.length; slotId++) {
            MenuElement element = elements[slotId];
            holder.addElement(slotId, element);
        }
        return holder;
    }

    /**
     * Copies all elements from this template into a holder, using the public API.
     *
     * @param holder the holder to copy into
     * @param <T>    the type of the holder
     * @return {@code holder}, for easy chaining
     * @see #applyRaw(SimpleElementHolder) The raw counterpart of this method
     */
    public <T extends ElementHolder> T apply(T holder) {
        MenuElement[] elements = getElements();
        for (int slotId = 0; slotId < elements.length; slotId++) {
            MenuElement element = elements[slotId];
            holder.addElement(slotId, element);
        }
        return holder;
    }
}

@@ -570,16 +570,42 @@
 ement;%0A%0A
+import java.util.BitSet;%0A%0A
 /**%0A * A
@@ -822,16 +822,89 @@
 older %7B%0A
+    private final BitSet placeholderSlots = new BitSet(INVENTORY_SIZE);%0A%0A
     /**%0A
@@ -1564,32 +1564,152 @@
 th; slotId++) %7B%0A
+            if (placeholderSlots.get(slotId)) %7B%0A                holder.addPlaceholder(slotId);%0A            %7D else %7B%0A    
             Menu
@@ -1756,33 +1756,40 @@
         
+    
 holder.
-add
+set
 Element
+Raw
 (slotId,
@@ -1791,32 +1791,46 @@
 otId, element);%0A
+            %7D%0A
         %7D%0A      
@@ -2339,32 +2339,152 @@
 th; slotId++) %7B%0A
+            if (placeholderSlots.get(slotId)) %7B%0A                holder.addPlaceholder(slotId);%0A            %7D else %7B%0A    
             Menu
@@ -2523,32 +2523,36 @@
 d%5D;%0A            
+ 
+   
 holder.addElemen
@@ -2563,32 +2563,46 @@
 otId, element);%0A
+            %7D%0A
         %7D%0A      
@@ -2623,11 +2623,115 @@
 r;%0A    %7D
+%0A%0A    @Override%0A    public void addPlaceholder(int slotId) %7B%0A        placeholderSlots.set(slotId);%0A    %7D
 %0A%7D%0A

make TemplateElementHolder use target's placeholders