diff --git a/core/src/main/java/nl/andrewl/aos_core/net/PlayerJoinMessage.java b/core/src/main/java/nl/andrewl/aos_core/net/PlayerJoinMessage.java index 2ec3ec4..07d10d2 100644 --- a/core/src/main/java/nl/andrewl/aos_core/net/PlayerJoinMessage.java +++ b/core/src/main/java/nl/andrewl/aos_core/net/PlayerJoinMessage.java @@ -1,5 +1,6 @@ package nl.andrewl.aos_core.net; +import nl.andrewl.aos_core.model.Player; import nl.andrewl.record_net.Message; /** @@ -11,4 +12,13 @@ public record PlayerJoinMessage( float px, float py, float pz, float vx, float vy, float vz, float ox, float oy -) implements Message {} +) implements Message { + public PlayerJoinMessage(Player player) { + this( + player.getId(), player.getUsername(), + player.getPosition().x, player.getPosition().y, player.getPosition().z, + player.getVelocity().x, player.getVelocity().y, player.getVelocity().z, + player.getOrientation().x, player.getOrientation().y + ); + } +} diff --git a/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java b/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java index c93e476..3648b7a 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java +++ b/server/src/main/java/nl/andrewl/aos2_server/PlayerManager.java @@ -1,6 +1,8 @@ package nl.andrewl.aos2_server; import nl.andrewl.aos_core.Net; +import nl.andrewl.aos_core.net.PlayerJoinMessage; +import nl.andrewl.aos_core.net.PlayerLeaveMessage; import nl.andrewl.aos_core.net.udp.DatagramInit; import nl.andrewl.aos_core.net.udp.PlayerUpdateMessage; import nl.andrewl.record_net.Message; @@ -29,6 +31,7 @@ public class PlayerManager { clientHandlers.put(player.getId(), handler); log.info("Registered player \"{}\" with id {}", player.getUsername(), player.getId()); player.setPosition(new Vector3f(0, 64, 0)); + broadcastTcpMessage(new PlayerJoinMessage(player)); broadcastUdpMessage(new PlayerUpdateMessage(player)); return player; } @@ -38,6 +41,7 @@ public class PlayerManager { if (handler != null) handler.shutdown(); players.remove(player.getId()); clientHandlers.remove(player.getId()); + broadcastTcpMessage(new PlayerLeaveMessage(player.getId())); log.info("Deregistered player \"{}\" with id {}", player.getUsername(), player.getId()); } @@ -73,6 +77,12 @@ public class PlayerManager { } } + public void broadcastTcpMessage(Message msg) { + for (var handler : getHandlers()) { + handler.sendTcpMessage(msg); + } + } + public void broadcastUdpMessage(Message msg) { try { byte[] data = Net.write(msg);