package net.engin33r.luaspigot.lua.lib;

import net.engin33r.luaspigot.lua.Function;
import net.engin33r.luaspigot.lua.Library;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.LuaValue;

/**
 * Library for interacting with the chat.
 */
@SuppressWarnings("unused")
public class ChatLibrary extends Library {
    public ChatLibrary() {
        registerFunction(new BroadcastFunction());
    }

    @Override
    public String getName() {
        return "chat";
    }

    private class BroadcastFunction extends Function {
        @Override
        public String getName() {
            return "broadcast";
        }

        @Override
        public Varargs call(Varargs args) {
            int n = args.narg();
            if (n == 0) return LuaValue.NIL;

            String str = "";
            for (int i = 1; i <= args.narg(); i++) {
                if (i > 1) str += "\t";
                str += args.checkjstring(i);
            }

            for (Player p : Bukkit.getOnlinePlayers()) {
                p.sendMessage(str);
            }
            return LuaValue.NIL;
        }
    }


}

@@ -68,24 +68,23 @@
 got.lua.
-Function
+Library
 ;%0Aimport
@@ -103,39 +103,57 @@
 r.luaspigot.lua.
-Library
+annotation.LibFunctionDef
 ;%0Aimport org.buk
@@ -386,335 +386,118 @@
     
-public ChatLibrary() %7B%0A        registerFunction(new BroadcastFunction());%0A    %7D%0A%0A    @Override%0A    public String getName() %7B%0A        return %22chat%22;%0A    %7D%0A%0A    private class BroadcastFunction extends Function %7B%0A        @Override%0A        public String getName() %7B%0A            return %22broadcast%22;%0A        %7D%0A%0A        @Override%0A    
+@Override%0A    public String getName() %7B%0A        return %22chat%22;%0A    %7D%0A%0A    @LibFunctionDef(name = %22broadcast%22)%0A
     
@@ -515,12 +515,17 @@
 rgs 
-call
+broadcast
 (Var
@@ -545,20 +545,16 @@
         
- 
-   
 int n = 
@@ -566,20 +566,16 @@
 narg();%0A
-    
         
@@ -616,20 +616,16 @@
         
-  
-  
 String s
@@ -633,20 +633,16 @@
 r = %22%22;%0A
-    
         
@@ -690,28 +690,24 @@
             
-   
- 
 if (i %3E 1) s
@@ -718,20 +718,16 @@
 = %22%5Ct%22;%0A
-    
         
@@ -767,20 +767,16 @@
         
-    
 %7D%0A%0A     
@@ -774,28 +774,24 @@
  %7D%0A%0A        
- 
-   
 for (Player 
@@ -835,20 +835,16 @@
         
-    
 p.sendMe
@@ -863,26 +863,18 @@
         
-  
-  %7D%0A    
+%7D%0A
         
@@ -902,20 +902,8 @@
     
-    %7D%0A    %7D%0A%0A
+%7D
 %0A%7D%0A

Use new annotation for chat library