Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.tr7zw.nbtapi.NBT;
import me.xginko.aef.commands.AEFCommand;
import me.xginko.aef.config.Config;
import me.xginko.aef.config.ConfigMigration;
import me.xginko.aef.config.Translation;
import me.xginko.aef.modules.AEFModule;
import me.xginko.aef.utils.KyoriUtil;
Expand Down Expand Up @@ -165,6 +166,13 @@ public void onEnable() {
prefixedLogger.info("Registering Permissions");
AEFPermission.registerAll();

prefixedLogger.info("Checking for config migration");
ConfigMigration configMigration = new ConfigMigration(this);
boolean migrated = configMigration.migrateIfNeeded();
if (migrated) {
prefixedLogger.info("Config migration completed successfully!");
}

prefixedLogger.info("Loading Config");
reloadConfiguration();

Expand Down Expand Up @@ -257,7 +265,7 @@ private void reloadConfiguration() {
if (config.update_checker_enabled) versionChecker = new VersionChecker(instance);
AEFModule.reloadModules();
AEFCommand.reloadCommands();
config.saveConfig();
config.saveAllConfigs();
} catch (Throwable t) {
prefixedLogger.error("Failed while loading config!", t);
}
Expand Down
199 changes: 127 additions & 72 deletions AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.cryptomorin.xseries.XSound;
import io.github.thatsmusic99.configurationmaster.api.ConfigFile;
import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
import io.github.thatsmusic99.configurationmaster.impl.CMMemorySection;
import me.xginko.aef.AnarchyExploitFixes;
import me.xginko.aef.utils.LocaleUtil;
import me.xginko.aef.utils.MathUtil;
Expand All @@ -12,13 +13,17 @@

import java.io.File;
import java.time.Duration;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;

public class Config {

private final ConfigFile config;
private final AnarchyExploitFixes plugin;
private final HashMap<String, ConfigFile> configFiles = new HashMap<>();
private static final Set<String> configSections = new HashSet<>(Arrays.asList(
"language", "general", "misc", "chat", "elytra",
"chunk-limits", "lag-preventions", "patches", "illegals",
"dupe-preventions", "preventions", "combat"
));
public final Locale default_lang;
public final Sound elytra_too_fast_sound;
public final Component cmd_say_format;
Expand All @@ -33,16 +38,28 @@ public class Config {
elytra_actionbar_enabled, elytra_show_chunkage, elytra_play_too_fast_sound,
elytra_teleport_back, elytra_calculate_3D, permissions_hook, update_checker_enabled;

public static Set<String> getConfigSections() {
return Collections.unmodifiableSet(configSections);
}

public Config() throws Exception {
AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance();
// Load config.yml with ConfigMaster
this.config = ConfigFile.loadConfig(new File(plugin.getDataFolder(), "config.yml"));
this.plugin = AnarchyExploitFixes.getInstance();

File settingsDir = new File(plugin.getDataFolder(), "settings");
if (!settingsDir.exists()) {
settingsDir.mkdirs();
}

// Initialize all config files
initConfigFile("config");
for (String section : configSections) {
initConfigFile("settings/" + section);
}

config.set("plugin-version", plugin.getPluginMeta().getVersion());
config.set("server-version", plugin.getServer().getVersion());
loadAllConfigs();

// Pre-structure to force order
structureConfig();
getConfig("config").set("plugin-version", plugin.getPluginMeta().getVersion());
getConfig("config").set("server-version", plugin.getServer().getVersion());

// Language Settings
this.default_lang = LocaleUtil.localeForLanguageTag(getString("language.default-language", "en_us", """
Expand Down Expand Up @@ -76,7 +93,7 @@ The time in ticks (1 sec = 20 ticks) a checked tps will be cached\s
Help command that shows a small command overview for players.""");
this.cmd_toggleConMsgs_enabled = getBoolean("general.commands.toggleconnectionmsgs.enable", true, """
If you don't use join leave/messages, you can set this to false.""");
config.addComment("general.commands", """
addComment("general.commands", """
A server restart is required when changing a command's enable status!""");

// Elytra Speed
Expand Down Expand Up @@ -115,121 +132,159 @@ The time in ticks (1 sec = 20 ticks) a checked tps will be cached\s
this.elytra_enable_netherceiling = getBoolean("elytra.elytra-speed.Nether-Ceiling.enable", true);

// Misc
config.addDefault("misc.join-leave-messages.enable", true); // add default here so enable option shows up first.
addDefault("misc.join-leave-messages.enable", true); // add default here so enable option shows up first.
this.connectionMsgsAreOnByDefault = getBoolean("misc.join-leave-messages.connection-messages-on-by-default", true, """
If set to true, players will see join/leave messages by default\s
and enter /toggleconnectionmsgs to disable them.\s
If set to false will work the other way around.""");
config.addDefault("misc.join-leave-messages.show-in-console", false); // add default here so show-in-console option is not misplaced.
addDefault("misc.join-leave-messages.show-in-console", false); // add default here so show-in-console option is not misplaced.

saveAllConfigs();
}

public void saveConfig() {
try {
config.save();
} catch (Exception e) {
AnarchyExploitFixes.prefixedLogger().error("Failed to save config file!", e);
}
private void initConfigFile(String name) throws Exception {
ConfigFile configFile = ConfigFile.loadConfig(new File(plugin.getDataFolder(), name + ".yml"));
configFiles.put(name, configFile);
}

private void structureConfig() {
createTitledSection("Language", "language");
createTitledSection("General", "general");
createTitledSection("Miscellaneous", "misc");
createTitledSection("Chat", "chat");
createTitledSection("Elytra", "elytra");
createTitledSection("Chunk Limits", "chunk-limits");
createTitledSection("Lag Preventions", "lag-preventions");
createTitledSection("Patches", "patches");
createTitledSection("Illegals", "illegals");
createTitledSection("Dupe Preventions", "dupe-preventions");
createTitledSection("Preventions", "preventions");
createTitledSection("Combat", "combat");
private ConfigFile getConfig(String name) {
return configFiles.getOrDefault(name, null);
}

public ConfigFile master() {
return config;
private void loadAllConfigs() {
for (ConfigFile config : configFiles.values()) {
try {
config.load();
} catch (Exception e) {
AnarchyExploitFixes.prefixedLogger().error("Failed to load config file!", e);
}
}
}

public void createTitledSection(String title, String path) {
config.addSection(title);
config.addDefault(path, null);
public void saveAllConfigs() {
for (ConfigFile config : configFiles.values()) {
try {
config.save();
} catch (Exception e) {
AnarchyExploitFixes.prefixedLogger().error("Failed to save config files!", e);
}
}
}

private String[] getConfigNameAndPath(String path) {
if (!path.contains(".")) {
return new String[]{"config", path};
}

String section = path.substring(0, path.indexOf("."));
String newPath = path.substring(path.indexOf(".") + 1);

if (configSections.contains(section)) {
return new String[]{"settings/" + section, newPath};
} else {
return new String[]{"config", path};
}
}

private <T> T getConfigValue(String path, T defaultValue, String comment, ConfigValueGetter<T> getter) {
String[] configAndPath = getConfigNameAndPath(path);
ConfigFile config = getConfig(configAndPath[0]);
if (config == null) return defaultValue;

if (comment != null) {
config.addDefault(configAndPath[1], defaultValue, comment);
} else {
config.addDefault(configAndPath[1], defaultValue);
}

return getter.get(config, configAndPath[1], defaultValue);
}

public boolean getBoolean(String path, boolean def, String comment) {
config.addDefault(path, def, comment);
return config.getBoolean(path, def);
return getConfigValue(path, def, comment, CMMemorySection::getBoolean);
}

public boolean getBoolean(String path, boolean def) {
config.addDefault(path, def);
return config.getBoolean(path, def);
return getConfigValue(path, def, null, CMMemorySection::getBoolean);
}

public String getString(String path, String def, String comment) {
config.addDefault(path, def, comment);
return config.getString(path, def);
return getConfigValue(path, def, comment, CMMemorySection::getString);
}

public String getString(String path, String def) {
config.addDefault(path, def);
return config.getString(path, def);
return getConfigValue(path, def, null, CMMemorySection::getString);
}

public double getDouble(String path, double def, String comment) {
config.addDefault(path, def, comment);
return config.getDouble(path, def);
return getConfigValue(path, def, comment, CMMemorySection::getDouble);
}

public double getDouble(String path, double def) {
config.addDefault(path, def);
return config.getDouble(path, def);
return getConfigValue(path, def, null, CMMemorySection::getDouble);
}

public int getInt(String path, int def, String comment) {
config.addDefault(path, def, comment);
return config.getInteger(path, def);
return getConfigValue(path, def, comment, CMMemorySection::getInteger);
}

public int getInt(String path, int def) {
config.addDefault(path, def);
return config.getInteger(path, def);
return getConfigValue(path, def, null, CMMemorySection::getInteger);
}

public long getLong(String path, long def, String comment) {
config.addDefault(path, def, comment);
return config.getLong(path, def);
return getConfigValue(path, def, comment, CMMemorySection::getLong);
}

public long getLong(String path, long def) {
config.addDefault(path, def);
return config.getLong(path, def);
return getConfigValue(path, def, null, CMMemorySection::getLong);
}

public List<String> getList(String path, List<String> def, String comment) {
config.addDefault(path, def, comment);
return config.getStringList(path);
return getConfigValue(path, def, comment, (config, p, d) -> config.getStringList(p));
}

public List<String> getList(String path, List<String> def) {
config.addDefault(path, def);
return config.getStringList(path);
return getConfigValue(path, def, null, (config, p, d) -> config.getStringList(p));
}

public ConfigSection getConfigSection(String path, Map<String, Object> defaultKeyValue) {
config.addDefault(path, null);
config.makeSectionLenient(path);
defaultKeyValue.forEach((string, object) -> config.addExample(path+"."+string, object));
return config.getConfigSection(path);
String[] configAndPath = getConfigNameAndPath(path);
ConfigFile config = getConfig(configAndPath[0]);
if (config == null) return null;
config.addDefault(configAndPath[1], null);
config.makeSectionLenient(configAndPath[1]);
defaultKeyValue.forEach((string, object) -> config.addExample(configAndPath[1] + "." + string, object));
return config.getConfigSection(configAndPath[1]);
}

public ConfigSection getConfigSection(String path, Map<String, Object> defaultKeyValue, String comment) {
config.addDefault(path, null, comment);
config.makeSectionLenient(path);
defaultKeyValue.forEach((string, object) -> config.addExample(path+"."+string, object));
return config.getConfigSection(path);
String[] configAndPath = getConfigNameAndPath(path);
ConfigFile config = getConfig(configAndPath[0]);
if (config == null) return null;
config.addDefault(configAndPath[1], null, comment);
config.makeSectionLenient(configAndPath[1]);
defaultKeyValue.forEach((string, object) -> config.addExample(configAndPath[1] + "." + string, object));
return config.getConfigSection(configAndPath[1]);
}

public void addComment(String path, String comment) {
config.addComment(path, comment);
String[] configAndPath = getConfigNameAndPath(path);
ConfigFile config = getConfig(configAndPath[0]);
if (config != null) {
config.addComment(configAndPath[1], comment);
}
}

public void addDefault(String path, Object value) {
String[] configAndPath = getConfigNameAndPath(path);
ConfigFile config = getConfig(configAndPath[0]);
if (config != null) {
config.addDefault(configAndPath[1], value);
}
}

private interface ConfigValueGetter<T> {
T get(ConfigFile config, String path, T defaultValue);
}
}
}
Loading