package de.craften.plugins.rpgplus.scripting.api;

import de.craften.plugins.rpgplus.components.inventory.ItemMatcher;
import de.craften.plugins.rpgplus.scripting.ScriptingModule;
import de.craften.plugins.rpgplus.scripting.util.ScriptUtil;
import de.craften.plugins.rpgplus.scripting.util.luaify.LuaFunction;
import de.craften.plugins.rpgplus.scripting.util.luaify.Luaify;
import org.luaj.vm2.*;

/**
 * Lua API for the player's inventory.
 */
public class InventoryModule extends LuaTable implements ScriptingModule {
    public InventoryModule() {
        Luaify.convertInPlace(this);
    }

    @LuaFunction("hasItems")
    public LuaBoolean hasItems(Varargs args) {
        LuaValue player = args.arg(1);
        for (int i = 2; i <= args.narg(); i++) {
            if (!checkItem(player, args.arg(i)).booleanValue()) {
                return LuaValue.FALSE;
            }
        }
        return LuaValue.TRUE;
    }

    @LuaFunction("giveItems")
    public Varargs giveItems(Varargs args) {
        LuaValue player = args.arg(1);

        LuaValue[] notFittingAmounts = new LuaValue[args.narg() - 1];
        for (int i = 2; i <= args.narg(); i++) {
            notFittingAmounts[i - 2] = giveItem(player, args.arg(i));
        }

        return LuaValue.varargsOf(notFittingAmounts);
    }

    @LuaFunction("takeItems")
    public Varargs takeItems(Varargs args) {
        LuaValue player = args.arg(1);

        LuaValue[] missingAmounts = new LuaValue[args.narg() - 1];
        for (int i = 2; i <= args.narg(); i++) {
            missingAmounts[i - 2] = takeItem(player, args.arg(i));
        }

        return LuaValue.varargsOf(missingAmounts);
    }

    private static LuaBoolean checkItem(LuaValue player, LuaValue item) {
        ItemMatcher matcher = ScriptUtil.createItemMatcher(item);
        return LuaValue.valueOf(matcher.matches(ScriptUtil.getPlayer(player).getInventory()));
    }

    private static LuaInteger giveItem(LuaValue player, LuaValue item) {
        ItemMatcher matcher = ScriptUtil.createItemMatcher(item);
        return LuaValue.valueOf(matcher.putInto(ScriptUtil.getPlayer(player).getInventory()));
    }

    private static LuaInteger takeItem(LuaValue player, LuaValue item) {
        ItemMatcher matcher = ScriptUtil.createItemMatcher(item);
        return LuaValue.valueOf(matcher.takeFrom(ScriptUtil.getPlayer(player).getInventory()));
    }

    @Override
    public LuaValue getModule() {
        return this;
    }

    @Override
    public void reset() {
        //nothing to do
    }
}

@@ -44,16 +44,346 @@
 g.api;%0A%0A
+import net.md_5.bungee.api.ChatColor;%0A%0Aimport org.bukkit.Bukkit;%0Aimport org.bukkit.entity.Player;%0Aimport org.bukkit.inventory.Inventory;%0Aimport org.bukkit.inventory.ItemStack;%0Aimport org.luaj.vm2.LuaBoolean;%0Aimport org.luaj.vm2.LuaInteger;%0Aimport org.luaj.vm2.LuaTable;%0Aimport org.luaj.vm2.LuaValue;%0Aimport org.luaj.vm2.Varargs;%0A%0A
 import d
@@ -438,24 +438,24 @@
 temMatcher;%0A
-
 import de.cr
@@ -700,31 +700,8 @@
 ify;
-%0Aimport org.luaj.vm2.*;
 %0A%0A/*
@@ -820,16 +820,20 @@
 odule %7B%0A
+%09%0A%09%0A
     publ
@@ -1963,32 +1963,815 @@
 Amounts);%0A    %7D%0A
+    %0A    @LuaFunction(%22openChest%22)%0A    public Varargs openChest(Varargs args) %7B%0A    %09%0A    %09Player p = ScriptUtil.getPlayer(args.arg(1));%0A       %09int slots = args.arg(2).optint(1)*9;%0A       %09String title = ChatColor.translateAlternateColorCodes('&', args.arg(3).optjstring(%22%22));%0A       %09Inventory inv = Bukkit.createInventory(null, slots, title);%0A       %09LuaTable itemTable = args.arg(4).opttable(null);%0A       %09if (itemTable != null) %7B%0A       %09%09for (int i = 0; i %3C slots; i++) %7B%0A       %09%09%09LuaValue value = itemTable.get(i);%0A       %09%09%09if (value != LuaValue.NIL) %7B%0A       %09%09%09%09ItemStack item = ScriptUtil.createItemMatcher(value).getItemStack();%0A           %09%09%09inv.setItem(i, item);%0A       %09%09%09%7D%0A       %09%09%7D%0A        %7D%0A       %09p.openInventory(inv);%0A    %0A    %09return LuaValue.NIL;%0A    %7D%0A    
 %0A    private sta

Add function to open a virtual inventory.