Removed all mention of SLF4J and Log4J
This commit is contained in:
parent
29d44a5b01
commit
29eb46bcaf
|
@ -17,8 +17,6 @@ import nl.andrewl.aos_core.net.world.ChunkHashMessage;
|
||||||
import nl.andrewl.aos_core.net.world.ChunkUpdateMessage;
|
import nl.andrewl.aos_core.net.world.ChunkUpdateMessage;
|
||||||
import nl.andrewl.record_net.Message;
|
import nl.andrewl.record_net.Message;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
@ -29,8 +27,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
public class Client implements Runnable {
|
public class Client implements Runnable {
|
||||||
private static final Logger log = LoggerFactory.getLogger(Client.class);
|
|
||||||
|
|
||||||
private final ClientConfig config;
|
private final ClientConfig config;
|
||||||
private final CommunicationHandler communicationHandler;
|
private final CommunicationHandler communicationHandler;
|
||||||
private final InputHandler inputHandler;
|
private final InputHandler inputHandler;
|
||||||
|
@ -79,13 +75,12 @@ public class Client implements Runnable {
|
||||||
try {
|
try {
|
||||||
communicationHandler.establishConnection();
|
communicationHandler.establishConnection();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Couldn't connect to the server: {}", e.getMessage());
|
System.err.println("Couldn't connect to the server: " + e.getMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
gameRenderer = new GameRenderer(this, inputHandler);
|
gameRenderer = new GameRenderer(this, inputHandler);
|
||||||
soundManager = new SoundManager();
|
soundManager = new SoundManager();
|
||||||
log.debug("Sound system initialized.");
|
|
||||||
|
|
||||||
long lastFrameAt = System.currentTimeMillis();
|
long lastFrameAt = System.currentTimeMillis();
|
||||||
while (!gameRenderer.windowShouldClose() && !communicationHandler.isDone()) {
|
while (!gameRenderer.windowShouldClose() && !communicationHandler.isDone()) {
|
||||||
|
|
|
@ -16,8 +16,6 @@ import nl.andrewl.record_net.Message;
|
||||||
import nl.andrewl.record_net.util.ExtendedDataInputStream;
|
import nl.andrewl.record_net.util.ExtendedDataInputStream;
|
||||||
import nl.andrewl.record_net.util.ExtendedDataOutputStream;
|
import nl.andrewl.record_net.util.ExtendedDataOutputStream;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
|
@ -31,8 +29,6 @@ import java.net.Socket;
|
||||||
* methods for sending messages and processing those we receive.
|
* methods for sending messages and processing those we receive.
|
||||||
*/
|
*/
|
||||||
public class CommunicationHandler {
|
public class CommunicationHandler {
|
||||||
private static final Logger log = LoggerFactory.getLogger(CommunicationHandler.class);
|
|
||||||
|
|
||||||
private final Client client;
|
private final Client client;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private DatagramSocket datagramSocket;
|
private DatagramSocket datagramSocket;
|
||||||
|
@ -52,7 +48,7 @@ public class CommunicationHandler {
|
||||||
InetAddress address = InetAddress.getByName(client.getConfig().serverHost);
|
InetAddress address = InetAddress.getByName(client.getConfig().serverHost);
|
||||||
int port = client.getConfig().serverPort;
|
int port = client.getConfig().serverPort;
|
||||||
String username = client.getConfig().username;
|
String username = client.getConfig().username;
|
||||||
log.info("Connecting to server at {}, port {}, with username \"{}\"...", address, port, username);
|
System.out.printf("Connecting to server at %s, port %d, with username \"%s\"...%n", address, port, username);
|
||||||
|
|
||||||
socket = new Socket(address, port);
|
socket = new Socket(address, port);
|
||||||
socket.setSoTimeout(1000);
|
socket.setSoTimeout(1000);
|
||||||
|
@ -66,12 +62,9 @@ public class CommunicationHandler {
|
||||||
}
|
}
|
||||||
if (response instanceof ConnectAcceptMessage acceptMessage) {
|
if (response instanceof ConnectAcceptMessage acceptMessage) {
|
||||||
this.clientId = acceptMessage.clientId();
|
this.clientId = acceptMessage.clientId();
|
||||||
log.debug("Connection accepted. My client id is {}.", clientId);
|
|
||||||
client.setMyPlayer(new ClientPlayer(clientId, username));
|
client.setMyPlayer(new ClientPlayer(clientId, username));
|
||||||
receiveInitialData();
|
receiveInitialData();
|
||||||
log.debug("Initial data received.");
|
|
||||||
establishDatagramConnection();
|
establishDatagramConnection();
|
||||||
log.info("Connection to server established. My client id is {}.", clientId);
|
|
||||||
new Thread(new TcpReceiver(in, client::onMessageReceived).withShutdownHook(this::shutdown)).start();
|
new Thread(new TcpReceiver(in, client::onMessageReceived).withShutdownHook(this::shutdown)).start();
|
||||||
new Thread(new UdpReceiver(datagramSocket, (msg, packet) -> client.onMessageReceived(msg))).start();
|
new Thread(new UdpReceiver(datagramSocket, (msg, packet) -> client.onMessageReceived(msg))).start();
|
||||||
} else {
|
} else {
|
||||||
|
@ -138,7 +131,6 @@ public class CommunicationHandler {
|
||||||
if (!connectionEstablished) {
|
if (!connectionEstablished) {
|
||||||
throw new IOException("Could not establish a datagram connection to the server after " + attempts + " attempts.");
|
throw new IOException("Could not establish a datagram connection to the server after " + attempts + " attempts.");
|
||||||
}
|
}
|
||||||
log.debug("Established datagram communication with the server.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getClientId() {
|
public int getClientId() {
|
||||||
|
|
|
@ -16,8 +16,6 @@ import org.joml.Matrix4f;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.lwjgl.glfw.*;
|
import org.lwjgl.glfw.*;
|
||||||
import org.lwjgl.opengl.GL;
|
import org.lwjgl.opengl.GL;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ -31,7 +29,6 @@ import static org.lwjgl.opengl.GL46.*;
|
||||||
* OpenGL context exists.
|
* OpenGL context exists.
|
||||||
*/
|
*/
|
||||||
public class GameRenderer {
|
public class GameRenderer {
|
||||||
private static final Logger log = LoggerFactory.getLogger(GameRenderer.class);
|
|
||||||
private static final float Z_NEAR = 0.01f;
|
private static final float Z_NEAR = 0.01f;
|
||||||
private static final float Z_FAR = 500f;
|
private static final float Z_FAR = 500f;
|
||||||
|
|
||||||
|
@ -76,7 +73,6 @@ public class GameRenderer {
|
||||||
long monitorId = glfwGetPrimaryMonitor();
|
long monitorId = glfwGetPrimaryMonitor();
|
||||||
GLFWVidMode primaryMonitorSettings = glfwGetVideoMode(monitorId);
|
GLFWVidMode primaryMonitorSettings = glfwGetVideoMode(monitorId);
|
||||||
if (primaryMonitorSettings == null) throw new IllegalStateException("Could not get information about the primary monitory.");
|
if (primaryMonitorSettings == null) throw new IllegalStateException("Could not get information about the primary monitory.");
|
||||||
log.debug("Primary monitor settings: Width: {}, Height: {}, FOV: {}", primaryMonitorSettings.width(), primaryMonitorSettings.height(), config.fov);
|
|
||||||
if (config.fullscreen) {
|
if (config.fullscreen) {
|
||||||
screenWidth = primaryMonitorSettings.width();
|
screenWidth = primaryMonitorSettings.width();
|
||||||
screenHeight = primaryMonitorSettings.height();
|
screenHeight = primaryMonitorSettings.height();
|
||||||
|
@ -88,7 +84,6 @@ public class GameRenderer {
|
||||||
}
|
}
|
||||||
if (windowHandle == 0) throw new RuntimeException("Failed to create GLFW window.");
|
if (windowHandle == 0) throw new RuntimeException("Failed to create GLFW window.");
|
||||||
inputHandler.setWindowId(windowHandle);
|
inputHandler.setWindowId(windowHandle);
|
||||||
log.debug("Initialized GLFW window.");
|
|
||||||
|
|
||||||
// Setup callbacks.
|
// Setup callbacks.
|
||||||
glfwSetKeyCallback(windowHandle, new PlayerInputKeyCallback(inputHandler));
|
glfwSetKeyCallback(windowHandle, new PlayerInputKeyCallback(inputHandler));
|
||||||
|
@ -101,7 +96,6 @@ public class GameRenderer {
|
||||||
}
|
}
|
||||||
glfwSetInputMode(windowHandle, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
|
glfwSetInputMode(windowHandle, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
|
||||||
glfwSetCursorPos(windowHandle, 0, 0);
|
glfwSetCursorPos(windowHandle, 0, 0);
|
||||||
log.debug("Set up window callbacks.");
|
|
||||||
|
|
||||||
glfwMakeContextCurrent(windowHandle);
|
glfwMakeContextCurrent(windowHandle);
|
||||||
glfwSwapInterval(1);
|
glfwSwapInterval(1);
|
||||||
|
@ -113,17 +107,14 @@ public class GameRenderer {
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glCullFace(GL_BACK);
|
glCullFace(GL_BACK);
|
||||||
log.debug("Initialized OpenGL context.");
|
|
||||||
|
|
||||||
this.chunkRenderer = new ChunkRenderer();
|
this.chunkRenderer = new ChunkRenderer();
|
||||||
log.debug("Initialized chunk renderer.");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.guiRenderer = new GuiRenderer();
|
this.guiRenderer = new GuiRenderer();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
log.debug("Initialized GUI renderer.");
|
|
||||||
|
|
||||||
this.modelRenderer = new ModelRenderer();
|
this.modelRenderer = new ModelRenderer();
|
||||||
try {
|
try {
|
||||||
|
@ -137,7 +128,6 @@ public class GameRenderer {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
log.debug("Initialized model renderer.");
|
|
||||||
updatePerspective(config.fov);
|
updatePerspective(config.fov);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,6 @@ package nl.andrewl.aos2_client.render.chunk;
|
||||||
|
|
||||||
import nl.andrewl.aos_core.model.world.Chunk;
|
import nl.andrewl.aos_core.model.world.Chunk;
|
||||||
import nl.andrewl.aos_core.model.world.World;
|
import nl.andrewl.aos_core.model.world.World;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL46.*;
|
import static org.lwjgl.opengl.GL46.*;
|
||||||
|
|
||||||
|
@ -11,8 +9,6 @@ import static org.lwjgl.opengl.GL46.*;
|
||||||
* Represents a 3d mesh for a chunk.
|
* Represents a 3d mesh for a chunk.
|
||||||
*/
|
*/
|
||||||
public class ChunkMesh {
|
public class ChunkMesh {
|
||||||
private static final Logger log = LoggerFactory.getLogger(ChunkMesh.class);
|
|
||||||
|
|
||||||
private final int vboId;
|
private final int vboId;
|
||||||
private final int vaoId;
|
private final int vaoId;
|
||||||
private final int eboId;
|
private final int eboId;
|
||||||
|
@ -49,20 +45,11 @@ public class ChunkMesh {
|
||||||
* Generates and loads this chunk's mesh into the allocated OpenGL buffers.
|
* Generates and loads this chunk's mesh into the allocated OpenGL buffers.
|
||||||
*/
|
*/
|
||||||
private void loadMesh(ChunkMeshGenerator meshGenerator) {
|
private void loadMesh(ChunkMeshGenerator meshGenerator) {
|
||||||
// long start = System.nanoTime();
|
|
||||||
var meshData = meshGenerator.generateMesh(chunk, world);
|
var meshData = meshGenerator.generateMesh(chunk, world);
|
||||||
// double dur = (System.nanoTime() - start) / 1_000_000.0;
|
|
||||||
this.indexCount = meshData.indexBuffer().limit();
|
this.indexCount = meshData.indexBuffer().limit();
|
||||||
// log.debug(
|
|
||||||
// "Generated mesh for chunk ({}, {}, {}) in {} ms. {} vertices and {} indices.",
|
|
||||||
// chunk.getPosition().x, chunk.getPosition().y, chunk.getPosition().z,
|
|
||||||
// dur,
|
|
||||||
// meshData.vertexBuffer().limit() / 9, indexCount
|
|
||||||
// );
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vboId);
|
glBindBuffer(GL_ARRAY_BUFFER, vboId);
|
||||||
glBufferData(GL_ARRAY_BUFFER, meshData.vertexBuffer(), GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, meshData.vertexBuffer(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, meshData.indexBuffer(), GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, meshData.indexBuffer(), GL_STATIC_DRAW);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,6 @@ import org.joml.Vector3f;
|
||||||
import org.lwjgl.openal.AL;
|
import org.lwjgl.openal.AL;
|
||||||
import org.lwjgl.openal.ALC;
|
import org.lwjgl.openal.ALC;
|
||||||
import org.lwjgl.openal.ALCCapabilities;
|
import org.lwjgl.openal.ALCCapabilities;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
|
@ -21,8 +19,6 @@ import static org.lwjgl.openal.ALC10.*;
|
||||||
* Main class for managing the OpenAL audio interface.
|
* Main class for managing the OpenAL audio interface.
|
||||||
*/
|
*/
|
||||||
public class SoundManager {
|
public class SoundManager {
|
||||||
private static final Logger log = LoggerFactory.getLogger(SoundManager.class);
|
|
||||||
|
|
||||||
private static final int SOURCE_COUNT = 32;
|
private static final int SOURCE_COUNT = 32;
|
||||||
|
|
||||||
private final long alContext;
|
private final long alContext;
|
||||||
|
@ -101,7 +97,7 @@ public class SoundManager {
|
||||||
public void play(String soundName, float gain, Vector3f position, Vector3f velocity) {
|
public void play(String soundName, float gain, Vector3f position, Vector3f velocity) {
|
||||||
Integer bufferId = getSoundBuffer(soundName);
|
Integer bufferId = getSoundBuffer(soundName);
|
||||||
if (bufferId == null) {
|
if (bufferId == null) {
|
||||||
log.warn("Attempted to play unknown sound \"{}\"", soundName);
|
System.err.printf("Attempted to play unknown sound \"%s\".%n", soundName);
|
||||||
} else {
|
} else {
|
||||||
SoundSource source = getNextAvailableSoundSource();
|
SoundSource source = getNextAvailableSoundSource();
|
||||||
if (source != null) {
|
if (source != null) {
|
||||||
|
@ -110,7 +106,7 @@ public class SoundManager {
|
||||||
source.setGain(gain);
|
source.setGain(gain);
|
||||||
source.play(bufferId);
|
source.play(bufferId);
|
||||||
} else {
|
} else {
|
||||||
log.warn("Couldn't get an available sound source to play sound \"{}\"", soundName);
|
System.err.printf("No sound sources available to play sound \"%s\".%n", soundName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
12
core/pom.xml
12
core/pom.xml
|
@ -47,18 +47,6 @@
|
||||||
<artifactId>snakeyaml</artifactId>
|
<artifactId>snakeyaml</artifactId>
|
||||||
<version>1.30</version>
|
<version>1.30</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.slf4j</groupId>
|
|
||||||
<artifactId>slf4j-api</artifactId>
|
|
||||||
<version>1.7.36</version>
|
|
||||||
</dependency>
|
|
||||||
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
|
||||||
<artifactId>log4j-slf4j-impl</artifactId>
|
|
||||||
<version>2.18.0</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
|
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
|
||||||
|
|
|
@ -18,8 +18,6 @@ import nl.andrewl.record_net.Message;
|
||||||
import nl.andrewl.record_net.util.ExtendedDataInputStream;
|
import nl.andrewl.record_net.util.ExtendedDataInputStream;
|
||||||
import nl.andrewl.record_net.util.ExtendedDataOutputStream;
|
import nl.andrewl.record_net.util.ExtendedDataOutputStream;
|
||||||
import org.joml.Vector3i;
|
import org.joml.Vector3i;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
@ -35,8 +33,6 @@ import java.util.concurrent.ForkJoinPool;
|
||||||
* from them.
|
* from them.
|
||||||
*/
|
*/
|
||||||
public class ClientCommunicationHandler {
|
public class ClientCommunicationHandler {
|
||||||
private static final Logger log = LoggerFactory.getLogger(ClientCommunicationHandler.class);
|
|
||||||
|
|
||||||
private final Server server;
|
private final Server server;
|
||||||
private final Socket socket;
|
private final Socket socket;
|
||||||
private final DatagramSocket datagramSocket;
|
private final DatagramSocket datagramSocket;
|
||||||
|
@ -100,7 +96,6 @@ public class ClientCommunicationHandler {
|
||||||
try {
|
try {
|
||||||
Message msg = Net.read(in);
|
Message msg = Net.read(in);
|
||||||
if (msg instanceof ConnectRequestMessage connectMsg) {
|
if (msg instanceof ConnectRequestMessage connectMsg) {
|
||||||
log.debug("Received connect request from player \"{}\"", connectMsg.username());
|
|
||||||
// Ensure the connection is valid.
|
// Ensure the connection is valid.
|
||||||
if (!UsernameChecker.isValid(connectMsg.username())) {
|
if (!UsernameChecker.isValid(connectMsg.username())) {
|
||||||
Net.write(new ConnectRejectMessage("Invalid username."), out);
|
Net.write(new ConnectRejectMessage("Invalid username."), out);
|
||||||
|
@ -119,9 +114,7 @@ public class ClientCommunicationHandler {
|
||||||
connectionEstablished = true;
|
connectionEstablished = true;
|
||||||
this.player = server.getPlayerManager().register(this, connectMsg.username());
|
this.player = server.getPlayerManager().register(this, connectMsg.username());
|
||||||
Net.write(new ConnectAcceptMessage(player.getId()), out);
|
Net.write(new ConnectAcceptMessage(player.getId()), out);
|
||||||
log.debug("Sent connect accept message.");
|
|
||||||
sendInitialData();
|
sendInitialData();
|
||||||
log.debug("Sent initial data.");
|
|
||||||
sendTcpMessage(ChatMessage.privateMessage("Welcome to the server, " + player.getUsername() + "."));
|
sendTcpMessage(ChatMessage.privateMessage("Welcome to the server, " + player.getUsername() + "."));
|
||||||
if (player.getTeam() != null) {
|
if (player.getTeam() != null) {
|
||||||
sendTcpMessage(ChatMessage.privateMessage("You've joined the " + player.getTeam().getName() + " team."));
|
sendTcpMessage(ChatMessage.privateMessage("You've joined the " + player.getTeam().getName() + " team."));
|
||||||
|
@ -134,7 +127,7 @@ public class ClientCommunicationHandler {
|
||||||
} 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.
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warn("An IOException occurred while attempting to establish a connection to a client.", e);
|
System.err.println("An IOException occurred while attempting to establish a connection to a client: " + e.getMessage());
|
||||||
}
|
}
|
||||||
attempts++;
|
attempts++;
|
||||||
}
|
}
|
||||||
|
@ -144,7 +137,7 @@ public class ClientCommunicationHandler {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
log.warn("Player couldn't connect after {} attempts. Aborting connection.", attempts);
|
System.err.printf("Player couldn't connect after %d attempts. Aborting connection.%n", attempts);
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,7 +167,7 @@ public class ClientCommunicationHandler {
|
||||||
DatagramPacket packet = new DatagramPacket(data, data.length, clientAddress, clientUdpPort);
|
DatagramPacket packet = new DatagramPacket(data, data.length, clientAddress, clientUdpPort);
|
||||||
sendDatagramPacket(packet);
|
sendDatagramPacket(packet);
|
||||||
} else {
|
} else {
|
||||||
log.warn("Can't send datagram packet because we don't know the client's UDP port yet.");
|
System.err.println("Can't send datagram packet because we don't know the client's UDP port yet.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,6 @@ import nl.andrewl.aos_core.net.client.*;
|
||||||
import nl.andrewl.aos_core.net.connect.DatagramInit;
|
import nl.andrewl.aos_core.net.connect.DatagramInit;
|
||||||
import nl.andrewl.record_net.Message;
|
import nl.andrewl.record_net.Message;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
|
@ -24,8 +22,6 @@ import java.util.regex.Pattern;
|
||||||
* the server, and components related to that.
|
* the server, and components related to that.
|
||||||
*/
|
*/
|
||||||
public class PlayerManager {
|
public class PlayerManager {
|
||||||
private static final Logger log = LoggerFactory.getLogger(PlayerManager.class);
|
|
||||||
|
|
||||||
private final Server server;
|
private final Server server;
|
||||||
private final Map<Integer, ServerPlayer> players = new HashMap<>();
|
private final Map<Integer, ServerPlayer> players = new HashMap<>();
|
||||||
private final Map<Integer, ClientCommunicationHandler> clientHandlers = new HashMap<>();
|
private final Map<Integer, ClientCommunicationHandler> clientHandlers = new HashMap<>();
|
||||||
|
@ -37,14 +33,14 @@ public class PlayerManager {
|
||||||
|
|
||||||
public synchronized ServerPlayer register(ClientCommunicationHandler handler, String username) {
|
public synchronized ServerPlayer register(ClientCommunicationHandler handler, String username) {
|
||||||
ServerPlayer player = new ServerPlayer(nextClientId++, username);
|
ServerPlayer player = new ServerPlayer(nextClientId++, username);
|
||||||
log.info("Registered player \"{}\" with id {}", 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);
|
||||||
clientHandlers.put(player.getId(), handler);
|
clientHandlers.put(player.getId(), handler);
|
||||||
String joinMessage;
|
String joinMessage;
|
||||||
Team team = findBestTeamForNewPlayer();
|
Team team = findBestTeamForNewPlayer();
|
||||||
if (team != null) {
|
if (team != null) {
|
||||||
player.setTeam(team);
|
player.setTeam(team);
|
||||||
log.info("Player \"{}\" joined the \"{}\" team.", player.getUsername(), team.getName());
|
System.out.printf("Player \"%s\" joined the \"%s\" team.%n", player.getUsername(), team.getName());
|
||||||
joinMessage = String.format("%s joined the %s team.", username, team.getName());
|
joinMessage = String.format("%s joined the %s team.", username, team.getName());
|
||||||
} else {
|
} else {
|
||||||
joinMessage = username + " joined the game.";
|
joinMessage = username + " joined the game.";
|
||||||
|
@ -70,7 +66,7 @@ public class PlayerManager {
|
||||||
players.remove(player.getId());
|
players.remove(player.getId());
|
||||||
clientHandlers.remove(player.getId());
|
clientHandlers.remove(player.getId());
|
||||||
broadcastTcpMessage(new PlayerLeaveMessage(player.getId()));
|
broadcastTcpMessage(new PlayerLeaveMessage(player.getId()));
|
||||||
log.info("Deregistered player \"{}\" with id {}", player.getUsername(), player.getId());
|
System.out.printf("Deregistered player \"%s\" with id %d.%n", player.getUsername(), player.getId());
|
||||||
broadcastTcpMessage(ChatMessage.announce(player.getUsername() + " left the game."));
|
broadcastTcpMessage(ChatMessage.announce(player.getUsername() + " left the game."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,7 +203,6 @@ public class PlayerManager {
|
||||||
if (handler != null) {
|
if (handler != null) {
|
||||||
handler.setClientUdpPort(packet.getPort());
|
handler.setClientUdpPort(packet.getPort());
|
||||||
handler.sendDatagramPacket(init);
|
handler.sendDatagramPacket(init);
|
||||||
log.debug("Echoed player \"{}\"'s UDP init packet.", getPlayer(init.clientId()).getUsername());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,7 +228,7 @@ public class PlayerManager {
|
||||||
handler.sendDatagramPacket(packet);
|
handler.sendDatagramPacket(packet);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warn("An error occurred while broadcasting a UDP message.", e);
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,7 +242,7 @@ public class PlayerManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warn("An error occurred while broadcasting a UDP message.", e);
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,6 @@ import nl.andrewl.aos_core.net.client.ClientOrientationState;
|
||||||
import nl.andrewl.aos_core.net.connect.DatagramInit;
|
import nl.andrewl.aos_core.net.connect.DatagramInit;
|
||||||
import nl.andrewl.record_net.Message;
|
import nl.andrewl.record_net.Message;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
@ -29,8 +27,6 @@ import java.util.List;
|
||||||
import java.util.concurrent.ForkJoinPool;
|
import java.util.concurrent.ForkJoinPool;
|
||||||
|
|
||||||
public class Server implements Runnable {
|
public class Server implements Runnable {
|
||||||
private static final Logger log = LoggerFactory.getLogger(Server.class);
|
|
||||||
|
|
||||||
private final ServerSocket serverSocket;
|
private final ServerSocket serverSocket;
|
||||||
private final DatagramSocket datagramSocket;
|
private final DatagramSocket datagramSocket;
|
||||||
private volatile boolean running;
|
private volatile boolean running;
|
||||||
|
@ -68,7 +64,7 @@ public class Server implements Runnable {
|
||||||
if (Files.isReadable(worldFile)) {
|
if (Files.isReadable(worldFile)) {
|
||||||
this.world = WorldIO.read(worldFile);
|
this.world = WorldIO.read(worldFile);
|
||||||
} else {
|
} else {
|
||||||
log.error("Cannot read world file: {}", worldFile.toAbsolutePath());
|
System.err.println("Cannot read world file: " + worldFile.toAbsolutePath());
|
||||||
this.world = Worlds.arena();
|
this.world = Worlds.arena();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,11 +79,11 @@ public class Server implements Runnable {
|
||||||
running = true;
|
running = true;
|
||||||
new Thread(new UdpReceiver(datagramSocket, this::handleUdpMessage)).start();
|
new Thread(new UdpReceiver(datagramSocket, this::handleUdpMessage)).start();
|
||||||
new Thread(worldUpdater).start();
|
new Thread(worldUpdater).start();
|
||||||
log.info("Started AoS2 Server on TCP/UDP port {}; now accepting connections.", serverSocket.getLocalPort());
|
System.out.printf("Started AoS2 Server on TCP/UDP port %d; now accepting connections.%n", serverSocket.getLocalPort());
|
||||||
while (running) {
|
while (running) {
|
||||||
acceptClientConnection();
|
acceptClientConnection();
|
||||||
}
|
}
|
||||||
log.info("Shutting down the server.");
|
System.out.println("Shutting down the server.");
|
||||||
playerManager.deregisterAll();
|
playerManager.deregisterAll();
|
||||||
worldUpdater.shutdown();
|
worldUpdater.shutdown();
|
||||||
datagramSocket.close(); // Shuts down the UdpReceiver.
|
datagramSocket.close(); // Shuts down the UdpReceiver.
|
||||||
|
@ -96,7 +92,6 @@ public class Server implements Runnable {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
log.info("Shutdown complete.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isRunning() {
|
public boolean isRunning() {
|
||||||
|
|
|
@ -2,8 +2,6 @@ package nl.andrewl.aos2_server.logic;
|
||||||
|
|
||||||
import nl.andrewl.aos2_server.Server;
|
import nl.andrewl.aos2_server.Server;
|
||||||
import org.joml.Math;
|
import org.joml.Math;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A runnable to run as a separate thread, to periodically update the server's
|
* A runnable to run as a separate thread, to periodically update the server's
|
||||||
|
@ -11,8 +9,6 @@ import org.slf4j.LoggerFactory;
|
||||||
* game engine, as it controls the game's main update pattern.
|
* game engine, as it controls the game's main update pattern.
|
||||||
*/
|
*/
|
||||||
public class WorldUpdater implements Runnable {
|
public class WorldUpdater implements Runnable {
|
||||||
private static final Logger log = LoggerFactory.getLogger(WorldUpdater.class);
|
|
||||||
|
|
||||||
private final Server server;
|
private final Server server;
|
||||||
private final float ticksPerSecond;
|
private final float ticksPerSecond;
|
||||||
private final float secondsPerTick;
|
private final float secondsPerTick;
|
||||||
|
@ -30,15 +26,16 @@ public class WorldUpdater implements Runnable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
final long msPerTick = (long) (Math.floor(1.0 / ticksPerSecond) * 1_000);
|
||||||
final long nsPerTick = (long) Math.floor((1.0 / ticksPerSecond) * 1_000_000_000.0);
|
final long nsPerTick = (long) Math.floor((1.0 / ticksPerSecond) * 1_000_000_000.0);
|
||||||
log.debug("Running world updater at {} ticks per second, or {} ns per tick.", ticksPerSecond, nsPerTick);
|
System.out.printf("Running world updater at %d ms/tick, or %d ns/tick.%n", msPerTick, nsPerTick);
|
||||||
running = true;
|
running = true;
|
||||||
while (running) {
|
while (running) {
|
||||||
long start = System.nanoTime();
|
long start = System.nanoTime();
|
||||||
tick(System.currentTimeMillis());
|
tick(System.currentTimeMillis());
|
||||||
long elapsedNs = System.nanoTime() - start;
|
long elapsedNs = System.nanoTime() - start;
|
||||||
if (elapsedNs > nsPerTick) {
|
if (elapsedNs > nsPerTick) {
|
||||||
log.warn("Took {} ns to do one tick, which is more than the desired {} ns per tick.", elapsedNs, nsPerTick);
|
System.err.printf("Took %d ns to do one tick, which is more than the desired %d ns per tick.%n", elapsedNs, nsPerTick);
|
||||||
} else {
|
} else {
|
||||||
long sleepTime = nsPerTick - elapsedNs;
|
long sleepTime = nsPerTick - elapsedNs;
|
||||||
long ms = sleepTime / 1_000_000;
|
long ms = sleepTime / 1_000_000;
|
||||||
|
|
|
@ -7,8 +7,6 @@ import nl.andrewl.aos_core.model.item.GunItemStack;
|
||||||
import nl.andrewl.aos_core.model.item.Inventory;
|
import nl.andrewl.aos_core.model.item.Inventory;
|
||||||
import nl.andrewl.aos_core.model.item.ItemTypes;
|
import nl.andrewl.aos_core.model.item.ItemTypes;
|
||||||
import nl.andrewl.aos_core.net.client.PlayerUpdateMessage;
|
import nl.andrewl.aos_core.net.client.PlayerUpdateMessage;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
@ -17,8 +15,6 @@ import java.util.ArrayList;
|
||||||
* needed for the server.
|
* needed for the server.
|
||||||
*/
|
*/
|
||||||
public class ServerPlayer extends Player {
|
public class ServerPlayer extends Player {
|
||||||
private static final Logger log = LoggerFactory.getLogger(ServerPlayer.class);
|
|
||||||
|
|
||||||
private final PlayerActionManager actionManager;
|
private final PlayerActionManager actionManager;
|
||||||
private final Inventory inventory;
|
private final Inventory inventory;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue