Skip to content

Commit c542ea8

Browse files
committed
Update details.
1 parent e40d369 commit c542ea8

File tree

17 files changed

+158
-40
lines changed

17 files changed

+158
-40
lines changed

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ import me.scoretwo.fastscript.api.expansion.ExpansionManager
44
import me.scoretwo.fastscript.api.format.FormatHeader
55
import me.scoretwo.fastscript.api.language.LanguageManager
66
import me.scoretwo.fastscript.api.plugin.ScriptPlugin
7-
import me.scoretwo.fastscript.api.script.custom.CustomScript
87
import me.scoretwo.fastscript.command.FSCommandNexus
98
import me.scoretwo.fastscript.config.SettingConfig
109
import me.scoretwo.fastscript.api.script.ScriptManager
10+
import me.scoretwo.fastscript.api.utils.ExecType
11+
import me.scoretwo.fastscript.api.utils.ExecUtils
1112
import me.scoretwo.fastscript.command.commands.ScriptCommand
1213
import me.scoretwo.utils.sender.GlobalPlayer
1314
import me.scoretwo.utils.sender.GlobalSender
1415
import net.md_5.bungee.api.ChatColor
1516

1617
class FastScript(val plugin: ScriptPlugin) {
1718

18-
val commandNexus: FSCommandNexus
19-
val scriptManager: ScriptManager
20-
val expansionManager: ExpansionManager
19+
lateinit var commandNexus: FSCommandNexus
20+
lateinit var scriptManager: ScriptManager
21+
lateinit var expansionManager: ExpansionManager
2122

2223
fun setPlaceholder(player: GlobalPlayer, string: String) = plugin.setPlaceholder(player, string)
2324

@@ -42,21 +43,35 @@ class FastScript(val plugin: ScriptPlugin) {
4243
if (!plugin.dataFolder.exists()) {
4344
plugin.dataFolder.mkdirs()
4445
}
45-
46+
val startTime = System.currentTimeMillis()
4647
settings = SettingConfig()
4748
languages = LanguageManager()
48-
reload("config")
49+
plugin.server.console.sendMessage(FormatHeader.TREE, "Loaded config and language system.§8(${System.currentTimeMillis() - startTime}ms)")
50+
51+
ExecUtils.execPeriod(ExecType.Loaded, "configs") {
52+
reload("config")
53+
}
54+
ExecUtils.execPeriod(ExecType.Initialized, "script manager") {
55+
scriptManager = ScriptManager()
56+
}
4957

50-
commandNexus = FSCommandNexus()
51-
scriptManager = ScriptManager()
52-
expansionManager = ExpansionManager().also {
53-
it.reload()
58+
ExecUtils.execPeriod(ExecType.Initialized, "expansion manager") {
59+
expansionManager = ExpansionManager()
5460
}
61+
ExecUtils.execPeriod(ExecType.Reloaded, "expansion manager") {
62+
expansionManager.reload()
63+
}
64+
65+
ExecUtils.execPeriod(ExecType.Initialized, "CommandNexus") {
66+
commandNexus = FSCommandNexus()
67+
}
68+
69+
stats = ExecType.Initialized
5570
}
5671

5772
fun reloadLanguage() {
5873
languages.current = languages.languages[settings.getString("Options.Language")] ?: languages.defaultLanguage.also {
59-
plugin.server.console.sendMessage(FormatHeader.ERROR, "Language loading failed. The file may not exist. The default language will be used: en_US")
74+
plugin.server.console.sendMessage(FormatHeader.ERROR, "Language loading failed. The file may not exist. The default language will be used: ${it.name}")
6075
}
6176
}
6277

@@ -94,6 +109,7 @@ class FastScript(val plugin: ScriptPlugin) {
94109

95110
companion object {
96111
lateinit var instance: FastScript
112+
var stats = ExecType.Initialize
97113

98114
fun setBootstrap(plugin: ScriptPlugin) {
99115
if (::instance.isInitialized) {
@@ -120,18 +136,18 @@ fun GlobalSender.sendMessage(format: FormatHeader, text: String, color: Boolean
120136
if (format == FormatHeader.DEBUG && !debug)
121137
return
122138
if (!color)
123-
this.sendMessage("${languages["format-header.${format.name}"]}${text}")
139+
this.sendMessage("${format.toLanguageFormat()}${text}")
124140
else
125141
this.sendMessage(
126-
ChatColor.translateAlternateColorCodes('&', "${languages["format-header.${format.name}"]}${text}"))
142+
ChatColor.translateAlternateColorCodes('&', "${format.toLanguageFormat()}${text}"))
127143
}
128144

129145

130146

131147
fun GlobalSender.sendMessage(format: FormatHeader, text: String, placeholders: Map<String, String>) {
132148
if (format == FormatHeader.DEBUG && !debug)
133149
return
134-
this.sendMessage("${languages["format-header.${format.name}"]}$text", placeholders)
150+
this.sendMessage("${format.toLanguageFormat()}$text", placeholders)
135151
}
136152

137153
fun GlobalSender.sendMessage(text: String, placeholders: Map<String, String>) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package me.scoretwo.fastscript.api.expansion
22

3+
import me.scoretwo.fastscript.FastScript
34
import me.scoretwo.fastscript.api.format.FormatHeader
5+
import me.scoretwo.fastscript.api.utils.ExecType
46
import me.scoretwo.fastscript.api.utils.process.ProcessResult
57
import me.scoretwo.fastscript.api.utils.process.ProcessResultType
68
import me.scoretwo.fastscript.expansion.javascript.JavaScriptExpansion
@@ -77,7 +79,9 @@ class ExpansionManager {
7779
plugin.server.console.sendMessage(FormatHeader.ERROR, "An exception occurred when loading extended ${file.name}, reason:\n§8${e.stackTraceToString()}")
7880
}
7981
}
80-
plugin.server.console.sendMessage(FormatHeader.INFO, "Loaded §b$total §7expansions, §a$success §7successes${if (fail == 0) "" else ", §c$fail §7failures"}.§8(${System.currentTimeMillis() - startTime}ms)")
82+
val format = if (FastScript.stats == ExecType.Loaded) FormatHeader.INFO else FormatHeader.TREE
83+
84+
plugin.server.console.sendMessage(format, "Loaded §b$total §7expansions, §a$success §7successes${if (fail == 0) "" else ", §c$fail §7failures"}.§8(${System.currentTimeMillis() - startTime}ms)")
8185
}
8286

8387
private fun fromFileExpansion(file: File): Pair<ProcessResult, FastScriptExpansion?> {
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
package me.scoretwo.fastscript.api.format
22

3-
enum class FormatHeader { INFO, WARN, ERROR, TIPS, HOOKED, DEBUG }
3+
import me.scoretwo.fastscript.languages
4+
5+
enum class FormatHeader {
6+
INFO, WARN, ERROR, TIPS, HOOKED, DEBUG, TREE;
7+
8+
fun toLanguageFormat(): String {
9+
if (this == TREE) {
10+
return " §7├ "
11+
}
12+
13+
return languages["format-header.${name}"]
14+
}
15+
16+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import me.scoretwo.fastscript.api.format.FormatHeader
55
import me.scoretwo.fastscript.api.script.custom.ConfigScriptOption
66
import me.scoretwo.fastscript.api.script.custom.CustomScript
77
import me.scoretwo.fastscript.api.script.temp.TempScript
8+
import me.scoretwo.fastscript.api.utils.ExecType
89
import me.scoretwo.fastscript.api.utils.process.ProcessResult
910
import me.scoretwo.fastscript.api.utils.process.ProcessResultType
1011
import me.scoretwo.fastscript.plugin
@@ -168,7 +169,8 @@ class ScriptManager {
168169
}
169170
}
170171
}
171-
plugin.server.console.sendMessage(FormatHeader.INFO, "Loaded §b$total §7scripts, §a$success §7successes${if (fail == 0) "" else ", §c$fail §7failures"}.§8(${System.currentTimeMillis() - startTime}ms)")
172+
val format = if (FastScript.stats == ExecType.Loaded) FormatHeader.INFO else FormatHeader.TREE
173+
plugin.server.console.sendMessage(format, "Loaded §b$total §7scripts, §a$success §7successes${if (fail == 0) "" else ", §c$fail §7failures"}.§8(${System.currentTimeMillis() - startTime}ms)")
172174
}
173175

174176
fun isConfigScriptOption(section: ConfigurationSection) =
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package me.scoretwo.fastscript.api.utils
2+
3+
/**
4+
* @author Score2
5+
* @date 2021/2/19 13:16
6+
*
7+
* @project FastScript
8+
*/
9+
enum class ExecType {
10+
Load,
11+
Loaded,
12+
Initialize,
13+
Initialized,
14+
Reload,
15+
Reloaded
16+
17+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package me.scoretwo.fastscript.api.utils
2+
3+
import me.scoretwo.fastscript.api.format.FormatHeader
4+
import me.scoretwo.fastscript.plugin
5+
import me.scoretwo.utils.server.task.TaskType
6+
import me.scoretwo.fastscript.*
7+
import me.scoretwo.fastscript.api.utils.process.ProcessResult
8+
import me.scoretwo.fastscript.api.utils.process.ProcessResultType
9+
10+
object ExecUtils {
11+
12+
fun execPeriod(execType: ExecType, execName: String, description: String? = null, taskType: TaskType = TaskType.SYNC, unit: () -> Unit): ProcessResult {
13+
val start = System.currentTimeMillis()
14+
15+
if (taskType == TaskType.ASYNC) {
16+
plugin.server.schedule.task(plugin, taskType) {
17+
try {
18+
unit.invoke()
19+
if (description == null)
20+
plugin.server.console.sendMessage(FormatHeader.TREE, "Async ${execType.name.toLowerCase()} $execName.§8(${System.currentTimeMillis() - start}ms)")
21+
else
22+
plugin.server.console.sendMessage(FormatHeader.TREE, "Async ${execType.name.toLowerCase()} $execName, $description.§8(${System.currentTimeMillis() - start}ms)")
23+
} catch (t: Throwable) {
24+
t.printStackTrace()
25+
plugin.server.console.sendMessage(FormatHeader.TREE, "§cAsync failed to invoke $execName.§8(${System.currentTimeMillis() - start}ms)")
26+
}
27+
}
28+
return ProcessResult(ProcessResultType.SUCCESS)
29+
} else {
30+
return try {
31+
unit.invoke()
32+
if (description == null)
33+
plugin.server.console.sendMessage(FormatHeader.TREE, "${execType.name} $execName.§8(${System.currentTimeMillis() - start}ms)")
34+
else
35+
plugin.server.console.sendMessage(FormatHeader.TREE, "{execType.name} $execName, $description.§8(${System.currentTimeMillis() - start}ms)")
36+
ProcessResult(ProcessResultType.SUCCESS)
37+
} catch (t: Throwable) {
38+
t.printStackTrace()
39+
plugin.server.console.sendMessage(FormatHeader.TREE, "§cFailed to invoke $execName.§8(${System.currentTimeMillis() - start}ms)")
40+
ProcessResult(ProcessResultType.FAILED)
41+
}
42+
}
43+
44+
}
45+
46+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.scoretwo.fastscript.command
22

33
import me.scoretwo.fastscript.FastScript
4+
import me.scoretwo.fastscript.command.commands.ExpansionCommand
45
import me.scoretwo.fastscript.command.commands.ReloadCommand
56
import me.scoretwo.fastscript.command.commands.ScriptCommand
67
import me.scoretwo.fastscript.languages
@@ -18,6 +19,7 @@ class FSCommandNexus: CommandNexus(FastScript.instance.plugin, arrayOf("FastScri
1819
init {
1920
register(ReloadCommand())
2021
register(ScriptCommand())
22+
register(ExpansionCommand())
2123
}
2224

2325
override var language = object : CommandLanguage {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package me.scoretwo.fastscript.command.commands
2+
3+
import me.scoretwo.fastscript.FastScript
4+
import me.scoretwo.fastscript.command.SimpleCommand
5+
import me.scoretwo.utils.command.SubCommand
6+
7+
/**
8+
* @author Score2
9+
* @date 2021/2/19 12:26
10+
*
11+
* @project FastScript
12+
*/
13+
class ExpansionCommand: SimpleCommand(arrayOf("expansion")) {
14+
15+
override var description = "有关拓展的设置."
16+
17+
init {
18+
subCommands.clear()
19+
FastScript.instance.expansionManager.expansions.forEach {
20+
val subCommand = nextBuilder().alias(it.name.toLowerCase(), it.sign.toLowerCase())
21+
.description("有关 ${it.name} 的命令")
22+
.build()
23+
subCommands.add(subCommand)
24+
}
25+
}
26+
27+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class ReloadCommand: SimpleCommand(arrayOf("reload")) {
1616

1717
override var description = "重新载入配置文件或设置."
1818

19-
override var moreArgs: Array<String>? = arrayOf()
20-
2119
override var customCommands: MutableMap<String, Pair<Array<String>?, String>> = mutableMapOf(
2220
"all" to Pair(null, "重新载入所有设置."),
2321
"config" to Pair(null, "重新载入配置文件."),

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import me.scoretwo.utils.command.executor.CommandExecutor
99
import me.scoretwo.utils.command.executor.Executors
1010
import me.scoretwo.utils.sender.GlobalSender
1111

12+
/**
13+
* @author Score2
14+
* @date 2021/1/3 21:42
15+
*
16+
* @project FastScript
17+
*/
1218
class ScriptCommand: SimpleCommand(arrayOf("script")) {
1319

1420
override var description = "操作脚本重载/评估/运行."
1521

16-
override var moreArgs: Array<String>? = arrayOf()
17-
1822
private val runCommand = nextBuilder()
1923
.alias("run")
2024
.description("执行这个脚本并得到返回值(:s 加在最后不返回消息)")

0 commit comments

Comments
 (0)