Move nullAllFields to ConfigurateConfigManager, only null fields when saving or loading

This commit is contained in:
Vankka 2024-07-20 21:19:22 +03:00
parent 49cdfcb307
commit 295bc1c6bf
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
17 changed files with 72 additions and 83 deletions

View File

@ -53,6 +53,7 @@ import org.spongepowered.configurate.yaml.ScalarStyle;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.nio.file.Path;
import java.util.*;
@ -62,7 +63,8 @@ import java.util.stream.Collectors;
public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurationLoader<CommentedConfigurationNode>>
implements ConfigManager<T>, ConfigLoaderProvider<LT> {
public static ThreadLocal<Boolean> CLEAN_MAPPER = ThreadLocal.withInitial(() -> false);
public static final ThreadLocal<Boolean> CLEAN_MAPPER = ThreadLocal.withInitial(() -> false);
private static final ThreadLocal<Boolean> SAVE_OR_LOAD = ThreadLocal.withInitial(() -> false);
public static NamingScheme NAMING_SCHEME = in -> {
in = Character.toLowerCase(in.charAt(0)) + in.substring(1);
@ -89,6 +91,31 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
this.cleanObjectMapper = cleanObjectMapperBuilder().build();
}
public static void nullAllFields(Object object) {
if (!SAVE_OR_LOAD.get()) {
return;
}
Class<?> clazz = object.getClass();
while (clazz != null) {
for (Field field : clazz.getFields()) {
if (field.getType().isPrimitive()) {
continue;
}
int modifiers = field.getModifiers();
if (!Modifier.isPublic(modifiers) || Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers)) {
continue;
}
try {
field.set(object, null);
} catch (IllegalAccessException ignored) {}
}
clazz = clazz.getSuperclass();
}
}
public Path filePath() {
return filePath;
}
@ -387,9 +414,14 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
node = getDefault(defaultConfig, false);
}
try {
SAVE_OR_LOAD.set(true);
this.configuration = objectMapper()
.get((Class<T>) defaultConfig.getClass())
.load(node);
} finally {
SAVE_OR_LOAD.set(false);
}
} catch (ConfigurateException e) {
Class<?> configClass = defaultConfig.getClass();
if (!configClass.isAnnotationPresent(ConfigSerializable.class)) {
@ -421,6 +453,11 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
}
protected void save(T config, Class<T> clazz, CommentedConfigurationNode node) throws SerializationException {
try {
SAVE_OR_LOAD.set(true);
objectMapper().get(clazz).save(config, node);
} finally {
SAVE_OR_LOAD.set(false);
}
}
}

View File

