Skip to content

Commit

Permalink
feat: Client can send JoinRequestPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
LapisBerry committed May 22, 2024
1 parent 6356d5b commit 03f0e4d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/main/java/com/lapisberry/Main.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand All @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/lapisberry/gui/scenes/JoinScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/lapisberry/net/Client.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -56,6 +57,10 @@ private void sendPacketToServer(ClientPacket packet) {
}
}

public void sendJoinRequestPacket(String username) {
sendPacketToServer(new JoinRequestPacket(username));
}

public void close() {
try {
socket.close();
Expand Down

0 comments on commit 03f0e4d

Please sign in to comment.