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

import org.bukkit.Effect;
import org.bukkit.Location;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;

import de.craften.plugins.rpgplus.RpgPlus;
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;

public class ParticleModule extends LuaTable implements ScriptingModule {

	public ParticleModule(final RpgPlus plugin) {
		Luaify.convertInPlace(this);
	}

	@LuaFunction("spawn")
	public void spawn(Varargs varargs) {

		Location loc = ScriptUtil.getLocation(varargs.arg(1));

		Effect effect = Effect
				.valueOf(varargs.arg(2).tojstring().toUpperCase());

		int id = varargs.toint(3);

		int data = varargs.toint(4);

		float offsetX = (float) varargs.optdouble(5, 0);
		float offsetY = (float) varargs.optdouble(6, 0);
		float offsetZ = (float) varargs.optdouble(7, 0);

		float speed = (float) varargs.optdouble(8, 0);

		int particleCount = varargs.optint(9, 1);

		int radius = varargs.optint(10, 100);

		loc.getWorld()
				.spigot()
				.playEffect(loc, effect, id, data, offsetX, offsetY, offsetZ,
						speed, particleCount, radius);

	}

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

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

}

@@ -876,74 +876,212 @@
 );%0D%0A
+%09%09
 %0D%0A%09%09
-int id = varargs.toint(3);%0D%0A%0D%0A%09%09int data = varargs.toint(4
+LuaTable options = varargs.checktable(3);%0D%0A%09%09%0D%0A%09%09int id = options.get(%22id%22).optint(0);%0D%0A%0D%0A%09%09int data = options.get(%22data%22).optint(0);%0D%0A%09%09%0D%0A%09%09LuaTable offset = options.get(%22offset%22).checktable(
 );%0D%0A
+%09%09
 %0D%0A%09%09
@@ -1096,39 +1096,47 @@
 fsetX = (float) 
-varargs
+offset.get(%22x%22)
 .optdouble(5, 0)
@@ -1134,11 +1134,8 @@
 ble(
-5, 
 0);%0D
@@ -1153,39 +1153,47 @@
 fsetY = (float) 
-varargs
+offset.get(%22y%22)
 .optdouble(6, 0)
@@ -1191,11 +1191,8 @@
 ble(
-6, 
 0);%0D
@@ -1210,39 +1210,47 @@
 fsetZ = (float) 
-varargs
+offset.get(%22z%22)
 .optdouble(7, 0)
@@ -1248,11 +1248,8 @@
 ble(
-7, 
 0);%0D
@@ -1275,23 +1275,36 @@
 (float) 
-varargs
+options.get(%22speed%22)
 .optdoub
@@ -1310,11 +1310,8 @@
 ble(
-8, 
 0);%0D
@@ -1335,31 +1335,44 @@
 Count = 
-varargs
+options.get(%22count%22)
 .optint(
 9, 1);%0D%0A
@@ -1367,11 +1367,8 @@
 int(
-9, 
 1);%0D
@@ -1385,23 +1385,37 @@
 adius = 
-varargs
+options.get(%22radius%22)
 .optint(
@@ -1419,14 +1419,8 @@
 nt(1
-0, 100
 );%0D%0A

Use a options table for the spawn function.