Skip to content

Commit

Permalink
fix&feat: 添加更多Lua类型与Java类型转换方法,添加网络请求方法
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileYik committed Jun 13, 2023
1 parent aeb79c6 commit 9150cef
Show file tree
Hide file tree
Showing 17 changed files with 948 additions and 106 deletions.
22 changes: 20 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
plugins {
id 'java'
id "de.jjohannes.extra-java-module-info" version "0.1"
}

group 'tk.smileyik'
version '1.7.1'
version '1.7.2'
archivesBaseName = 'LuaInMinecraftBukkit'

repositories {
Expand All @@ -20,14 +21,26 @@ repositories {
}

dependencies {
implementation 'mysql:mysql-connector-java:8.0.28'
implementation 'org.luaj:luaj-jse:3.0.1'
implementation 'org.spigotmc:spigot-api:1.16.2-R0.1-SNAPSHOT'
implementation 'org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}

extraJavaModuleInfo {
// This does not have to be a complete description (e.g. here 'org.apache.commons.collections' does not export anything here).
// It only needs to be good enough to work in the context of this application we are building.
module('luaj-jse-3.0.1.jar','org.luaj','3.0.1') {
exports "org.luaj.vm2"
exports "org.luaj.vm2.lib.jse"
}
automaticModule("commons-lang-2.6.jar", "org.apache.commons.lang")
automaticModule("bungeecord-chat-1.16-R0.3.jar", "bungeecord-chat")
automaticModule("guava-21.0.jar", "guava")
automaticModule("gson-2.8.0.jar", "gson")
}

test {
useJUnitPlatform()
}
Expand All @@ -48,4 +61,9 @@ task copyTask(type: Copy) {
dependsOn(deleteOldBuilds)
from "${buildDir}/libs/${archivesBaseName}-${version}.jar"
into "${projectDir}/server/plugins/"
}

compileJava {
options.compilerArgs << "--add-exports=java.base/sun.net.www.protocol.https=ALL-UNNAMED"
options.compilerArgs << "--add-exports=java.base/sun.net.www.protocol.http=ALL-UNNAMED"
}
156 changes: 156 additions & 0 deletions docs/LuaHelper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@

### getClass
+ **Description**: 通过全类名去获取java类类型.
+ **Return Type**: **Class<?>**
+ **Return Desc**: 如果类存在则返回对应的类
+ **Usage**: `getClass(name:String)`

|Name|Type|Description|
|-|-|-|
|name|**String**|全类名|
### toIntArray
+ **Description**: 将Lua中只含整形的表转换为java中的整形数组.
+ **Return Type**: **int[]**
+ **Return Desc**: 转换成Java中的整形数组.
+ **Usage**: `toIntArray(table:LuaObject)`

|Name|Type|Description|
|-|-|-|
|table|**LuaObject**|只含整形的表(数组).|
### toIntArray
+ **Description**: 将Lua中只含整形的表转换为java中的整形数组.
+ **Return Type**: **int[]**
+ **Return Desc**: 转换成Java中的整形数组.
+ **Usage**: `toIntArray(table:LuaTable)`

|Name|Type|Description|
|-|-|-|
|table|**LuaTable**|只含整形的表(数组).|
### toDoubleArray
+ **Description**: 将Lua中只含浮点数的表转换为java中的double类型数组.
+ **Return Type**: **double[]**
+ **Return Desc**: 转换成Java中的double类型数组.
+ **Usage**: `toDoubleArray(table:LuaObject)`

|Name|Type|Description|
|-|-|-|
|table|**LuaObject**|只含小数的表(数组).|
### toDoubleArray
+ **Description**: 将Lua中只含浮点数的表转换为java中的double类型数组.
+ **Return Type**: **double[]**
+ **Return Desc**: 转换成Java中的double类型数组.
+ **Usage**: `toDoubleArray(table:LuaTable)`

|Name|Type|Description|
|-|-|-|
|table|**LuaTable**|只含小数的表(数组).|
### toFloatArray
+ **Description**: 将Lua中只含浮点数的表转换为java中的float类型数组.
+ **Return Type**: **float[]**
+ **Return Desc**: 转换成Java中的float类型数组.
+ **Usage**: `toFloatArray(table:LuaObject)`

|Name|Type|Description|
|-|-|-|
|table|**LuaObject**|只含小数的表(数组).|
### toFloatArray
+ **Description**: 将Lua中只含浮点数的表转换为java中的float类型数组.
+ **Return Type**: **float[]**
+ **Return Desc**: 转换成Java中的float类型数组.
+ **Usage**: `toFloatArray(table:LuaTable)`

|Name|Type|Description|
|-|-|-|
|table|**LuaTable**|只含小数的表(数组).|
### toStringArray
+ **Description**: 将Lua中只含字符串的表转换为java中的String类型数组.
+ **Return Type**: **String[]**
+ **Return Desc**: 转换成Java中的String类型数组.
+ **Usage**: `toStringArray(table:LuaObject)`

|Name|Type|Description|
|-|-|-|
|table|**LuaObject**|只含字符串的表(数组).|
### toStringArray
+ **Description**: 将Lua中只含字符串的表转换为java中的String类型数组.
+ **Return Type**: **String[]**
+ **Return Desc**: 转换成Java中的String类型数组.
+ **Usage**: `toStringArray(table:LuaTable)`

|Name|Type|Description|
|-|-|-|
|table|**LuaTable**|只含字符串的表(数组).|
### toArray
+ **Description**: 将Lua中只含指定类型的表转换为java中的指定类型数组.
+ **Return Type**: **T[]**
+ **Return Desc**: 转换成Java中的指定类型数组.
+ **Usage**: `toArray(t:T, table:LuaObject)`

|Name|Type|Description|
|-|-|-|
|t|**T**|需要转换成的Java类型实例.|
|table|**LuaObject**|只含指定类型的表(数组).|
### toArray
+ **Description**: 将Lua中只含指定类型的表转换为java中的指定类型数组.
+ **Return Type**: **T[]**
+ **Return Desc**: 转换成Java中的指定类型数组.
+ **Usage**: `toArray(t:T, table:LuaTable)`

|Name|Type|Description|
|-|-|-|
|t|**T**|需要转换成的Java类型实例.|
|table|**LuaTable**|只含指定类型的表(数组).|
### toObjectArray
+ **Description**: 将Lua中的表(数组)转换为java中的Object类型数组.
+ **Return Type**: **Object[]**
+ **Return Desc**: @return
+ **Usage**: `toObjectArray(table:LuaObject)`

|Name|Type|Description|
|-|-|-|
|table|**LuaObject**|表(数组)|
### toObjectArray
+ **Description**: 将Lua中的表(数组)转换为java中的Object类型数组.
+ **Return Type**: **Object[]**
+ **Return Desc**: @return
+ **Usage**: `toObjectArray(table:LuaTable)`

|Name|Type|Description|
|-|-|-|
|table|**LuaTable**|表(数组)|
### toStringStringMap
+ **Description**: 将Lua中的只包含字符串的数组(下标为数字且未显式指定数字下标的Table)(如{"a", "b"}而非{2 = "b"})转换为Java中的Map类型.
+ **Return Type**: **String>**
+ **Return Desc**: Map<String, String>类型HashMap.
+ **Usage**: `toStringStringMap(table:LuaObject)`

|Name|Type|Description|
|-|-|-|
|table|**LuaObject**|Lua中的表|
### toStringStringMap
+ **Description**: 将Lua中的只包含字符串的数组(下标为数字且未显式指定数字下标的Table)(如{"a", "b"}而非{2 = "b"})转换为Java中的Map类型.
+ **Return Type**: **String>**
+ **Return Desc**: Map<String, String>类型HashMap.
+ **Usage**: `toStringStringMap(table:LuaTable)`

|Name|Type|Description|
|-|-|-|
|table|**LuaTable**|Lua中的表|
### toObjectObjectMap
+ **Description**: 将Lua中的数组(下标为数字且未显式指定数字下标的Table)(如{"a", "b"}而非{2 = "b"})转换为Java中的Map类型.
+ **Return Type**: **Object>**
+ **Return Desc**: Map<Object, Object>类型HashMap.
+ **Usage**: `toObjectObjectMap(table:LuaObject)`

|Name|Type|Description|
|-|-|-|
|table|**LuaObject**|Lua中的表|
### toObjectObjectMap
+ **Description**: 将Lua中的数组(下标为数字且未显式指定数字下标的Table)(如{"a", "b"}而非{2 = "b"})转换为Java中的Map类型.
+ **Return Type**: **Object>**
+ **Return Desc**: Map<Object, Object>类型HashMap.
+ **Usage**: `toObjectObjectMap(table:LuaTable)`

|Name|Type|Description|
|-|-|-|
|table|**LuaTable**|Lua中的表|
119 changes: 100 additions & 19 deletions docs/LuaPluginHelper.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,136 @@

### luaBukkit.helper:isLoadPlugin
### isLoadPlugin
+ **Description**: 指定脚本插件是否加载.
+ **Return Type**: **boolean**
+ **Return Desc**: 如果已经加载则返回true.
+ **Usage**: `luaBukkit.helper:isLoadPlugin(id:String)`
+ **Usage**: `isLoadPlugin(id:String)`

|Name|Type|Description|
|-|-|-|
|id|**String**|脚本插件id|
### luaBukkit.helper:newLuaRunnable
### newLuaRunnable
+ **Description**: 以指定id对应的函数为run方法主体创建一个Runnable子类实例
+ **Return Type**: **LuaRunnable**
+ **Return Desc**: LuaRunnable实例
+ **Usage**: `luaBukkit.helper:newLuaRunnable(id:String)`
+ **Usage**: `newLuaRunnable(id:String)`

|Name|Type|Description|
|-|-|-|
|id|**String**|函数路径|
### luaBukkit.helper:newLuaRunnable
### newLuaRunnable
+ **Description**: 以指定id对应的函数为run方法主体创建一个Runnable子类实例
+ **Return Type**: **LuaRunnable**
+ **Return Desc**: LuaRunnable实例
+ **Usage**: `luaBukkit.helper:newLuaRunnable(id:String, obj:Object)`
+ **Usage**: `newLuaRunnable(id:String, obj:Object)`

|Name|Type|Description|
|-|-|-|
|id|**String**|函数路径|
|obj|**Object**|带入的参数.|
### luaBukkit.helper:getPlugin
### getPlugin
+ **Description**: 获取本插件实例对象.
+ **Return Type**: **Plugin**
+ **Return Desc**: 本插件的实例对象.
+ **Usage**: `luaBukkit.helper:getPlugin()`
+ **Usage**: `getPlugin()`

|Name|Type|Description|
|-|-|-|

### luaBukkit.helper:getClass
+ **Description**: 通过全类名去获取java类类型.
+ **Return Type**: **Class<?>**
+ **Return Desc**: 如果类存在则返回对应的类
+ **Usage**: `luaBukkit.helper:getClass(name:String)`

|Name|Type|Description|
|-|-|-|
|name|**String**|全类名|
### luaBukkit.helper:isNativeMode
### isNativeMode
+ **Description**: 插件是否在Native模式下运行.
+ **Return Type**: **boolean**
+ **Return Desc**: 如果是在Native模式下运行则返回true.
+ **Usage**: `luaBukkit.helper:isNativeMode()`
+ **Usage**: `isNativeMode()`

|Name|Type|Description|
|-|-|-|

### httpAction
+ **Description**: 以插件身份发送一个HTTP请求.
+ **Return Type**: **void**
+ **Return Desc**:
+ **Usage**: `httpAction(plugin:LuaPlugin, urlString:String, body:byte[], preOperation:Object, callback:Object, errorCallback:Object)`

|Name|Type|Description|
|-|-|-|
|plugin|**LuaPlugin**|Lua插件实例.|
|urlString|**String**|链接字符串|
|body|**byte[]**|请求体,如果传入null则代表不需要请求体.|
|preOperation|**Object**|前置操作Lua函数闭包,将会传入一个HttpURLConnection实例给闭包.|
|callback|**Object**|一个Lua函数闭包,会将ResponseCode以及HTTP请求的文本结果作为字符串传递给闭包.|
|errorCallback|**Object**|一个Lua函数闭包,其只在运行过程中出现错误时调用,会将Exception实例传给闭包.|
### httpAction
+ **Description**: 以插件身份发送一个HTTP请求.
+ **Return Type**: **void**
+ **Return Desc**:
+ **Usage**: `httpAction(plugin:LuaPlugin, urlString:String, method:String, body:byte[], connectTimeout:int, readTimeout:int, instanceFollowRedirects:boolean, Map<String:, header:String>, useCaches:boolean, callback:Object, errorCallback:Object)`

|Name|Type|Description|
|-|-|-|
|plugin|**LuaPlugin**|Lua插件实例.|
|urlString|**String**|链接字符串|
|method|**String**|请求方法|
|body|**byte[]**|请求体,如果传入null则代表不需要请求体.|
|connectTimeout|**int**|连接等待时间(毫秒).|
|readTimeout|**int**|读取等待时间(毫秒).|
|instanceFollowRedirects|**boolean**|是否跟随重定向.|
|Map<String|****|null|
|header|**String>**|需要添加的请求头,为null则代表不需要添加.|
|useCaches|**boolean**|是否使用缓存.|
|callback|**Object**|一个Lua函数闭包,会将ResponseCode以及HTTP请求的文本结果作为字符串传递给闭包.|
|errorCallback|**Object**|一个Lua函数闭包,其只在运行过程中出现错误时调用,会将Exception实例传给闭包.|
### httpAction
+ **Description**: 以插件身份发送一个HTTP请求.
+ **Return Type**: **void**
+ **Return Desc**:
+ **Usage**: `httpAction(plugin:LuaPlugin, urlString:String, method:String, body:String, bodyEncode:String, connectTimeout:int, readTimeout:int, instanceFollowRedirects:boolean, Map<String:, header:String>, useCaches:boolean, callback:Object, errorCallback:Object)`

|Name|Type|Description|
|-|-|-|
|plugin|**LuaPlugin**|Lua插件实例.|
|urlString|**String**|链接字符串|
|method|**String**|请求方法|
|body|**String**|字符串请求体,如果传入null则代表不需要请求体.|
|bodyEncode|**String**|字符串请求体编码格式.|
|connectTimeout|**int**|连接等待时间(毫秒).|
|readTimeout|**int**|读取等待时间(毫秒).|
|instanceFollowRedirects|**boolean**|是否跟随重定向.|
|Map<String|****|null|
|header|**String>**|需要添加的请求头,为null则代表不需要添加.|
|useCaches|**boolean**|是否使用缓存.|
|callback|**Object**|一个Lua函数闭包,会将ResponseCode以及HTTP请求的文本结果作为字符串传递给闭包.|
|errorCallback|**Object**|一个Lua函数闭包,其只在运行过程中出现错误时调用,会将Exception实例传给闭包.|
### httpGet
+ **Description**: 以插件身份发送一个HTTP的GET请求.
+ **Return Type**: **void**
+ **Return Desc**:
+ **Usage**: `httpGet(plugin:LuaPlugin, urlString:String, connectTimeout:int, readTimeout:int, instanceFollowRedirects:boolean, Map<String:, header:String>, useCaches:boolean, callback:Object, errorCallback:Object)`

|Name|Type|Description|
|-|-|-|
|plugin|**LuaPlugin**|Lua插件实例.|
|urlString|**String**|链接字符串|
|connectTimeout|**int**|连接等待时间(毫秒).|
|readTimeout|**int**|读取等待时间(毫秒).|
|instanceFollowRedirects|**boolean**|是否跟随重定向.|
|Map<String|****|null|
|header|**String>**|需要添加的请求头,为null则代表不需要添加.|
|useCaches|**boolean**|是否使用缓存.|
|callback|**Object**|一个Lua函数闭包,会将ResponseCode以及HTTP请求的文本结果作为字符串传递给闭包.|
|errorCallback|**Object**|一个Lua函数闭包,其只在运行过程中出现错误时调用,会将Exception实例传给闭包.|
### httpGet
+ **Description**: 以插件身份发送一个HTTP的GET请求.
+ **Return Type**: **void**
+ **Return Desc**:
+ **Usage**: `httpGet(plugin:LuaPlugin, urlString:String, connectTimeout:int, readTimeout:int, instanceFollowRedirects:boolean, useCaches:boolean, callback:Object, errorCallback:Object)`

|Name|Type|Description|
|-|-|-|
|plugin|**LuaPlugin**|Lua插件实例.|
|urlString|**String**|链接字符串|
|connectTimeout|**int**|连接等待时间(毫秒).|
|readTimeout|**int**|读取等待时间(毫秒).|
|instanceFollowRedirects|**boolean**|是否跟随重定向.|
|useCaches|**boolean**|是否使用缓存.|
|callback|**Object**|一个Lua函数闭包,会将ResponseCode以及HTTP请求的文本结果作为字符串传递给闭包.|
|errorCallback|**Object**|一个Lua函数闭包,其只在运行过程中出现错误时调用,会将Exception实例传给闭包.|
9 changes: 9 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module LuaInMinecraftBukkit.main {
requires java.base;
requires org.luaj;
requires org.bukkit;
requires java.logging;
requires java.net.http;

opens org.keplerproject.luajava to java.base;
}
2 changes: 2 additions & 0 deletions src/main/java/org/keplerproject/luajava/LuaJavaAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public static int objectIndex(int luaState, Object obj, String methodName)
ret = method.invoke(obj, objs);
}
} catch (Exception e) {
e.printStackTrace();
throw new LuaException(e);
}

Expand Down Expand Up @@ -605,6 +606,7 @@ private static Method getMethod(LuaState L, Class<?> clazz,
m.getParameterCount() != size) {
continue;
}

boolean okMethod = true;
Class<?>[] parameters = m.getParameterTypes();
for (int i = 0; i < size; ++i) {
Expand Down
Loading

0 comments on commit 9150cef

Please sign in to comment.