Skip to content

Commit ea3a078

Browse files
committed
Update and Fixed bugs.
1 parent 7a92afe commit ea3a078

File tree

11 files changed

+122
-10
lines changed

11 files changed

+122
-10
lines changed

build.gradle

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,27 @@ repositories {
1313
maven {url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'}
1414
maven {url 'https://oss.sonatype.org/content/repositories/snapshots'}
1515
maven {url 'https://repo.md-5.net/repository/public'}
16-
1716
maven {url 'https://repo.extendedclip.com/content/repositories/placeholderapi/'}
17+
maven {url 'http://maven.aliyun.com/nexus/content/groups/public'}
1818
}
1919

2020
dependencies {
2121
compile 'commons-io:commons-io:2.7'
22-
implementation "org.jetbrains.kotlin:kotlin-stdlib"
22+
compile 'com.alibaba:fastjson:1.2.9'
23+
compile "org.jetbrains.kotlin:kotlin-stdlib"
24+
implementation 'org.yaml:snakeyaml:1.27'
25+
implementation 'org.apache.commons:commons-lang3:3.10'
2326
implementation 'org.spigotmc:spigot-api:1.16.3-R0.1-SNAPSHOT'
2427
implementation 'net.md-5:bungeecord-api:1.16-R0.3'
2528
implementation 'me.clip:placeholderapi:2.10.9'
2629
}
2730

31+
jar {
32+
from {
33+
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
34+
}
35+
}
36+
2837
processResources {
2938
from(sourceSets.main.resources.srcDirs) {
3039
include 'plugin.yml'
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
package me.scoretwo.fastscript.api.exception
22

3-
class ScriptException: Exception() {
4-
}
3+
open class ScriptException(msg: String): Exception(msg)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package me.scoretwo.fastscript.api.exception
2+
3+
class ScriptNotFound: ScriptException("Unknown script, it doesn't exist.")

src/main/kotlin/me/scoretwo/fastscript/api/script/InternalScript.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ import java.io.File
55

66
class InternalScript(file: File): Script(file) {
77

8-
9-
108
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
package me.scoretwo.fastscript.api.script
22

33
class ScriptManager {
4+
5+
val scripts = mutableListOf<Script>()
6+
7+
fun getScript(name: String): Script? {
8+
for (script in scripts) {
9+
if (script.name == name) return script
10+
}
11+
return null
12+
}
13+
414
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package me.scoretwo.fastscript.config
2+
3+
class ScriptImport {
4+
5+
companion object {
6+
7+
class Object(clazz: String, args: Array<Any?>)
8+
class Static(clazz: String)
9+
class Method(name: String, args: Array<Any?>)
10+
11+
}
12+
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package me.scoretwo.fastscript.config
2+
3+
class ScriptOption {
4+
5+
6+
}

src/main/kotlin/me/scoretwo/fastscript/utils/Utils.kt

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,58 @@ import java.lang.reflect.Method
1010
object Utils {
1111

1212
enum class MethodTypes { OBJECT, STATIC }
13-
class Empty { companion object { val instance = Empty() } }
1413

1514
/**
1615
* 直接对指定对象 new 来获得 instance
1716
*/
18-
fun findClass(script: Script, target: String): Class<*> {
17+
fun findClass(script: Script, target: String): Class<*>? {
1918
return try {
2019
Class.forName(target)
2120
} catch (e: ClassNotFoundException) {
2221
e.printStackTrace()
2322
FastScript.main.sendConsoleMessage("&7[&2Fast&aScript&7] &cERROR &8| &c脚本 &4${script.name} 没有找到类 ${target}, 依赖于该类的方法将不会起作用!")
24-
Empty.instance.javaClass
23+
null
2524
}
2625
}
2726

27+
/**
28+
* 对指定动态对象执行某个方法并返回结果
29+
*/
2830
fun getObjectMethodResults(script: Script, clazz: Class<*>, methodName: String, args: Array<Any?> = arrayOf()): Any? {
31+
return getObjectMethodResults(script, clazz, arrayOf(), methodName, args)
32+
}
33+
34+
/**
35+
* 对指定动态对象执行某个方法并返回结果
36+
*/
37+
fun getObjectMethodResults(script: Script, clazz: Class<*>, initArgs: Array<Any?>, methodName: String, args: Array<Any?> = arrayOf()): Any? {
2938
val method = clazz.getMethod(methodName)
30-
return getMethodResults(script, method, clazz.newInstance(), args)
39+
val constructor = clazz.getDeclaredConstructor()
40+
constructor.isAccessible = true
41+
return getMethodResults(script, method, constructor.newInstance(initArgs), args)
3142
}
3243

44+
/**
45+
* 对指定动态对象执行某个方法并返回结果
46+
* 给定指定对象
47+
*/
48+
fun getObjectMethodResults(script: Script, objects: Any?, methodName: String, args: Array<Any?> = arrayOf()): Any? {
49+
if (objects == null) return null
50+
val method = objects.javaClass.getMethod(methodName)
51+
return getMethodResults(script, method, objects, args)
52+
}
53+
54+
/**
55+
* 对指定静态对象执行某个方法并返回结果
56+
*/
3357
fun getStaticMethodResults(script: Script, clazz: Class<*>, methodName: String, args: Array<Any?> = arrayOf()): Any? {
3458
val method = clazz.getMethod(methodName)
3559
return getMethodResults(script, method, null, args)
3660
}
3761

62+
/**
63+
* 对指定对象执行某个方法并返回结果
64+
*/
3865
private fun getMethodResults(script: Script, method: Method, obj: Any?, args: Array<Any?>): Any? {
3966
try {
4067
return method.invoke(obj, args)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
function main() {
3+
server.getConsoleSender().sendMessage("[Example] Running script: example")
4+
}
5+
6+
7+
function playerJoinEvent() {
8+
player.sendMessage("[Example] Welcome to the server!")
9+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 脚本引擎
2+
engine: nashorn
3+
4+
# 导入自定义 object 作为变量
5+
import:
6+
# a: 'static|org.bukkit.Bukkit#getServer|args1|args2...'
7+
8+
# b: 'object|org.bukkit.util.Vector|1|2...'
9+
10+
c:
11+
init:
12+
class: 'org.bukkit.util.Vector'
13+
args:
14+
- 1
15+
- 100
16+
- 1
17+
d:
18+
object:
19+
class: 'org.bukkit.util.Vector'
20+
args: []
21+
method:
22+
name: 'length'
23+
args: []
24+
25+
e:
26+
static:
27+
class: 'org.bukkit.Bukkit'
28+
method:
29+
name: 'getServer'
30+
args: []

0 commit comments

Comments
 (0)