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

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.TwoArgFunction;

import de.craften.plugins.rpgplus.components.entitymanager.BasicManagedEntity;
import de.craften.plugins.rpgplus.components.entitymanager.ManagedEntity;
import de.craften.plugins.rpgplus.components.entitymanager.MovementType;

public class EntitySpawner {

    public void installOn(LuaTable object){
        object.set("registerEntity", new TwoArgFunction() {
            
            @Override
            public LuaValue call(LuaValue entityType, LuaValue optionsArg) {
                
                EntityType type = EntityType.valueOf(entityType.checkjstring());
                Class<? extends Entity> entityClass = type.getEntityClass();
                
                LuaTable options = optionsArg.checktable();
                
                ManagedEntity<? extends Entity> entity = new BasicManagedEntity<>(entityClass, new Location(
                        Bukkit.getWorld(options.get("world").checkjstring()),
                        options.get("x").checkdouble(),
                        options.get("y").checkdouble(),
                        options.get("z").checkdouble()));
                
                entity.setName(options.get("name").optjstring(""));
                entity.setIsTakingDamage(options.get("invulnerable").optboolean(false));
                entity.setMovementType(MovementType.valueOf(options.get("movementType").optjstring("LOCAL")));
                
                return EntityWrapper.wrap(entity);
            }
        });
    }
    
}

@@ -66,25 +66,78 @@
 ort 
-org.bukkit.Bukk
+de.craften.plugins.rpgplus.components.entitymanager.BasicManagedEnt
 it
+y
 ;%0D%0Ai
@@ -146,436 +146,392 @@
 ort 
-org.bukkit.Location;%0D%0Aimport org.bukkit.entity.Entity;%0D%0Aimport org.bukkit.entity.EntityType;%0D%0Aimport org.luaj.vm2.LuaTable;%0D%0Aimport org.luaj.vm2.LuaValue;%0D%0Aimport org.luaj.vm2.lib.TwoArgFunction;%0D%0A%0D%0Aimport de.craften.plugins.rpgplus.components.entitymanager.BasicManagedEntity;%0D%0Aimport de.craften.plugins.rpgplus.components.entitymanager.ManagedEntity;%0D%0Aimport de.craften.plugins.rpgplus.components.entitymanager.MovementType;%0D%0A
+de.craften.plugins.rpgplus.components.entitymanager.ManagedEntity;%0D%0Aimport de.craften.plugins.rpgplus.components.entitymanager.MovementType;%0D%0Aimport org.bukkit.Bukkit;%0D%0Aimport org.bukkit.Location;%0D%0Aimport org.bukkit.entity.EntityType;%0D%0Aimport org.luaj.vm2.LuaTable;%0D%0Aimport org.luaj.vm2.LuaValue;%0D%0Aimport org.luaj.vm2.lib.TwoArgFunction;%0D%0A%0D%0A/**%0D%0A * Lua API for spawning entities.%0D%0A */
 %0D%0Apu
@@ -558,18 +558,16 @@
 wner %7B%0D%0A
-%0D%0A
     publ
@@ -600,16 +600,17 @@
  object)
+ 
 %7B%0D%0A     
@@ -628,22 +628,13 @@
 et(%22
-registerEntity
+spawn
 %22, n
@@ -656,28 +656,16 @@
 on() %7B%0D%0A
-            
 %0D%0A      
@@ -759,34 +759,16 @@
 Arg) %7B%0D%0A
-                %0D%0A
         
@@ -845,104 +845,8 @@
 );%0D%0A
-                Class%3C? extends Entity%3E entityClass = type.getEntityClass();%0D%0A                %0D%0A
     
@@ -902,32 +902,16 @@
 ble();%0D%0A
-                
 %0D%0A      
@@ -937,26 +937,8 @@
 tity
-%3C? extends Entity%3E
  ent
@@ -968,17 +968,25 @@
 ntity%3C%3E(
-e
+type.getE
 ntityCla
@@ -987,16 +987,18 @@
 ityClass
+()
 , new Lo
@@ -1258,32 +1258,16 @@
 e()));%0D%0A
-                
 %0D%0A      
@@ -1547,16 +1547,33 @@
         
+entity.spawn();%0D%0A
 %0D%0A      
@@ -1657,13 +1657,7 @@
  %7D%0D%0A
-    %0D%0A
 %7D%0D%0A

Rename entity register function and immediately spawn the entity.