option to send all console message to render chat box (and Log improvements)

This commit is contained in:
Bixilon 2021-02-26 21:36:32 +01:00
parent bc9ffd84c3
commit 38bd37b9f0
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 52 additions and 24 deletions

View File

@ -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)

View File

@ -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<ChatFormattingCode> 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);
}
}
}
}
}

View File

@ -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<ChatFormattingCode> = BetterHashSet()
constructor(text: String, color: RGBColor?, formatting: BetterHashSet<ChatFormattingCode>) {
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<Node>): ObservableList<Node> {
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)

View File

@ -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;

View File

@ -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<String> LOG_QUEUE = new LinkedBlockingQueue<>();
private static final LinkedBlockingQueue<ChatComponent> 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);
}
/**