-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.gradle.kts
217 lines (183 loc) · 6.73 KB
/
build.gradle.kts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import io.papermc.hangarpublishplugin.model.Platforms
import java.net.HttpURLConnection
import java.net.URI
plugins {
kotlin("jvm") version "2.0.21"
id("io.papermc.hangar-publish-plugin") version "0.1.2"
id("com.modrinth.minotaur") version "2.8.7"
id("com.gradleup.shadow") version "8.3.5"
}
group = "dev.jsinco.recipes"
version = "BX3.4.7"
repositories {
mavenCentral()
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
maven("https://repo.jsinco.dev/releases")
}
dependencies {
compileOnly("com.dre.brewery:BreweryX:3.4.5-SNAPSHOT#4")
compileOnly("org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT")
compileOnly("net.luckperms:api:5.4")
}
kotlin {
jvmToolchain(21)
}
tasks {
jar {
enabled = false
}
shadowJar {
archiveClassifier.set("")
}
build {
dependsOn(shadowJar)
}
withType<JavaCompile>().configureEach {
options.encoding = "UTF-8"
}
register("publishRelease") {
println("Publishing a new release to: modrinth and hangar")
finalizedBy(modrinth)
finalizedBy("publishPluginPublicationToHangar")
doLast {
val webhook = DiscordWebhook(System.getenv("DISCORD_WEBHOOK") ?: return@doLast, false)
webhook.message = "@everyone"
webhook.embedTitle = "Recipes - v${project.version}"
webhook.embedDescription = readChangeLog()
webhook.embedThumbnailUrl = "https://cdn.modrinth.com/data/F6Rdllwv/a51de91e8f7dca5303e4055c0d54e2e510efae7d.png"
webhook.send()
}
}
}
hangarPublish {
publications.register("plugin") {
version.set(project.version.toString())
channel.set("Release")
id.set("Recipes-BreweryX-Addon")
apiKey.set(System.getenv("HANGAR_TOKEN") ?: return@register)
platforms {
register(Platforms.PAPER) {
jar.set(tasks.shadowJar.flatMap { it.archiveFile })
platformVersions.set(listOf("1.21.x"))
}
}
changelog.set(readChangeLog())
}
}
modrinth {
projectId.set("breweryrecipesaddon") // This can be the project ID or the slug. Either will work!
versionNumber.set(project.version.toString())
versionType.set("release") // This is the default -- can also be `beta` or `alpha`
uploadFile.set(tasks.shadowJar)
token.set(System.getenv("MODRINTH_TOKEN") ?: return@modrinth)
loaders.addAll("bukkit", "spigot", "paper", "purpur", "folia")
gameVersions.addAll("1.20.5", "1.20.6","1.21", "1.21.1", "1.21.2", "1.21.3", "1.21.4")
changelog.set(readChangeLog())
}
fun readChangeLog(): String {
val text: String = System.getenv("CHANGELOG") ?: file("CHANGELOG.md").run {
if (exists()) readText() else "No Changelog found."
}
return text.replace("\${version}", project.version.toString())
}
class DiscordWebhook(
val webhookUrl: String,
var defaultThumbnail: Boolean = true
) {
var message: String = "content"
var username: String = "BreweryX Updates"
var avatarUrl: String = "https://github.com/breweryteam.png"
var embedTitle: String = "Embed Title"
var embedDescription: String = "Embed Description"
var embedColor: String = "F5E083"
var embedThumbnailUrl: String? = if (defaultThumbnail) avatarUrl else null
var embedImageUrl: String? = null
private fun hexStringToInt(hex: String): Int {
val hexWithoutPrefix = hex.removePrefix("#")
return hexWithoutPrefix.toInt(16)
}
private fun buildToJson(): String {
val json = JsonObject()
json.addProperty("username", username)
json.addProperty("avatar_url", avatarUrl)
json.addProperty("content", message)
val embed = JsonObject()
embed.addProperty("title", embedTitle)
embed.addProperty("description", embedDescription)
embed.addProperty("color", hexStringToInt(embedColor))
embedThumbnailUrl?.let {
val thumbnail = JsonObject()
thumbnail.addProperty("url", it)
embed.add("thumbnail", thumbnail)
}
embedImageUrl?.let {
val image = JsonObject()
image.addProperty("url", it)
embed.add("image", image)
}
val embeds = JsonArray()
createEmbeds().forEach(embeds::add)
json.add("embeds", embeds)
return json.toString()
}
private fun createEmbeds(): List<JsonObject> {
if (embedDescription.length <= 2000) {
return listOf(JsonObject().apply {
addProperty("title", embedTitle)
addProperty("description", embedDescription)
addProperty("color", embedColor.toInt(16))
embedThumbnailUrl?.let {
val thumbnail = JsonObject()
thumbnail.addProperty("url", it)
add("thumbnail", thumbnail)
}
embedImageUrl?.let {
val image = JsonObject()
image.addProperty("url", it)
add("image", image)
}
})
}
val embeds = mutableListOf<JsonObject>()
var description = embedDescription
while (description.isNotEmpty()) {
val chunk = description.substring(0, 2000)
description = description.substring(2000)
embeds.add(JsonObject().apply {
addProperty("title", embedTitle)
addProperty("description", chunk)
addProperty("color", embedColor.toInt(16))
embedThumbnailUrl?.let {
val thumbnail = JsonObject()
thumbnail.addProperty("url", it)
add("thumbnail", thumbnail)
}
embedImageUrl?.let {
val image = JsonObject()
image.addProperty("url", it)
add("image", image)
}
})
}
return embeds
}
fun send() {
val url = URI(webhookUrl).toURL()
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/json")
connection.doOutput = true
connection.outputStream.use { outputStream ->
outputStream.write(buildToJson().toByteArray())
val responseCode = connection.responseCode
println("POST Response Code :: $responseCode")
if (responseCode == HttpURLConnection.HTTP_OK) {
println("Message sent successfully.")
} else {
println("Failed to send message.")
}
}
}
}