From 004ab1e220fd7117c02390092661c918166f6bfb Mon Sep 17 00:00:00 2001 From: 1e99 <1e99@1e99.eu> Date: Sun, 17 Nov 2024 18:29:19 +0100 Subject: [PATCH] refactor client --- client-fabric/build.gradle | 14 +++++++++ client-fabric/resources/svc.mixins.json | 1 + .../client/fabric/FabricSimplerVoiceChat.java | 8 +---- .../eu/e99/svc/client/fabric/SVCClient.java | 24 +++++++++------ .../fabric/mixin/PlayerEntityMixin.java | 30 +++++++++++++++++++ .../eu/e99/svc/packet/AuthResponsePacket.java | 1 + server/src/eu/e99/svc/server/Server.java | 1 - 7 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 client-fabric/src/eu/e99/svc/client/fabric/mixin/PlayerEntityMixin.java diff --git a/client-fabric/build.gradle b/client-fabric/build.gradle index 9eb9a19..1f303ec 100644 --- a/client-fabric/build.gradle +++ b/client-fabric/build.gradle @@ -13,6 +13,14 @@ dependencies { modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + implementation "dev.onvoid.webrtc:webrtc-java:0.8.0" + implementation group: "dev.onvoid.webrtc", name: "webrtc-java", version: "0.8.0", classifier: "windows-x86_64" + implementation group: "dev.onvoid.webrtc", name: "webrtc-java", version: "0.8.0", classifier: "macos-x86_64" + implementation group: "dev.onvoid.webrtc", name: "webrtc-java", version: "0.8.0", classifier: "macos-aarch64" + implementation group: "dev.onvoid.webrtc", name: "webrtc-java", version: "0.8.0", classifier: "linux-x86_64" + implementation group: "dev.onvoid.webrtc", name: "webrtc-java", version: "0.8.0", classifier: "linux-aarch64" + implementation group: "dev.onvoid.webrtc", name: "webrtc-java", version: "0.8.0", classifier: "linux-aarch32" + implementation project(':common') } @@ -43,3 +51,9 @@ jar { rename { "${it}_${project.base.archivesName.get()}" } } } + +runClient { + // I need to do it here, IntelliJ doesn't recognize the mod and I couldn't figure out how to set env variables + systemProperty "devauth.enabled", System.getenv("DEVAUTH_ENABLED") + systemProperty "devauth.account", System.getenv("DEVAUTH_ACCOUNT") +} diff --git a/client-fabric/resources/svc.mixins.json b/client-fabric/resources/svc.mixins.json index 7b7c489..82dbe62 100644 --- a/client-fabric/resources/svc.mixins.json +++ b/client-fabric/resources/svc.mixins.json @@ -3,6 +3,7 @@ "package": "eu.e99.svc.client.fabric.mixin", "compatibilityLevel": "JAVA_21", "mixins": [ + "PlayerEntityMixin" ], "injectors": { "defaultRequire": 1 diff --git a/client-fabric/src/eu/e99/svc/client/fabric/FabricSimplerVoiceChat.java b/client-fabric/src/eu/e99/svc/client/fabric/FabricSimplerVoiceChat.java index e69b612..3fe4aa7 100644 --- a/client-fabric/src/eu/e99/svc/client/fabric/FabricSimplerVoiceChat.java +++ b/client-fabric/src/eu/e99/svc/client/fabric/FabricSimplerVoiceChat.java @@ -3,13 +3,10 @@ package eu.e99.svc.client.fabric; import net.fabricmc.api.ClientModInitializer; import net.minecraft.client.MinecraftClient; import net.minecraft.client.session.Session; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import javax.net.ssl.SSLSocketFactory; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; -import java.util.UUID; public class FabricSimplerVoiceChat implements ClientModInitializer { @@ -31,13 +28,10 @@ public class FabricSimplerVoiceChat implements ClientModInitializer { MinecraftClient client = MinecraftClient.getInstance(); Session session = client.getSession(); - SVCClient voiceChat = new SVCClient( "localhost", 6969, - session.getAccessToken(), - session.getUsername(), - session.getUuidOrNull(), + client, socketFactory ); Thread.ofPlatform().start(voiceChat::start); diff --git a/client-fabric/src/eu/e99/svc/client/fabric/SVCClient.java b/client-fabric/src/eu/e99/svc/client/fabric/SVCClient.java index c0a4d59..b3c7b1f 100644 --- a/client-fabric/src/eu/e99/svc/client/fabric/SVCClient.java +++ b/client-fabric/src/eu/e99/svc/client/fabric/SVCClient.java @@ -6,6 +6,8 @@ import eu.e99.svc.SimplerVoiceChat; import eu.e99.svc.auth.MojangAPI; import eu.e99.svc.io.BinaryMessage; import eu.e99.svc.packet.*; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.session.Session; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; @@ -33,17 +35,13 @@ public class SVCClient { private final String host; private final int port; - private final String accessToken; - private final String username; - private final UUID uuid; + private final MinecraftClient client; private final SSLSocketFactory socketFactory; - public SVCClient(String host, int port, String accessToken, String username, UUID uuid, SSLSocketFactory socketFactory) { + public SVCClient(String host, int port, MinecraftClient client, SSLSocketFactory socketFactory) { this.host = host; this.port = port; - this.accessToken = accessToken; - this.username = username; - this.uuid = uuid; + this.client = client; this.socketFactory = socketFactory; } @@ -64,6 +62,8 @@ public class SVCClient { } System.out.printf("Successfully authenticated.%n"); + + while (true) {} } catch (IOException e) { System.out.printf("Failed to connect.%n"); e.printStackTrace(System.out); @@ -91,9 +91,15 @@ public class SVCClient { case AuthRequestPacket authRequest -> { System.out.printf("Joining server to authenticate...%n"); + Session session = this.client.getSession(); + boolean ok; try { - ok = MojangAPI.joinServer(this.accessToken, this.uuid, authRequest.serverId); + ok = MojangAPI.joinServer( + session.getAccessToken(), + session.getUuidOrNull(), + authRequest.serverId + ); } catch (Exception e) { System.out.printf("Failed to join server:%n"); e.printStackTrace(System.out); @@ -106,7 +112,7 @@ public class SVCClient { } AuthResponsePacket authResponse = new AuthResponsePacket(); - authResponse.username = this.username; + authResponse.username = session.getUsername(); conn.writePacket(authResponse); } case AuthSuccessPacket authComplete -> { diff --git a/client-fabric/src/eu/e99/svc/client/fabric/mixin/PlayerEntityMixin.java b/client-fabric/src/eu/e99/svc/client/fabric/mixin/PlayerEntityMixin.java new file mode 100644 index 0000000..fcd2ef7 --- /dev/null +++ b/client-fabric/src/eu/e99/svc/client/fabric/mixin/PlayerEntityMixin.java @@ -0,0 +1,30 @@ +package eu.e99.svc.client.fabric.mixin; + +import com.mojang.authlib.GameProfile; +import dev.onvoid.webrtc.RTCPeerConnection; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(PlayerEntity.class) +public class PlayerEntityMixin { + + @Shadow @Final private GameProfile gameProfile; + + @Inject(method = "", at = @At("RETURN")) + private void svc$init(World world, BlockPos pos, float yaw, GameProfile gameProfile, CallbackInfo ci) { + System.out.printf("Added %s.%n", gameProfile.getName()); + } + + @Inject(method = "remove", at = @At("HEAD")) + private void svc$remove(Entity.RemovalReason reason, CallbackInfo ci) { + System.out.printf("Removed %s.%n", this.gameProfile.getName()); + } +} diff --git a/common/src/eu/e99/svc/packet/AuthResponsePacket.java b/common/src/eu/e99/svc/packet/AuthResponsePacket.java index cb2a425..e4e149d 100644 --- a/common/src/eu/e99/svc/packet/AuthResponsePacket.java +++ b/common/src/eu/e99/svc/packet/AuthResponsePacket.java @@ -5,6 +5,7 @@ import eu.e99.svc.io.Reader; import eu.e99.svc.io.Writer; import java.io.IOException; +import java.util.UUID; public class AuthResponsePacket implements BinaryMessage { diff --git a/server/src/eu/e99/svc/server/Server.java b/server/src/eu/e99/svc/server/Server.java index 7570bf6..308212c 100644 --- a/server/src/eu/e99/svc/server/Server.java +++ b/server/src/eu/e99/svc/server/Server.java @@ -7,7 +7,6 @@ import eu.e99.svc.auth.PlayerProfile; import eu.e99.svc.io.BinaryMessage; import eu.e99.svc.packet.*; -import java.io.EOFException; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket;