Skip to content

Commit 3ce1c73

Browse files
Feature/main and api validation (#1)
1 parent 2945e68 commit 3ce1c73

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/main/kotlin/net/minecrell/pluginyml/bukkit/BukkitPlugin.kt

+30-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ import org.gradle.api.artifacts.result.ResolvedComponentResult
1616
class BukkitPlugin : PlatformPlugin<BukkitPluginDescription>("Bukkit", "plugin.yml") {
1717

1818
companion object {
19-
@JvmStatic private val VALID_NAME = Regex("^[A-Za-z0-9 _.-]+$")
19+
@JvmStatic
20+
private val VALID_NAME = Regex("^[A-Za-z0-9 _.-]+$")
21+
22+
@JvmStatic
23+
private val VALID_API_VERSION = Regex("^1\\.[0-9]+$")
24+
25+
@JvmStatic
26+
private val INVALID_NAMESPACES =
27+
listOf("net.minecraft.", "org.bukkit.", "io.papermc.", "com.destroystokoyo.paper.", "org.spigotmc")
2028
}
2129

2230
override fun createExtension(project: Project) = BukkitPluginDescription(project)
@@ -36,12 +44,25 @@ class BukkitPlugin : PlatformPlugin<BukkitPluginDescription>("Bukkit", "plugin.y
3644
override fun validate(description: BukkitPluginDescription) {
3745
val name = description.name ?: throw InvalidPluginDescriptionException("Plugin name is not set")
3846
if (!VALID_NAME.matches(name)) throw InvalidPluginDescriptionException("Invalid plugin name: should match $VALID_NAME")
47+
if (description.apiVersion != null) {
48+
val apiVersion = description.apiVersion!!
49+
val splitVersion = apiVersion.split("\\.").map { v -> v.toInt() }
50+
if (splitVersion.size == 2) {
51+
if (!VALID_API_VERSION.matches(apiVersion)) throw InvalidPluginDescriptionException("Invalid api version: should match $VALID_API_VERSION")
52+
if (apiVersion < "1.13") throw InvalidPluginDescriptionException("Invalid api version: should be at least 1.13")
53+
} else if (splitVersion.size == 3) {
54+
if (splitVersion[1] < 20) throw InvalidPluginDescriptionException("Invalid api version: Minor versions are not supported before 1.20.5")
55+
if (splitVersion[1] == 20 && splitVersion[2] < 5) throw InvalidPluginDescriptionException("Invalid api version: Minor versions are not supported before 1.20.5")
56+
} else {
57+
throw InvalidPluginDescriptionException("Invalid api version: $VALID_API_VERSION")
58+
}
59+
}
3960

4061
if (description.version.isNullOrEmpty()) throw InvalidPluginDescriptionException("Plugin version is not set")
4162

4263
val main = description.main ?: throw InvalidPluginDescriptionException("Main class is not defined")
4364
if (main.isEmpty()) throw InvalidPluginDescriptionException("Main class cannot be empty")
44-
if (main.startsWith("org.bukkit.")) throw InvalidPluginDescriptionException("Main may not be within the org.bukkit namespace")
65+
validateNamespace(main, "Main")
4566

4667
for (command in description.commands) {
4768
if (command.name.contains(':')) throw InvalidPluginDescriptionException("Command '${command.name}' cannot contain ':'")
@@ -55,4 +76,11 @@ class BukkitPlugin : PlatformPlugin<BukkitPluginDescription>("Bukkit", "plugin.y
5576
}
5677
}
5778

79+
private fun validateNamespace(namespace: String, name: String) {
80+
for (invalidNamespace in INVALID_NAMESPACES) {
81+
if (namespace.startsWith(invalidNamespace)) {
82+
throw InvalidPluginDescriptionException("$name may not be within the $invalidNamespace namespace")
83+
}
84+
}
85+
}
5886
}

0 commit comments

Comments
 (0)