LuaInMinecraftBukkit II
English | 中文
简介
LuaInMinecraftBukkit II 是一款 Minecraft Bukkit 服务器插件,旨在通过 Lua 脚本与 Bukkit 服务器进行交互。
Lua 是一种小型脚本语言,语法非常简单且执行速度不错。想象一下,用一种无需编译即可运行的轻量级脚本来编写 Bukkit 插件会有多美妙。如果你需要修改某些内容,只需更改并重新加载脚本即可——这简直像做梦一样。
与前代版本的差异
与前一代版本相比,本版本更侧重于原生 Lua 虚拟机。同样,本版本也基于 luajava 项目。然而,本版本使用的是一个 克隆的 luajava 仓库。与原始仓库相比,克隆的 luajava 仓库从根本上重写了部分反射功能,进一步简化了 Lua 调用 Java 方法的流程,并在 C 语言层面提供了非常友好的异常信息。
它能做什么?
主要功能:
- 命令注册:注册任意你所需的命令,它会自动生成帮助信息和命令层级结构。
- 事件监听:监听任意你想要的 Bukkit 事件,即使是来自其他插件的自定义事件。
- Lua 池化:通过 Lua 池化,你可以将 Lua 闭包提交到其他状态机中运行,从而实现真正的多线程并行执行:示例。
其他特性包括:
- 快速反射:Lua 的反射组件现在使用基于 MethodHandle 的快速反射,其速度显著优于标准反射。
- 自动重载:插件可以监控 Lua 脚本文件的变化并自动重新加载它们。
然而,依赖于 Java 的反射和动态代理机制,目前可以在 Lua 脚本中继承 Java 接口并调用任何 Java 类型的公共方法或公共属性。这意味着该插件可以动态加载脚本并享受 Java 功能的一个子集。当然,反射并非万能药,仍有许多情况无法在 Lua 端处理。在这种情况下,Java 需要为 Lua 搭建桥梁。但在开发过程中,我会尽量简化 Lua 与 Java 之间的交互。
除了上述提到的内容外,类似于第一代版本,它还可以加载用 C/C++ 编写的动态链接库。当然,这些都是 Lua 语言本身支持的特性。
Lua 池化
得益于 luajava 的不懈努力,
现在可以在多个线程上执行不同的 Lua 代码。
在本插件中,每个 Lua 环境配置都有一个主 Lua 虚拟机。
如果没有池化的 Lua 虚拟机,当主 Lua 虚拟机正在运行时,
如果另一个线程 B 需要获取主 Lua 虚拟机的执行权(例如,当触发 Bukkit 事件或执行 Bukkit 异步任务时)来运行 Lua 代码
(如 Lua 闭包函数),线程 B 必须阻塞并等待主 Lua 虚拟机。
只有当主 Lua 虚拟机完成其当前任务后,线程 B 才能获取
执行权来运行 Lua 代码。Lua 代码执行完毕后,必须释放
主 Lua 虚拟机的执行权。如果线程 B 执行的代码耗时很长,主 Lua 虚拟机将被线程 B 占用,
从而阻塞其他 Lua 代码的执行,直到线程 B 释放执行权。
通过 Lua 池,在相同场景下,当线程 B 获取了 主 Lua 虚拟机 的执行权后,它会立即将要执行的 Lua 代码及相关数据转移到 另一个 Lua 虚拟机。一旦转移完成,它将立即释放 主 Lua 虚拟机 的执行权,并获取 另一个 Lua 虚拟机 的执行权来运行已转移的代码。这使得线程 B 能够在极短的时间内释放 主 Lua 虚拟机 的执行权,而无需等待 Lua 代码执行完毕,从而确保 主 Lua 虚拟机 能够快速响应其他调用。
以下是一个更具体的示例。下面的代码调用了两个 Bukkit 异步任务,且这两个任务都是无限循环。
local import = require "import"
local Thread = import "java.lang.Thread"
for i = 1, 2 do
luaBukkit.helper:asyncCall(luaBukkit.env:pooledCallable(
function ()
while true do
luaBukkit.log:info(Thread:currentThread():getName() .. " async call " .. i .. "!")
Thread:sleep(1000)
end
end
))
end
当启用 Lua 池且容量为 2 时,上述代码输出如下内容。你可以看到来自多个线程的输出,并且可以使用 stop 命令关闭服务器:
[13:14:41 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 3 - LuaInMinecraftBukkitII async call 1!
[13:14:41 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 1 - LuaInMinecraftBukkitII async call 2!
[13:14:42 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 1 - LuaInMinecraftBukkitII async call 2!
[13:14:42 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 3 - LuaInMinecraftBukkitII async call 1!
当禁用 Lua 池时,上述代码输出如下内容。你只能看到一个线程的输出,虽然 stop 命令已被确认,但服务器无法关闭,因为它无法获取执行权以释放来自无限循环的 Lua VM 资源:
[13:17:49 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
[13:17:52 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
stop
[13:17:53 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
[13:17:53 INFO]: Stopping the server
[13:17:53 INFO]: Stopping server
[13:17:53 INFO]: [LuaInMinecraftBukkitII] Disabling LuaInMinecraftBukkitII vmaster-59748d7+luajava-master-a3ddaf6+java-21
[13:17:53 INFO]: [LuaInMinecraftBukkitII] [LuaEnv default] Shutdown auto-reload.
[13:17:54 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
[13:17:55 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
[13:17:56 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
JNIBridge
得益于 cffi 的支持,JNIBridge 应运而生!现在可以与动态链接库进行交互。JNIBridge 仍在不断改进中,但目前支持:
下一阶段计划中的功能包括:
下图展示了一个简单的玩家加入事件,但事件处理逻辑由 Cpp 端管理。

LuaInMinecraftBukkit II
English | 中文
Introduction
LuaInMinecraftBukkit II is a Minecraft Bukkit server plugin that aims to use Lua scripts to interact with Bukkit servers.
Lua is a small scripting language with very simple syntax and decent execution speed. Just imagine how wonderful it would be to write a Bukkit plugin with a lightweight script that doesn't need to be compiled to run. If you need to modify something, you can just change it and reload the script—it's like a dream.
Differences from the Previous Generation
Compared to the previous generation, this version focuses more on the native Lua virtual machine. Similarly, this version is also based on the luajava project. However, this version uses a cloned luajava repository. Compared to the original repository, the cloned luajava repository has fundamentally rewritten parts of the reflection functionality, further simplifying the process of Lua calling Java methods, and provides very friendly exception messages on the C language side.
What Can It Do?
Major functions:
- Command Registration: Register any command you want, and it will automatically generate help information and command hierarchy.
- Event Listening: Listen for any Bukkit event you want, even if it's a custom event from another plugin.
- Lua Pooling: By pooling Lua, you can submit Lua closures to run on other state machines to achieve true multi-threaded parallel execution: Example.
Other features include:
- Fast Reflection: The reflection component for Lua now uses MethodHandle-based fast reflection, which is significantly faster than standard reflection.
- Auto-Reload: The plugin can monitor changes to Lua script files and automatically reload them.
However, relying on Java's reflection and dynamic proxy mechanisms, it is currently possible to inherit Java interfaces and call any public method or public attribute of a Java type in a Lua script. This means this plugin can dynamically load scripts and enjoy a subset of Java's functionality. Of course, reflection isn't a silver bullet, and there will still be many situations that cannot be handled on the Lua side. In such cases, Java needs to bridge the gap for Lua. But in the development process, I will try to simplify the interaction between Lua and Java.
In addition to what has been mentioned, similar to the first generation, it can also load dynamic link libraries written in C/C++. Of course, these are features that the Lua language itself supports.
Lua Pooling
Thanks to the tireless efforts of luajava,
it is now possible to execute different Lua code on multiple threads.
In this plugin, each Lua environment configuration has a main Lua virtual machine.
Without a pooled Lua virtual machine, when the main Lua virtual machine is running,
if another thread B needs to acquire the execution rights of the main Lua virtual machine
(e.g., when a Bukkit event is triggered or for a Bukkit asynchronous task) to run Lua code
(such as a Lua closure function), thread B must block and wait for the main Lua virtual machine.
Only after the main Lua virtual machine completes its current task can thread B acquire
the execution rights to run the Lua code. After the Lua code finishes, it must release
the execution rights of the main Lua virtual machine. If the code executed by thread B
takes a very long time, the main Lua virtual machine will remain occupied by thread B,
blocking the execution of other Lua code until thread B releases the execution rights.
With a Lua pool, in the same scenario, after thread B acquires the execution rights of
the main Lua virtual machine, it will immediately transfer the Lua code to be executed
and all related data to another Lua virtual machine. Once the transfer is complete,
it will immediately release the execution rights of the main Lua virtual machine
and acquire the execution rights of the other Lua virtual machine to run the transferred code.
This allows thread B to release the execution rights of the main Lua virtual machine in
a very short amount of time, without waiting for the Lua code to finish, ensuring that the
main Lua virtual machine can respond quickly to other calls.
Here is a more specific example. The following code calls two Bukkit asynchronous tasks, and both tasks are infinite loops.
local import = require "import"
local Thread = import "java.lang.Thread"
for i = 1, 2 do
luaBukkit.helper:asyncCall(luaBukkit.env:pooledCallable(
function ()
while true do
luaBukkit.log:info(Thread:currentThread():getName() .. " async call " .. i .. "!")
Thread:sleep(1000)
end
end
))
end
When Lua pooling is enabled and the capacity is 2, the code above outputs the following content.
You can see output from multiple threads, and the server can be shut down with a stop command:
[13:14:41 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 3 - LuaInMinecraftBukkitII async call 1!
[13:14:41 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 1 - LuaInMinecraftBukkitII async call 2!
[13:14:42 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 1 - LuaInMinecraftBukkitII async call 2!
[13:14:42 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 3 - LuaInMinecraftBukkitII async call 1!
When Lua pooling is disabled, the code above outputs the following.
You can see output from only one thread, and while the stop command is acknowledged,
the server cannot be shut down because it cannot acquire the execution rights to
release the Lua VM resources from the infinite loop:
[13:17:49 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
[13:17:52 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
stop
[13:17:53 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
[13:17:53 INFO]: Stopping the server
[13:17:53 INFO]: Stopping server
[13:17:53 INFO]: [LuaInMinecraftBukkitII] Disabling LuaInMinecraftBukkitII vmaster-59748d7+luajava-master-a3ddaf6+java-21
[13:17:53 INFO]: [LuaInMinecraftBukkitII] [LuaEnv default] Shutdown auto-reload.
[13:17:54 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
[13:17:55 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
[13:17:56 INFO]: [LuaInMinecraftBukkitII] Craft Scheduler Thread - 2 - LuaInMinecraftBukkitII async call 2!
JNIBridge
Thanks to the support of cffi, JNIBridge was born! Now it's possible to interact with dynamic link libraries. JNIBridge is still being improved, but currently supports:
- Java method invocation
- Basic type conversion
Features planned for the next stage include:
- Field access and assignment
- Array access and assignment
The image below shows a simple player join event, but the event handling logic is managed on the Cpp side.

评论 0
登录后参与评分
还没有评论,来抢沙发吧