Use ansi component serializer

This commit is contained in:
Luck 2023-08-05 10:09:30 +01:00
parent 6d8b6bd3fe
commit b8d1f52d7d
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
6 changed files with 19 additions and 141 deletions

View File

@ -24,7 +24,7 @@ For more information, see the wiki article on [Why LuckPerms?](https://luckperms
LuckPerms uses Gradle to handle dependencies & building.
#### Requirements
* Java 8 JDK or newer
* Java 17 JDK or newer
* Git
#### Compiling from source

View File

@ -18,21 +18,21 @@ dependencies {
api 'com.google.guava:guava:31.1-jre'
api 'io.netty:netty-all:4.1.93.Final'
api('net.kyori:adventure-api:4.11.0') {
api('net.kyori:adventure-api:4.14.0') {
exclude(module: 'adventure-bom')
exclude(module: 'checker-qual')
exclude(module: 'annotations')
}
api('net.kyori:adventure-text-serializer-gson:4.11.0') {
api('net.kyori:adventure-text-serializer-gson:4.14.0') {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
exclude(module: 'gson')
}
api('net.kyori:adventure-text-serializer-legacy:4.11.0') {
api('net.kyori:adventure-text-serializer-legacy:4.14.0') {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
}
api('net.kyori:adventure-text-serializer-plain:4.11.0') {
api('net.kyori:adventure-text-serializer-plain:4.14.0') {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
}
@ -40,7 +40,14 @@ dependencies {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
}
api('net.kyori:ansi:1.0.1')
api('net.kyori:adventure-text-serializer-ansi:4.14.0') {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
exclude(module: 'annotations')
}
api('net.kyori:ansi:1.0.3') {
exclude(module: 'annotations')
}
}
blossom {

View File

@ -26,8 +26,9 @@
package me.lucko.luckperms.standalone.app.integration;
import me.lucko.luckperms.standalone.app.LuckPermsApplication;
import me.lucko.luckperms.standalone.app.utils.AnsiUtils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer;
import net.kyori.ansi.ColorLevel;
import java.util.Set;
import java.util.UUID;
@ -46,7 +47,7 @@ public class SingletonPlayer {
private static final UUID UUID = new UUID(0, 0);
/** A message sink that prints the component to stdout */
private static final Consumer<Component> PRINT_TO_STDOUT = component -> LuckPermsApplication.LOGGER.info(AnsiUtils.format(component));
private static final Consumer<Component> PRINT_TO_STDOUT = component -> LuckPermsApplication.LOGGER.info(ANSIComponentSerializer.ansi().serialize(component));
/** Singleton instance */
public static final SingletonPlayer INSTANCE = new SingletonPlayer();

View File

@ -1,130 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.standalone.app.utils;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.flattener.ComponentFlattener;
import net.kyori.adventure.text.flattener.FlattenerListener;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.ansi.ANSIComponentRenderer;
import net.kyori.ansi.StyleOps;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
/**
* Utility to format a {@link Component} as an ANSI string.
*/
public final class AnsiUtils {
private AnsiUtils() {}
public static String format(Component component) {
ANSIComponentRenderer.ToString<Style> formatter = ANSIComponentRenderer.toString(AdventureStyleOps.INSTANCE);
ComponentFlattener.basic().flatten(component, new AnsiFlattenerListener(formatter));
return formatter.asString();
}
private static final class AnsiFlattenerListener implements FlattenerListener {
private final ANSIComponentRenderer.ToString<Style> formatter;
AnsiFlattenerListener(ANSIComponentRenderer.ToString<Style> formatter) {
this.formatter = formatter;
}
@Override
public void pushStyle(@NotNull Style style) {
this.formatter.pushStyle(style);
}
@Override
public void component(@NotNull String text) {
this.formatter.text(text);
}
@Override
public void popStyle(@NotNull Style style) {
this.formatter.popStyle(style);
}
}
private static final class AdventureStyleOps implements StyleOps<Style> {
private static final AdventureStyleOps INSTANCE = new AdventureStyleOps();
@Override
public State bold(@NotNull Style style) {
return state(style.decoration(TextDecoration.BOLD));
}
@Override
public State italics(@NotNull Style style) {
return state(style.decoration(TextDecoration.ITALIC));
}
@Override
public State underlined(@NotNull Style style) {
return state(style.decoration(TextDecoration.UNDERLINED));
}
@Override
public State strikethrough(@NotNull Style style) {
return state(style.decoration(TextDecoration.STRIKETHROUGH));
}
@Override
public State obfuscated(@NotNull Style style) {
return state(style.decoration(TextDecoration.OBFUSCATED));
}
@Override
public @Range(from = -1L, to = 16777215L) int color(@NotNull Style style) {
TextColor color = style.color();
return color == null ? StyleOps.COLOR_UNSET : color.value();
}
@Override
public @Nullable String font(@NotNull Style style) {
Key font = style.font();
return font == null ? null : font.asString();
}
private static State state(TextDecoration.State state) {
switch (state) {
case TRUE:
return State.TRUE;
case FALSE:
return State.FALSE;
case NOT_SET:
return State.UNSET;
default:
throw new AssertionError();
}
}
}
}

View File

@ -13,6 +13,7 @@ test {
excludeTags 'docker'
}
}
systemProperty('net.kyori.ansi.colorLevel', 'indexed16')
}
jacocoTestReport {

View File

@ -34,7 +34,6 @@ import me.lucko.luckperms.common.plugin.logging.Log4jPluginLogger;
import me.lucko.luckperms.common.plugin.logging.PluginLogger;
import me.lucko.luckperms.standalone.app.LuckPermsApplication;
import net.luckperms.api.platform.Platform;
import org.jetbrains.annotations.VisibleForTesting;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -69,7 +68,7 @@ public class LPStandaloneBootstrap implements LuckPermsBootstrap, LoaderBootstra
this.plugin = new LPStandalonePlugin(this);
}
@VisibleForTesting
// visible for testing
protected LPStandaloneBootstrap(LuckPermsApplication loader, ClassPathAppender classPathAppender) {
this.loader = loader;
@ -79,7 +78,7 @@ public class LPStandaloneBootstrap implements LuckPermsBootstrap, LoaderBootstra
this.plugin = createTestPlugin();
}
@VisibleForTesting
// visible for testing
protected LPStandalonePlugin createTestPlugin() {
return new LPStandalonePlugin(this);
}