From 8f866aefd0ef51a7446f0186d3c796fbafdce8b5 Mon Sep 17 00:00:00 2001 From: CoasterFreakDE Date: Wed, 4 Sep 2024 14:53:17 +0200 Subject: [PATCH] feat(#1): ShaderStatusChangedEvent closes #1 --- build.gradle.kts | 2 ++ .../com/liamxsage/shaderapi/ShaderAPI.kt | 9 ++++++-- .../com/liamxsage/shaderapi/Variables.kt | 7 +++--- .../events/ShaderStatusChangedEvent.kt | 21 +++++++++++++++++ .../shaderapi/listeners/ShaderAPIListener.kt | 23 +++++++++++++++---- .../listeners/ShaderStatusChangeListener.kt | 15 ++++++++++++ 6 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 src/main/kotlin/com/liamxsage/shaderapi/events/ShaderStatusChangedEvent.kt create mode 100644 src/main/kotlin/com/liamxsage/shaderapi/listeners/ShaderStatusChangeListener.kt diff --git a/build.gradle.kts b/build.gradle.kts index 7182098..c82b848 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -22,6 +22,8 @@ val ktorVersion: String by project val mcCoroutineVersion: String by project repositories { + mavenLocal() + maven("https://repo.papermc.io/repository/maven-public/") maven("https://nexus.flawcra.cc/repository/maven-mirrors/") } diff --git a/src/main/kotlin/com/liamxsage/shaderapi/ShaderAPI.kt b/src/main/kotlin/com/liamxsage/shaderapi/ShaderAPI.kt index 787fb64..060820c 100644 --- a/src/main/kotlin/com/liamxsage/shaderapi/ShaderAPI.kt +++ b/src/main/kotlin/com/liamxsage/shaderapi/ShaderAPI.kt @@ -1,6 +1,7 @@ package com.liamxsage.shaderapi import com.liamxsage.shaderapi.listeners.ShaderAPIListener +import com.liamxsage.shaderapi.listeners.ShaderStatusChangeListener import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin import org.bukkit.plugin.messaging.Messenger @@ -33,8 +34,12 @@ class ShaderAPI : JavaPlugin() { // Plugin startup logic val time = measureTimeMillis { val messenger: Messenger = Bukkit.getMessenger() - messenger.registerIncomingPluginChannel(this, SHADERAPI_INCOMING_CHANNEL, ShaderAPIListener()) - messenger.registerOutgoingPluginChannel(this, SHADERAPI_OUTGOING_CHANNEL) + val listener = ShaderAPIListener() + messenger.registerIncomingPluginChannel(this, SHADERAPI_REQUEST_CHANNEL, listener) + messenger.registerIncomingPluginChannel(this, SHADERAPI_STATUS_CHANNEL, listener) + messenger.registerOutgoingPluginChannel(this, SHADERAPI_SEND_DATA_CHANNEL) + + Bukkit.getPluginManager().registerEvents(ShaderStatusChangeListener(), this) } println("Plugin enabled in $time ms") } diff --git a/src/main/kotlin/com/liamxsage/shaderapi/Variables.kt b/src/main/kotlin/com/liamxsage/shaderapi/Variables.kt index 9cbfa1d..bf39487 100644 --- a/src/main/kotlin/com/liamxsage/shaderapi/Variables.kt +++ b/src/main/kotlin/com/liamxsage/shaderapi/Variables.kt @@ -2,6 +2,7 @@ package com.liamxsage.shaderapi import net.minecraft.resources.ResourceLocation -const val SHADERAPI_INCOMING_CHANNEL = "shaderapi:request_shader_url" -const val SHADERAPI_OUTGOING_CHANNEL = "shaderapi:receive_shader_url" -val SHADERAPI_RESOURCELOCATION = ResourceLocation.parse(SHADERAPI_OUTGOING_CHANNEL) \ No newline at end of file +const val SHADERAPI_REQUEST_CHANNEL = "shaderapi:request_shader_url" +const val SHADERAPI_STATUS_CHANNEL = "shaderapi:status_response" +const val SHADERAPI_SEND_DATA_CHANNEL = "shaderapi:receive_shader_url" +val SHADERAPI_RESOURCELOCATION = ResourceLocation.parse(SHADERAPI_SEND_DATA_CHANNEL) \ No newline at end of file diff --git a/src/main/kotlin/com/liamxsage/shaderapi/events/ShaderStatusChangedEvent.kt b/src/main/kotlin/com/liamxsage/shaderapi/events/ShaderStatusChangedEvent.kt new file mode 100644 index 0000000..2cf0b26 --- /dev/null +++ b/src/main/kotlin/com/liamxsage/shaderapi/events/ShaderStatusChangedEvent.kt @@ -0,0 +1,21 @@ +package com.liamxsage.shaderapi.events + +import org.bukkit.entity.Player +import org.bukkit.event.Event +import org.bukkit.event.HandlerList + +class ShaderStatusChangedEvent(val player: Player, val status: String) : Event() { + + override fun getHandlers(): HandlerList { + return HANDLERS + } + + companion object { + val HANDLERS = HandlerList() + + @JvmStatic + fun getHandlerList(): HandlerList { + return HANDLERS + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/liamxsage/shaderapi/listeners/ShaderAPIListener.kt b/src/main/kotlin/com/liamxsage/shaderapi/listeners/ShaderAPIListener.kt index 2a69226..3851574 100644 --- a/src/main/kotlin/com/liamxsage/shaderapi/listeners/ShaderAPIListener.kt +++ b/src/main/kotlin/com/liamxsage/shaderapi/listeners/ShaderAPIListener.kt @@ -1,11 +1,11 @@ package com.liamxsage.shaderapi.listeners -import com.liamxsage.klassicx.extensions.getLogger -import com.liamxsage.shaderapi.SHADERAPI_INCOMING_CHANNEL +import com.liamxsage.shaderapi.SHADERAPI_REQUEST_CHANNEL import com.liamxsage.shaderapi.SHADERAPI_RESOURCELOCATION +import com.liamxsage.shaderapi.SHADERAPI_STATUS_CHANNEL import com.liamxsage.shaderapi.ShaderAPI +import com.liamxsage.shaderapi.events.ShaderStatusChangedEvent import com.liamxsage.shaderapi.resource.ShaderPackInfo -import com.liamxsage.shaderapi.resource.ShaderPackInfoImpl import io.netty.buffer.ByteBuf import io.netty.buffer.Unpooled import net.minecraft.network.FriendlyByteBuf @@ -21,15 +21,30 @@ import java.net.URI class ShaderAPIListener : PluginMessageListener { + private val handleIncomingPackets = mapOf( + SHADERAPI_REQUEST_CHANNEL to this::onRequestShaderUrl, + SHADERAPI_STATUS_CHANNEL to this::onShaderStatusResponse + ) + private val shaderPackInfo: ShaderPackInfo = ShaderPackInfo.shaderPackInfo() .uri(URI.create(ShaderAPI.instance.config.getString("shaderPack") ?: "https://cdn.modrinth.com/data/HVnmMxH1/versions/pAOQ9Amz/ComplementaryReimagined_r5.2.2.zip")) .computeHashAndBuild().get() override fun onPluginMessageReceived(channel: String, player: Player, message: ByteArray?) { - if (SHADERAPI_INCOMING_CHANNEL != channel) return // Ensure it's the correct channel + if (channel !in handleIncomingPackets.keys) return // Ensure it's the correct channel + + handleIncomingPackets[channel]?.invoke(player, message) + } + + private fun onRequestShaderUrl(player: Player, message: ByteArray?) { sendShaderUrl(player, shaderPackInfo) } + private fun onShaderStatusResponse(player: Player, message: ByteArray?) { + val statusResponse = message?.let { String(it, Charsets.UTF_8) } ?: return + ShaderStatusChangedEvent(player, statusResponse).callEvent() + } + private fun sendShaderUrl(player: Player, shaderPackInfo: ShaderPackInfo) { try { // Create a ByteBuf to hold the data diff --git a/src/main/kotlin/com/liamxsage/shaderapi/listeners/ShaderStatusChangeListener.kt b/src/main/kotlin/com/liamxsage/shaderapi/listeners/ShaderStatusChangeListener.kt new file mode 100644 index 0000000..6832770 --- /dev/null +++ b/src/main/kotlin/com/liamxsage/shaderapi/listeners/ShaderStatusChangeListener.kt @@ -0,0 +1,15 @@ +package com.liamxsage.shaderapi.listeners + +import com.liamxsage.klassicx.extensions.getLogger +import com.liamxsage.shaderapi.events.ShaderStatusChangedEvent +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener + +class ShaderStatusChangeListener : Listener { + + @EventHandler + fun onShaderStatusChangedEvent(event: ShaderStatusChangedEvent): Unit = with(event) { + getLogger().info("Shader Status Response by ${player.name}: $status") + } + +} \ No newline at end of file