Skip to content

Commit 698a46f

Browse files
committed
Fixed bugs.
1 parent d23c63e commit 698a46f

File tree

10 files changed

+150
-56
lines changed

10 files changed

+150
-56
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ import me.scoretwo.fastscript.api.script.Script
88
import me.scoretwo.fastscript.command.ScriptCommandNexus
99
import me.scoretwo.fastscript.config.SettingConfig
1010
import me.scoretwo.fastscript.api.script.ScriptManager
11-
import me.scoretwo.fastscript.utils.Utils
1211
import me.scoretwo.utils.sender.GlobalPlayer
1312
import me.scoretwo.utils.sender.GlobalSender
14-
import me.scoretwo.utils.syntaxes.StreamUtils
1513
import net.md_5.bungee.api.ChatColor
16-
import java.io.File
1714

1815
class FastScript(val plugin: ScriptPlugin) {
1916

@@ -54,7 +51,9 @@ class FastScript(val plugin: ScriptPlugin) {
5451

5552
commandNexus = ScriptCommandNexus()
5653
scriptManager = ScriptManager()
57-
expansionManager = ExpansionManager()
54+
expansionManager = ExpansionManager().also {
55+
it.reload()
56+
}
5857
}
5958

6059
/**
@@ -70,7 +69,6 @@ class FastScript(val plugin: ScriptPlugin) {
7069
plugin.dataFolder.mkdirs()
7170
}
7271
plugin.reload()
73-
expansionManager.reload()
7472
initInternalScripts()
7573
scriptManager.loadScripts()
7674
}
@@ -94,20 +92,26 @@ val scripts = mutableListOf<Script>()
9492
lateinit var settings: SettingConfig
9593
lateinit var language: LanguageManager
9694

97-
fun GlobalSender.sendMessage(formatHeader: FormatHeader, strings: Array<String>, colorIndex: Boolean = true) {
98-
strings.forEach {
99-
this.sendMessage(formatHeader, it, colorIndex)
100-
}
95+
fun GlobalSender.sendMessage(format: FormatHeader, texts: Array<String>, color: Boolean = true) = texts.forEach {
96+
this.sendMessage(format, it, color)
10197
}
10298

103-
fun GlobalSender.sendMessage(formatHeader: FormatHeader, string: String, colorIndex: Boolean = true) {
104-
if (colorIndex)
105-
this.sendMessage("${language["format-header.${formatHeader.name}"]}${string}")
99+
fun GlobalSender.sendMessage(format: FormatHeader, text: String, color: Boolean = true) =
100+
if (!color)
101+
this.sendMessage("${language["format-header.${format.name}"]}${text}")
106102
else
107103
this.sendMessage(
108-
ChatColor.translateAlternateColorCodes('&',"${"${language["format-header.${formatHeader.name}"]}${string}"}${string}"))
104+
ChatColor.translateAlternateColorCodes('&', "${language["format-header.${format.name}"]}${text}"))
105+
106+
107+
fun GlobalSender.sendMessage(format: FormatHeader, text: String, placeholders: Map<String, String>) =
108+
this.sendMessage("${language["format-header.${format.name}"]}$text", placeholders)
109+
110+
fun GlobalSender.sendMessage(text: String, placeholders: Map<String, String>) {
111+
var rawText = text
112+
placeholders.forEach {
113+
rawText = rawText.replace(it.key, it.value)
114+
}
109115
}
110116

111-
fun String.setPlaceholder(player: GlobalPlayer): String {
112-
return FastScript.instance.setPlaceholder(player, this)
113-
}
117+
fun String.setPlaceholder(player: GlobalPlayer) = FastScript.instance.setPlaceholder(player, this)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package me.scoretwo.fastscript.api.expansion
2+
3+
import me.scoretwo.utils.bukkit.configuration.yaml.ConfigurationSection
4+
import java.util.*
5+
import kotlin.NullPointerException
6+
import kotlin.jvm.Throws
7+
8+
/**
9+
* @author Score2
10+
* @date 2021/2/4 21:41
11+
*
12+
* @project FastScript
13+
*/
14+
class ExpansionDescription(val name: String, val main: String, val version: String? = null, val authors: Array<String> = arrayOf(), val description: String? = null) {
15+
16+
companion object {
17+
18+
@Throws(NullPointerException::class)
19+
fun readConfig(section: ConfigurationSection) = ExpansionDescription(
20+
section.getString("name") ?: "Expansion-${UUID.randomUUID()}",
21+
section.getString("main") ?: throw NullPointerException("'main' cannot be null!"),
22+
section.getString("version"),
23+
section.getStringList("authors").toTypedArray(),
24+
section.getString("description")
25+
)
26+
}
27+
}

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

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@ package me.scoretwo.fastscript.api.expansion
22

33
import me.scoretwo.fastscript.FastScript
44
import me.scoretwo.fastscript.api.format.FormatHeader
5+
import me.scoretwo.fastscript.api.utils.process.ProcessResult
6+
import me.scoretwo.fastscript.api.utils.process.ProcessResultType
57
import me.scoretwo.fastscript.expansion.javascript.JavaScriptExpansion
8+
import me.scoretwo.fastscript.expansion.typeengine.TypeEngineExpansion
69
import me.scoretwo.fastscript.plugin
710
import me.scoretwo.fastscript.sendMessage
11+
import me.scoretwo.utils.bukkit.configuration.yaml.file.YamlConfiguration
812
import me.scoretwo.utils.syntaxes.findClass
913
import java.io.File
1014
import java.net.URL
1115
import java.net.URLClassLoader
16+
import java.util.jar.JarFile
1217

1318
class ExpansionManager {
1419

1520
val expansionFolder = File(plugin.dataFolder, "expansions")
1621
val expansions = mutableSetOf<FastScriptExpansion>()
22+
val localExpansions = mutableMapOf<ExpansionDescription, FastScriptExpansion>()
1723

1824
init {
1925
register(JavaScriptExpansion())
@@ -29,38 +35,99 @@ class ExpansionManager {
2935
expansions.remove(expansion)
3036
}
3137

38+
fun getExpansion(name: String): FastScriptExpansion? {
39+
for (expansion in expansions) {
40+
if (expansion.name == name)
41+
return expansion
42+
}
43+
return null
44+
}
45+
3246
@Synchronized
3347
fun reload() {
48+
val startTime = System.currentTimeMillis()
3449
expansions.clear()
3550

3651
register(JavaScriptExpansion())
3752

3853
if (!expansionFolder.exists())
3954
expansionFolder.mkdirs()
4055

56+
var success = 0
57+
var fail = 0
58+
var total = 0
59+
4160
for (file in expansionFolder.listFiles() ?: arrayOf()) {
42-
val expansionClass = try {
43-
file.findClass(FastScriptExpansion::class.java)
44-
} catch (e: Exception) {
45-
e.printStackTrace()
46-
plugin.server.console.sendMessage(FormatHeader.ERROR, "An exception occurred when loading extended ${file.name}, the reason:\n${e.message}")
47-
continue
48-
}
61+
total++
62+
val rawExpansion = fromFileExpansion(file)
4963

50-
if (expansionClass == null) {
51-
plugin.server.console.sendMessage(FormatHeader.ERROR, "Unable to load the extension ${file.name} because it has no FastScriptExpansion class available!")
64+
if (rawExpansion.first.type == ProcessResultType.FAILED) {
65+
fail++
5266
continue
5367
}
5468

5569
try {
56-
expansions.add(expansionClass.newInstance())
70+
expansions.add(rawExpansion.second!!)
71+
success++
5772
} catch (e: Exception) {
5873
e.printStackTrace()
59-
plugin.server.console.sendMessage(FormatHeader.ERROR, "An exception occurred when loading extended ${file.name}, the reason:\n${e.message}")
74+
plugin.server.console.sendMessage(FormatHeader.ERROR, "An exception occurred when loading extended ${file.name}, the reason:\n§8${e.stackTraceToString()}")
6075
}
6176
}
77+
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)")
78+
}
79+
80+
private fun fromFileExpansion(file: File): Pair<ProcessResult, FastScriptExpansion?> {
81+
val description: ExpansionDescription
82+
val expansionClass = try {
83+
val url = file.toURI().toURL()
84+
val method = URLClassLoader::class.java.getDeclaredMethod("addURL", URL::class.java)
85+
method.isAccessible = true
86+
method.invoke(plugin.pluginClassLoader, url)
87+
88+
val jarFile = JarFile(file)
89+
description = try {
90+
ExpansionDescription.readConfig(YamlConfiguration().also { it.load(jarFile.getInputStream(jarFile.getJarEntry("expansion.yml")).reader()) })
91+
} catch (e: Exception) {
92+
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()}")
94+
return Pair(ProcessResult(ProcessResultType.FAILED), null)
95+
}
96+
val clazz = try {
97+
Class.forName(description.main)
98+
} catch (e: Exception) {
99+
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()}")
101+
return Pair(ProcessResult(ProcessResultType.FAILED), null)
102+
}
103+
104+
val instance = try {
105+
clazz.newInstance()
106+
} catch (e: Exception) {
107+
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()}")
109+
return Pair(ProcessResult(ProcessResultType.FAILED), null)
110+
}
111+
112+
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.")
114+
return Pair(ProcessResult(ProcessResultType.FAILED), null)
115+
}
62116

117+
clazz.asSubclass(FastScriptExpansion::class.java)
118+
} catch (e: Exception) {
119+
e.printStackTrace()
120+
plugin.server.console.sendMessage(FormatHeader.ERROR, "An exception occurred when loading extended '${file.name}', the reason:\n§8${e.stackTraceToString()}")
121+
return Pair(ProcessResult(ProcessResultType.FAILED), null)
122+
}
63123

