Skip to content

Commit 7b2c732

Browse files
committed
Update ScriptManager and CommandManager.
1 parent 4bfcbb2 commit 7b2c732

File tree

22 files changed

+354
-88
lines changed

22 files changed

+354
-88
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
![](http://mc3.roselle.vip:602/FastScript/big_logo.gif)
2-
<font color=#8E8E8E size=3>The idea of the LOGO comes from @Arasple's TrMenu.</font>
2+
>The idea of the LOGO comes from @Arasple's TrMenu.
33
***
44
### About
55
FastScript is a Spigot plugin, which can run JavaScript-based scripts more efficiently.
66

7-
> At present, the project is still unfinished and cannot be used.
7+
>At present, the project is still unfinished and cannot be used.
88
***
99
### Build
1010
In the project root directory:
1111
```
12-
./gradle jar
12+
./gradle shadowJar
1313
```
1414
***
1515
### Links

build.gradle

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
id 'org.jetbrains.kotlin.jvm' version '1.4.10'
3+
id 'com.github.johnrengelman.shadow' version '2.0.4'
34
}
45

56
group 'me.scoretwo'
@@ -19,21 +20,27 @@ repositories {
1920
}
2021

2122
dependencies {
22-
compile 'commons-io:commons-io:2.7'
23-
compile 'com.alibaba:fastjson:1.2.9'
23+
compile "org.bstats:bstats-bukkit:1.7"
24+
compile "commons-io:commons-io:2.7"
25+
compile "com.alibaba:fastjson:1.2.9"
2426
compile "org.jetbrains.kotlin:kotlin-stdlib"
2527
compile "com.andreapivetta.kolor:kolor:1.0.0"
26-
implementation 'org.yaml:snakeyaml:1.27'
27-
implementation 'org.apache.commons:commons-lang3:3.10'
28-
implementation 'org.spigotmc:spigot-api:1.16.3-R0.1-SNAPSHOT'
29-
implementation 'net.md-5:bungeecord-api:1.16-R0.3'
30-
implementation 'me.clip:placeholderapi:2.10.9'
28+
implementation "org.yaml:snakeyaml:1.27"
29+
implementation "org.apache.commons:commons-lang3:3.10"
30+
implementation "org.spigotmc:spigot-api:1.16.3-R0.1-SNAPSHOT"
31+
implementation "net.md-5:bungeecord-api:1.16-R0.3"
32+
implementation "me.clip:placeholderapi:2.10.9"
3133
}
3234

