Fix :i18n module and get rid of MockDiscordSRV requirement

This commit is contained in:
Vankka 2023-07-19 18:36:40 +03:00
parent 0e9a2e6e1c
commit 4dc56a6aa4
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
10 changed files with 68 additions and 140 deletions

View File

@ -22,12 +22,18 @@ import com.discordsrv.bukkit.config.main.BukkitConfig;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.config.configurate.manager.abstraction.ServerConfigManager;
import java.nio.file.Path;
public class BukkitConfigManager extends ServerConfigManager<BukkitConfig> {
public BukkitConfigManager(DiscordSRV discordSRV) {
super(discordSRV);
}
public BukkitConfigManager(Path dataDirectory) {
super(dataDirectory);
}
@Override
public BukkitConfig createConfiguration() {
return new BukkitConfig();

View File

@ -22,12 +22,18 @@ import com.discordsrv.bukkit.config.connection.BukkitConnectionConfig;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.config.configurate.manager.ConnectionConfigManager;
import java.nio.file.Path;
public class BukkitConnectionConfigManager extends ConnectionConfigManager<BukkitConnectionConfig> {
public BukkitConnectionConfigManager(DiscordSRV discordSRV) {
super(discordSRV);
}
public BukkitConnectionConfigManager(Path dataDirectory) {
super(dataDirectory);
}
@Override
public BukkitConnectionConfig createConfiguration() {
return new BukkitConnectionConfig();

View File

@ -26,6 +26,8 @@ import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.nio.file.Path;
public abstract class ConnectionConfigManager<C extends ConnectionConfig>
extends TranslatedConfigManager<C, YamlConfigurationLoader>
implements YamlConfigLoaderProvider {
@ -34,6 +36,10 @@ public abstract class ConnectionConfigManager<C extends ConnectionConfig>
super(discordSRV);
}
protected ConnectionConfigManager(Path dataDirectory) {
super(dataDirectory);
}
@Override
public ConfigurationOptions configurationOptions(ObjectMapper.Factory objectMapper) {
return super.configurationOptions(objectMapper)

View File

@ -26,6 +26,8 @@ import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.nio.file.Path;
public abstract class MainConfigManager<C extends MainConfig>
extends TranslatedConfigManager<C, YamlConfigurationLoader>
implements YamlConfigLoaderProvider {
@ -34,6 +36,10 @@ public abstract class MainConfigManager<C extends MainConfig>
super(discordSRV);
}
protected MainConfigManager(Path dataDirectory) {
super(dataDirectory);
}
@Override
public ConfigurationOptions configurationOptions(ObjectMapper.Factory objectMapper) {
return super.configurationOptions(objectMapper)

View File

@ -68,7 +68,6 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
return in;
};
protected final DiscordSRV discordSRV;
private final Path filePath;
private final ObjectMapper.Factory objectMapper;
private final ObjectMapper.Factory cleanObjectMapper;
@ -77,8 +76,11 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
protected T configuration;
public ConfigurateConfigManager(DiscordSRV discordSRV) {
this.discordSRV = discordSRV;
this.filePath = discordSRV.dataDirectory().resolve(fileName());
this(discordSRV.dataDirectory());
}
protected ConfigurateConfigManager(Path dataDirectory) {
this.filePath = dataDirectory.resolve(fileName());
this.objectMapper = objectMapperBuilder().build();
this.cleanObjectMapper = cleanObjectMapperBuilder().build();
this.loader = createLoader(filePath, nodeOptions());
@ -161,6 +163,13 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
});
return ObjectMapper.factoryBuilder()
.defaultNamingScheme(NAMING_SCHEME)
.addDiscoverer(new OrderedFieldDiscovererProxy<>((FieldDiscoverer<Object>) FieldDiscoverer.emptyConstructorObject(), fieldOrder))
.addDiscoverer(new OrderedFieldDiscovererProxy<>((FieldDiscoverer<Object>) FieldDiscoverer.record(), fieldOrder));
}
public ObjectMapper.Factory.Builder objectMapperBuilder() {
return commonObjectMapperBuilder()
.addProcessor(Comment.class, (data, fieldType) -> {
Processor<Object> processor = Processor.comments().make(data, fieldType);
@ -174,14 +183,7 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
}
}
};
})
.defaultNamingScheme(NAMING_SCHEME)
.addDiscoverer(new OrderedFieldDiscovererProxy<>((FieldDiscoverer<Object>) FieldDiscoverer.emptyConstructorObject(), fieldOrder))
.addDiscoverer(new OrderedFieldDiscovererProxy<>((FieldDiscoverer<Object>) FieldDiscoverer.record(), fieldOrder));
}
public ObjectMapper.Factory.Builder objectMapperBuilder() {
return commonObjectMapperBuilder();
});
}
protected ObjectMapper.Factory.Builder cleanObjectMapperBuilder() {

View File

@ -26,12 +26,18 @@ import com.discordsrv.common.config.main.channels.base.server.ServerBaseChannelC
import com.discordsrv.common.config.main.channels.base.server.ServerChannelConfig;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import java.nio.file.Path;
public abstract class ServerConfigManager<T extends MainConfig> extends MainConfigManager<T> {
public ServerConfigManager(DiscordSRV discordSRV) {
super(discordSRV);
}
protected ServerConfigManager(Path dataDirectory) {
super(dataDirectory);
}
@Override
public IChannelConfig.Serializer getChannelConfigSerializer(ObjectMapper.Factory mapperFactory) {
return new IChannelConfig.Serializer(mapperFactory, ServerBaseChannelConfig.class, ServerChannelConfig.class);

View File

@ -32,6 +32,7 @@ import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -39,10 +40,17 @@ import java.util.List;
public abstract class TranslatedConfigManager<T extends Config, LT extends AbstractConfigurationLoader<CommentedConfigurationNode>>
extends ConfigurateConfigManager<T, LT> {
private final DiscordSRV discordSRV;
private String header;
public TranslatedConfigManager(DiscordSRV discordSRV) {
super(discordSRV);
this.discordSRV = discordSRV;
}
protected TranslatedConfigManager(Path dataDirectory) {
super(dataDirectory);
this.discordSRV = null;
}
@Override
@ -100,6 +108,10 @@ public abstract class TranslatedConfigManager<T extends Config, LT extends Abstr
}
private ConfigurationNode getTranslationRoot() throws ConfigurateException {
if (discordSRV == null) {
return null;
}
String languageCode = discordSRV.locale().getISO3Language();
URL resourceURL = discordSRV.getClass().getClassLoader()
.getResource("translations/" + languageCode + ".yml");

View File

@ -20,7 +20,6 @@ package com.discordsrv.config;
import com.discordsrv.bukkit.config.manager.BukkitConfigManager;
import com.discordsrv.bukkit.config.manager.BukkitConnectionConfigManager;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.config.Config;
import com.discordsrv.common.config.configurate.annotation.Untranslated;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
@ -34,6 +33,8 @@ import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -44,10 +45,11 @@ import java.util.Map;
*/
public final class DiscordSRVTranslation {
private static final DiscordSRV discordSRV = new MockDiscordSRV();
private static final Path DATA_DIRECTORY = Paths.get(".");
private static final List<TranslatedConfigManager<? extends Config, ?>> CONFIGS = Arrays.asList(
new BukkitConfigManager(discordSRV),
new BukkitConnectionConfigManager(discordSRV)
new BukkitConfigManager(DATA_DIRECTORY),
new BukkitConnectionConfigManager(DATA_DIRECTORY)
);
public static void main(String[] args) throws ConfigurateException {
@ -61,8 +63,7 @@ public final class DiscordSRVTranslation {
try {
Untranslated.Type type = data.value();
if (type.isValue()) {
Object value = destination.get(Object.class);
if (type.isComment()/* || !(value instanceof String)*/) {
if (type.isComment()) {
destination.set(null);
} else {
destination.set("");
@ -91,7 +92,7 @@ public final class DiscordSRVTranslation {
.addProcessor(Untranslated.class, untranslatedProcessorFactory)
.build();
TranslationConfigManagerProxy<?> configManagerProxy = new TranslationConfigManagerProxy<>(discordSRV, mapperFactory, configManager);
TranslationConfigManagerProxy<?> configManagerProxy = new TranslationConfigManagerProxy<>(DATA_DIRECTORY, mapperFactory, configManager);
CommentedConfigurationNode configurationNode = configManagerProxy.getDefaultNode(mapperFactory);
convertCommentsToOptions(configurationNode, commentSection);

View File

@ -1,118 +0,0 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2023 Austin "Scarsz" Shapiro, Henri "Vankka" Schubin and DiscordSRV 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.discordsrv.config;
import com.discordsrv.common.AbstractDiscordSRV;
import com.discordsrv.common.bootstrap.IBootstrap;
import com.discordsrv.common.bootstrap.LifecycleManager;
import com.discordsrv.common.command.game.handler.ICommandHandler;
import com.discordsrv.common.config.connection.ConnectionConfig;
import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.configurate.manager.ConnectionConfigManager;
import com.discordsrv.common.config.configurate.manager.MainConfigManager;
import com.discordsrv.common.console.Console;
import com.discordsrv.common.debug.data.OnlineMode;
import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.logging.backend.impl.JavaLoggerImpl;
import com.discordsrv.common.player.provider.AbstractPlayerProvider;
import com.discordsrv.common.plugin.PluginManager;
import com.discordsrv.common.scheduler.Scheduler;
import dev.vankka.dependencydownload.classpath.ClasspathAppender;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Path;
import java.nio.file.Paths;
@SuppressWarnings("ConstantConditions")
public class MockDiscordSRV extends AbstractDiscordSRV<IBootstrap, MainConfig, ConnectionConfig> {
public MockDiscordSRV() {
super(new IBootstrap() {
@Override
public Logger logger() {
return JavaLoggerImpl.getRoot();
}
@Override
public ClasspathAppender classpathAppender() {
return null;
}
@Override
public ClassLoader classLoader() {
return null;
}
@Override
public LifecycleManager lifecycleManager() {
return null;
}
@Override
public Path dataDirectory() {
return null;
}
});
}
@Override
public Path dataDirectory() {
return Paths.get("");
}
@Override
public Scheduler scheduler() {
return null;
}
@Override
public Console console() {
return null;
}
@Override
public PluginManager pluginManager() {
return null;
}
@Override
public OnlineMode onlineMode() {
return null;
}
@Override
public ICommandHandler commandHandler() {
return null;
}
@Override
public @NotNull AbstractPlayerProvider<?, ?> playerProvider() {
return null;
}
@Override
public ConnectionConfigManager<ConnectionConfig> connectionConfigManager() {
return null;
}
@Override
public MainConfigManager<MainConfig> configManager() {
return null;
}
}

View File

@ -18,12 +18,13 @@
package com.discordsrv.config;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.config.configurate.manager.loader.YamlConfigLoaderProvider;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.configurate.manager.loader.YamlConfigLoaderProvider;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.nio.file.Path;
public class TranslationConfigManagerProxy<C>
extends ConfigurateConfigManager<C, YamlConfigurationLoader>
implements YamlConfigLoaderProvider {
@ -31,8 +32,8 @@ public class TranslationConfigManagerProxy<C>
private final ObjectMapper.Factory objectMapper;
private final ConfigurateConfigManager<C, ?> configManager;
public TranslationConfigManagerProxy(DiscordSRV discordSRV, ObjectMapper.Factory objectMapper, ConfigurateConfigManager<C, ?> configManager) {
super(discordSRV);
public TranslationConfigManagerProxy(Path dataDirectory, ObjectMapper.Factory objectMapper, ConfigurateConfigManager<C, ?> configManager) {
super(dataDirectory);
this.objectMapper = objectMapper;
this.configManager = configManager;
}