@ -18,7 +18,7 @@
package com.discordsrv.common.config.main.channels;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
@ -29,7 +29,7 @@ import java.util.List;
public class ChannelLockingConfig {
public ChannelLockingConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
public Channels channels = new Channels();

View File

@ -18,7 +18,7 @@
package com.discordsrv.common.config.main.channels;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.configurate.annotation.Untranslated;
import com.discordsrv.common.config.main.generic.DiscordIgnoresConfig;
import com.discordsrv.common.config.main.generic.MentionsConfig;
@ -33,7 +33,7 @@ import java.util.regex.Pattern;
public class DiscordToMinecraftChatConfig {
public DiscordToMinecraftChatConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
@Comment("Requires the \"Message Content Intent\"")

View File

@ -21,7 +21,7 @@ package com.discordsrv.common.config.main.channels;
import com.discordsrv.api.discord.entity.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import com.discordsrv.api.event.events.message.receive.game.JoinMessageReceiveEvent;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.configurate.annotation.Constants;
import com.discordsrv.common.config.configurate.annotation.Untranslated;
import com.discordsrv.common.config.main.generic.IMessageConfig;
@ -33,7 +33,7 @@ import org.spongepowered.configurate.objectmapping.meta.Comment;
public class JoinMessageConfig implements IMessageConfig {
public JoinMessageConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
public Boolean enabled = true;

View File

@ -20,7 +20,7 @@ package com.discordsrv.common.config.main.channels;
import com.discordsrv.api.discord.entity.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.configurate.annotation.Constants;
import com.discordsrv.common.config.configurate.annotation.Untranslated;
import com.discordsrv.common.config.main.generic.IMessageConfig;
@ -31,7 +31,7 @@ import org.spongepowered.configurate.objectmapping.meta.Comment;
public class LeaveMessageConfig implements IMessageConfig {
public LeaveMessageConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
public Boolean enabled = true;

View File

@ -19,7 +19,7 @@
package com.discordsrv.common.config.main.channels;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.configurate.annotation.DefaultOnly;
import com.discordsrv.common.config.configurate.annotation.Untranslated;
import com.discordsrv.common.config.main.generic.IMessageConfig;
@ -34,7 +34,7 @@ import java.util.regex.Pattern;
public class MinecraftToDiscordChatConfig implements IMessageConfig {
public MinecraftToDiscordChatConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
public Boolean enabled = true;

View File

@ -18,7 +18,7 @@
package com.discordsrv.common.config.main.channels;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.main.generic.DiscordIgnoresConfig;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
@ -27,7 +27,7 @@ import org.spongepowered.configurate.objectmapping.meta.Comment;
public class MirroringConfig {
public MirroringConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
@Comment("Requires the \"Message Content Intent\"")

View File

@ -19,7 +19,7 @@
package com.discordsrv.common.config.main.channels;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.configurate.annotation.Untranslated;
import com.discordsrv.common.config.main.generic.IMessageConfig;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
@ -28,7 +28,7 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
public class StartMessageConfig implements IMessageConfig {
public StartMessageConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
public Boolean enabled = true;

View File

@ -19,7 +19,7 @@
package com.discordsrv.common.config.main.channels;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.configurate.annotation.Untranslated;
import com.discordsrv.common.config.main.generic.IMessageConfig;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
@ -28,7 +28,7 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
public class StopMessageConfig implements IMessageConfig {
public StopMessageConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
public Boolean enabled = true;

View File

@ -18,7 +18,7 @@
package com.discordsrv.common.config.main.channels.base;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.main.generic.DestinationConfig;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Setting;
@ -27,7 +27,7 @@ import org.spongepowered.configurate.objectmapping.meta.Setting;
public class ChannelConfig extends BaseChannelConfig implements IChannelConfig {
public ChannelConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
@Setting(nodeFromParent = true)

View File

@ -18,7 +18,7 @@
package com.discordsrv.common.config.main.channels.base.proxy;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.main.channels.base.IChannelConfig;
import com.discordsrv.common.config.main.generic.DestinationConfig;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
@ -28,7 +28,7 @@ import org.spongepowered.configurate.objectmapping.meta.Setting;
public class ProxyChannelConfig extends ProxyBaseChannelConfig implements IChannelConfig {
public ProxyChannelConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
@Setting(nodeFromParent = true)

View File

@ -18,7 +18,7 @@
package com.discordsrv.common.config.main.channels.base.server;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.main.channels.base.IChannelConfig;
import com.discordsrv.common.config.main.generic.DestinationConfig;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
@ -28,7 +28,7 @@ import org.spongepowered.configurate.objectmapping.meta.Setting;
public class ServerChannelConfig extends ServerBaseChannelConfig implements IChannelConfig {
public ServerChannelConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
@Setting(nodeFromParent = true)

View File

@ -20,7 +20,7 @@ package com.discordsrv.common.config.main.channels.proxy;
import com.discordsrv.api.discord.entity.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.configurate.annotation.Untranslated;
import com.discordsrv.common.config.main.generic.IMessageConfig;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
@ -29,7 +29,7 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
public class ServerSwitchMessageConfig implements IMessageConfig {
public ServerSwitchMessageConfig() {
ConfigUtil.nullAllFields(true);
ConfigurateConfigManager.nullAllFields(true);
}
public Boolean enabled = false;

View File

@ -20,7 +20,7 @@ package com.discordsrv.common.config.main.channels.server;
import com.discordsrv.api.discord.entity.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.configurate.annotation.Untranslated;
import com.discordsrv.common.config.main.generic.IMessageConfig;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
@ -29,7 +29,7 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
public class AwardMessageConfig implements IMessageConfig {
public AwardMessageConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
public Boolean enabled = true;

View File

@ -20,7 +20,7 @@ package com.discordsrv.common.config.main.channels.server;
import com.discordsrv.api.discord.entity.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.configurate.annotation.Untranslated;
import com.discordsrv.common.config.main.generic.IMessageConfig;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
@ -29,7 +29,7 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
public class DeathMessageConfig implements IMessageConfig {
public DeathMessageConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
public Boolean enabled = true;

View File

@ -18,7 +18,7 @@
package com.discordsrv.common.config.main.channels.server;
import com.discordsrv.common.config.util.ConfigUtil;
import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager;
import com.discordsrv.common.config.configurate.annotation.Order;
import com.discordsrv.common.config.main.channels.JoinMessageConfig;
import org.jetbrains.annotations.Nullable;
@ -26,7 +26,7 @@ import org.jetbrains.annotations.Nullable;
public class ServerJoinMessageConfig extends JoinMessageConfig {
public ServerJoinMessageConfig() {
ConfigUtil.nullAllFields(this);
ConfigurateConfigManager.nullAllFields(this);
}
@Order(10)

View File

@ -1,48 +0,0 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2024 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.common.config.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public final class ConfigUtil {
private ConfigUtil() {}
public static void nullAllFields(Object object) {
Class<?> clazz = object.getClass();
while (clazz != null) {
for (Field field : clazz.getFields()) {
if (field.getType().isPrimitive()) {
continue;
}
int modifiers = field.getModifiers();
if (!Modifier.isPublic(modifiers) || Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers)) {
continue;
}
try {
field.set(object, null);
} catch (IllegalAccessException ignored) {}
}
clazz = clazz.getSuperclass();
}
}
}