124+
if (expansionClass == null) {
125+
plugin.server.console.sendMessage(FormatHeader.ERROR, "Unable to load the extension '${file.name}' because it has no FastScriptExpansion class available!")
126+
return Pair(ProcessResult(ProcessResultType.FAILED), null)
127+
}
128+
return Pair(ProcessResult(ProcessResultType.SUCCESS), expansionClass.newInstance().also {
129+
localExpansions[description] = it
130+
})
64131
}
65132

66133
fun unregister(name: String) = expansions.forEach {

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ class Language {
3030

3131
val config = YamlConfiguration().also {
3232
it.set("FORMAT-HEADER", YamlConfiguration().also {
33-
it.set("INFO", "&7[&2Fast&aScript&7] &bINFO &8| &7")
34-
it.set("WARN", "&7[&2Fast&aScript&7] &eWARN &8| &7")
35-
it.set("ERROR", "&7[&2Fast&aScript&7] &cERROR &8| &7")
36-
it.set("TIPS", "&7[&2Fast&aScript&7] &2TIPS &8| &7")
37-
it.set("HOOKED", "&7[&2Fast&aScript&7] &6HOOKED &8| &7")
38-
it.set("DEBUG", "&7[&2Fast&aScript&7] &3DEBUG &8| &7")
33+
it.set("INFO", "&7[&3Fast&bScript&7] &bINFO &8| &7")
34+
it.set("WARN", "&7[&3Fast&bScript&7] &eWARN &8| &7")
35+
it.set("ERROR", "&7[&3Fast&bScript&7] &cERROR &8| &7")
36+
it.set("TIPS", "&7[&3Fast&bScript&7] &2TIPS &8| &7")
37+
it.set("HOOKED", "&7[&3Fast&bScript&7] &6HOOKED &8| &7")
38+
it.set("DEBUG", "&7[&3Fast&bScript&7] &3DEBUG &8| &7")
3939
})
4040
it.set("COMMAND-SECTIONS", YamlConfiguration().also {
4141
it.set("COMMAND_ONLY_CONSOLE", "This command can only be executed on the console.")
@@ -46,18 +46,10 @@ class Language {
4646

4747
}
4848

49-
operator fun set(node: String, any: Any?) = config.set(
50-
let {
51-
if (config.contains(config.getLowerCaseNode(node))) {
52-
config.getLowerCaseNode(node)
53-
} else {
54-
node.toUpperCase()
55-
}
56-
}
57-
, any)
49+
operator fun set(node: String, any: Any?) = config.set(node.toUpperCase(), any)
5850

59-
operator fun get(node: String) = config.getString(config.getLowerCaseNode(node))!!
51+
operator fun get(node: String) = config.getString(node.toUpperCase())!!
6052

61-
fun getList(node: String) = config.getStringList(config.getLowerCaseNode(node))!!
53+
fun getList(node: String) = config.getString(node.toUpperCase())!!
6254

6355
}

FastScript-common/src/main/kotlin/me/scoretwo/fastscript/api/utils/process/ProcessResult.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package me.scoretwo.fastscript.api.utils.process
22

33
import me.scoretwo.fastscript.api.exception.ProcessException
44

5-
class ProcessResult(type: ProcessResultType, message: String? = null) {
5+
class ProcessResult(val type: ProcessResultType, message: String? = null) {
66

77
var message: String = message ?: type.message
88

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ class ScriptInclude(
5050
constructor.newInstance(*obj.second!!.toTypedArray())
5151
} catch (e: Exception) {
5252
e.printStackTrace()
53-
plugin.server.console.sendMessage(FormatHeader.ERROR, "脚本 §c${script.description.name} §7执行初始化时发生错误, 错误如下:")
54-
plugin.server.console.sendMessage("§7${e.message}")
53+
plugin.server.console.sendMessage(FormatHeader.ERROR, "脚本 §c${script.description.name} §7执行初始化时发生错误, 错误如下:\n§8${e.stackTraceToString()}")
5554
null
5655
}
5756
}
@@ -73,17 +72,15 @@ class ScriptInclude(
7372
Class.forName(target)
7473
} catch (e: ClassNotFoundException) {
7574
e.printStackTrace()
76-
plugin.server.console.sendMessage(FormatHeader.ERROR, "脚本 §c${script.description.name} §7没有找到类 §c${target}§7, 错误如下:")
77-
plugin.server.console.sendMessage("§7${e.message}")
75+
plugin.server.console.sendMessage(FormatHeader.ERROR, "脚本 §c${script.description.name} §7没有找到类 §c${target}§7, 错误如下:\n§8${e.stackTraceToString()}")
7876
null
7977
}
8078

8179
fun accessMethod(script: Script, `object`: Any?, method: Method) = try {
8280
method.invoke(`object`, obj!!.second)
8381
} catch (e: Exception) {
8482
e.printStackTrace()
85-
plugin.server.console.sendMessage(FormatHeader.ERROR, "脚本 §c${script.description.name} §7访问方法 §c${method.name} §7时发生错误, 错误如下:")
86-
plugin.server.console.sendMessage("§7${e.message}")
83+
plugin.server.console.sendMessage(FormatHeader.ERROR, "脚本 §c${script.description.name} §7访问方法 §c${method.name} §7时发生错误, 错误如下:\n§8${e.stackTraceToString()}")
8784
null
8885
}
8986

FastScript-plugin/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
6363
relocate("org.apache","me.scoretwo.utils.shaded.org.apache")
6464
relocate("org.bstats","me.scoretwo.utils.shaded.org.bstats")
6565

66+
exclude("META-INF/versions/9/module-info.class")
67+
exclude("META-INF/*.kotlin_module")
68+
6669
classifier = null
6770
}
6871

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ group = "me.scoretwo"
1212
version = "1.0.1-SNAPSHOT"
1313
description = "FastScript is a Spigot plugin, which can run JavaScript-based scripts more efficiently."
1414

15-
defaultTasks = mutableListOf("publishToMavenLocal")
15+
defaultTasks = mutableListOf("ShadowJar", "publishToMavenLocal")
1616

1717
extra.apply {
1818
set("commonsVersion", "2.0.7-SNAPSHOT")

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import org.spongepowered.api.event.game.state.GamePreInitializationEvent
88
import org.spongepowered.api.plugin.Dependency
99
import org.spongepowered.api.plugin.Plugin
1010
import org.spongepowered.api.event.game.state.GameStoppingEvent
11+
import org.spongepowered.api.event.message.MessageEvent
1112
import org.spongepowered.api.plugin.PluginContainer
1213

1314
@Plugin(
1415
id = "fastscript",
1516
name = "FastScript",
16-
version = "1.0.1-SNAPSHOT",
17-
description = "FastScript is a Spigot plugin, which can run JavaScript-based scripts more efficiently.",
1817
authors = ["Score2"],
1918
dependencies = [Dependency(id = "placeholderapi", optional = true)]
2019
)
@@ -37,4 +36,11 @@ class SpongeBootStrap @Inject constructor(val pluginContainer: PluginContainer)
3736
spongePlugin.disable()
3837
}
3938

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

version-control/FastScript-velocity/src/main/kotlin/me/scoretwo/fastscript/velocity/VelocityBootStrap.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import java.util.logging.Logger
1515
@Plugin(
1616
id = "fastscript",
1717
name = "FastScript",
18-
version = "1.0.1-SNAPSHOT",
19-
description = "FastScript is a Spigot plugin, which can run JavaScript-based scripts more efficiently.",
2018
authors = ["Score2"]
2119
)
2220
class VelocityBootStrap {

0 commit comments

Comments
 (0)