package net.walterhiggins.scriptcraft;

import java.io.File;
import java.io.FileReader;
import javax.script.*;

import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.*;



public class ScriptCraftPlugin extends JavaPlugin
{
    // right now all ops share the same JS context/scope
    // need to look at possibly having context/scope per operator
    //protected Map<CommandSender,ScriptCraftEvaluator> playerContexts = new HashMap<CommandSender,ScriptCraftEvaluator>();
    protected ScriptEngine engine = null;
    
    @Override
        public void onEnable(){
        if (this.engine == null){
            try{
                ScriptEngineManager factory = new ScriptEngineManager();
                File boot = new File("js-plugins2/core/_scriptcraft.js");
                this.engine = factory.getEngineByName("JavaScript");
                this.engine.put("__engine",engine);
                this.engine.put("__plugin",this);
                this.engine.put("__script",boot.getCanonicalPath().replaceAll("\\\\","/"));
                this.engine.eval(new FileReader(boot));  
            }catch(Exception e){
                e.printStackTrace();
            }
            
        }
    }
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    {
        if(cmd.getName().equalsIgnoreCase("js"))
        { 
            this.engine.put("__self",sender);
            String javascriptCode = "";
            for (int i = 0;i < args.length; i++){
                javascriptCode = javascriptCode + args[i] + " ";
            }
            try{
                Object result = this.engine.eval(javascriptCode);
                if (result != null)
                    sender.sendMessage(result.toString());
                return true;
            }catch (Exception e){
                sender.sendMessage(e.getMessage());
                e.printStackTrace();
            }
            return true;
        } 
        return false; 
    }

}

@@ -96,18 +96,147 @@
 java
-x.script.*
+.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport javax.script.*;%0Aimport java.util.zip.ZipEntry;%0Aimport java.util.zip.ZipInputStream
 ;%0A%0Ai
@@ -657,54 +657,1771 @@
     
-%0A    @Override%0A        public void onEnable()%7B
+private static final String JS_PLUGINS_DIR = %22js-plugins%22;%0A    %0A    @Override%0A        public void onEnable()%0A    %7B%0A        //%0A        // does the js-plugins directory exist?%0A        //%0A        File jsPlugins = new File(JS_PLUGINS_DIR);%0A        if (!jsPlugins.exists())%0A        %7B%0A            getLogger().info(%22Directory %22 + JS_PLUGINS_DIR + %22 does not exist.%22);%0A            getLogger().info(%22Initializing %22 + JS_PLUGINS_DIR + %22 directory with contents from plugin archive.%22);%0A            %0A            jsPlugins.mkdir();%0A            %0A            ZipInputStream zis = new ZipInputStream(getResource(JS_PLUGINS_DIR + %22.zip%22));%0A            ZipEntry entry;%0A            try %7B%0A                while ( ( entry = zis.getNextEntry() ) != null)%0A                %7B%0A                    String filename = entry.getName();%0A                    getLogger().info(%22Unzipping %22 + filename);%0A                    File newFile = new File(jsPlugins.getName() + File.separator + filename);%0A %0A                    //create all non exists folders%0A                    //else you will hit FileNotFoundException for compressed folder%0A                    if (entry.isDirectory())%7B%0A                        newFile.mkdirs();%0A                    %7Delse%7B%0A                        FileOutputStream fout = new FileOutputStream(newFile);             %0A                        for (int c = zis.read(); c != -1; c = zis.read()) %7B%0A                            fout.write(c);%0A                        %7D%0A                        fout.close();%0A%0A                    %7D%0A                    zis.closeEntry();%0A                %7D%0A                zis.close();%0A            %7Dcatch (IOException ioe)%7B%0A                getLogger().warning(ioe.getMessage());%0A                ioe.printStackTrace();%0A            %7D%0A        %7D%0A        
 %0A   
@@ -2582,20 +2582,26 @@
 ile(
-%22js-plugins2
+JS_PLUGINS_DIR + %22
 /cor
@@ -3026,29 +3026,16 @@
       %7D%0A
-            %0A
         

rework - moved logic from java to js