Skip to content

Commit

Permalink
Merge pull request #85 from Crazy-Crew/dev
Browse files Browse the repository at this point in the history
A few plugin enhancements here & there.
  • Loading branch information
ryderbelserion committed Feb 6, 2023
2 parents 4ec3372 + 9065e6c commit 0190c41
Show file tree
Hide file tree
Showing 65 changed files with 1,641 additions and 223 deletions.
16 changes: 15 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
.gradle
.idea

build
build

build-logic/.gradle
build-logic/build

core/build

platforms/paper/build
platforms/paper/run/*

!platforms/paper/run/eula.txt
!platforms/paper/run/bukkit.yml
!platforms/paper/run/spigot.yml
!platforms/paper/run/server.properties
!platforms/paper/run/config
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
[![Contact][discord-shield]][discord-url]
![CodeFactor][codefactor-shield]

![Spigot](https://cdn.jsdelivr.net/gh/intergrav/devins-badges/assets/compact/unsupported/spigot_vector.svg)
![Paper](https://cdn.jsdelivr.net/gh/intergrav/devins-badges/assets/compact/supported/paper_vector.svg)
![Purpur](https://cdn.jsdelivr.net/gh/intergrav/devins-badges/assets/compact/supported/purpur_vector.svg)

Expand Down Expand Up @@ -109,4 +108,4 @@ Distributed under the MIT License. See [`LICENSE`](/LICENSE) for more informatio
[license-shield]: https://img.shields.io/github/license/Crazy-Crew/CrazyEnvoys.svg?style=flat&logo=appveyor
[license-url]: https://github.com/Crazy-Crew/CrazyEnvoys/blob/master/LICENSE

[codefactor-shield]: https://img.shields.io/codefactor/grade/github/crazy-crew/CrazyEnvoys/main?style=for-the-badge
[codefactor-shield]: https://img.shields.io/codefactor/grade/github/crazy-crew/CrazyEnvoys/main?style=flat&logo=appveyor
20 changes: 20 additions & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
`kotlin-dsl`
}

repositories {
gradlePluginPortal()
}

dependencies {
implementation(tools.jetbrains)
implementation(tools.license)
implementation(tools.shadowJar)

// For the webhook tasks, this applies to the build-logic only
implementation(tools.ktor.gson)
implementation(tools.ktor.core)
implementation(tools.ktor.cio)
implementation(tools.ktor.cn)
implementation(tools.kotlinx)
}
11 changes: 11 additions & 0 deletions build-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@file:Suppress("UnstableApiUsage")

dependencyResolutionManagement {
versionCatalogs {
create("tools") {
from(files("../gradle/tools.versions.toml"))
}
}

repositories.gradlePluginPortal()
}
28 changes: 28 additions & 0 deletions build-logic/src/main/kotlin/crazyenvoys.base-plugin.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
`java-library`

`maven-publish`

id("com.github.hierynomus.license")

id("com.github.johnrengelman.shadow")
}

license {
header = rootProject.file("LICENSE")
encoding = "UTF-8"

mapping("java", "JAVADOC_STYLE")

include("**/*.java")
}

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(project.properties["java_version"].toString()))
}

tasks {
compileJava {
options.release.set(project.properties["java_version"].toString().toInt())
}
}
12 changes: 12 additions & 0 deletions build-logic/src/main/kotlin/crazyenvoys.paper-plugin.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id("crazyenvoys.root-plugin")
}

repositories {
maven("https://repo.papermc.io/repository/maven-public/")

/**
* NBT API
*/
maven("https://repo.codemc.io/repository/maven-public/")
}
28 changes: 28 additions & 0 deletions build-logic/src/main/kotlin/crazyenvoys.root-plugin.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import task.ReleaseWebhook
import task.WebhookExtension

plugins {
id("crazyenvoys.base-plugin")
}

repositories {
maven("https://repo.triumphteam.dev/snapshots/")

maven("https://repo.crazycrew.us/plugins/")

maven("https://libraries.minecraft.net/")

maven("https://jitpack.io/")

mavenCentral()
}

tasks {
// Creating the extension to be available on the root gradle
val webhookExtension = extensions.create("webhook", WebhookExtension::class)

// Register the task
register<ReleaseWebhook>("webhook") {
extension = webhookExtension
}
}
51 changes: 51 additions & 0 deletions build-logic/src/main/kotlin/task/ReleaseWebhook.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package task

import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.headers
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.append
import io.ktor.serialization.gson.gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction

