This repository was archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathBukkitPlugin.kt
92 lines (79 loc) · 4.56 KB
/
BukkitPlugin.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Minecrell <https://github.com/Minecrell>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.minecrell.pluginyml.bukkit
import net.minecrell.pluginyml.InvalidPluginDescriptionException
import net.minecrell.pluginyml.PlatformPlugin
import net.minecrell.pluginyml.collectLibraries
import org.gradle.api.Project
import org.gradle.api.artifacts.result.ResolvedComponentResult
class BukkitPlugin : PlatformPlugin<BukkitPluginDescription>("Bukkit", "plugin.yml") {
companion object {
@JvmStatic
private val VALID_NAME = Regex("^[A-Za-z0-9 _.-]+$")
@JvmStatic
private val VALID_API_VERSION = Regex("^1\\.[0-9]+$")
@JvmStatic
private val INVALID_NAMESPACES = listOf("net.minecraft.", "org.bukkit.", "io.papermc.", "com.destroystokoyo.paper.", "org.spigotmc")
}
override fun createExtension(project: Project) = BukkitPluginDescription(project)
override fun setDefaults(project: Project, description: BukkitPluginDescription) {
description.name = description.name ?: project.name
description.version = description.version ?: project.version.toString()
description.description = description.description ?: project.description
description.website = description.website ?: project.findProperty("url")?.toString()
description.author = description.author ?: project.findProperty("author")?.toString()
}
override fun setLibraries(libraries: ResolvedComponentResult?, description: BukkitPluginDescription) {
description.libraries = libraries.collectLibraries(description.libraries)
}
override fun validate(description: BukkitPluginDescription) {
val name = description.name ?: throw InvalidPluginDescriptionException("Plugin name is not set")
if (!VALID_NAME.matches(name)) throw InvalidPluginDescriptionException("Invalid plugin name: should match $VALID_NAME")
if (description.apiVersion != null) {
val apiVersion = description.apiVersion!!
if (!VALID_API_VERSION.matches(apiVersion)) throw InvalidPluginDescriptionException("Invalid api version: should match $VALID_API_VERSION")
if (apiVersion < "1.13") throw InvalidPluginDescriptionException("Invalid api version: should be at least 1.13")
}
if (description.version.isNullOrEmpty()) throw InvalidPluginDescriptionException("Plugin version is not set")
val main = description.main ?: throw InvalidPluginDescriptionException("Main class is not defined")
if (main.isEmpty()) throw InvalidPluginDescriptionException("Main class cannot be empty")
validateNamespace(main, "Main")
for (command in description.commands) {
if (command.name.contains(':')) throw InvalidPluginDescriptionException("Command '${command.name}' cannot contain ':'")
command.aliases?.forEach { alias ->
if (alias.contains(':')) throw InvalidPluginDescriptionException("Alias '$alias' of '${command.name}' cannot contain ':'")
}
}
if (description.provides?.all(VALID_NAME::matches) == false) {
throw InvalidPluginDescriptionException("Invalid plugin provides name: all should match $VALID_NAME")
}
}
private fun validateNamespace(namespace: String, name: String) {
for (invalidNamespace in INVALID_NAMESPACES) {
if (namespace.startsWith(invalidNamespace)) {
throw InvalidPluginDescriptionException("$name may not be within the $invalidNamespace namespace")
}
}
}
}