Skip to content

Commit 1e67467

Browse files
committed
Update commands.
1 parent a47547c commit 1e67467

File tree

12 files changed

+179
-44
lines changed

12 files changed

+179
-44
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ class FastScript(val plugin: ScriptPlugin) {
106106
var debug = false
107107

108108
lateinit var plugin: ScriptPlugin
109-
val scripts = mutableListOf<CustomScript>()
110109

111110
lateinit var settings: SettingConfig
112111
lateinit var languages: LanguageManager

FastScript-common/src/main/kotlin/me/scoretwo/fastscript/api/expansion/ExpansionManager.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package me.scoretwo.fastscript.api.expansion
22

3-
import me.scoretwo.fastscript.FastScript
43
import me.scoretwo.fastscript.api.format.FormatHeader
54
import me.scoretwo.fastscript.api.utils.process.ProcessResult
65
import me.scoretwo.fastscript.api.utils.process.ProcessResultType
76
import me.scoretwo.fastscript.expansion.javascript.JavaScriptExpansion
8-
import me.scoretwo.fastscript.expansion.typeengine.TypeEngineExpansion
97
import me.scoretwo.fastscript.plugin
108
import me.scoretwo.fastscript.sendMessage
119
import me.scoretwo.utils.bukkit.configuration.yaml.file.YamlConfiguration
12-
import me.scoretwo.utils.syntaxes.findClass
1310
import java.io.File
1411
import java.net.URL
1512
import java.net.URLClassLoader
@@ -22,8 +19,6 @@ class ExpansionManager {
2219
val localExpansions = mutableMapOf<ExpansionDescription, FastScriptExpansion>()
2320

2421
init {
25-
register(JavaScriptExpansion())
26-
2722
if (!expansionFolder.exists()) expansionFolder.mkdirs()
2823
}
2924

@@ -55,15 +50,14 @@ class ExpansionManager {
5550
fun reload() {
5651
val startTime = System.currentTimeMillis()
5752
expansions.clear()
58-
59-
register(JavaScriptExpansion())
53+
register(JavaScriptExpansion().reload())
6054

6155
if (!expansionFolder.exists())
6256
expansionFolder.mkdirs()
6357

64-
var success = 0
58+
var success = 1
6559
var fail = 0
66-
var total = 0
60+
var total = 1
6761

6862
for (file in expansionFolder.listFiles() ?: arrayOf()) {
6963
total++
@@ -75,9 +69,10 @@ class ExpansionManager {
7569
}
7670

7771
try {
78-
expansions.add(rawExpansion.second!!)
72+
expansions.add(rawExpansion.second!!.reload())
7973
success++
8074
} catch (e: Exception) {
75+
fail++
8176
e.printStackTrace()
8277
plugin.server.console.sendMessage(FormatHeader.ERROR, "An exception occurred when loading extended ${file.name}, reason:\n§8${e.stackTraceToString()}")
8378
}

FastScript-common/src/main/kotlin/me/scoretwo/fastscript/api/expansion/FastScriptExpansion.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ abstract class FastScriptExpansion {
1010
abstract val fileSuffix: String
1111

1212
abstract val needEval: Boolean
13+
14+
abstract fun reload(): FastScriptExpansion
15+
1316
abstract fun eval(script: Script, sender: GlobalSender): Any?
1417
abstract fun eval(text: String, sender: GlobalSender): Any?
1518
abstract fun execute(script: Script, sender: GlobalSender, main: String = script.option.main, args: Array<Any?> = arrayOf()): Any?

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ScriptManager {
2424

2525
init {
2626
if (!folders[0].exists()) {
27+
folders[0].mkdirs()
2728
"""
2829
function main() {
2930
sender.sendMessage("&athis is demo.")
@@ -51,6 +52,7 @@ class ScriptManager {
5152

5253
return@let scriptFile
5354
}
55+
return Pair(null, ProcessResult(ProcessResultType.FAILED, "The script file extension is not supported!"))
5456
}
5557
val script = CustomScript(
5658
object : ScriptDescription {
@@ -61,11 +63,12 @@ class ScriptManager {
6163
override val authors: Array<String> = arrayOf("FastScript")
6264

6365
},
64-
object : ConfigScriptOption() {
65-
66-
}
66+
ConfigScriptOption(),
67+
mutableListOf(file)
6768
)
6869

70+
scripts[scriptName] = script
71+
return Pair(script, ProcessResult(ProcessResultType.SUCCESS))
6972
}
7073

7174
/**
@@ -138,9 +141,6 @@ class ScriptManager {
138141

139142
return Pair(script, ProcessResult(ProcessResultType.SUCCESS))
140143
}
141-
// {
142-
// scripts.add(CustomScript(file))
143-
// }
144144

145145
/**
146146
* 暂时同步, 异步以后写

FastScript-common/src/main/kotlin/me/scoretwo/fastscript/api/script/custom/ConfigScriptOption.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import me.scoretwo.utils.bukkit.configuration.yaml.patchs.getLowerCaseNode
77
import me.scoretwo.utils.bukkit.configuration.yaml.patchs.loadConfiguration
88
import java.io.File
99

10-
open class ConfigScriptOption(val file: File, val config: YamlConfiguration = file.loadConfiguration()): ScriptOption {
10+
open class ConfigScriptOption(val file: File? = null, val config: YamlConfiguration = file?.loadConfiguration() ?: YamlConfiguration().also { it.set("main", "main") }): ScriptOption {
1111
override val main: String = config.getString(config.getLowerCaseNode("main"))
1212
override val meta = mutableMapOf<String, String>().also { map ->
13-
config.getStringList(config.getLowerCaseNode("meta")).forEach {
13+
config.getStringList(config.getLowerCaseNode("meta"))?.forEach {
1414
map[it.substringBefore(":")] = it.substringAfter(":")
1515
}
1616
}

FastScript-common/src/main/kotlin/me/scoretwo/fastscript/api/script/custom/CustomScript.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ open class CustomScript(
1313
): Script(description, configOption) {
1414

1515
open fun reload() {
16-
if (!configOption.file.exists()) {
16+
if (configOption.file != null && !configOption.file.exists()) {
1717
configOption.config.save(configOption.file)
1818
}
1919
mergeToTexts()

FastScript-common/src/main/kotlin/me/scoretwo/fastscript/api/script/temp/TempScript.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TempScript(texts : MutableMap<String, String> = mutableMapOf()): Script(Te
2121
return null
2222
}
2323

24-
override fun execute(sign: String, sender: GlobalSender, main: String = option.main, args: Array<Any?> = arrayOf()): Any? {
24+
override fun execute(sign: String, sender: GlobalSender, main: String, args: Array<Any?>): Any? {
2525
for (expansion in FastScript.instance.expansionManager.expansions) {
2626
if (expansion.sign != sign)
2727
continue

FastScript-common/src/main/kotlin/me/scoretwo/fastscript/command/FSCommandNexus.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import me.scoretwo.fastscript.command.commands.ScriptCommand
66
import me.scoretwo.fastscript.languages
77
import me.scoretwo.utils.command.CommandBuilder
88
import me.scoretwo.utils.command.CommandNexus
9+
import me.scoretwo.utils.command.SubCommand
910
import me.scoretwo.utils.command.executor.Executors
1011
import me.scoretwo.utils.command.helper.HelpGenerator
1112
import me.scoretwo.utils.command.language.CommandLanguage
@@ -25,10 +26,13 @@ class FSCommandNexus: CommandNexus(FastScript.instance.plugin, arrayOf("FastScri
2526
override val COMMAND_ONLY_PLAYER = languages["COMMAND-TIPS.COMMAND_ONLY_PLAYER"]
2627
override val COMMAND_UNKNOWN_USAGE = languages["COMMAND-TIPS.COMMAND_UNKNOWN_USAGE"]
2728
}
28-
/*
29-
override var helpGenerator = object : HelpGenerator {
29+
/* override var helpGenerator = object : HelpGenerator {
3030
31-
override fun translateTexts(parents: MutableList<String>, args: MutableList<String>): MutableList<MutableList<TextComponent>> {
31+
override fun translateTexts(
32+
command: SubCommand,
33+
parents: MutableList<String>,
34+
args: MutableList<String>
35+
): MutableList<MutableList<TextComponent>> {
3236
return mutableListOf()
3337
}
3438
Lines changed: 137 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,152 @@
11
package me.scoretwo.fastscript.command.commands
22

3+
import me.scoretwo.fastscript.FastScript
4+
import me.scoretwo.fastscript.api.format.FormatHeader
35
import me.scoretwo.fastscript.command.SimpleCommand
4-
import me.scoretwo.fastscript.plugin
6+
import me.scoretwo.fastscript.sendMessage
57
import me.scoretwo.utils.command.SubCommand
8+
import me.scoretwo.utils.command.executor.CommandExecutor
9+
import me.scoretwo.utils.command.executor.Executors
610
import me.scoretwo.utils.sender.GlobalSender
711

812
class ScriptCommand: SimpleCommand(arrayOf("script")) {
913

10-
override fun execute(sender: GlobalSender, parents: Array<String>, args: Array<String>): Boolean {
11-
if (args.isEmpty()) {
12-
// list script
13-
return true
14-
}
14+
init {
15+
scriptCommand = this
16+
}
1517

16-
if (args[0] == "") {
18+
private val runCommand = nextBuilder()
19+
.alias("run")
20+
.description("执行这个脚本并得到返回值(:s 加在最后不返回消息)")
21+
.executor(object : Executors {
22+
override fun execute(sender: GlobalSender, parents: Array<String>, args: Array<String>): Boolean {
23+
val noReturn = if (args.isEmpty()) false else args[args.size - 1] == ":s"
1724

18-
}
25+
val script = FastScript.instance.scriptManager.scripts[parents[parents.size - 4]] ?: let {
26+
// 似乎不会发生?
27+
sender.sendMessage(FormatHeader.WARN, "没有找到目标脚本 §c${parents[parents.size - 4]}§7! 请检查名称.")
28+
return true
29+
}
30+
31+
val args0: Array<Any?> = when {
32+
args.size < 2 -> arrayOf()
33+
noReturn -> arrayOf(*args.sliceArray(1..args.size - 2))
34+
else -> arrayOf(*args.sliceArray(1 until args.size))
35+
}
36+
37+
val result = when {
38+
args.isEmpty() -> script.execute(parents[parents.size - 3], sender)
39+
args.size >= 2 -> script.execute(parents[parents.size - 3], sender, args[0], args0)
40+
else -> null
41+
}
42+
43+
if (!noReturn) {
44+
sender.sendMessage(FormatHeader.INFO, "脚本 §b${script.name} §7的运行结果: §b${result}")
45+
}
46+
47+
return true
48+
}
49+
50+
override fun tabComplete(sender: GlobalSender, parents: Array<String>, args: Array<String>) = mutableListOf(":s")
51+
})
52+
.build()
53+
54+
private val evaluateCommand = nextBuilder()
55+
.alias("evaluate")
56+
.description("评估这个脚本并获得返回值(:s 加在最后不返回消息)")
57+
.executor(object : Executors {
58+
override fun execute(sender: GlobalSender, parents: Array<String>, args: Array<String>): Boolean {
59+
val noReturn = if (args.isEmpty()) false else args[args.size - 1] == ":s"
60+
61+
val script = FastScript.instance.scriptManager.scripts[parents[parents.size - 4]] ?: let {
62+
// 似乎不会发生?
63+
sender.sendMessage(FormatHeader.WARN, "没有找到目标脚本 §c${parents[parents.size - 4]}§7! 请检查名称.")
64+
return true
65+
}
1966

67+
val result = script.eval(parents[parents.size - 3], sender)
2068

21-
return true
69+
if (!noReturn) {
70+
sender.sendMessage(FormatHeader.INFO, "脚本 §b${script.name} §7的评估结果: §b${result}")
71+
}
72+
73+
return true
74+
}
75+
76+
override fun tabComplete(sender: GlobalSender, parents: Array<String>, args: Array<String>) = mutableListOf(":s")
77+
})
78+
.build()
79+
80+
private val reloadCommand = nextBuilder()
81+
.alias("reload")
82+
.description("重新载入这个脚本")
83+
.execute(object : CommandExecutor {
84+
override fun execute(sender: GlobalSender, parents: Array<String>, args: Array<String>): Boolean {
85+
val script = FastScript.instance.scriptManager.scripts[parents[parents.size - 4]] ?: let {
86+
// 似乎不会发生?
87+
sender.sendMessage(FormatHeader.WARN, "没有找到目标脚本 §c${parents[parents.size - 4]}§7! 请检查名称.")
88+
return true
89+
}
90+
91+
script.reload()
92+
sender.sendMessage(FormatHeader.INFO, "脚本 §b${script.name} §7的配置文件重新载入完成.")
93+
return true
94+
}
95+
})
96+
.build()
97+
98+
private val infoCommand = nextBuilder()
99+
.alias("info")
100+
.description("显示有关这个脚本的信息")
101+
.execute(object : CommandExecutor {
102+
override fun execute(sender: GlobalSender, parents: Array<String>, args: Array<String>): Boolean {
103+
val script = FastScript.instance.scriptManager.scripts[parents[parents.size - 4]] ?: let {
104+
// 似乎不会发生?
105+
sender.sendMessage(FormatHeader.WARN, "没有找到目标脚本 §c${parents[parents.size - 4]}§7! 请检查名称.")
106+
return true
107+
}
108+
109+
sender.sendMessage(FormatHeader.INFO, StringBuilder("脚本 §b${script.name} §7的相关信息:").also { builder ->
110+
script.description.version?.also {
111+
builder.append(" §7版本: §2$it\n")
112+
}
113+
script.description.authors.let {
114+
if (it.isEmpty())
115+
return@let
116+
builder.append(" §7编写者: §3${it.joinToString("§7, §3")}\n")
117+
}
118+
script.description.description?.also {
119+
builder.append(" §7描述: §f$it\n")
120+
}
121+
builder.append(" §7主函数: §a${script.description.main}\n")
122+
script.bindExpansions.let { expansions ->
123+
if (expansions.isEmpty())
124+
return@let
125+
val signs = mutableListOf<String>().also { signs -> expansions.forEach { expansion -> signs.add(expansion.sign) } }
126+
127+
builder.append(" §7拓展挂钩: §6${signs.joinToString("§7, §6")}\n")
128+
}
129+
}.toString())
130+
return true
131+
}
132+
})
133+
.build()
134+
135+
fun reload() {
136+
subCommands.clear()
137+
FastScript.instance.scriptManager.scripts.forEach {
138+
val subCommand = nextBuilder()
139+
.alias(it.value.description.name)
140+
.description("一个自定义脚本")
141+
.subCommand(runCommand)
142+
.subCommand(evaluateCommand)
143+
.subCommand(reloadCommand)
144+
.subCommand(infoCommand)
145+
.build()
146+
subCommands.add(subCommand)
147+
}
22148
}
23149

24150

25-
}
151+
}
152+
lateinit var scriptCommand: ScriptCommand
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package me.scoretwo.fastscript.expansion.javascript
22

33
import me.scoretwo.fastscript.expansion.typeengine.TypeEngineExpansion
4+
import javax.script.ScriptEngine
45

56
class JavaScriptExpansion: TypeEngineExpansion() {
6-
override val name: String = "JavaScript"
7-
override val sign: String = name.toLowerCase()
8-
override val fileSuffix: String = "js"
7+
override val name = "JavaScript"
8+
override val sign = "nashorn"
9+
override val fileSuffix = "js"
10+
override val engine = scriptEngineManager.getEngineByName("js")
911
}

0 commit comments

Comments
 (0)