-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Console): Start working on console implementation
refactor(Logger): Start working on logger implementation
- Loading branch information
Showing
13 changed files
with
640 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/thecrownstudios/minestomlauncher/console/ConsoleAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/thecrownstudios/minestomlauncher/console/ConsoleSetup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/thecrownstudios/minestomlauncher/console/ConsoleState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/thecrownstudios/minestomlauncher/console/ConsoleThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/thecrownstudios/minestomlauncher/console/DelegatingCompleter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/thecrownstudios/minestomlauncher/console/DelegatingHighlighter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/thecrownstudios/minestomlauncher/console/MinecraftCommandCompleter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
)); | ||
} | ||
*/ | ||
} | ||
|
||
} |
Oops, something went wrong.