Skip to content

Commit 9c78d84

Browse files
committed
fix: possible fix for the abnormal probability problem when reloading the Lua environment
1 parent 7db86c6 commit 9c78d84

File tree

1 file changed

+67
-64
lines changed

1 file changed

+67
-64
lines changed

src/main/java/org/eu/smileyik/luaInMinecraftBukkitII/luaState/LuaStateEnv.java

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -132,71 +132,74 @@ public LuaStateFacade createLuaState() {
132132
String luaLibrary = new File(
133133
LuaInMinecraftBukkit.instance().getDataFolder(), LuaInMinecraftBukkit.LUA_LIB_FOLDER)
134134
.getAbsolutePath();
135-
lua.openLibs();
136-
lua.setThrowableHook(exp -> {
137-
DebugLogger.debug(exp);
138-
while (exp.getCause() != null) {
139-
exp = exp.getCause();
140-
}
141-
return exp;
142-
});
143-
lua.getGlobal("package", LuaTable.class)
144-
.mapResultValue(table -> {
145-
return table.get("cpath")
146-
.mapValue(path -> {
147-
for (String fileType : NativeLoader.getDynamicFileType()) {
148-
path += ";" + rootDir.getAbsolutePath() + "/?" + fileType;
149-
path += ";" + luaLibrary + "/?" + fileType;
150-
}
151-
return path;
152-
})
153-
.mapResultValue(cpath -> table.put("cpath", cpath))
154-
.mapResultValue(it -> {
155-
return table.get("path")
156-
.mapValue(path -> (path) +
157-
";" + rootDir.getAbsolutePath() + "/?.lua" +
158-
";" + luaLibrary + "/?.lua" +
159-
";?.lua"
160-
)
161-
.mapResultValue(path -> table.put("path", path));
162-
});
163-
})
164-
.ifFailureThen(it -> {
165-
DebugLogger.debug(DebugLogger.WARN,
166-
"Error initializing lua package path: %s", it.getMessage());
167-
DebugLogger.debug(DebugLogger.ERROR, it);
168-
});
169-
lua.getGlobal("require", ILuaCallable.class)
170-
.mapResultValue(callable ->
171-
lua.setGlobal("require", new WrapperedRequireFunction(lua, callable, rootDir, this)))
172-
.ifFailureThen(it -> {
173-
DebugLogger.debug(DebugLogger.WARN,
174-
"Error initializing lua package path: %s", it.getMessage());
175-
DebugLogger.debug(DebugLogger.ERROR, it);
176-
});
135+
lua.lock(l -> {
136+
lua.openLibs();
137+
lua.setThrowableHook(exp -> {
138+
DebugLogger.debug(exp);
139+
while (exp.getCause() != null) {
140+
exp = exp.getCause();
141+
}
142+
return exp;
143+
});
144+
lua.getGlobal("package", LuaTable.class)
145+
.mapResultValue(table -> {
146+
return table.get("cpath")
147+
.mapValue(path -> {
148+
for (String fileType : NativeLoader.getDynamicFileType()) {
149+
path += ";" + rootDir.getAbsolutePath() + "/?" + fileType;
150+
path += ";" + luaLibrary + "/?" + fileType;
151+
}
152+
return path;
153+
})
154+
.mapResultValue(cpath -> table.put("cpath", cpath))
155+
.mapResultValue(it -> {
156+
return table.get("path")
157+
.mapValue(path -> (path) +
158+
";" + rootDir.getAbsolutePath() + "/?.lua" +
159+
";" + luaLibrary + "/?.lua" +
160+
";?.lua"
161+
)
162+
.mapResultValue(path -> table.put("path", path));
163+
});
164+
})
165+
.ifFailureThen(it -> {
166+
DebugLogger.debug(DebugLogger.WARN,
167+
"Error initializing lua package path: %s", it.getMessage());
168+
DebugLogger.debug(DebugLogger.ERROR, it);
169+
});
170+
lua.getGlobal("require", ILuaCallable.class)
171+
.mapResultValue(callable ->
172+
lua.setGlobal("require", new WrapperedRequireFunction(lua, callable, rootDir, this)))
173+
.ifFailureThen(it -> {
174+
DebugLogger.debug(DebugLogger.WARN,
175+
"Error initializing lua package path: %s", it.getMessage());
176+
DebugLogger.debug(DebugLogger.ERROR, it);
177+
});
177178

178-
LuaInMinecraftBukkit plugin = LuaInMinecraftBukkit.instance();
179-
lua.newTable();
180-
lua.toJavaObject(-1)
181-
.mapResultValue(obj -> {
182-
LuaTable table = ((LuaTable) obj).asTable();
183-
return table.put("env", luaEnv)
184-
.mapResultValue(it -> table.put("helper", LuaHelper.class))
185-
.mapResultValue(it -> table.put("io", LuaIOHelper.class))
186-
.mapResultValue(it -> table.put("bukkit", Bukkit.class))
187-
.mapResultValue(it -> table.put("plugin", plugin))
188-
.mapResultValue(it -> table.put("server", plugin.getServer()))
189-
.mapResultValue(it -> table.put("log", plugin.getLogger()))
190-
.mapResultValue(it -> table.put("out", System.out))
191-
.mapResultValue(it -> Result.success(table));
192-
})
193-
.mapResultValue(table -> lua.setGlobal("luaBukkit", table))
194-
.ifFailureThen(err -> {
195-
DebugLogger.debug(DebugLogger.WARN,
196-
"Error initializing global variable 'luaBukkit': %s", err.getMessage());
197-
DebugLogger.debug(DebugLogger.ERROR, err);
198-
});
199-
lua.setJustUseFirstMethod(config.isJustUseFirstMethod());
179+
LuaInMinecraftBukkit plugin = LuaInMinecraftBukkit.instance();
180+
lua.newTable();
181+
lua.toJavaObject(-1)
182+
.mapResultValue(obj -> {
183+
LuaTable table = ((LuaTable) obj).asTable();
184+
return table.put("env", luaEnv)
185+
.mapResultValue(it -> table.put("helper", LuaHelper.class))
186+
.mapResultValue(it -> table.put("io", LuaIOHelper.class))
187+
.mapResultValue(it -> table.put("bukkit", Bukkit.class))
188+
.mapResultValue(it -> table.put("plugin", plugin))
189+
.mapResultValue(it -> table.put("server", plugin.getServer()))
190+
.mapResultValue(it -> table.put("log", plugin.getLogger()))
191+
.mapResultValue(it -> table.put("out", System.out))
192+
.mapResultValue(it -> Result.success(table));
193+
})
194+
.mapResultValue(table -> lua.setGlobal("luaBukkit", table))
195+
.ifFailureThen(err -> {
196+
DebugLogger.debug(DebugLogger.WARN,
197+
"Error initializing global variable 'luaBukkit': %s", err.getMessage());
198+
DebugLogger.debug(DebugLogger.ERROR, err);
199+
});
200+
lua.setJustUseFirstMethod(config.isJustUseFirstMethod());
201+
lua.setTop(0);
202+
});
200203
return lua;
201204
}
202205

0 commit comments

Comments
 (0)