33-
jar {
34-
from {
35-
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
35+
shadowJar {
36+
dependencies {
37+
include("org.bstats:bstats-bukkit:1.7")
38+
include("commons-io:commons-io:2.7")
39+
include("com.alibaba:fastjson:1.2.9")
40+
include("org.jetbrains.kotlin:kotlin-stdlib")
41+
include("com.andreapivetta.kolor:kolor:1.0.0")
3642
}
43+
relocate("org.bstats", "me.scoretwo.fastscript.bstats")
3744
}
3845

3946
processResources {

gradle/wrapper/gradle-wrapper.jar

57.5 KB
Binary file not shown.

src/main/kotlin/me/scoretwo/fastscript/FastScript.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,49 @@ package me.scoretwo.fastscript
33
import com.andreapivetta.kolor.*
44
import me.scoretwo.fastscript.api.plugin.FastScriptMain
55
import me.scoretwo.fastscript.api.script.ScriptManager
6+
import me.scoretwo.fastscript.commands.CommandManager
67
import java.io.File
78

89
class FastScript(main: FastScriptMain) {
910

1011
private val main: FastScriptMain = main
1112

1213
val scriptManager = ScriptManager()
14+
val commandManager = CommandManager()
1315

1416
val dataFolder = main.getDataFolder()
1517
val classLoader = main.getPluginClassLoader()
18+
19+
1620
fun setPlaceholder(player: Any, string: String) = main.setPlaceholder(player, string)
1721
fun sendMessage(sender: Any, string: String, colorIndex: Boolean) = main.sendMessage(sender, string, colorIndex)
22+
fun sendMessage(sender: Any, strings: Array<String>, colorIndex: Boolean) = strings.forEach { main.sendMessage(sender, it, colorIndex) }
1823

1924
init {
2025
CONSOLE = main.CONSOLE
2126
}
2227

28+
/**
29+
* 初始化内置脚本
30+
* 暂时弃坑
31+
*/
2332
fun initInternalScripts() {
2433

2534
}
2635

2736
fun onReload() {
2837
main.onReload()
2938
initInternalScripts()
39+
scriptManager.loadScripts()
3040
}
3141

3242
/**
33-
* 用于随机点亮 FastScript 的 Logo
43+
* 用于随机点亮 FastScript 的 Logo.
3444
* 该创意来源于 TrMenu
3545
* @author Arasple
3646
*/
3747

38-
fun printLogo() = mutableListOf(
48+
fun printLogo() = arrayOf(
3949
"___________ __ _________ .__ __ ",
4050
"\\_ _____/____ _______/ |_/ _____/ ___________|__|______/ |_ ",
4151
" | __) \\__ \\ / ___/\\ __\\_____ \\_/ ___\\_ __ \\ \\____ \\ __\\",
@@ -55,13 +65,13 @@ class FastScript(main: FastScriptMain) {
5565
it[index] = String(line).replaceFirst(replace, "§${arrayOf('9', 'b', '3').random()}$replace§8")
5666
}
5767
}
58-
printColors(it)
68+
sendMessage(CONSOLE, it, false)
5969
}
6070

6171
companion object {
6272
lateinit var instance: FastScript
6373
var CONSOLE = Any()
64-
74+
/*
6575
@JvmStatic
6676
fun main(args: Array<out String>) {
6777
@@ -124,10 +134,14 @@ class FastScript(main: FastScriptMain) {
124134
}
125135
println()
126136
}
137+
*/
127138

128139
fun sendMessage(sender: Any, string: String, colorIndex: Boolean = false) {
129140
instance.sendMessage(sender, string, colorIndex)
130141
}
142+
fun sendMessage(sender: Any, strings: Array<String>, colorIndex: Boolean = false) {
143+
instance.sendMessage(sender, strings, colorIndex)
144+
}
131145

132146
fun setBootstrap(main: FastScriptMain) {
133147
if (instance != null) {
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
package me.scoretwo.fastscript.api.script
22

3-
class CustomScript {
3+
import java.io.File
4+
5+
class CustomScript(file: File): Script(file) {
6+
7+
8+
49
}
Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,92 @@
11
package me.scoretwo.fastscript.api.script
22

3+
import me.scoretwo.fastscript.FastScript
4+
import me.scoretwo.fastscript.api.script.ScriptImport.Companion.TYPE.*
5+
import me.scoretwo.fastscript.api.yaml.YAMLObject
6+
import me.scoretwo.fastscript.config.SettingConfig
37
import me.scoretwo.fastscript.utils.Utils
48
import java.io.File
59
import java.io.FileInputStream
610
import java.io.InputStream
11+
import javax.script.ScriptEngine
12+
import javax.script.ScriptEngineManager
713

814
abstract class Script {
915

1016
val name: String
11-
var file: File? = null
17+
var scriptFile: File? = null
18+
var optionFile: File? = null
1219
var inputStream: InputStream
20+
val scriptOption: ScriptOption
21+
22+
lateinit var scriptEngine: ScriptEngine
1323

1424
constructor(file: File): this(file.name.substring(0, file.name.indexOf(".") - 1), FileInputStream(file)) {
15-
this.file = file
25+
this.scriptFile = file
1626
}
1727

1828
constructor(name: String, inputStream: InputStream) {
1929
this.inputStream = inputStream
2030
this.name = name
31+
32+
optionFile = File(scriptFile!!.parentFile, "${name}.yml")
33+
34+
if (scriptFile != null && !optionFile!!.exists()) {
35+
this.scriptOption = SettingConfig.instance.defaultScriptOption
36+
} else {
37+
this.scriptOption = ScriptOption.fromConfigSection(YAMLObject.loadConfiguration(optionFile!!))
38+
}
39+
40+
onReload()
2141
}
2242

2343
fun onReload() {
24-
if (file == null) return
25-
Utils.saveDefaultResource(file!!, inputStream)
26-
this.inputStream = FileInputStream(file!!)
44+
if (scriptFile == null) return
45+
Utils.saveDefaultResource(scriptFile!!, inputStream)
46+
val fileInputStream = FileInputStream(scriptFile!!)
47+
this.inputStream = fileInputStream
48+
fileInputStream.close()
49+
50+
this.scriptEngine = ScriptEngineManager(FastScript.instance.classLoader).getEngineByName(scriptOption.engine)
51+
52+
// 处理脚本的导入
53+
// 0 容错
54+
scriptOption.import.forEach {
55+
56+
when (it.type) {
57+
INIT -> {
58+
this.scriptEngine.put(
59+
it.name,
60+
Utils.getObjectInit(this, Utils.findClass(this, it.obj!!.clazz)!!, it.obj.args)
61+
)
62+
}
63+
OBJECT -> {
64+
this.scriptEngine.put(
65+
it.name,
66+
Utils.getObjectMethodResults(
67+
this,
68+
Utils.findClass(this, it.obj!!.clazz)!!,
69+
it.obj.args,
70+
it.met!!.name,
71+
it.met.args
72+
)
73+
)
74+
}
75+
STATIC -> {
76+
this.scriptEngine.put(
77+
it.name,
78+
Utils.getStaticMethodResults(
79+
this,
80+
Utils.findClass(this, it.obj!!.clazz)!!,
81+
it.met!!.name
82+
)
83+
)
84+
}
85+
else -> {
86+
FastScript.sendMessage(FastScript.CONSOLE, "§7[§2Fast§aScript§7] §cERROR §8| §7脚本 §c$name §7元素 §c${it.name} §7导入失败, 无法识别该元素的形式.")
87+
}
88+
}
89+
}
2790
}
2891

2992
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package me.scoretwo.fastscript.api.script
22

3-
import me.scoretwo.fastscript.config.YAMLObject
3+
import com.alibaba.fastjson.JSONObject
4+
import me.scoretwo.fastscript.api.yaml.YAMLObject
45

5-
class ScriptImport(val name: String, val TYPE: TYPE, val obj: Object?, val met: Method?) {
6+
class ScriptImport(val name: String, val type: TYPE, val obj: Object?, val met: Method?) {
67

78

89

@@ -11,6 +12,11 @@ class ScriptImport(val name: String, val TYPE: TYPE, val obj: Object?, val met:
1112
class Object(val clazz: String, val args: Array<Any?> = arrayOf())
1213
class Method(val name: String, val args: Array<Any?> = arrayOf())
1314

15+
16+
fun fromConfigSection(name: String, jsonObject: JSONObject): ScriptImport {
17+
return fromConfigSection(name, jsonObject)
18+
}
19+
1420
fun fromConfigSection(name: String, yamlObject: YAMLObject): ScriptImport {
1521
val obj: Object?
1622
val met: Method?

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package me.scoretwo.fastscript.api.script
22

3+
import me.scoretwo.fastscript.FastScript
4+
import me.scoretwo.fastscript.config.SettingConfig
5+
import java.io.File
6+
37
class ScriptManager {
48

9+
val defaultScriptPath = File(FastScript.instance.dataFolder, "scripts")
10+
511
val scripts = mutableListOf<Script>()
612

713
fun getScript(name: String): Script? {
@@ -11,4 +17,27 @@ class ScriptManager {
1117
return null
1218
}
1319

20+
fun loadScript(file: File) {
21+
scripts.add(CustomScript(file))
22+
}
23+
24+
/**
25+
* 暂时同步, 异步以后写
26+
*/
27+
@Synchronized
28+
fun loadScripts() {
29+
scripts.clear()
30+
defaultScriptPath.listFiles()?.forEach { loadScript(it) }
31+
32+
for (file in SettingConfig.instance.scriptPaths) {
33+
if (file.isDirectory) {
34+
file.listFiles()?.forEach { loadScript(it) }
35+
} else {
36+
loadScript(file)
37+
}
38+
}
39+
40+
41+
}
42+
1443
}
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
package me.scoretwo.fastscript.api.script
22

3-
class ScriptOption(engine: String, import: MutableList<ScriptImport>, main: String? = null) {
3+
import com.alibaba.fastjson.JSONObject
4+
import me.scoretwo.fastscript.api.yaml.YAMLObject
45

6+
class ScriptOption(val engine: String, val import: MutableList<ScriptImport>, val main: String? = null) {
57

8+
companion object {
69

10+
fun fromConfigSection(yamlObject: YAMLObject): ScriptOption {
11+
val engine = yamlObject.getLowerCaseString("engine")!!
12+
val main = yamlObject.getLowerCaseString("main")!!
13+
14+
val import = mutableListOf<ScriptImport>()
15+
16+
for (i in yamlObject.getLowerCaseYAMLObject("import")) {
17+
18+
import.add(ScriptImport.fromConfigSection(i.key, i.value as JSONObject))
19+
20+
}
21+
22+
return ScriptOption(engine, import, main)
23+
}
24+
25+
26+
}
727

828
}

src/main/kotlin/me/scoretwo/fastscript/config/YAMLArray.kt renamed to src/main/kotlin/me/scoretwo/fastscript/api/yaml/YAMLArray.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package me.scoretwo.fastscript.config
1+
package me.scoretwo.fastscript.api.yaml
22

33
import com.alibaba.fastjson.JSONArray
44

5-
class YAMLArray: JSONArray {
5+
open class YAMLArray: JSONArray {
66

77
constructor(jsonArray: JSONArray): super(jsonArray)
88

0 commit comments

Comments
 (0)