From 38bd37b9f0e7d14eb9d4fad37a211c7be47c0cdc Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 26 Feb 2021 21:36:32 +0100 Subject: [PATCH] option to send all console message to render chat box (and Log improvements) --- .../minosoft/config/StaticConfiguration.java | 1 + .../minosoft/data/text/BaseComponent.java | 16 +++++++++-- .../minosoft/data/text/TextComponent.kt | 27 ++++++++++-------- .../protocol/protocol/ProtocolDefinition.java | 4 +++ .../de/bixilon/minosoft/util/logging/Log.java | 28 +++++++++++-------- 5 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/config/StaticConfiguration.java b/src/main/java/de/bixilon/minosoft/config/StaticConfiguration.java index e184d4052..280ceecc3 100644 --- a/src/main/java/de/bixilon/minosoft/config/StaticConfiguration.java +++ b/src/main/java/de/bixilon/minosoft/config/StaticConfiguration.java @@ -22,6 +22,7 @@ public class StaticConfiguration { public static final boolean DEBUG_MODE = true; // if true, additional checks will be made to validate data, ... Decreases performance public static final boolean BIOME_DEBUG_MODE = false; // colors all biomes according to the biome hashCode public static final boolean DEBUG_SLOW_LOADING = false; // if true, many Thread.sleep will be executed and the start will be delayed (by a lot) + public static final boolean SHOW_LOG_MESSAGES_IN_CHAT = false; // prints all console messages in the chat box public static String CONFIG_FILENAME = "minosoft.json"; // Filename of minosoft's base configuration (located in AppData/Minosoft/config) public static boolean SKIP_MOJANG_AUTHENTICATION; // disables all connections to mojang public static boolean COLORED_LOG = true; // the log should be colored with ANSI (does not affect base components) diff --git a/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.java b/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.java index 522bd8e21..745b13d1a 100644 --- a/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.java +++ b/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.java @@ -49,7 +49,7 @@ public class BaseComponent extends ChatComponent { public BaseComponent(MinecraftLocaleManager localeManager, @Nullable ChatComponent parent, String text) { // legacy String StringBuilder currentText = new StringBuilder(); - RGBColor color = ChatColors.WHITE; + RGBColor color = null; BetterHashSet formattingCodes = new BetterHashSet<>(); StringCharacterIterator iterator = new StringCharacterIterator(text); while (iterator.current() != CharacterIterator.DONE) { @@ -180,7 +180,7 @@ public class BaseComponent extends ChatComponent { @Override public String toString() { - return PostChatFormattingCodes.RESET.getANSI() + getANSIColoredMessage(); + return getLegacyText(); } @Override @@ -245,4 +245,16 @@ public class BaseComponent extends ChatComponent { public boolean isEmpty() { return this.parts.isEmpty(); } + + public void applyDefaultColor(RGBColor color) { + for (var part : this.parts) { + if (part instanceof BaseComponent baseComponent) { + baseComponent.applyDefaultColor(color); + } else if (part instanceof TextComponent textComponent) { + if (textComponent.getColor() == null) { + textComponent.setColor(color); + } + } + } + } } diff --git a/src/main/java/de/bixilon/minosoft/data/text/TextComponent.kt b/src/main/java/de/bixilon/minosoft/data/text/TextComponent.kt index a020bcf8f..f231d5b87 100644 --- a/src/main/java/de/bixilon/minosoft/data/text/TextComponent.kt +++ b/src/main/java/de/bixilon/minosoft/data/text/TextComponent.kt @@ -39,18 +39,17 @@ import java.util.function.Consumer open class TextComponent : ChatComponent { private val text: String - var color: RGBColor = ChatColors.WHITE + var color: RGBColor? = null + private set var formatting: BetterHashSet = BetterHashSet() constructor(text: String, color: RGBColor?, formatting: BetterHashSet) { this.text = text - if (color != null) { - this.color = color - } + this.color = color this.formatting = formatting } - constructor(text: String, color: RGBColor) { + constructor(text: String, color: RGBColor?) { this.text = text this.color = color } @@ -89,7 +88,7 @@ open class TextComponent : ChatComponent { return this } - fun setColor(color: RGBColor): TextComponent { + fun setColor(color: RGBColor?): TextComponent { this.color = color return this } @@ -115,12 +114,14 @@ open class TextComponent : ChatComponent { } override fun toString(): String { - return ansiColoredMessage + return legacyText } override fun getANSIColoredMessage(): String { val builder = StringBuilder() - builder.append(ChatColors.getANSIColorByRGBColor(this.color)) + this.color?.let { + builder.append(ChatColors.getANSIColorByRGBColor(it)) + } for (formattingCode in this.formatting) { if (formattingCode is PreChatFormattingCodes) { @@ -139,9 +140,11 @@ open class TextComponent : ChatComponent { override fun getLegacyText(): String { val output = StringBuilder() - val colorChar = ChatColors.getColorId(color) - if (colorChar != null) { - output.append(ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR).append(Integer.toHexString(colorChar)) + if (color != null) { + val colorChar = ChatColors.getColorId(color) + if (colorChar != null) { + output.append(ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR).append(Integer.toHexString(colorChar)) + } } formatting.forEach(Consumer { chatFormattingCode: ChatFormattingCode -> output.append(ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR).append(chatFormattingCode.char) }) output.append(text) @@ -155,6 +158,7 @@ open class TextComponent : ChatComponent { override fun getJavaFXText(nodes: ObservableList): ObservableList { val text = Text(text) + val color = this.color ?: ProtocolDefinition.DEFAULT_COLOR text.fill = Color.WHITE if (Minosoft.getConfig().config.chat.colored) { text.fill = Color.rgb(color.red, color.green, color.blue) @@ -204,6 +208,7 @@ open class TextComponent : ChatComponent { } override fun addVerticies(startPosition: Vec2, offset: Vec2, perspectiveMatrix: Mat4, binding: FontBindings, font: Font, hudScale: HUDScale, mesh: HUDFontMesh, maxSize: Vec2) { + val color = this.color ?: ProtocolDefinition.DEFAULT_COLOR fun drawLetterVertex(position: Vec3, uv: Vec2, atlasPage: Int) { val matrixPosition = perspectiveMatrix * Vec4(position.x, position.y, 0f, 1f) diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java index 2a80c24bb..8d063d433 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java @@ -14,6 +14,8 @@ package de.bixilon.minosoft.protocol.protocol; import de.bixilon.minosoft.data.mappings.ResourceLocation; +import de.bixilon.minosoft.data.text.ChatColors; +import de.bixilon.minosoft.data.text.RGBColor; import java.net.InetAddress; import java.util.regex.Pattern; @@ -98,6 +100,8 @@ public final class ProtocolDefinition { public static final ResourceLocation AIR_RESOURCE_LOCATION = new ResourceLocation("air"); + public static final RGBColor DEFAULT_COLOR = ChatColors.WHITE; + static { // java does (why ever) not allow to directly assign a null InetAddress tempInetAddress; 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 6fa1877ae..2d4e8d28f 100644 --- a/src/main/java/de/bixilon/minosoft/util/logging/Log.java +++ b/src/main/java/de/bixilon/minosoft/util/logging/Log.java @@ -13,9 +13,12 @@ package de.bixilon.minosoft.util.logging; +import de.bixilon.minosoft.Minosoft; import de.bixilon.minosoft.config.StaticConfiguration; +import de.bixilon.minosoft.data.ChatTextPositions; +import de.bixilon.minosoft.data.text.BaseComponent; import de.bixilon.minosoft.data.text.ChatColors; -import de.bixilon.minosoft.data.text.PostChatFormattingCodes; +import de.bixilon.minosoft.data.text.ChatComponent; import de.bixilon.minosoft.data.text.RGBColor; import java.io.PrintStream; @@ -25,7 +28,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<>(); 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); @@ -38,15 +41,20 @@ public class Log { new Thread(() -> { while (true) { // something to print - String message; + ChatComponent message; try { message = LOG_QUEUE.take(); } catch (InterruptedException e) { e.printStackTrace(); continue; } - SYSTEM_OUT_STREAM.println(message); + SYSTEM_OUT_STREAM.println(message.getANSIColoredMessage()); + if (StaticConfiguration.SHOW_LOG_MESSAGES_IN_CHAT) { + for (var connection : Minosoft.CONNECTIONS.values()) { + connection.getSender().sendFakeChatMessage(message, ChatTextPositions.CHAT_BOX); + } + } // ToDo: log to file } }, "Log").start(); @@ -98,15 +106,13 @@ public class Log { builder.append(level.name()); builder.append("] "); builder.append(prefix); + var component = (BaseComponent) ChatComponent.valueOf(builder.toString()); + var messageComponent = (BaseComponent) ChatComponent.valueOf(message); if (color != null && StaticConfiguration.COLORED_LOG) { - builder.append(ChatColors.getANSIColorByRGBColor(color)); - builder.append(message); - builder.append(PostChatFormattingCodes.RESET.getANSI()); - } else { - builder.append(message); + messageComponent.applyDefaultColor(color); } - builder.append(PostChatFormattingCodes.RESET.getANSI()); - LOG_QUEUE.add(builder.toString()); + component.append(messageComponent); + LOG_QUEUE.add(component); } /**