mirror of
https://github.com/Suiranoil/SkinRestorer.git
synced 2026-01-16 04:42:12 +00:00
better provider config
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package net.lionarius.skinrestorer;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import net.lionarius.skinrestorer.config.BuiltInProviderConfig;
|
||||
import net.lionarius.skinrestorer.config.Config;
|
||||
import net.lionarius.skinrestorer.platform.Services;
|
||||
import net.lionarius.skinrestorer.skin.SkinIO;
|
||||
@@ -60,9 +61,18 @@ public final class SkinRestorer {
|
||||
SkinRestorer.reloadConfig();
|
||||
|
||||
SkinRestorer.providersRegistry.register(EmptySkinProvider.PROVIDER_NAME, SkinProvider.EMPTY, false);
|
||||
SkinRestorer.providersRegistry.register(MojangSkinProvider.PROVIDER_NAME, SkinProvider.MOJANG);
|
||||
SkinRestorer.providersRegistry.register(ElyBySkinProvider.PROVIDER_NAME, SkinProvider.ELY_BY);
|
||||
SkinRestorer.providersRegistry.register(MineskinSkinProvider.PROVIDER_NAME, SkinProvider.MINESKIN);
|
||||
|
||||
SkinRestorer.registerDefaultSkinProvider(MojangSkinProvider.PROVIDER_NAME, SkinProvider.MOJANG, SkinRestorer.getConfig().providersConfig().mojang());
|
||||
SkinRestorer.registerDefaultSkinProvider(ElyBySkinProvider.PROVIDER_NAME, SkinProvider.ELY_BY, SkinRestorer.getConfig().providersConfig().ely_by());
|
||||
SkinRestorer.registerDefaultSkinProvider(MineskinSkinProvider.PROVIDER_NAME, SkinProvider.MINESKIN, SkinRestorer.getConfig().providersConfig().mineskin());
|
||||
}
|
||||
|
||||
private static void registerDefaultSkinProvider(String defaultName, SkinProvider provider, BuiltInProviderConfig config) {
|
||||
var isDefaultName = config.name().equals(defaultName);
|
||||
SkinRestorer.providersRegistry.register(defaultName, provider, config.enabled() && isDefaultName);
|
||||
|
||||
if (!isDefaultName)
|
||||
SkinRestorer.providersRegistry.register(config.name(), provider, config.enabled());
|
||||
}
|
||||
|
||||
public static void onServerStarted(MinecraftServer server) {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package net.lionarius.skinrestorer.config;
|
||||
|
||||
public record BuiltInProviderConfig(boolean enabled, String name, CacheConfig cache) {
|
||||
public boolean isValid() {
|
||||
return this.name != null && this.cache != null && !this.name.isEmpty() && this.cache.isValid();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package net.lionarius.skinrestorer.config;
|
||||
|
||||
public record CacheConfig(boolean enabled, long duration) {
|
||||
public boolean isValid() {
|
||||
return this.duration > 0;
|
||||
}
|
||||
}
|
||||
@@ -25,11 +25,9 @@ public final class Config {
|
||||
|
||||
private long requestTimeout = 10;
|
||||
|
||||
private long mojangCacheDuration = 60;
|
||||
private long elybyCacheDuration = 60;
|
||||
private long mineskinCacheDuration = 300;
|
||||
private ProvidersConfig providers = ProvidersConfig.DEFAULT;
|
||||
|
||||
public String getLanguage() {
|
||||
public String language() {
|
||||
return this.language;
|
||||
}
|
||||
|
||||
@@ -41,28 +39,20 @@ public final class Config {
|
||||
return this.fetchSkinOnFirstJoin;
|
||||
}
|
||||
|
||||
public FirstJoinSkinProvider getFirstJoinSkinProvider() {
|
||||
public FirstJoinSkinProvider firstJoinSkinProvider() {
|
||||
return this.firstJoinSkinProvider;
|
||||
}
|
||||
|
||||
public Optional<Proxy> getProxy() {
|
||||
public Optional<Proxy> proxy() {
|
||||
return Optional.ofNullable(this.parsedProxy);
|
||||
}
|
||||
|
||||
public long getRequestTimeout() {
|
||||
public long requestTimeout() {
|
||||
return this.requestTimeout;
|
||||
}
|
||||
|
||||
public long getMojangCacheDuration() {
|
||||
return this.mojangCacheDuration;
|
||||
}
|
||||
|
||||
public long getElybyCacheDuration() {
|
||||
return this.elybyCacheDuration;
|
||||
}
|
||||
|
||||
public long getMineskinCacheDuration() {
|
||||
return this.mineskinCacheDuration;
|
||||
public ProvidersConfig providersConfig() {
|
||||
return this.providers;
|
||||
}
|
||||
|
||||
public static Config load(Path path) {
|
||||
@@ -86,14 +76,20 @@ public final class Config {
|
||||
}
|
||||
|
||||
private void verifyAndFix() {
|
||||
if (this.language == null || this.language.isEmpty())
|
||||
if (this.language == null || this.language.isEmpty()) {
|
||||
SkinRestorer.LOGGER.warn("Language config is null or empty, defaulting to 'en_us'");
|
||||
this.language = "en_us";
|
||||
}
|
||||
|
||||
if (this.firstJoinSkinProvider == null)
|
||||
if (this.firstJoinSkinProvider == null) {
|
||||
SkinRestorer.LOGGER.warn("FirstJoinSkinProvider config is null, defaulting to MOJANG");
|
||||
this.firstJoinSkinProvider = FirstJoinSkinProvider.MOJANG;
|
||||
}
|
||||
|
||||
if (this.proxy == null)
|
||||
if (this.proxy == null) {
|
||||
SkinRestorer.LOGGER.warn("Proxy config is null, defaulting to an empty string");
|
||||
this.proxy = "";
|
||||
}
|
||||
|
||||
if (!this.proxy.isEmpty()) {
|
||||
try {
|
||||
@@ -104,16 +100,17 @@ public final class Config {
|
||||
}
|
||||
}
|
||||
|
||||
if (this.requestTimeout <= 0)
|
||||
if (this.requestTimeout <= 0) {
|
||||
SkinRestorer.LOGGER.warn("Request timeout config is less than or equal to 0, defaulting to 10");
|
||||
this.requestTimeout = 10;
|
||||
}
|
||||
|
||||
if (this.mojangCacheDuration < 0)
|
||||
this.mojangCacheDuration = 60;
|
||||
if (this.providers == null) {
|
||||
SkinRestorer.LOGGER.warn("Providers config is null, using default");
|
||||
this.providers = ProvidersConfig.DEFAULT;
|
||||
}
|
||||
|
||||
if (this.elybyCacheDuration < 0)
|
||||
this.elybyCacheDuration = 60;
|
||||
|
||||
if (this.mineskinCacheDuration < 0)
|
||||
this.mineskinCacheDuration = 300;
|
||||
if (!this.providers.isValid())
|
||||
this.providers.fix();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package net.lionarius.skinrestorer.config;
|
||||
|
||||
import net.lionarius.skinrestorer.SkinRestorer;
|
||||
import net.lionarius.skinrestorer.skin.provider.ElyBySkinProvider;
|
||||
import net.lionarius.skinrestorer.skin.provider.MineskinSkinProvider;
|
||||
import net.lionarius.skinrestorer.skin.provider.MojangSkinProvider;
|
||||
|
||||
public final class ProvidersConfig {
|
||||
public static final ProvidersConfig DEFAULT = new ProvidersConfig(
|
||||
new BuiltInProviderConfig(true, MojangSkinProvider.PROVIDER_NAME, new CacheConfig(true, 60)),
|
||||
new BuiltInProviderConfig(true, ElyBySkinProvider.PROVIDER_NAME, new CacheConfig(true, 60)),
|
||||
new BuiltInProviderConfig(true, MineskinSkinProvider.PROVIDER_NAME, new CacheConfig(true, 300))
|
||||
);
|
||||
|
||||
private BuiltInProviderConfig mojang;
|
||||
private BuiltInProviderConfig ely_by;
|
||||
private BuiltInProviderConfig mineskin;
|
||||
|
||||
public ProvidersConfig(BuiltInProviderConfig mojang, BuiltInProviderConfig ely_by, BuiltInProviderConfig mineskin) {
|
||||
this.mojang = mojang;
|
||||
this.ely_by = ely_by;
|
||||
this.mineskin = mineskin;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
if (this == ProvidersConfig.DEFAULT)
|
||||
return true;
|
||||
|
||||
return this.mojang == null || !this.mojang.isValid()
|
||||
|| this.ely_by == null || !this.ely_by.isValid()
|
||||
|| this.mineskin == null || !this.mineskin.isValid();
|
||||
}
|
||||
|
||||
public void fix() {
|
||||
if (this == ProvidersConfig.DEFAULT)
|
||||
return;
|
||||
|
||||
if (this.mojang == null || !this.mojang.isValid()) {
|
||||
SkinRestorer.LOGGER.warn("Mojang provider config is invalid, using default");
|
||||
this.mojang = ProvidersConfig.DEFAULT.mojang();
|
||||
}
|
||||
|
||||
if (this.ely_by == null || !this.ely_by.isValid()) {
|
||||
SkinRestorer.LOGGER.warn("Ely.By provider config is invalid, using default");
|
||||
this.ely_by = ProvidersConfig.DEFAULT.ely_by();
|
||||
}
|
||||
|
||||
if (this.mineskin == null || !this.mineskin.isValid()) {
|
||||
SkinRestorer.LOGGER.warn("Mineskin provider config is invalid, using default");
|
||||
this.mineskin = ProvidersConfig.DEFAULT.mineskin();
|
||||
}
|
||||
}
|
||||
|
||||
public BuiltInProviderConfig mojang() {
|
||||
return this.mojang;
|
||||
}
|
||||
|
||||
public BuiltInProviderConfig ely_by() {
|
||||
return this.ely_by;
|
||||
}
|
||||
|
||||
public BuiltInProviderConfig mineskin() {
|
||||
return this.mineskin;
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ public abstract class ServerLoginPacketListenerImplMixin {
|
||||
|
||||
if (originalSkin == null && SkinRestorer.getConfig().fetchSkinOnFirstJoin()) {
|
||||
var context = new SkinProviderContext(
|
||||
SkinRestorer.getConfig().getFirstJoinSkinProvider().getName(),
|
||||
SkinRestorer.getConfig().firstJoinSkinProvider().getName(),
|
||||
authenticatedProfile.getName(),
|
||||
null
|
||||
);
|
||||
|
||||
@@ -38,13 +38,14 @@ public final class ElyBySkinProvider implements SkinProvider {
|
||||
} catch (URISyntaxException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void createCache() {
|
||||
var config = SkinRestorer.getConfig().providersConfig().ely_by();
|
||||
var time = config.cache().enabled() ? config.cache().duration() : 0;
|
||||
|
||||
SKIN_CACHE = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(SkinRestorer.getConfig().getElybyCacheDuration(), TimeUnit.SECONDS)
|
||||
.expireAfterWrite(time, TimeUnit.SECONDS)
|
||||
.build(new CacheLoader<>() {
|
||||
@Override
|
||||
public @NotNull Optional<Property> load(@NotNull String key) throws Exception {
|
||||
|
||||
@@ -39,8 +39,11 @@ public final class MineskinSkinProvider implements SkinProvider {
|
||||
}
|
||||
|
||||
public static void createCache() {
|
||||
var config = SkinRestorer.getConfig().providersConfig().mineskin();
|
||||
var time = config.cache().enabled() ? config.cache().duration() : 0;
|
||||
|
||||
SKIN_CACHE = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(SkinRestorer.getConfig().getMineskinCacheDuration(), TimeUnit.SECONDS)
|
||||
.expireAfterWrite(time, TimeUnit.SECONDS)
|
||||
.build(new CacheLoader<>() {
|
||||
@Override
|
||||
public @NotNull Optional<Property> load(@NotNull Pair<URI, SkinVariant> key) throws Exception {
|
||||
|
||||
@@ -61,8 +61,11 @@ public final class MojangSkinProvider implements SkinProvider {
|
||||
}
|
||||
|
||||
public static void createCache() {
|
||||
var config = SkinRestorer.getConfig().providersConfig().mojang();
|
||||
var time = config.cache().enabled() ? config.cache().duration() : 0;
|
||||
|
||||
SKIN_CACHE = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(SkinRestorer.getConfig().getMojangCacheDuration(), TimeUnit.SECONDS)
|
||||
.expireAfterWrite(time, TimeUnit.SECONDS)
|
||||
.build(new CacheLoader<>() {
|
||||
@Override
|
||||
public @NotNull Optional<Property> load(@NotNull UUID key) throws Exception {
|
||||
|
||||
@@ -42,7 +42,7 @@ public final class SkinProviderRegistry {
|
||||
|
||||
public void register(@NotNull String name, @NotNull SkinProvider provider, boolean isPublic) {
|
||||
if (this.registry.containsKey(name))
|
||||
throw new IllegalArgumentException("Skin provider with name " + name + " is already registered");
|
||||
return;
|
||||
|
||||
this.registry.put(name, new Entry(provider, isPublic));
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public final class Translation {
|
||||
}
|
||||
|
||||
public static void reloadTranslations() {
|
||||
translations = Translation.loadTranslationMap(SkinRestorer.getConfig().getLanguage());
|
||||
translations = Translation.loadTranslationMap(SkinRestorer.getConfig().language());
|
||||
}
|
||||
|
||||
private static ImmutableMap<String, String> loadTranslationMap(String lang) {
|
||||
|
||||
@@ -30,11 +30,11 @@ public final class WebUtils {
|
||||
private static HttpClient buildClient() {
|
||||
var builder = HttpClient.newBuilder();
|
||||
|
||||
var proxy = SkinRestorer.getConfig().getProxy();
|
||||
var proxy = SkinRestorer.getConfig().proxy();
|
||||
proxy.ifPresent(value -> builder.proxy(ProxySelector.of(InetSocketAddress.createUnresolved(value.host(), value.port()))));
|
||||
|
||||
try {
|
||||
builder.connectTimeout(Duration.of(SkinRestorer.getConfig().getRequestTimeout(), ChronoUnit.SECONDS));
|
||||
builder.connectTimeout(Duration.of(SkinRestorer.getConfig().requestTimeout(), ChronoUnit.SECONDS));
|
||||
} catch (IllegalArgumentException e) {
|
||||
SkinRestorer.LOGGER.error("failed to set request timeout", e);
|
||||
builder.connectTimeout(Duration.of(10, ChronoUnit.SECONDS));
|
||||
|
||||
Reference in New Issue
Block a user