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

import de.craften.plugins.rpgplus.RpgPlus;
import de.craften.plugins.rpgplus.components.dialogs.AnswerHandler;
import de.craften.plugins.rpgplus.scripting.util.ScriptUtil;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.ThreeArgFunction;
import org.luaj.vm2.lib.VarArgFunction;
import org.luaj.vm2.lib.jse.CoerceLuaToJava;

import java.awt.*;

/**
 * Provides an API for dialogs.
 */
public class Dialogs extends LuaTable {
    public Dialogs(final RpgPlus plugin) {
        set("ask", new VarArgFunction() {
            @Override
            public Varargs invoke(Varargs varargs) {
                Entity entity = (Entity) CoerceLuaToJava.coerce(varargs.arg(1), Entity.class);
                Player player = ScriptUtil.getPlayer(varargs.arg(2));
                final LuaFunction callback = varargs.checkfunction(4);
                plugin.getDialogs().ask(entity, player, varargs.checkjstring(3), new AnswerHandler() {
                    @Override
                    public void handleAnswer(Player player, String answer) {
                        callback.invoke(LuaValue.valueOf(answer));
                    }
                });
                return LuaValue.NIL;
            }
        });
    }
}

@@ -286,41 +286,8 @@
 er;%0A
-import org.bukkit.plugin.Plugin;%0A
 impo
@@ -404,16 +404,56 @@
 arargs;%0A
+import org.luaj.vm2.lib.OneArgFunction;%0A
 import o
@@ -576,28 +576,8 @@
 a;%0A%0A
-import java.awt.*;%0A%0A
 /**%0A
@@ -652,16 +652,17 @@
 Table %7B%0A
+%0A
     publ
@@ -829,16 +829,22 @@
         
+final 
 Entity e
@@ -1233,16 +1233,22 @@
 eAnswer(
+final 
 Player p
@@ -1339,50 +1339,849 @@
 wer)
-);%0A                    %7D%0A                %7D
+, new OneArgFunction() %7B%0A                            @Override%0A                            public LuaValue call(LuaValue message) %7B%0A                                plugin.getDialogs().tell(entity, player, message.checkjstring());%0A                                return LuaValue.NIL;%0A                            %7D%0A                        %7D);%0A                    %7D%0A                %7D);%0A                return LuaValue.NIL;%0A            %7D%0A        %7D);%0A%0A        set(%22tell%22, new ThreeArgFunction() %7B%0A            @Override%0A            public LuaValue call(LuaValue luaEntity, LuaValue luaPlayer, LuaValue message) %7B%0A                Entity entity = (Entity) CoerceLuaToJava.coerce(luaEntity, Entity.class);%0A                Player player = ScriptUtil.getPlayer(luaPlayer);%0A                plugin.getDialogs().tell(entity, player, message.checkjstring()
 );%0A 

Add a respond function as second parameter in the ask callback. Add a lua function to tell the player something in a dialog.