Skip to content

Commit

Permalink
fix: 修复文件随便加载的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
npofsi committed Aug 18, 2018
1 parent 9e623c6 commit b0fd6d1
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/main/java/pro/npofsi/rmpescript/RMPEScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}


Expand Down
1 change: 1 addition & 0 deletions src/main/java/pro/npofsi/rmpescript/beta/CommandBoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();) {
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/pro/npofsi/rmpescript/broadcast/FMLEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,Stack<FMLEventCallback>> eventStore=new TreeMap<String,Stack<FMLEventCallback>>();
public static void registerCallback(String name,FMLEventCallback callback){
private Map<String,Stack<FMLEventCallback>> eventStore=new TreeMap<String,Stack<FMLEventCallback>>();
public void registerCallback(String name,FMLEventCallback callback){
if(eventStore.get(name)==null) { eventStore.put(name,new Stack<FMLEventCallback>());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<FMLEventCallback> stk=eventStore.get(eventName);
if(stk != null) for (int i = 0; i < stk.size();i++) stk.elementAt(i).call(event);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/pro/npofsi/rmpescript/data/DataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

8 changes: 5 additions & 3 deletions src/main/java/pro/npofsi/rmpescript/runtime/JSLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
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){
this.srcName=srcName;
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);
Expand All @@ -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));
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void refresh(){
ScriptManager.getInstance().refresh(refreshFromFiles());
}
public Map<String,String> refreshFromFiles(){
File[] fs=DataStore.getInstance().getScriptDir().listFiles(new DataStore.isFileFilter());
File[] fs=DataStore.getInstance().getScriptDir().listFiles(new DataStore.fileTypeFilter("js"));
Map<String,String> scripts=new TreeMap<String, String>();
for (File f : fs){
try {
Expand Down

0 comments on commit b0fd6d1

Please sign in to comment.