Skip to content

Commit

Permalink
refactor(Console): Start working on console implementation
Browse files Browse the repository at this point in the history
refactor(Logger): Start working on logger implementation
  • Loading branch information
xGab0 committed Jun 19, 2024
1 parent 1dae251 commit 970407a
Show file tree
Hide file tree
Showing 13 changed files with 640 additions and 12 deletions.
36 changes: 26 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,41 @@ repositories {
}

dependencies {
val minestom_version = findProperty("minestom_version")
val minestom_extensions_version = findProperty("minestom_extensions_version")
val jnoise_version = findProperty("jnoise_version")
val polar_version = findProperty("polar_version")
val minimessage_version = findProperty("minimessage_version")
val jackson_version = findProperty("jackson_version")

// Important
val minestom_version = findProperty("minestom_version")
val minestom_extensions_version = findProperty("minestom_extensions_version")

val slimeloader_version = findProperty("slimeloader_version")
val polar_version = findProperty("polar_version")

val minimessage_version = findProperty("minimessage_version")
val jackson_version = findProperty("jackson_version")
val jnoise_version = findProperty("jnoise_version")

val ansi_version = findProperty("ansi_version")
val jansi_version = findProperty("jansi_version")
val jline_version = findProperty("jline_version")
val jline_terminal_jansi_version = findProperty("jline_terminal_jansi_version")
val log4j_version = findProperty("log4j_version")

// Minestom modules
implementation("net.minestom", "minestom-snapshots", "$minestom_version")
implementation("dev.hollowcube", "minestom-ce-extensions", "$minestom_extensions_version")
implementation("de.articdive", "jnoise-pipeline", "$jnoise_version")

// World formats
implementation("com.github.CatDevz", "SlimeLoader", "master-SNAPSHOT")
implementation("com.github.CatDevz", "SlimeLoader", "$slimeloader_version")
implementation("dev.hollowcube", "polar", "$polar_version")

// Misc
implementation("net.kyori", "adventure-text-minimessage", "$minimessage_version")
implementation("com.fasterxml.jackson.core", "jackson-databind", "$jackson_version")
implementation("de.articdive", "jnoise-pipeline", "$jnoise_version")

// Terminal & Logging
implementation("net.kyori", "ansi", "$ansi_version")
implementation("org.fusesource.jansi", "jansi", "$jansi_version")
implementation("org.jline", "jline", "$jline_version")
implementation("org.jline", "jline-terminal-jansi", "$jline_terminal_jansi_version")
implementation("org.apache.logging.log4j", "log4j-core", "$log4j_version")
}

