Implement ComponentLogger (#1022)
This commit is contained in:
@@ -93,6 +93,7 @@ dependencies {
|
||||
implementation(project(":velocity-native"))
|
||||
|
||||
implementation(libs.bundles.log4j)
|
||||
implementation(libs.kyori.ansi)
|
||||
implementation(libs.netty.codec)
|
||||
implementation(libs.netty.codec.haproxy)
|
||||
implementation(libs.netty.codec.http)
|
||||
|
@@ -79,6 +79,7 @@ import com.velocitypowered.proxy.tablist.VelocityTabList;
|
||||
import com.velocitypowered.proxy.tablist.VelocityTabListLegacy;
|
||||
import com.velocitypowered.proxy.util.ClosestLocaleMatcher;
|
||||
import com.velocitypowered.proxy.util.DurationUtils;
|
||||
import com.velocitypowered.proxy.util.TranslatableMapper;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import java.net.InetSocketAddress;
|
||||
@@ -100,9 +101,6 @@ import net.kyori.adventure.platform.facet.FacetPointers;
|
||||
import net.kyori.adventure.platform.facet.FacetPointers.Type;
|
||||
import net.kyori.adventure.pointer.Pointers;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.KeybindComponent;
|
||||
import net.kyori.adventure.text.TranslatableComponent;
|
||||
import net.kyori.adventure.text.flattener.ComponentFlattener;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
@@ -125,9 +123,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
|
||||
|
||||
private static final int MAX_PLUGIN_CHANNELS = 1024;
|
||||
private static final PlainTextComponentSerializer PASS_THRU_TRANSLATE =
|
||||
PlainTextComponentSerializer.builder().flattener(
|
||||
ComponentFlattener.basic().toBuilder().mapper(KeybindComponent.class, c -> "")
|
||||
.mapper(TranslatableComponent.class, TranslatableComponent::key).build()).build();
|
||||
PlainTextComponentSerializer.builder().flattener(TranslatableMapper.FLATTENER).build();
|
||||
static final PermissionProvider DEFAULT_PERMISSIONS = s -> PermissionFunction.ALWAYS_UNDEFINED;
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(ConnectedPlayer.class);
|
||||
|
@@ -35,8 +35,7 @@ import net.kyori.adventure.platform.facet.FacetPointers.Type;
|
||||
import net.kyori.adventure.pointer.Pointers;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.kyori.adventure.translation.GlobalTranslator;
|
||||
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
|
||||
import net.minecrell.terminalconsole.SimpleTerminalConsole;
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -55,6 +54,8 @@ import org.jline.reader.LineReaderBuilder;
|
||||
public final class VelocityConsole extends SimpleTerminalConsole implements ConsoleCommandSource {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(VelocityConsole.class);
|
||||
private static final ComponentLogger componentLogger = ComponentLogger
|
||||
.logger(VelocityConsole.class);
|
||||
|
||||
private final VelocityServer server;
|
||||
private PermissionFunction permissionFunction = ALWAYS_TRUE;
|
||||
@@ -72,9 +73,7 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons
|
||||
@Override
|
||||
public void sendMessage(@NonNull Identity identity, @NonNull Component message,
|
||||
@NonNull MessageType messageType) {
|
||||
Component translated = GlobalTranslator.render(message, ClosestLocaleMatcher.INSTANCE
|
||||
.lookupClosest(Locale.getDefault()));
|
||||
logger.info(LegacyComponentSerializer.legacySection().serialize(translated));
|
||||
componentLogger.info(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -26,6 +26,7 @@ import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -49,6 +50,7 @@ class VelocityPluginModule implements Module {
|
||||
binder.bind(description.getMainClass()).in(Scopes.SINGLETON);
|
||||
|
||||
binder.bind(Logger.class).toInstance(LoggerFactory.getLogger(description.getId()));
|
||||
binder.bind(ComponentLogger.class).toInstance(ComponentLogger.logger(description.getId()));
|
||||
binder.bind(Path.class).annotatedWith(DataDirectory.class)
|
||||
.toInstance(basePluginPath.resolve(description.getId()));
|
||||
binder.bind(PluginDescription.class).toInstance(description);
|
||||
|
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Velocity Contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.velocitypowered.proxy.provider;
|
||||
|
||||
import com.velocitypowered.proxy.util.TranslatableMapper;
|
||||
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
|
||||
import net.kyori.adventure.text.logger.slf4j.ComponentLoggerProvider;
|
||||
import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Velocity ComponentLogger Provider.
|
||||
*/
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
public final class ComponentLoggerProviderImpl implements ComponentLoggerProvider {
|
||||
private static final ANSIComponentSerializer SERIALIZER = ANSIComponentSerializer.builder()
|
||||
.flattener(TranslatableMapper.FLATTENER)
|
||||
.build();
|
||||
|
||||
@Override
|
||||
public @NotNull ComponentLogger logger(
|
||||
final @NotNull LoggerHelper helper,
|
||||
final @NotNull String name
|
||||
) {
|
||||
return helper.delegating(LoggerFactory.getLogger(name), SERIALIZER::serialize);
|
||||
}
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Velocity Contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.velocitypowered.proxy.util;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TranslatableComponent;
|
||||
import net.kyori.adventure.text.flattener.ComponentFlattener;
|
||||
import net.kyori.adventure.translation.GlobalTranslator;
|
||||
import net.kyori.adventure.translation.TranslationRegistry;
|
||||
import net.kyori.adventure.translation.Translator;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
|
||||
/**
|
||||
* Velocity Translation Mapper.
|
||||
*/
|
||||
public enum TranslatableMapper implements BiConsumer<TranslatableComponent, Consumer<Component>> {
|
||||
INSTANCE;
|
||||
|
||||
public static final ComponentFlattener FLATTENER = ComponentFlattener.basic().toBuilder()
|
||||
.complexMapper(TranslatableComponent.class, TranslatableMapper.INSTANCE)
|
||||
.build();
|
||||
|
||||
@Override
|
||||
public void accept(
|
||||
final TranslatableComponent translatableComponent,
|
||||
final Consumer<Component> componentConsumer
|
||||
) {
|
||||
for (final Translator source : GlobalTranslator.translator().sources()) {
|
||||
if (source instanceof TranslationRegistry
|
||||
&& ((TranslationRegistry) source).contains(translatableComponent.key())) {
|
||||
componentConsumer.accept(GlobalTranslator.render(translatableComponent,
|
||||
ClosestLocaleMatcher.INSTANCE.lookupClosest(Locale.getDefault())));
|
||||
return;
|
||||
}
|
||||
}
|
||||
final @Nullable String fallback = translatableComponent.fallback();
|
||||
if (fallback == null) {
|
||||
return;
|
||||
}
|
||||
for (final Translator source : GlobalTranslator.translator().sources()) {
|
||||
if (source instanceof TranslationRegistry
|
||||
&& ((TranslationRegistry) source).contains(fallback)) {
|
||||
componentConsumer.accept(
|
||||
GlobalTranslator.render(Component.translatable(fallback),
|
||||
ClosestLocaleMatcher.INSTANCE.lookupClosest(Locale.getDefault())));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
com.velocitypowered.proxy.provider.ComponentLoggerProviderImpl
|
Reference in New Issue
Block a user