From 03f0e4df4514896f9087e61afa190dc501c66187 Mon Sep 17 00:00:00 2001 From: LapisBerry Date: Wed, 22 May 2024 17:30:35 +0700 Subject: [PATCH] feat: Client can send JoinRequestPacket --- src/main/java/com/lapisberry/Main.java | 11 ++++++++++- .../java/com/lapisberry/gui/scenes/JoinScene.java | 6 +++++- src/main/java/com/lapisberry/net/Client.java | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/lapisberry/Main.java b/src/main/java/com/lapisberry/Main.java index a8b2faf..e765d23 100644 --- a/src/main/java/com/lapisberry/Main.java +++ b/src/main/java/com/lapisberry/Main.java @@ -1,6 +1,7 @@ package com.lapisberry; import com.lapisberry.gui.scenes.JoinScene; +import com.lapisberry.gui.scenes.LobbyScene; import com.lapisberry.net.Client; import com.lapisberry.net.Server; import com.lapisberry.utils.exceptions.ConnectionRefusedException; @@ -23,7 +24,7 @@ public void start(Stage stage) { primaryStage = stage; primaryStage.setTitle("MaKillMai"); primaryStage.getIcons().add(new Image(ClassLoader.getSystemResource("images/makillmai-icon.png").toString())); - primaryStage.setScene(new JoinScene()); + goToJoinScene(); primaryStage.setOnCloseRequest(event -> { closeServer(); closeClient(); @@ -36,6 +37,14 @@ public void start(Stage stage) { primaryStage.getScene().getRoot().requestFocus(); } + public static void goToJoinScene() { + primaryStage.setScene(new JoinScene()); + } + + public static void goToLobbyScene() { + primaryStage.setScene(new LobbyScene()); + } + public static void createServer() { server = new Server(); new Thread(server, "Server thread").start(); diff --git a/src/main/java/com/lapisberry/gui/scenes/JoinScene.java b/src/main/java/com/lapisberry/gui/scenes/JoinScene.java index 5d6868a..97569c5 100644 --- a/src/main/java/com/lapisberry/gui/scenes/JoinScene.java +++ b/src/main/java/com/lapisberry/gui/scenes/JoinScene.java @@ -92,8 +92,12 @@ private JoinButton(String text) { return; } try { + // Create client using host IP address, might throw ConnectionRefusedException Main.createClient(ipAddress.getText()); - Main.getPrimaryStage().setScene(new LobbyScene()); + // Send join request packet to server + Main.getClient().sendJoinRequestPacket(username.getText()); + // Go to lobby scene + Main.goToLobbyScene(); } catch (ConnectionRefusedException ex) { alertContainer.alert("Connection refused."); } diff --git a/src/main/java/com/lapisberry/net/Client.java b/src/main/java/com/lapisberry/net/Client.java index fe7c77c..f2165d9 100644 --- a/src/main/java/com/lapisberry/net/Client.java +++ b/src/main/java/com/lapisberry/net/Client.java @@ -1,6 +1,7 @@ package com.lapisberry.net; import com.lapisberry.net.packets.ClientPacket; +import com.lapisberry.net.packets.JoinRequestPacket; import com.lapisberry.net.packets.ServerPacket; import com.lapisberry.utils.Config; import com.lapisberry.utils.exceptions.ConnectionRefusedException; @@ -56,6 +57,10 @@ private void sendPacketToServer(ClientPacket packet) { } } + public void sendJoinRequestPacket(String username) { + sendPacketToServer(new JoinRequestPacket(username)); + } + public void close() { try { socket.close();