diff --git a/src/main/java/pro/npofsi/rmpescript/RMPEScript.java b/src/main/java/pro/npofsi/rmpescript/RMPEScript.java index 4676c35..f9992d1 100644 --- a/src/main/java/pro/npofsi/rmpescript/RMPEScript.java +++ b/src/main/java/pro/npofsi/rmpescript/RMPEScript.java @@ -62,7 +62,7 @@ public void init(FMLInitializationEvent event) Log.i("RMPEScript starting..."); ScriptFileManager.getInstance().refresh(); ScriptManager.getInstance().runAllScripts(null); - FMLEventHandler.runCallBack("init",event); + FMLEventHandler.getInstance().runCallBack("init",event); } @EventHandler @@ -75,7 +75,7 @@ public void serverStarting(FMLServerStartingEvent event) { CommandManager.getInstance().refresh((ServerCommandManager) Intent.getServer().getCommandManager()); CommandManager.getInstance().register(new CommandRMPE()); Level.setWorld(event.getServer().getEntityWorld()); - FMLEventHandler.runCallBack("serverStarting",event); + FMLEventHandler.getInstance().runCallBack("serverStarting",event); } diff --git a/src/main/java/pro/npofsi/rmpescript/beta/CommandBoom.java b/src/main/java/pro/npofsi/rmpescript/beta/CommandBoom.java index 94b034a..9a78829 100644 --- a/src/main/java/pro/npofsi/rmpescript/beta/CommandBoom.java +++ b/src/main/java/pro/npofsi/rmpescript/beta/CommandBoom.java @@ -31,6 +31,7 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args EntityPlayer player = (EntityPlayer) sender; // 接下来的代码我直接抄的Szszss的教程: http://www.hakugyokurou.net/wordpress/?p=225 // 这串代码可以获取玩家周围一定范围的生物列表 + List list = player.world.getEntitiesWithinAABB(EntityLiving.class, new AxisAlignedBB((double)player.posX - 30D, (double)player.posY - 20D, (double)player.posZ - 30D, (double)player.posX + 30D, (double)player.posY + 20D, (double)player.posZ + 30D)); // 使用Iterator遍历list for (Iterator iterator = list.iterator(); iterator.hasNext();) { diff --git a/src/main/java/pro/npofsi/rmpescript/broadcast/FMLEventHandler.java b/src/main/java/pro/npofsi/rmpescript/broadcast/FMLEventHandler.java index 6fb008f..609abff 100644 --- a/src/main/java/pro/npofsi/rmpescript/broadcast/FMLEventHandler.java +++ b/src/main/java/pro/npofsi/rmpescript/broadcast/FMLEventHandler.java @@ -9,25 +9,25 @@ public class FMLEventHandler { -// public FMLEventHandler(){} -// public static FMLEventHandler getInstance(){ -// return ClassHolder.sInstance; -// } -// private static class ClassHolder{ -// private static final FMLEventHandler sInstance = new FMLEventHandler(); -// } + public FMLEventHandler(){} + public static FMLEventHandler getInstance(){ + return ClassHolder.sInstance; + } + private static class ClassHolder{ + private static final FMLEventHandler sInstance = new FMLEventHandler(); + } //这个统一形式定义的接口导致不能使用rhino所提供的interface的简写形式,会导致rhino找不到相应的类型 public interface FMLEventCallback{ public void call(FMLEvent event); } - private static Map> eventStore=new TreeMap>(); - public static void registerCallback(String name,FMLEventCallback callback){ + private Map> eventStore=new TreeMap>(); + public void registerCallback(String name,FMLEventCallback callback){ if(eventStore.get(name)==null) { eventStore.put(name,new Stack());eventStore.get(name).push(callback); }else{ eventStore.get(name).push(callback); } } - public static boolean unregisterCallback(String name,FMLEventCallback callback){ + public boolean unregisterCallback(String name,FMLEventCallback callback){ if(eventStore.get(name)!=null) { return eventStore.get(name).remove(callback); }return false; } - public static void runCallBack(String eventName,FMLEvent event){ + public void runCallBack(String eventName,FMLEvent event){ Stack stk=eventStore.get(eventName); if(stk != null) for (int i = 0; i < stk.size();i++) stk.elementAt(i).call(event); } diff --git a/src/main/java/pro/npofsi/rmpescript/data/DataStore.java b/src/main/java/pro/npofsi/rmpescript/data/DataStore.java index 1eaca87..a3cdf61 100644 --- a/src/main/java/pro/npofsi/rmpescript/data/DataStore.java +++ b/src/main/java/pro/npofsi/rmpescript/data/DataStore.java @@ -63,5 +63,12 @@ public boolean accept(File f){ return f.isFile(); } } + public static class fileTypeFilter implements FileFilter { + String type; + public fileTypeFilter(String type){this.type=type;} + public boolean accept(File f){ + return f.isFile()&&f.getName().endsWith("."+type); + } + } } diff --git a/src/main/java/pro/npofsi/rmpescript/runtime/JSLoader.java b/src/main/java/pro/npofsi/rmpescript/runtime/JSLoader.java index be3d241..a516018 100644 --- a/src/main/java/pro/npofsi/rmpescript/runtime/JSLoader.java +++ b/src/main/java/pro/npofsi/rmpescript/runtime/JSLoader.java @@ -10,6 +10,7 @@ public class JSLoader extends Loader{ private String script,srcName; + private Context cx=null; // private Object ScriptManager= pro.npofsi.rmpescript.runtime.ScriptManager.getInstance(); public JSLoader(String srcName,String script){ @@ -17,10 +18,11 @@ public JSLoader(String srcName,String script){ this.script=script; this.setName(srcName); } - private Context cx = Context.enter(); + @Override public void run(){ synchronized(ScriptManager.getInstance()){ + cx = Context.enter(); try { Scriptable scope = cx.initStandardObjects(); Object result = cx.evaluateString(scope, script, srcName+RMPEScript.randomTag(), 1, null); @@ -34,7 +36,7 @@ public void run(){ } public void eval(String code){ synchronized(ScriptManager.getInstance()){ - try { + if(cx != null)try { Scriptable scope = cx.initStandardObjects(); Object result = cx.evaluateString(scope, code, srcName+RMPEScript.randomTag(), 1, null); RMPEScript.Log.i("Script eval end: "+cx.toString(result)); @@ -48,7 +50,7 @@ public void eval(String code){ public void remove(){ synchronized(ScriptManager.getInstance()){ try { - cx.exit(); + if(cx!= null)cx.exit(); this.join(); } catch (InterruptedException e) { e.printStackTrace(); diff --git a/src/main/java/pro/npofsi/rmpescript/runtime/ScriptFileManager.java b/src/main/java/pro/npofsi/rmpescript/runtime/ScriptFileManager.java index 49780ad..84cbc2a 100644 --- a/src/main/java/pro/npofsi/rmpescript/runtime/ScriptFileManager.java +++ b/src/main/java/pro/npofsi/rmpescript/runtime/ScriptFileManager.java @@ -22,7 +22,7 @@ public void refresh(){ ScriptManager.getInstance().refresh(refreshFromFiles()); } public Map refreshFromFiles(){ - File[] fs=DataStore.getInstance().getScriptDir().listFiles(new DataStore.isFileFilter()); + File[] fs=DataStore.getInstance().getScriptDir().listFiles(new DataStore.fileTypeFilter("js")); Map scripts=new TreeMap(); for (File f : fs){ try {