tasks {
Expand Down
10 changes: 8 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ description = A launcher made for Minestom
# libraries versions
minestom_version = f1d5940855
minestom_extensions_version = 1.2.0
jnoise_version = 4.1.0-SNAPSHOT
polar_version = 1.9.5
slimeloader_version = master-SNAPSHOT
minimessage_version = 4.14.0
jackson_version = 2.15.2
jackson_version = 2.15.2
jnoise_version = 4.1.0-SNAPSHOT
ansi_version = 1.0.3
jansi_version = 2.4.1
jline_version = 3.26.2
jline_terminal_jansi_version = 3.26.2
log4j_version = 2.23.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.thecrownstudios.minestomlauncher.console;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.appender.rewrite.RewritePolicy;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.jetbrains.annotations.Nullable;
import org.jline.reader.LineReader;

final class ConsoleAppender extends AbstractAppender {
private final LineReader lineReader;
private final @Nullable RewritePolicy rewriter;

ConsoleAppender(
final LineReader lineReader,
final String logPattern,
final @Nullable RewritePolicy rewritePolicy
) {
super(
"Console",
null,
PatternLayout.newBuilder().withPattern(logPattern).build(),
false,
new Property[0]
);
this.lineReader = lineReader;
this.rewriter = rewritePolicy;
}

private LogEvent rewrite(final LogEvent event) {
return this.rewriter == null ? event : this.rewriter.rewrite(event);
}

@Override
public void append(final LogEvent event) {
if (this.lineReader.isReading()) {
this.lineReader.callWidget(LineReader.CLEAR);
}

this.lineReader.getTerminal().writer().print(this.getLayout().toSerializable(this.rewrite(event)).toString());

if (this.lineReader.isReading()) {
this.lineReader.callWidget(LineReader.REDRAW_LINE);
this.lineReader.callWidget(LineReader.REDISPLAY);
}
this.lineReader.getTerminal().writer().flush();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.thecrownstudios.minestomlauncher.console;

import java.nio.file.Paths;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.jetbrains.annotations.Nullable;
import org.jline.reader.Completer;
import org.jline.reader.Highlighter;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;

public final class ConsoleSetup {

private ConsoleSetup() {
}

public static final String APP_NAME = "Minestom Server";
public static final String LOG_PATTERN = "%highlight{[%d{HH:mm:ss} %level] [%t]: [%logger{1}]}{FATAL=red, ERROR=red, WARN=yellow, INFO=default, DEBUG=yellow, TRACE=blue} %paperMinecraftFormatting{%msg}%n";


private static LineReader buildLineReader(
final Completer completer,
final Highlighter highlighter
) {
return LineReaderBuilder.builder()
.appName(APP_NAME)
.variable(LineReader.HISTORY_FILE, Paths.get(".console_history"))
.completer(completer)
.highlighter(highlighter)
.option(LineReader.Option.INSERT_TAB, false)
.option(LineReader.Option.DISABLE_EVENT_EXPANSION, true)
.option(LineReader.Option.COMPLETE_IN_WORD, true)
.build();
}

public static ConsoleState init(
final @Nullable Object remapper,
final String logPattern
) {
final DelegatingCompleter delegatingCompleter = new DelegatingCompleter();
final DelegatingHighlighter delegatingHighlighter = new DelegatingHighlighter();
final LineReader lineReader = buildLineReader(
delegatingCompleter,
delegatingHighlighter
);

final ConsoleAppender consoleAppender = new ConsoleAppender(
lineReader,
LOG_PATTERN,
null
//remapper != null ? new RemappingRewriter(remapper) : null
);
consoleAppender.start();

final Logger logger = (Logger) LogManager.getRootLogger();
final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
final LoggerConfig loggerConfig = loggerContext.getConfiguration().getLoggerConfig(logger.getName());

// replace SysOut appender with ConsoleAppender
loggerConfig.removeAppender("SysOut");
loggerConfig.addAppender(consoleAppender, loggerConfig.getLevel(), null);
loggerContext.updateLoggers();

return new ConsoleState(lineReader, delegatingCompleter, delegatingHighlighter);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.thecrownstudios.minestomlauncher.console;

import org.jline.reader.LineReader;

public record ConsoleState(
LineReader lineReader,
DelegatingCompleter completer,
DelegatingHighlighter highlighter
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.thecrownstudios.minestomlauncher.console;

import net.minestom.server.MinecraftServer;
import net.minestom.server.ServerProcess;
import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.UserInterruptException;

public final class ConsoleThread extends Thread {

private static final String TERMINAL_PROMPT = "> ";
private static final String STOP_COMMAND = "stop";

private final ServerProcess server;
private final LineReader lineReader;

public ConsoleThread(
final ServerProcess server,
final LineReader lineReader
) {
super("Console thread");
this.server = server;
this.lineReader = lineReader;
}

@Override
public void run() {
MinecraftServer.LOGGER.info("Initialized Better Fabric Console console thread.");
this.acceptInput();
}

private static boolean isRunning(final ServerProcess server) {
//return !server.isStopped() && server.isRunning();
return server.isAlive();
}

private void acceptInput() {
while (isRunning(this.server)) {
try {
final String input = this.lineReader.readLine(TERMINAL_PROMPT).trim();
if (input.isEmpty()) {
continue;
}

//this.server.handleConsoleInput(input, this.server.createCommandSourceStack());
if (input.equals(STOP_COMMAND)) {
break;
}
} catch (final EndOfFileException | UserInterruptException ex) {
//this.server.handleConsoleInput(STOP_COMMAND, this.server.createCommandSourceStack());
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.thecrownstudios.minestomlauncher.console;

import java.util.List;

import org.jetbrains.annotations.Nullable;
import org.jline.reader.Candidate;
import org.jline.reader.Completer;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;

public final class DelegatingCompleter implements Completer {
private @Nullable Completer delegate;

@Override
public void complete(final LineReader reader, final ParsedLine line, final List<Candidate> candidates) {
if (this.delegate != null) {
this.delegate.complete(reader, line, candidates);
}
}

public void delegateTo(final Completer completer) {
this.delegate = completer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.thecrownstudios.minestomlauncher.console;

import java.util.regex.Pattern;

import org.jetbrains.annotations.Nullable;
import org.jline.reader.Highlighter;
import org.jline.reader.LineReader;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;

public final class DelegatingHighlighter implements Highlighter {
private @Nullable Highlighter delegate;

@Override
public AttributedString highlight(final LineReader reader, final String buffer) {
if (this.delegate != null) {
return this.delegate.highlight(reader, buffer);
}
final AttributedStringBuilder builder = new AttributedStringBuilder();
builder.append(buffer, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
return builder.toAttributedString();
}

@Override
public void setErrorPattern(final Pattern errorPattern) {
if (this.delegate != null) {
this.delegate.setErrorPattern(errorPattern);
}
}

@Override
public void setErrorIndex(final int errorIndex) {
if (this.delegate != null) {
this.delegate.setErrorIndex(errorIndex);
}
}

public void delegateTo(final Highlighter highlighter) {
this.delegate = highlighter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.thecrownstudios.minestomlauncher.console;

import java.util.List;
import java.util.Optional;
//import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer;
import net.minestom.server.MinecraftServer;
import org.jline.reader.Candidate;
import org.jline.reader.Completer;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;

public record MinecraftCommandCompleter(MinecraftServer server) implements Completer {

@Override
public void complete(final LineReader reader, final ParsedLine line, final List<Candidate> candidates) {
/*
final StringReader stringReader = Util.prepareStringReader(line.line());
final ParseResults<CommandSourceStack> results = this.server.getCommands().getDispatcher().parse(stringReader, this.server.createCommandSourceStack());
final CompletableFuture<Suggestions> suggestionsFuture = this.server.getCommands().getDispatcher().getCompletionSuggestions(results, line.cursor());
final Suggestions suggestions = suggestionsFuture.join();
for (final Suggestion suggestion : suggestions.getList()) {
final String suggestionText = suggestion.getText();
if (suggestionText.isEmpty()) {
continue;
}
final @Nullable String description = Optional.ofNullable(suggestion.getTooltip())
.map(tooltip -> {
final Component tooltipComponent = ComponentUtils.fromMessage(tooltip);
return tooltipComponent.equals(Component.empty()) ? null : tooltipComponent.asComponent();
})
.map(adventure -> ANSIComponentSerializer.ansi().serialize(adventure))
.orElse(null);
candidates.add(new Candidate(
suggestionText,
suggestionText,
null,
description,
null,
null,
false
));
}
*/
}

}
Loading

0 comments on commit 970407a

Please sign in to comment.