From f36408be131859c778fa2f955993906fd8b60e67 Mon Sep 17 00:00:00 2001 From: CoasterFreakDE Date: Mon, 2 Sep 2024 23:08:27 +0200 Subject: [PATCH] feat: Config and groups --- .../com/liamxsage/shaderapi/ShaderAPI.kt | 12 ++++++ .../com/liamxsage/shaderapi/Variables.kt | 5 --- .../shaderapi/extensions/PlayerExtensions.kt | 42 ------------------- .../shaderapi/listeners/ShaderAPIListener.kt | 30 ++++--------- src/main/resources/config.yml | 5 +++ 5 files changed, 26 insertions(+), 68 deletions(-) delete mode 100644 src/main/kotlin/com/liamxsage/shaderapi/extensions/PlayerExtensions.kt create mode 100644 src/main/resources/config.yml diff --git a/src/main/kotlin/com/liamxsage/shaderapi/ShaderAPI.kt b/src/main/kotlin/com/liamxsage/shaderapi/ShaderAPI.kt index 0b457e6..787fb64 100644 --- a/src/main/kotlin/com/liamxsage/shaderapi/ShaderAPI.kt +++ b/src/main/kotlin/com/liamxsage/shaderapi/ShaderAPI.kt @@ -4,6 +4,7 @@ import com.liamxsage.shaderapi.listeners.ShaderAPIListener import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin import org.bukkit.plugin.messaging.Messenger +import java.util.* import kotlin.system.measureTimeMillis class ShaderAPI : JavaPlugin() { @@ -17,7 +18,18 @@ class ShaderAPI : JavaPlugin() { instance = this } + lateinit var serverGroup: String + override fun onEnable() { + saveDefaultConfig() + + serverGroup = config.getString("serverGroup") ?: "" + if (serverGroup.isEmpty() || serverGroup == "null") { + serverGroup = UUID.randomUUID().toString() + config["serverGroup"] = serverGroup + saveConfig() + } + // Plugin startup logic val time = measureTimeMillis { val messenger: Messenger = Bukkit.getMessenger() diff --git a/src/main/kotlin/com/liamxsage/shaderapi/Variables.kt b/src/main/kotlin/com/liamxsage/shaderapi/Variables.kt index 56c5ba2..9cbfa1d 100644 --- a/src/main/kotlin/com/liamxsage/shaderapi/Variables.kt +++ b/src/main/kotlin/com/liamxsage/shaderapi/Variables.kt @@ -2,11 +2,6 @@ package com.liamxsage.shaderapi import net.minecraft.resources.ResourceLocation -var PREFIX = "ShaderAPI >> " -val BLOCK_PREFIX = - " ShaderAPI ◆" - - 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 diff --git a/src/main/kotlin/com/liamxsage/shaderapi/extensions/PlayerExtensions.kt b/src/main/kotlin/com/liamxsage/shaderapi/extensions/PlayerExtensions.kt deleted file mode 100644 index f6d7a2d..0000000 --- a/src/main/kotlin/com/liamxsage/shaderapi/extensions/PlayerExtensions.kt +++ /dev/null @@ -1,42 +0,0 @@ -package com.liamxsage.shaderapi.extensions - -import com.liamxsage.shaderapi.BLOCK_PREFIX -import com.liamxsage.shaderapi.PREFIX -import dev.fruxz.stacked.text -import org.bukkit.* -import org.bukkit.command.CommandSender -import org.bukkit.entity.Player - - -fun Player.sendMessagePrefixed(message: String) { - this.sendMessage(text(PREFIX + message)) -} - -fun CommandSender.sendMessagePrefixed(message: String) = sendMessage(text(PREFIX + message)) - -fun broadcastPrefixed(message: String) = Bukkit.broadcast(text(PREFIX + message)) - -fun CommandSender.sendMessageBlock(vararg lines: String) { - sendEmtpyLine() - sendMessage(text(BLOCK_PREFIX)) - sendEmtpyLine() - lines.forEach { sendMessage(text(it)) } - sendEmtpyLine() -} - -fun CommandSender.sendEmtpyLine() = sendMessage(text(" ")) - -fun Player.soundExecution() { - playSound(location, Sound.ENTITY_ITEM_PICKUP, .75F, 2F) - playSound(location, Sound.ITEM_ARMOR_EQUIP_LEATHER, .25F, 2F) - playSound(location, Sound.ITEM_ARMOR_EQUIP_CHAIN, .1F, 2F) -} - - -fun Player.sendDeniedSound() = playSound(location, "minecraft:block.note_block.bass", 1f, 1f) - -fun Player.sendSuccessSound() = playSound(location, "minecraft:block.note_block.pling", 1f, 1f) - -fun Player.sendTeleportSound() = playSound(location, "minecraft:block.note_block.harp", 1f, 1f) - -fun Player.sendOpenSound() = playSound(location, "minecraft:block.note_block.chime", 1f, 1f) \ 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 fa75b61..2a69226 100644 --- a/src/main/kotlin/com/liamxsage/shaderapi/listeners/ShaderAPIListener.kt +++ b/src/main/kotlin/com/liamxsage/shaderapi/listeners/ShaderAPIListener.kt @@ -3,6 +3,7 @@ package com.liamxsage.shaderapi.listeners import com.liamxsage.klassicx.extensions.getLogger import com.liamxsage.shaderapi.SHADERAPI_INCOMING_CHANNEL import com.liamxsage.shaderapi.SHADERAPI_RESOURCELOCATION +import com.liamxsage.shaderapi.ShaderAPI import com.liamxsage.shaderapi.resource.ShaderPackInfo import com.liamxsage.shaderapi.resource.ShaderPackInfoImpl import io.netty.buffer.ByteBuf @@ -20,37 +21,24 @@ import java.net.URI class ShaderAPIListener : PluginMessageListener { + 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 - - also { - if (message == null) return@also - if (message.isEmpty()) return@also - val shaderUrl = message.toString(Charsets.UTF_8) - getLogger().info("Shader URL received: $shaderUrl") - } - - val shaderUrl = "https://cdn.modrinth.com/data/HVnmMxH1/versions/pAOQ9Amz/ComplementaryReimagined_r5.2.2.zip" - - ShaderPackInfo.shaderPackInfo() - .uri(URI.create(shaderUrl)) - .computeHashAndBuild().thenApply { - getLogger().info("Shader hash: ${it.hash()}") - sendShaderUrl(player, shaderUrl, it.hash()) - getLogger().info("Shader URL sent") - } + sendShaderUrl(player, shaderPackInfo) } - private fun sendShaderUrl(player: Player, shaderUrl: String, hash: String) { + private fun sendShaderUrl(player: Player, shaderPackInfo: ShaderPackInfo) { try { // Create a ByteBuf to hold the data val byteBuf = FriendlyByteBuf(Unpooled.buffer()) // Write the shader URL to the buffer as UTF-8 - byteBuf.writeUtf(shaderUrl) - byteBuf.writeUtf(hash) - byteBuf.writeUtf("global") + byteBuf.writeUtf(shaderPackInfo.uri().toString()) + byteBuf.writeUtf(shaderPackInfo.hash()) + byteBuf.writeUtf(ShaderAPI.instance.serverGroup) // Send the data to the client using the correct channel diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..50810c2 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,5 @@ +# If you want to group your servers, you can do so here +serverGroup: null + +# ShaderPack Infos +shaderPack: https://cdn.modrinth.com/data/HVnmMxH1/versions/pAOQ9Amz/ComplementaryReimagined_r5.2.2.zip \ No newline at end of file