1.0.0.3 - new modules

Added utility, menu, thirdparty and exposed modules
A few little fixes
This commit is contained in:
redtea committed 2024-03-28 10:12:38 +03:00
1 parent e5a7e08e56
commit 9655d10025
63 files changed
+5334 -12

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
+116
View File
@@ -0,0 +1,116 @@
plugins {
id("java")
kotlin("jvm")
kotlin("plugin.serialization") version "1.8.21"
id("com.github.johnrengelman.shadow") version "7.1.2"
id("maven-publish")
id("signing")
}
group = "tech.carcadex"
val globalArtifactId = "kotlinbukkitkit-thirdparty"
val globalName = "kotlinbukkitkit-thirdparty"
repositories {
mavenCentral()
maven {
name = "spigotmc-repo"
url = uri("https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
}
}
dependencies {
compileOnly("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT")
api(project(":architecture"))
api(project(":utility"))
compileOnly("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
}
tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(8)
}
kotlin {
sourceSets.main {
kotlin.srcDir("build/generated/ksp/main/kotlin")
}
sourceSets.test {
kotlin.srcDir("build/generated/ksp/test/kotlin")
}
}
java {
withJavadocJar()
withSourcesJar()
}
publishing {
publications {
create<MavenPublication>("maven") {
artifactId = globalArtifactId
groupId = "tech.carcadex"
version = version
from(components["java"])
versionMapping {
usage("java-api") {
fromResolutionOf("runtimeClasspath")
}
usage("java-runtime") {
fromResolutionResult()
}
}
pom {
artifactId = globalArtifactId
groupId = "tech.carcadex"
version = version
name.set(globalArtifactId)
description.set(globalArtifactId)
url.set("https://github.com/CarcadeX/KotlinBukkitKit")
licenses {
license {
name.set("MIT")
url.set("https://mit-license.org/")
}
}
developers {
developer {
id.set("itzRedTea")
name.set("Red Tea")
email.set("red__tea@outlook.com")
}
}
scm {
connection.set("scm:https://github.com/CarcadeX/$globalName.git")
developerConnection.set("git@github.com:CarcadeX/$globalName.git")
url.set("https://github.com/CarcadeX/KotlinBukkitKit")
}
}
}
}
repositories {
maven {
val releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
val snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
url = uri(if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl)
credentials {
username = "itzRedTea"
password = project.findProperty("password").toString()
}
}
}
}
signing {
useGpgCmd()
sign(publishing.publications.getByName("maven"))
}
@@ -0,0 +1,55 @@
package tech.carcadex.kotlinbukkitkit.bungeecord
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin
import org.bukkit.plugin.messaging.PluginMessageListener
import java.nio.ByteBuffer
import java.nio.charset.Charset
object BungeeCordController: PluginMessageListener {
private val queue = mutableListOf<BungeeCordRequest>()
private var initialized = false;
private var plugin: Plugin? = null
fun init(plugin: Plugin) {
if(initialized) return
this.plugin = plugin
Bukkit.getServer().messenger.registerOutgoingPluginChannel(plugin, "BungeeCord")
Bukkit.getServer().messenger.registerIncomingPluginChannel(plugin, "BungeeCord", this)
initialized = true
}
fun Plugin.initBungeeCordController() {
init(this)
}
override fun onPluginMessageReceived(channel: String, player: Player, message: ByteArray) {
if (channel != "BungeeCord") return
val buffer = ByteBuffer.wrap(message)
val subChannel = buffer.readUTF()
val request = queue.firstOrNull { it.subChannel == subChannel }
if(request?.responseCallback != null) {
val infoBuffer = buffer.slice()
val info = ByteArray(infoBuffer.remaining())
infoBuffer.get(info)
request.responseCallback.invoke(info)
queue.remove(request)
}
}
fun sendBungeeCord(player: Player, message: ByteArray) {
if(!initialized) {
Bukkit.getLogger().severe("Could not execute sendBungeeCord method, because BungeeCordController" +
" has not been initialized!")
}
player.sendPluginMessage(plugin!!, "BungeeCord", message)
}
fun addToQueue(request: BungeeCordRequest) = queue.add(request)
private fun ByteBuffer.readUTF() = String(ByteArray(short.toInt()).apply { get(this) }, Charset.forName("UTF-8"))
}
@@ -0,0 +1,23 @@
package tech.carcadex.kotlinbukkitkit.bungeecord
import com.google.common.io.ByteStreams
import org.bukkit.entity.Player
typealias ResponseCallback = (message: ByteArray) -> Unit
class BungeeCordRequest(
val player: Player,
val subChannel: String,
val request: ByteArray? = null,
val responseCallback: ResponseCallback? = null
) {
fun send() {
val out = ByteStreams.newDataOutput()
out.writeUTF(subChannel)
if (request != null) out.write(request)
player.sendBungeeCord(out.toByteArray())
if (responseCallback != null) BungeeCordController.addToQueue(this)
}
}
@@ -0,0 +1,58 @@
package tech.carcadex.kotlinbukkitkit.bungeecord
import org.bukkit.entity.Player
import com.google.common.io.ByteStreams
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
// TODO: replace controller with KotlinPlugin lifecycle extension
public val Player.bungeecord get() = BungeeCord(this)
public fun Player.sendBungeeCord(message: ByteArray) = BungeeCordController.sendBungeeCord(this, message)
@JvmInline
public value class BungeeCord(private val player: Player) {
public fun sendToServer(server: String) = BungeeCordRequest(
player,
"Connect",
ByteStreams.newDataOutput().apply{writeUTF(server)}.toByteArray()
).send()
public fun retrieveIp(callback: (address: String, port: Int) -> Unit) = BungeeCordRequest(player, "IP") {
val input = ByteStreams.newDataInput(it)
val ip = input.readUTF()
val port = input.readInt()
callback(ip, port)
}.send()
public suspend fun retrieveIp(): Pair<String, Int> = suspendCoroutine { continuation ->
retrieveIp { address, port -> continuation.resume(address to port) }
}
public fun onlinePlayerAt(server: String = "ALL", callback: (playerCount: Int) -> Unit)
= BungeeCordRequest(player, "PlayerCount", ByteStreams.newDataOutput().apply{writeUTF(server)}.toByteArray()) {
val input = ByteStreams.newDataInput(it)
val serverSendTo = input.readUTF()
val playerCount = input.readInt()
callback(playerCount)
}.send()
public suspend fun onlinePlayerAt(server: String = "ALL"): Int = suspendCoroutine { continuation ->
onlinePlayerAt { continuation.resume(it) }
}
public fun retrieveServerName(callback: (server: String) -> Unit) = BungeeCordRequest(player, "GetServer") {
val input = ByteStreams.newDataInput(it)
val server = input.readUTF()
callback(server)
}.send()
public suspend fun retrieveServerName(): String = suspendCoroutine { continuation ->
retrieveServerName { continuation.resume(it) }
}
public fun kickPlayer(player: String, reason: String) = BungeeCordRequest(this.player, "KickPlayer",
ByteStreams.newDataOutput().apply{writeUTF(player);writeUTF(reason)}.toByteArray()).send()
}