Skip to content

Commit

Permalink
Merge pull request #7 from Liams-Services/feat/1-shader-response
Browse files Browse the repository at this point in the history
feat: Shader Response
  • Loading branch information
CoasterFreakDE authored Sep 4, 2024
2 parents 8fd478d + b5aa4cb commit 3644fc6
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.liamxsage.shaderapi.Constants.logger
import com.liamxsage.shaderapi.ShaderReceivePayload
import com.liamxsage.shaderapi.client.config.ConfigManager
import com.liamxsage.shaderapi.client.config.ShaderPackAcceptState
import com.liamxsage.shaderapi.client.functions.sendShaderStatusResponse
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
import net.irisshaders.iris.Iris
import net.minecraft.client.MinecraftClient
Expand All @@ -13,6 +14,7 @@ import java.io.File
import java.net.HttpURLConnection
import java.net.URI
import java.util.*
import kotlin.reflect.jvm.jvmName

class ShaderReceivePayloadHandler : ClientPlayNetworking.PlayPayloadHandler<ShaderReceivePayload> {
override fun receive(payload: ShaderReceivePayload, context: ClientPlayNetworking.Context) {
Expand All @@ -36,8 +38,9 @@ class ShaderReceivePayloadHandler : ClientPlayNetworking.PlayPayloadHandler<Shad
return@ConfirmScreen
}

context.client().setScreen(null)
context.client().setScreen(null)
ConfigManager.addServerGroup(serverGroup, ShaderPackAcceptState.DENY)
sendShaderStatusResponse(ShaderStatusResponse.DENIED)

}, Text.of("Shader Pack Available"), Text.of("Do you want to download and apply the shader pack?")))
}
Expand All @@ -51,8 +54,9 @@ class ShaderReceivePayloadHandler : ClientPlayNetworking.PlayPayloadHandler<Shad
try {
val file = File(MinecraftClient.getInstance().runDirectory, "downloads/$hash.zip")
return file.exists()
} catch (e: Exception) {
e.printStackTrace()
} catch (exception: Exception) {
exception.printStackTrace()
sendShaderStatusResponse(exception::class.jvmName.uppercase())
}
return false
}
Expand All @@ -67,6 +71,7 @@ class ShaderReceivePayloadHandler : ClientPlayNetworking.PlayPayloadHandler<Shad
// Download the shader pack
val shaderPackFile: File = downloadShaderPack(url, shaderHash) ?: run {
logger.warn("Shader Pack Download failed")
sendShaderStatusResponse(ShaderStatusResponse.DOWNLOAD_FAILED)
return
}

Expand All @@ -82,8 +87,10 @@ class ShaderReceivePayloadHandler : ClientPlayNetworking.PlayPayloadHandler<Shad
Iris.getIrisConfig().setShadersEnabled(true)
Iris.getIrisConfig().save()
Iris.reload()
sendShaderStatusResponse(ShaderStatusResponse.SUCCESS)
} catch (exception: Exception) {
exception.printStackTrace()
sendShaderStatusResponse(ShaderStatusResponse.APPLY_FAILED)
}
}
}
Expand All @@ -107,10 +114,12 @@ class ShaderReceivePayloadHandler : ClientPlayNetworking.PlayPayloadHandler<Shad
return if (renamed) {
File(minecraftClient.runDirectory, "downloads/$hash.zip")
} else {
sendShaderStatusResponse(ShaderStatusResponse.IO_EXCEPTION)
null
}
} catch (e: Exception) {
e.printStackTrace()
} catch (exception: Exception) {
exception.printStackTrace()
sendShaderStatusResponse(exception::class.jvmName.uppercase())
}
return null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.liamxsage.shaderapi.client

enum class ShaderStatusResponse {
SUCCESS, DENIED, DOWNLOAD_FAILED, APPLY_FAILED, IO_EXCEPTION
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.liamxsage.shaderapi.client.functions

import com.liamxsage.shaderapi.ShaderStatusResponsePayload
import com.liamxsage.shaderapi.client.ShaderStatusResponse
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking

fun sendShaderStatusResponse(statusResponse: ShaderStatusResponse) {
ClientPlayNetworking.send(ShaderStatusResponsePayload(statusResponse.name))
}

fun sendShaderStatusResponse(statusResponse: String) {
ClientPlayNetworking.send(ShaderStatusResponsePayload(statusResponse))
}
3 changes: 3 additions & 0 deletions src/main/kotlin/com/liamxsage/shaderapi/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ object Constants {
@JvmStatic
val RECEIVE_SHADER_PACKET_ID: Identifier = Identifier.of("shaderapi", "receive_shader_url")

@JvmStatic
val STATUS_RESPONSE_PACKET_ID: Identifier = Identifier.of("shaderapi", "status_response")

@JvmStatic
val logger: Logger = LoggerFactory.getLogger(Shaderapi::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.liamxsage.shaderapi;

import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;


public record ShaderStatusResponsePayload(String statusResponse) implements CustomPayload {

public static final Id<ShaderStatusResponsePayload> ID = new Id<>(Constants.getSTATUS_RESPONSE_PACKET_ID());
public static final PacketCodec<RegistryByteBuf, ShaderStatusResponsePayload> CODEC = PacketCodec.tuple(
PacketCodecs.STRING, ShaderStatusResponsePayload::statusResponse,
ShaderStatusResponsePayload::new);

@Override
public Id<? extends CustomPayload> getId() {
return ID;
}
}
4 changes: 4 additions & 0 deletions src/main/kotlin/com/liamxsage/shaderapi/Shaderapi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ class Shaderapi : ModInitializer {
override fun onInitialize() {
logger.info("Initializing ShaderAPI")

// Client 2 Server
PayloadTypeRegistry.playC2S().register(ShaderRequestPayload.ID, ShaderRequestPayload.CODEC)
PayloadTypeRegistry.playC2S().register(ShaderStatusResponsePayload.ID, ShaderStatusResponsePayload.CODEC)

// Server 2 Client
PayloadTypeRegistry.playS2C().register(ShaderReceivePayload.ID, ShaderReceivePayload.CODEC)

logger.info("ShaderAPI initialized")
Expand Down

0 comments on commit 3644fc6

Please sign in to comment.