/** Task to send webhooks to discord. */
abstract class ReleaseWebhook : DefaultTask() {

/** Configured extension. */
@get:Input
lateinit var extension: WebhookExtension

/** Ktor client for easy requests. */
private val client = HttpClient(CIO) {
install(ContentNegotiation) {
gson()
}
}

@TaskAction
fun webhook() {
// The webhook url configured in the gradle.properties
val url = System.getenv("DISCORD_WEBHOOK")

runBlocking(Dispatchers.IO) {
val response = client.post(url) {
headers {
append(HttpHeaders.ContentType, ContentType.Application.Json)
}

setBody(extension.build())
}

// Should be using logger, but eh
println("Webhook result: ${response.status}")
}
}
}
190 changes: 190 additions & 0 deletions build-logic/src/main/kotlin/task/WebhookExtension.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package task

import com.google.gson.annotations.SerializedName
import java.awt.Color
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

/** Extension to simplify customizing the webhook. */
abstract class WebhookExtension {

private var content: String = ""
private var username: String = ""
private var avatar: String = ""
private val embeds: MutableList<Embed> = mutableListOf()

fun content(content: String) {
this.content = content;
}

fun username(username: String) {
this.username = username;
}

fun avatar(avatar: String) {
this.avatar = avatar;
}

fun embeds(builder: EmbedsBuilder.() -> Unit) {
embeds.addAll(EmbedsBuilder().apply(builder).build())
}

internal fun build(): Webhook {
return Webhook(
content,
username,
avatar,
false,
embeds.toList()
)
}

class EmbedsBuilder {
private val embeds: MutableList<Embed> = mutableListOf()

fun embed(builder: EmbedBuilder.() -> Unit) {
embeds.add(EmbedBuilder().apply(builder).build())
}

internal fun build() = embeds.toList()
}

class EmbedBuilder {
private var title: String? = null
private var description: String? = null
private var url: String? = null
private var timestamp: String = ""
private var color: Int? = null
private var footer: Footer? = null
private var image: Image? = null
private var thumbnail: Image? = null
private var provider: Provider? = null
private var author: Author? = null
private var fields: List<Field>? = null

fun title(title: String) {
this.title = title
}

fun description(description: String) {
this.description = description
}

fun url(url: String) {
this.url = url
}

fun timestamp(date: LocalDateTime) {
this.timestamp = date.format(DateTimeFormatter.ISO_DATE_TIME)
}

fun color(color: Color) {
this.color = color.toInt()
}

fun footer(text: String, icon: String? = null) {
this.footer = Footer(text, icon)
}

fun image(url: String) {
this.image = Image(url)
}

fun thumbnail(url: String) {
this.thumbnail = Image(url)
}

fun provider(name: String? = null, url: String? = null) {
this.provider = Provider(name, url)
}

fun author(name: String, url: String? = null, icon: String? = null) {
this.author = Author(name, url, icon)
}

fun fields(builder: FieldsBuilder.() -> Unit) {
this.fields = FieldsBuilder().apply(builder).build()
}

internal fun build() = Embed(
title,
description,
url,
timestamp,
color,
footer,
image,
thumbnail,
provider,
author,
fields,
)
}

class FieldsBuilder {
private val fields: MutableList<Field> = mutableListOf()

fun field(name: String, value: String, inline: Boolean = false) {
fields.add(Field(name, value, inline))
}

internal fun build() = fields.toList()
}

data class Webhook(
val content: String,
val username: String,
@SerializedName("avatar_url") val avatarUrl: String,
val tts: Boolean,
val embeds: List<Embed>,
)

data class Embed(
val title: String?,
val description: String?,
val url: String?,
val timestamp: String,
val color: Int?,
val footer: Footer?,
val image: Image?,
val thumbnail: Image?,
val provider: Provider?,
val author: Author?,
val fields: List<Field>?,
)

data class Image(
val url: String,
)

data class Author(
val name: String,
val url: String?,
@SerializedName("icon_url") val iconUrl: String?,
)

data class Provider(
val name: String?,
val url: String?,
)

data class Footer(
val text: String,
@SerializedName("icon_url") val iconUrl: String?,
)

data class Field(
val name: String,
val value: String,
val inline: Boolean?,
)
}

/** Turns color into integer for webhook, using this because [Color]'s rgb method returns negatives. */
private fun Color.toInt(): Int {
val red = red shl 16 and 0xFF0000
val green = green shl 8 and 0x00FF00
val blue = blue and 0x0000FF

return red or green or blue
}
Loading

0 comments on commit 0190c41

Please sign in to comment.