Skip to content

Commit 358a7db

Browse files
committed
Update and Fixed bugs.
1 parent 698a46f commit 358a7db

File tree

8 files changed

+102
-51
lines changed

8 files changed

+102
-51
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import me.scoretwo.fastscript.api.format.FormatHeader
55
import me.scoretwo.fastscript.api.language.LanguageManager
66
import me.scoretwo.fastscript.api.plugin.ScriptPlugin
77
import me.scoretwo.fastscript.api.script.Script
8-
import me.scoretwo.fastscript.command.ScriptCommandNexus
8+
import me.scoretwo.fastscript.command.FSCommandNexus
99
import me.scoretwo.fastscript.config.SettingConfig
1010
import me.scoretwo.fastscript.api.script.ScriptManager
1111
import me.scoretwo.utils.sender.GlobalPlayer
@@ -14,7 +14,7 @@ import net.md_5.bungee.api.ChatColor
1414

1515
class FastScript(val plugin: ScriptPlugin) {
1616

17-
val commandNexus: ScriptCommandNexus
17+
val commandNexus: FSCommandNexus
1818
val scriptManager: ScriptManager
1919
val expansionManager: ExpansionManager
2020

@@ -43,13 +43,14 @@ class FastScript(val plugin: ScriptPlugin) {
4343
}
4444

4545
settings = SettingConfig()
46+
settings.reload()
4647

47-
language = LanguageManager()
48-
language.current = language.languages[settings.getString("Options.Language")] ?: language.defaultLanguage.also {
48+
languages = LanguageManager()
49+
languages.current = languages.languages[settings.getString("Options.Language")] ?: languages.defaultLanguage.also {
4950
plugin.server.console.sendMessage(FormatHeader.ERROR, "Language loading failed. The file may not exist. The default language will be used: en_US")
5051
}
5152

52-
commandNexus = ScriptCommandNexus()
53+
commandNexus = FSCommandNexus()
5354
scriptManager = ScriptManager()
5455
expansionManager = ExpansionManager().also {
5556
it.reload()
@@ -68,6 +69,7 @@ class FastScript(val plugin: ScriptPlugin) {
6869
if (!plugin.dataFolder.exists()) {
6970
plugin.dataFolder.mkdirs()
7071
}
72+
settings.reload()
7173
plugin.reload()
7274
initInternalScripts()
7375
scriptManager.loadScripts()
@@ -86,26 +88,35 @@ class FastScript(val plugin: ScriptPlugin) {
8688
}
8789

8890
}
91+
var debug = false
92+
8993
lateinit var plugin: ScriptPlugin
9094
val scripts = mutableListOf<Script>()
9195

9296
lateinit var settings: SettingConfig
93-
lateinit var language: LanguageManager
97+
lateinit var languages: LanguageManager
9498

9599
fun GlobalSender.sendMessage(format: FormatHeader, texts: Array<String>, color: Boolean = true) = texts.forEach {
96100
this.sendMessage(format, it, color)
97101
}
98102

99-
fun GlobalSender.sendMessage(format: FormatHeader, text: String, color: Boolean = true) =
103+
fun GlobalSender.sendMessage(format: FormatHeader, text: String, color: Boolean = true) {
104+
if (format == FormatHeader.DEBUG && !debug)
105+
return
100106
if (!color)
101-
this.sendMessage("${language["format-header.${format.name}"]}${text}")
107+
this.sendMessage("${languages["format-header.${format.name}"]}${text}")
102108
else
103109
this.sendMessage(
104-
ChatColor.translateAlternateColorCodes('&', "${language["format-header.${format.name}"]}${text}"))
110+
ChatColor.translateAlternateColorCodes('&', "${languages["format-header.${format.name}"]}${text}"))
111+
}
105112

106113

107-
fun GlobalSender.sendMessage(format: FormatHeader, text: String, placeholders: Map<String, String>) =
108-
this.sendMessage("${language["format-header.${format.name}"]}$text", placeholders)
114+
115+
fun GlobalSender.sendMessage(format: FormatHeader, text: String, placeholders: Map<String, String>) {
116+
if (format == FormatHeader.DEBUG && !debug)
117+
return
118+
this.sendMessage("${languages["format-header.${format.name}"]}$text", placeholders)
119+
}
109120

110121
fun GlobalSender.sendMessage(text: String, placeholders: Map<String, String>) {
111122
var rawText = text

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

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

66
abstract class Config(val file: File) : YamlConfiguration() {
77

8-
init {
9-
reload()
10-
}
11-
128
open fun reload() {
139
if (!file.exists()) {
1410
file.parentFile.mkdirs()

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class ExpansionManager {
7171
success++
7272
} catch (e: Exception) {
7373
e.printStackTrace()
74-
plugin.server.console.sendMessage(FormatHeader.ERROR, "An exception occurred when loading extended ${file.name}, the reason:\n§8${e.stackTraceToString()}")
74+
plugin.server.console.sendMessage(FormatHeader.ERROR, "An exception occurred when loading extended ${file.name}, reason:\n§8${e.stackTraceToString()}")
7575
}
7676
}
7777
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)")
@@ -90,34 +90,34 @@ class ExpansionManager {
9090
ExpansionDescription.readConfig(YamlConfiguration().also { it.load(jarFile.getInputStream(jarFile.getJarEntry("expansion.yml")).reader()) })
9191
} catch (e: Exception) {
9292
e.printStackTrace()
93-
plugin.server.console.sendMessage(FormatHeader.ERROR, "An error occurred while loading the expansion '${file.name}' description file, the reason:\n§8${e.stackTraceToString()}")
93+
plugin.server.console.sendMessage(FormatHeader.ERROR, "An error occurred while loading the expansion '${file.name}' description file, reason:\n§8${e.stackTraceToString()}")
9494
return Pair(ProcessResult(ProcessResultType.FAILED), null)
9595
}
9696
val clazz = try {
9797
Class.forName(description.main)
9898
} catch (e: Exception) {
9999
e.printStackTrace()
100-
plugin.server.console.sendMessage(FormatHeader.ERROR, "An error occurred while loading the main class ${description.main} of expansion '${file.name}', the reason:\n§8${e.stackTraceToString()}")
100+
plugin.server.console.sendMessage(FormatHeader.ERROR, "An error occurred while loading the main class ${description.main} of expansion '${file.name}', reason:\n§8${e.stackTraceToString()}")
101101
return Pair(ProcessResult(ProcessResultType.FAILED), null)
102102
}
103103

104104
val instance = try {
105105
clazz.newInstance()
106106
} catch (e: Exception) {
107107
e.printStackTrace()
108-
plugin.server.console.sendMessage(FormatHeader.ERROR, "An error occurred while loading the main class ${description.main} of expansion '${file.name}', the reason:\n§8${e.stackTraceToString()}")
108+
plugin.server.console.sendMessage(FormatHeader.ERROR, "An error occurred while loading the main class ${description.main} of expansion '${file.name}', reason:\n§8${e.stackTraceToString()}")
109109
return Pair(ProcessResult(ProcessResultType.FAILED), null)
110110
}
111111

112112
if (instance !is FastScriptExpansion) {
113-
plugin.server.console.sendMessage(FormatHeader.ERROR, "An error occurred while loading the main class ${description.main} of expansion '${file.name}', the reason: §cThe main class does not depend on FastScriptExpansion.")
113+
plugin.server.console.sendMessage(FormatHeader.ERROR, "An error occurred while loading the main class ${description.main} of expansion '${file.name}', reason: §cThe main class does not depend on FastScriptExpansion.")
114114
return Pair(ProcessResult(ProcessResultType.FAILED), null)
115115
}
116116

117117
clazz.asSubclass(FastScriptExpansion::class.java)
118118
} catch (e: Exception) {
119119
e.printStackTrace()
120-
plugin.server.console.sendMessage(FormatHeader.ERROR, "An exception occurred when loading extended '${file.name}', the reason:\n§8${e.stackTraceToString()}")
120+
plugin.server.console.sendMessage(FormatHeader.ERROR, "An exception occurred when loading extended '${file.name}', reason:\n§8${e.stackTraceToString()}")
121121
return Pair(ProcessResult(ProcessResultType.FAILED), null)
122122
}
123123

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Language {
3737
it.set("HOOKED", "&7[&3Fast&bScript&7] &6HOOKED &8| &7")
3838
it.set("DEBUG", "&7[&3Fast&bScript&7] &3DEBUG &8| &7")
3939
})
40-
it.set("COMMAND-SECTIONS", YamlConfiguration().also {
40+
it.set("COMMAND-TIPS", YamlConfiguration().also {
4141
it.set("COMMAND_ONLY_CONSOLE", "This command can only be executed on the console.")
4242
it.set("COMMAND_ONLY_PLAYER", "This command can only be executed by the player.")
4343
it.set("COMMAND_NO_PERMISSION", "You do not have permission to execute the command.")

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

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

33
import me.scoretwo.fastscript.FastScript
4+
import me.scoretwo.fastscript.api.format.FormatHeader
45
import me.scoretwo.fastscript.api.utils.process.ProcessResult
56
import me.scoretwo.fastscript.api.utils.process.ProcessResultType
67
import me.scoretwo.fastscript.plugin
8+
import me.scoretwo.fastscript.sendMessage
79
import me.scoretwo.fastscript.settings
810
import me.scoretwo.utils.bukkit.configuration.yaml.ConfigurationSection
911
import me.scoretwo.utils.bukkit.configuration.yaml.patchs.getLowerCaseNode
@@ -20,16 +22,16 @@ class ScriptManager {
2022
/**
2123
* 仅接受文件后缀为yml的文件或者可用的脚本文件夹才能被处理
2224
*/
23-
private fun loadScript(file: File): ProcessResult {
24-
if (file.name.contains(" ")) return ProcessResult(ProcessResultType.FAILED, "File name cannot contain spaces!")
25+
private fun loadScript(file: File): Pair<Script?, ProcessResult> {
26+
if (file.name.contains(" ")) return Pair(null, ProcessResult(ProcessResultType.FAILED, "File name cannot contain spaces!"))
2527
if (file.isDirectory) {
2628
return loadFromFolderScript(file)
2729
}
2830

29-
val scriptName= if (file.name.endsWith(".yml"))
31+
val scriptName = if (file.name.endsWith(".yml"))
3032
file.name.substringBeforeLast(".")
3133
else
32-
return ProcessResult(ProcessResultType.OTHER, "The file does not belong to the script, skip reading!")
34+
return Pair(null, ProcessResult(ProcessResultType.OTHER, "The file does not belong to the script, skip reading!"))
3335

3436
val options = ScriptOptions(file)
3537
val script = Script(ScriptDescription.fromSection(options.config), options)
@@ -43,11 +45,16 @@ class ScriptManager {
4345
}
4446
}
4547

48+
script.scriptProcessor.forEach {
49+
if (it.value.needEval)
50+
script.eval(it.key, plugin.server.console)
51+
}
52+
4653
scripts[file.name.substringBeforeLast(".")] = script
47-
return ProcessResult(ProcessResultType.SUCCESS)
54+
return Pair(script, ProcessResult(ProcessResultType.SUCCESS))
4855
}
4956

50-
private fun loadFromFolderScript(folder: File): ProcessResult {
57+
private fun loadFromFolderScript(folder: File): Pair<Script?, ProcessResult> {
5158
val optionsFiles = arrayOf("option.yml", "${folder.name}.yml", "setting.yml")
5259

5360
val optionsFile: File = optionsFiles.let {
@@ -56,7 +63,7 @@ class ScriptManager {
5663
if (file.exists()) return@let file
5764
}
5865

59-
return ProcessResult(ProcessResultType.FAILED, "Option file not found in ${folder.name}.")
66+
return Pair(null, ProcessResult(ProcessResultType.FAILED, "Option file not found in ${folder.name}."))
6067
}
6168
val options = ScriptOptions(optionsFile)
6269
val script = Script(ScriptDescription.fromSection(options.config), options)
@@ -68,9 +75,15 @@ class ScriptManager {
6875
}
6976
}
7077
}
78+
79+
script.scriptProcessor.forEach {
80+
if (it.value.needEval)
81+
script.eval(it.key, plugin.server.console)
82+
}
83+
7184
scripts[folder.name] = script
7285

73-
return ProcessResult(ProcessResultType.SUCCESS)
86+
return Pair(script, ProcessResult(ProcessResultType.SUCCESS))
7487
}
7588
// {
7689
// scripts.add(CustomScript(file))
@@ -81,16 +94,31 @@ class ScriptManager {
8194
*/
8295
@Synchronized
8396
fun loadScripts() {
97+
val startTime = System.currentTimeMillis()
8498
scripts.clear()
8599
folders[0].mkdirs()
86100
folders[0].listFiles()?.forEach { loadScript(it) }
87101

102+
var total = 0
103+
var success = 0
104+
var fail = 0
105+
88106
settings.getStringList(settings.getLowerCaseNode("load-script-files")).forEach {
89107
val file = File(it)
90108

91-
if (file.isDirectory && file.exists()) file.listFiles()?.forEach { loadScript(it) }
92-
109+
if (file.isDirectory && file.exists()) file.listFiles()?.forEach {
110+
loadScript(it).also {
111+
total++
112+
if (it.second.type == ProcessResultType.FAILED || it.first == null) {
113+
fail++
114+
plugin.server.console.sendMessage(FormatHeader.ERROR, "An error occurred while loading script ${file.name}, reason: §8${it.second.message}")
115+
}
116+
else if (it.second.type == ProcessResultType.SUCCESS)
117+
success++
118+
}
119+
}
93120
}
121+
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)")
94122
}
95123

96124
fun isConfigScriptOption(section: ConfigurationSection) =
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package me.scoretwo.fastscript.command
2+
3+
import me.scoretwo.fastscript.FastScript
4+
import me.scoretwo.fastscript.languages
5+
import me.scoretwo.utils.command.CommandBuilder
6+
import me.scoretwo.utils.command.CommandNexus
7+
import me.scoretwo.utils.command.helper.HelpGenerator
8+
import me.scoretwo.utils.command.language.CommandLanguage
9+
import net.md_5.bungee.api.chat.TextComponent
10+
11+
class FSCommandNexus: CommandNexus(FastScript.instance.plugin, arrayOf("FastScript", "script", "fs")) {
12+
13+
override var language = object : CommandLanguage {
14+
override val COMMAND_NO_PERMISSION = languages["COMMAND-TIPS.COMMAND_NO_PERMISSION"]
15+
override val COMMAND_ONLY_CONSOLE = languages["COMMAND-TIPS.COMMAND_ONLY_CONSOLE"]
16+
override val COMMAND_ONLY_PLAYER = languages["COMMAND-TIPS.COMMAND_ONLY_PLAYER"]
17+
override val COMMAND_UNKNOWN_USAGE = languages["COMMAND-TIPS.COMMAND_UNKNOWN_USAGE"]
18+
}
19+
20+
override var helpGenerator = object : HelpGenerator {
21+
override val description: HelpGenerator.Companion.Description
22+
get() = TODO("Not yet implemented")
23+
24+
override fun translateTexts(parents: MutableList<String>, args: MutableList<String>): MutableList<MutableList<TextComponent>> {
25+
TODO("Not yet implemented")
26+
}
27+
28+
}
29+
30+
init {
31+
registerBuilder()
32+
.alias("reload")
33+
}
34+
35+
}

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

version-control/FastScript-sponge/src/main/kotlin/me/scoretwo/fastscript/sponge/SpongeBootStrap.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,4 @@ class SpongeBootStrap @Inject constructor(val pluginContainer: PluginContainer)
3636
spongePlugin.disable()
3737
}
3838

39-
@Listener
40-
fun execute(e: MessageEvent) {
41-
if (e.message.toPlain().contains("fastscript")) {
42-
e.isMessageCancelled = true
43-
}
44-
}
45-
4639
}

0 commit comments

Comments
 (0)