From 3f1aa921e37870348b956e6ff89f83f54de46f39 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sat, 27 Feb 2021 15:55:36 +0100 Subject: [PATCH] rendering: limit chat messages, improve to chat logging, movement: send rotation or position or both --- .../bixilon/minosoft/gui/rendering/Camera.kt | 24 +++++++++----- .../hud/elements/text/HUDChatElement.kt | 7 +++++ .../play/PacketPlayerPositionSending.java | 31 ++++++------------- .../play/PacketPlayerRotationSending.java | 15 +++++---- .../de/bixilon/minosoft/util/logging/Log.java | 12 +++---- 5 files changed, 46 insertions(+), 43 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/Camera.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/Camera.kt index 32146aefa..fd0dc52f4 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/Camera.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/Camera.kt @@ -27,6 +27,8 @@ import de.bixilon.minosoft.gui.rendering.chunk.Frustum import de.bixilon.minosoft.gui.rendering.shader.Shader import de.bixilon.minosoft.protocol.network.Connection import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketPlayerPositionAndRotationSending +import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketPlayerPositionSending +import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketPlayerRotationSending import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition import glm_.glm import glm_.mat4x4.Mat4 @@ -48,8 +50,9 @@ class Camera( var pitch = 0.0 private var zoom = 0f - private var lastPositionChange = 0L + private var lastMovementPacketSent = 0L private var currentPositionSent = false + private var currentRotationSent = false var cameraFront = Vec3(0.0f, 0.0f, -1.0f) var cameraRight = Vec3(0.0f, 0.0f, -1.0f) @@ -164,6 +167,7 @@ class Camera( } if (lastPosition != cameraPosition) { recalculateViewProjectionMatrix() + currentPositionSent = false sendPositionToServer() } @@ -242,24 +246,30 @@ class Camera( cameraRight = cameraFront.cross(CAMERA_UP_VEC3).normalize() cameraUp = cameraRight.cross(cameraFront).normalize() recalculateViewProjectionMatrix() + currentRotationSent = false sendPositionToServer() } fun draw() { - if (!currentPositionSent) { + if (!currentPositionSent || !currentRotationSent) { sendPositionToServer() } } private fun sendPositionToServer() { - if (System.currentTimeMillis() - lastPositionChange > ProtocolDefinition.TICK_TIME) { - // ToDo: Replace this with proper movement and only send it, when our position changed - connection.sendPacket(PacketPlayerPositionAndRotationSending(feetLocation, EntityRotation(yaw, pitch), false)) - lastPositionChange = System.currentTimeMillis() + if (System.currentTimeMillis() - lastMovementPacketSent > ProtocolDefinition.TICK_TIME) { + if (!currentPositionSent && !currentPositionSent) { + connection.sendPacket(PacketPlayerPositionAndRotationSending(feetLocation, EntityRotation(yaw, pitch), false)) + } else if (!currentPositionSent) { + connection.sendPacket(PacketPlayerPositionSending(feetLocation, false)) + } else { + connection.sendPacket(PacketPlayerRotationSending(EntityRotation(yaw, pitch), false)) + } + lastMovementPacketSent = System.currentTimeMillis() currentPositionSent = true + currentRotationSent = true return } - currentPositionSent = false } fun setPosition(location: Position) { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDChatElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDChatElement.kt index f6de0604f..66ce76abd 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDChatElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDChatElement.kt @@ -25,6 +25,9 @@ class HUDChatElement(hudTextElement: HUDTextElement) : HUDText { init { hudTextElement.connection.registerEvent(EventInvokerCallback { + if (chatMessages.size > MAX_MESSAGES_IN_CHAT) { + chatMessages.remove(chatMessages.iterator().next()) + } chatMessages.add(Pair(it.message, System.currentTimeMillis())) }) } @@ -40,4 +43,8 @@ class HUDChatElement(hudTextElement: HUDTextElement) : HUDText { } } } + + companion object { + const val MAX_MESSAGES_IN_CHAT = 20 + } } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketPlayerPositionSending.java b/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketPlayerPositionSending.java index 7de110016..88b212222 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketPlayerPositionSending.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketPlayerPositionSending.java @@ -13,6 +13,7 @@ package de.bixilon.minosoft.protocol.packets.serverbound.play; +import de.bixilon.minosoft.data.entities.Position; import de.bixilon.minosoft.protocol.network.Connection; import de.bixilon.minosoft.protocol.packets.ServerboundPacket; import de.bixilon.minosoft.protocol.protocol.OutPacketBuffer; @@ -22,43 +23,29 @@ import de.bixilon.minosoft.util.logging.Log; import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_14W06B; public class PacketPlayerPositionSending implements ServerboundPacket { - private final double x; - private final double feetY; - private final double headY; - private final double z; + private final Position position; private final boolean onGround; - public PacketPlayerPositionSending(double x, double feetY, double headY, double z, boolean onGround) { - this.x = x; - this.feetY = feetY; - this.headY = headY; - this.z = z; - this.onGround = onGround; - } - - public PacketPlayerPositionSending(double x, double feetY, double z, boolean onGround) { - this.x = x; - this.feetY = feetY; - this.headY = feetY - 1.62F; - this.z = z; + public PacketPlayerPositionSending(Position position, boolean onGround) { + this.position = position; this.onGround = onGround; } @Override public OutPacketBuffer write(Connection connection) { OutPacketBuffer buffer = new OutPacketBuffer(connection, Packets.Serverbound.PLAY_PLAYER_POSITION); - buffer.writeDouble(this.x); - buffer.writeDouble(this.feetY); + buffer.writeDouble(this.position.getX()); + buffer.writeDouble(this.position.getY()); if (buffer.getVersionId() < V_14W06B) { - buffer.writeDouble(this.headY); + buffer.writeDouble(0.0); // TODo } - buffer.writeDouble(this.z); + buffer.writeDouble(this.position.getZ()); buffer.writeBoolean(this.onGround); return buffer; } @Override public void log() { - Log.protocol(String.format("[OUT] Sending player position: %s %s %s", this.x, this.headY, this.z)); + Log.protocol(String.format("[OUT] Sending player position (position=%s)", this.position)); } } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketPlayerRotationSending.java b/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketPlayerRotationSending.java index 1311666b3..163581eba 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketPlayerRotationSending.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketPlayerRotationSending.java @@ -13,6 +13,7 @@ package de.bixilon.minosoft.protocol.packets.serverbound.play; +import de.bixilon.minosoft.data.entities.EntityRotation; import de.bixilon.minosoft.protocol.network.Connection; import de.bixilon.minosoft.protocol.packets.ServerboundPacket; import de.bixilon.minosoft.protocol.protocol.OutPacketBuffer; @@ -20,27 +21,25 @@ import de.bixilon.minosoft.protocol.protocol.Packets; import de.bixilon.minosoft.util.logging.Log; public class PacketPlayerRotationSending implements ServerboundPacket { - private final float yaw; - private final float pitch; + private final EntityRotation rotation; private final boolean onGround; - public PacketPlayerRotationSending(float yaw, float pitch, boolean onGround) { - this.yaw = yaw; - this.pitch = pitch; + public PacketPlayerRotationSending(EntityRotation rotation, boolean onGround) { + this.rotation = rotation; this.onGround = onGround; } @Override public OutPacketBuffer write(Connection connection) { OutPacketBuffer buffer = new OutPacketBuffer(connection, Packets.Serverbound.PLAY_PLAYER_ROTATION); - buffer.writeFloat(this.yaw); - buffer.writeFloat(this.pitch); + buffer.writeFloat(this.rotation.getYaw()); + buffer.writeFloat(this.rotation.getPitch()); buffer.writeBoolean(this.onGround); return buffer; } @Override public void log() { - Log.protocol(String.format("[OUT] Sending player rotation (yaw=%s, pitch=%s)", this.yaw, this.pitch)); + Log.protocol(String.format("[OUT] Sending player rotation (rotation=%s)", this.rotation)); } } diff --git a/src/main/java/de/bixilon/minosoft/util/logging/Log.java b/src/main/java/de/bixilon/minosoft/util/logging/Log.java index 2d4e8d28f..f45076f57 100644 --- a/src/main/java/de/bixilon/minosoft/util/logging/Log.java +++ b/src/main/java/de/bixilon/minosoft/util/logging/Log.java @@ -20,6 +20,7 @@ import de.bixilon.minosoft.data.text.BaseComponent; import de.bixilon.minosoft.data.text.ChatColors; import de.bixilon.minosoft.data.text.ChatComponent; import de.bixilon.minosoft.data.text.RGBColor; +import de.bixilon.minosoft.util.Pair; import java.io.PrintStream; import java.text.SimpleDateFormat; @@ -28,7 +29,7 @@ import java.util.concurrent.LinkedBlockingQueue; public class Log { public static final long MINOSOFT_START_TIME = System.currentTimeMillis(); private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); - private static final LinkedBlockingQueue LOG_QUEUE = new LinkedBlockingQueue<>(); + private static final LinkedBlockingQueue> LOG_QUEUE = new LinkedBlockingQueue<>(); // prefix, message private static final PrintStream SYSTEM_ERR_STREAM = System.err; private static final PrintStream SYSTEM_OUT_STREAM = System.out; private static final PrintStream ERROR_PRINT_STREAM = new LogPrintStream(LogLevels.WARNING); @@ -41,18 +42,18 @@ public class Log { new Thread(() -> { while (true) { // something to print - ChatComponent message; + Pair message; try { message = LOG_QUEUE.take(); } catch (InterruptedException e) { e.printStackTrace(); continue; } - SYSTEM_OUT_STREAM.println(message.getANSIColoredMessage()); + SYSTEM_OUT_STREAM.println(message.getKey().getANSIColoredMessage() + message.getValue().getANSIColoredMessage()); if (StaticConfiguration.SHOW_LOG_MESSAGES_IN_CHAT) { for (var connection : Minosoft.CONNECTIONS.values()) { - connection.getSender().sendFakeChatMessage(message, ChatTextPositions.CHAT_BOX); + connection.getSender().sendFakeChatMessage(message.getValue(), ChatTextPositions.CHAT_BOX); } } // ToDo: log to file @@ -111,8 +112,7 @@ public class Log { if (color != null && StaticConfiguration.COLORED_LOG) { messageComponent.applyDefaultColor(color); } - component.append(messageComponent); - LOG_QUEUE.add(component); + LOG_QUEUE.add(new Pair<>(component, messageComponent)); } /**