Some more logic updates for server management.

This commit is contained in:
Andrew Lalis 2022-08-02 10:14:18 +02:00
parent 20295596fa
commit 9aeb5cd048
4 changed files with 27 additions and 16 deletions

View File

@ -55,7 +55,7 @@ public class CommunicationHandler {
socket.setSoTimeout(1000); socket.setSoTimeout(1000);
in = Net.getInputStream(socket.getInputStream()); in = Net.getInputStream(socket.getInputStream());
out = Net.getOutputStream(socket.getOutputStream()); out = Net.getOutputStream(socket.getOutputStream());
Net.write(new ConnectRequestMessage(username), out); Net.write(new ConnectRequestMessage(username, true), out);
Message response = Net.read(in); Message response = Net.read(in);
socket.setSoTimeout(0); socket.setSoTimeout(0);
if (response instanceof ConnectRejectMessage rejectMessage) { if (response instanceof ConnectRejectMessage rejectMessage) {

View File

@ -2,4 +2,10 @@ package nl.andrewl.aos_core.net.connect;
import nl.andrewl.record_net.Message; import nl.andrewl.record_net.Message;
public record ConnectRequestMessage(String username) implements Message {} /**
* The first message that a client sends via TCP to the server, to indicate
* that they'd like to join.
* @param username The player's chosen username.
* @param spectator Whether the player wants to be a spectator.
*/
public record ConnectRequestMessage(String username, boolean spectator) implements Message {}

View File

@ -111,8 +111,7 @@ public class ClientCommunicationHandler {
// Try to set the TCP timeout back to 0 now that we've got the correct request. // Try to set the TCP timeout back to 0 now that we've got the correct request.
socket.setSoTimeout(0); socket.setSoTimeout(0);
this.clientAddress = socket.getInetAddress(); this.clientAddress = socket.getInetAddress();
connectionEstablished = true; this.player = server.getPlayerManager().register(this, connectMsg.username(), connectMsg.spectator());
this.player = server.getPlayerManager().register(this, connectMsg.username());
Net.write(new ConnectAcceptMessage(player.getId()), out); Net.write(new ConnectAcceptMessage(player.getId()), out);
sendInitialData(); sendInitialData();
sendTcpMessage(ChatMessage.privateMessage("Welcome to the server, " + player.getUsername() + ".")); sendTcpMessage(ChatMessage.privateMessage("Welcome to the server, " + player.getUsername() + "."));
@ -123,6 +122,7 @@ public class ClientCommunicationHandler {
TcpReceiver tcpReceiver = new TcpReceiver(in, this::handleTcpMessage) TcpReceiver tcpReceiver = new TcpReceiver(in, this::handleTcpMessage)
.withShutdownHook(() -> server.getPlayerManager().deregister(this.player)); .withShutdownHook(() -> server.getPlayerManager().deregister(this.player));
new Thread(tcpReceiver).start(); new Thread(tcpReceiver).start();
connectionEstablished = true;
} }
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
// Ignore this one, since this will happen if the client doesn't send data properly. // Ignore this one, since this will happen if the client doesn't send data properly.
@ -231,7 +231,7 @@ public class ClientCommunicationHandler {
out.writeFloat(player.getOrientation().y()); out.writeFloat(player.getOrientation().y());
out.writeBoolean(player.isCrouching()); out.writeBoolean(player.isCrouching());
out.writeInt(player.getInventory().getSelectedItemStack().getType().getId()); out.writeInt(player.getInventory().getSelectedItemStack() == null ? -1 : player.getInventory().getSelectedItemStack().getType().getId());
out.writeByte(player.getInventory().getSelectedBlockValue()); out.writeByte(player.getInventory().getSelectedBlockValue());
out.writeInt(player.getMode().ordinal()); out.writeInt(player.getMode().ordinal());
} }

View File

@ -29,15 +29,18 @@ public class PlayerManager {
this.server = server; this.server = server;
} }
public synchronized ServerPlayer register(ClientCommunicationHandler handler, String username) { public synchronized ServerPlayer register(ClientCommunicationHandler handler, String username, boolean spectator) {
Team team = findBestTeamForNewPlayer(); PlayerMode mode = spectator ? PlayerMode.SPECTATOR : PlayerMode.NORMAL;
ServerPlayer player = new ServerPlayer(nextClientId++, username, team, PlayerMode.NORMAL); Team team = mode != PlayerMode.NORMAL ? null : findBestTeamForNewPlayer();
var inv = player.getInventory(); ServerPlayer player = new ServerPlayer(nextClientId++, username, team, mode);
inv.getItemStacks().add(new GunItemStack(ItemTypes.RIFLE)); if (player.getMode() == PlayerMode.NORMAL || player.getMode() == PlayerMode.CREATIVE) {
inv.getItemStacks().add(new GunItemStack(ItemTypes.AK_47)); var inv = player.getInventory();
inv.getItemStacks().add(new GunItemStack(ItemTypes.WINCHESTER)); inv.getItemStacks().add(new GunItemStack(ItemTypes.RIFLE));
inv.getItemStacks().add(new BlockItemStack(ItemTypes.BLOCK, 50, (byte) 1)); inv.getItemStacks().add(new GunItemStack(ItemTypes.AK_47));
inv.setSelectedIndex(0); inv.getItemStacks().add(new GunItemStack(ItemTypes.WINCHESTER));
inv.getItemStacks().add(new BlockItemStack(ItemTypes.BLOCK, 50, (byte) 1));
inv.setSelectedIndex(0);
}
System.out.printf("Registered player \"%s\" with id %d.%n", player.getUsername(), player.getId()); System.out.printf("Registered player \"%s\" with id %d.%n", player.getUsername(), player.getId());
players.put(player.getId(), player); players.put(player.getId(), player);
@ -57,11 +60,13 @@ public class PlayerManager {
player.getVelocity().x(), player.getVelocity().y(), player.getVelocity().z(), player.getVelocity().x(), player.getVelocity().y(), player.getVelocity().z(),
player.getOrientation().x(), player.getOrientation().y(), player.getOrientation().x(), player.getOrientation().y(),
player.isCrouching(), player.isCrouching(),
player.getInventory().getSelectedItemStack().getType().getId(), player.getInventory().getSelectedItemStack() == null ? -1 : player.getInventory().getSelectedItemStack().getType().getId(),
player.getInventory().getSelectedBlockValue(), player.getInventory().getSelectedBlockValue(),
player.getMode() player.getMode()
), player); ), player);
broadcastTcpMessageToAllBut(ChatMessage.announce(joinMessage), player); if (player.getMode() != PlayerMode.SPECTATOR) {
broadcastTcpMessageToAllBut(ChatMessage.announce(joinMessage), player);
}
return player; return player;
} }