1+ import com.google.gson.JsonArray
2+ import com.google.gson.JsonObject
13import io.papermc.hangarpublishplugin.model.Platforms
4+ import java.net.HttpURLConnection
5+ import java.net.URI
26
37plugins {
48 id(" java" )
@@ -7,7 +11,7 @@ plugins {
711}
812
913group = " dev.jsinco.brewery.garden"
10- version = " BX3.4.6 "
14+ version = " BX3.4.7 "
1115
1216repositories {
1317 mavenCentral()
@@ -16,13 +20,34 @@ repositories {
1620}
1721
1822dependencies {
19- compileOnly(" com.dre.brewery:BreweryX:3.4.6 " )
23+ compileOnly(" com.dre.brewery:BreweryX:3.4.7 " )
2024 compileOnly(" io.papermc.paper:paper-api:1.21.4-R0.1-SNAPSHOT" )
2125
2226 compileOnly(" org.projectlombok:lombok:1.18.30" )
2327 annotationProcessor(" org.projectlombok:lombok:1.18.30" )
2428}
2529
30+
31+ tasks {
32+ withType<JavaCompile > {
33+ options.encoding = " UTF-8"
34+ }
35+
36+ register(" publishRelease" ) {
37+ dependsOn(modrinth)
38+ finalizedBy(" publishPluginPublicationToHangar" )
39+
40+ doLast {
41+ val webhook = DiscordWebhook (System .getenv(" DISCORD_WEBHOOK" ) ? : return @doLast, false )
42+ webhook.message = " @everyone"
43+ webhook.embedTitle = " BreweryGarden - v${project.version} "
44+ webhook.embedDescription = readChangeLog()
45+ webhook.embedThumbnailUrl = " https://cdn.modrinth.com/data/3TaOMjJ9/5e44a541ba38ce5d8567207a4b75183658756d57_96.webp"
46+ webhook.send()
47+ }
48+ }
49+ }
50+
2651java {
2752 toolchain.languageVersion = JavaLanguageVersion .of(21 )
2853}
@@ -32,7 +57,7 @@ hangarPublish {
3257 version.set(project.version.toString())
3358 channel.set(" Release" )
3459 id.set(project.name)
35- apiKey.set(System .getenv(" HANGAR_TOKEN" ))
60+ apiKey.set(System .getenv(" HANGAR_TOKEN" ) ? : return @register )
3661 platforms {
3762 register(Platforms .PAPER ) {
3863 jar.set(tasks.jar.flatMap { it.archiveFile })
@@ -44,10 +69,10 @@ hangarPublish {
4469}
4570
4671modrinth {
47- token.set(System .getenv(" MODRINTH_TOKEN" ))
48- projectId.set(project.name) // This can be the project ID or the slug. Either will work!
72+ token.set(System .getenv(" MODRINTH_TOKEN" ) ? : return @modrinth )
73+ projectId.set(project.name.lowercase())
4974 versionNumber.set(project.version.toString())
50- versionType.set(" release" ) // This is the default -- can also be `beta` or `alpha`
75+ versionType.set(" release" )
5176 uploadFile.set(tasks.jar)
5277 loaders.addAll(" paper" , " purpur" , " folia" )
5378 gameVersions.addAll(" 1.21.3" , " 1.21.4" )
@@ -59,4 +84,115 @@ fun readChangeLog(): String {
5984 if (exists()) readText() else " No Changelog found."
6085 }
6186 return text.replace(" \$ {version}" , project.version.toString())
87+ }
88+
89+ class DiscordWebhook (
90+ val webhookUrl : String ,
91+ var defaultThumbnail : Boolean = true
92+ ) {
93+
94+ var message: String = " content"
95+ var username: String = " BreweryX Updates"
96+ var avatarUrl: String = " https://github.com/breweryteam.png"
97+ var embedTitle: String = " Embed Title"
98+ var embedDescription: String = " Embed Description"
99+ var embedColor: String = " F5E083"
100+ var embedThumbnailUrl: String? = if (defaultThumbnail) avatarUrl else null
101+ var embedImageUrl: String? = null
102+
103+ private fun hexStringToInt (hex : String ): Int {
104+ val hexWithoutPrefix = hex.removePrefix(" #" )
105+ return hexWithoutPrefix.toInt(16 )
106+ }
107+
108+ private fun buildToJson (): String {
109+ val json = JsonObject ()
110+ json.addProperty(" username" , username)
111+ json.addProperty(" avatar_url" , avatarUrl)
112+ json.addProperty(" content" , message)
113+
114+ val embed = JsonObject ()
115+ embed.addProperty(" title" , embedTitle)
116+ embed.addProperty(" description" , embedDescription)
117+ embed.addProperty(" color" , hexStringToInt(embedColor))
118+
119+ embedThumbnailUrl?.let {
120+ val thumbnail = JsonObject ()
121+ thumbnail.addProperty(" url" , it)
122+ embed.add(" thumbnail" , thumbnail)
123+ }
124+
125+ embedImageUrl?.let {
126+ val image = JsonObject ()
127+ image.addProperty(" url" , it)
128+ embed.add(" image" , image)
129+ }
130+
131+ val embeds = JsonArray ()
132+ createEmbeds().forEach(embeds::add)
133+
134+ json.add(" embeds" , embeds)
135+ return json.toString()
136+ }
137+
138+ private fun createEmbeds (): List <JsonObject > {
139+ if (embedDescription.length <= 2000 ) {
140+ return listOf (JsonObject ().apply {
141+ addProperty(" title" , embedTitle)
142+ addProperty(" description" , embedDescription)
143+ addProperty(" color" , embedColor.toInt(16 ))
144+ embedThumbnailUrl?.let {
145+ val thumbnail = JsonObject ()
146+ thumbnail.addProperty(" url" , it)
147+ add(" thumbnail" , thumbnail)
148+ }
149+ embedImageUrl?.let {
150+ val image = JsonObject ()
151+ image.addProperty(" url" , it)
152+ add(" image" , image)
153+ }
154+ })
155+ }
156+ val embeds = mutableListOf<JsonObject >()
157+ var description = embedDescription
158+ while (description.isNotEmpty()) {
159+ val chunk = description.substring(0 , 2000 )
160+ description = description.substring(2000 )
161+ embeds.add(JsonObject ().apply {
162+ addProperty(" title" , embedTitle)
163+ addProperty(" description" , chunk)
164+ addProperty(" color" , embedColor.toInt(16 ))
165+ embedThumbnailUrl?.let {
166+ val thumbnail = JsonObject ()
167+ thumbnail.addProperty(" url" , it)
168+ add(" thumbnail" , thumbnail)
169+ }
170+ embedImageUrl?.let {
171+ val image = JsonObject ()
172+ image.addProperty(" url" , it)
173+ add(" image" , image)
174+ }
175+ })
176+ }
177+ return embeds
178+ }
179+
180+ fun send () {
181+ val url = URI (webhookUrl).toURL()
182+ val connection = url.openConnection() as HttpURLConnection
183+ connection.requestMethod = " POST"
184+ connection.setRequestProperty(" Content-Type" , " application/json" )
185+ connection.doOutput = true
186+ connection.outputStream.use { outputStream ->
187+ outputStream.write(buildToJson().toByteArray())
188+
189+ val responseCode = connection.responseCode
190+ println (" POST Response Code :: $responseCode " )
191+ if (responseCode == HttpURLConnection .HTTP_OK ) {
192+ println (" Message sent successfully." )
193+ } else {
194+ println (" Failed to send message." )
195+ }
196+ }
197+ }
62198}
0 commit comments