Skip to content

Commit

Permalink
feat(#1): ShaderStatusChangedEvent
Browse files Browse the repository at this point in the history
closes #1
  • Loading branch information
CoasterFreakDE committed Sep 4, 2024
1 parent 3004bd9 commit 8f866ae
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
}

Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/com/liamxsage/shaderapi/ShaderAPI.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/com/liamxsage/shaderapi/Variables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
}

}

0 comments on commit 8f866ae

Please sign in to comment.