Fix crash when CommandManager calling Plugin#getConfig before config is migrated

This commit is contained in:
Ben Woo 2025-01-03 20:15:13 +08:00
parent 33e440ffec
commit a5ba42285f
4 changed files with 68 additions and 11 deletions

View File

@ -15,6 +15,7 @@ import com.dumptruckman.minecraft.util.Logging;
import io.vavr.control.Try;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
@ -101,7 +102,8 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
// Load our configs first as we need them for everything else.
var config = configProvider.get();
if (!config.isLoaded()) {
var loadSuccess = config.load().andThenTry(config::save).isSuccess();
if (!loadSuccess || !config.isLoaded()) {
Logging.severe("Your configs were not loaded.");
Logging.severe("Please check your configs and restart the server.");
this.getServer().getPluginManager().disablePlugin(this);
@ -367,6 +369,40 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
this.pluginCount -= 1;
}
/**
* {@inheritDoc}
*/
@Override
public @NotNull FileConfiguration getConfig() {
MVCoreConfig mvCoreConfig = this.configProvider.get();
var config = mvCoreConfig.getConfig();
if (config != null && mvCoreConfig.isLoaded()) {
return config;
}
var loadSuccess = mvCoreConfig.load().isSuccess();
if (!loadSuccess || !mvCoreConfig.isLoaded()) {
throw new RuntimeException("Failed to load configs");
}
return mvCoreConfig.getConfig();
}
/**
* {@inheritDoc}
*/
@Override
public void reloadConfig() {
this.configProvider.get().load();
}
/**
* {@inheritDoc}
*/
@Override
public void saveConfig() {
this.configProvider.get().save();
}
/**
* {@inheritDoc}
*/

View File

@ -9,6 +9,8 @@ import java.util.Objects;
import com.dumptruckman.minecraft.util.Logging;
import io.vavr.control.Try;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -37,7 +39,11 @@ public class MVCoreConfig implements MVConfig {
private final StringPropertyHandle stringPropertyHandle;
@Inject
MVCoreConfig(@NotNull MultiverseCore core, @NotNull PluginManager pluginManager, @NotNull MVCommandManager commandManager) {
MVCoreConfig(
@NotNull MultiverseCore core,
@NotNull PluginManager pluginManager,
@NotNull Provider<MVCommandManager> commandManager // config needs to be instantiated before the command manager
) {
this.configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME);
this.configNodes = new MVCoreConfigNodes(pluginManager, commandManager);
this.configHandle = CommentedYamlConfigHandle.builder(configPath, configNodes.getNodes())
@ -73,8 +79,6 @@ public class MVCoreConfig implements MVConfig {
.build())
.build();
this.stringPropertyHandle = new StringPropertyHandle(configHandle);
load();
save();
}
private void migrateFromOldConfigFile() {
@ -95,8 +99,12 @@ public class MVCoreConfig implements MVConfig {
@Override
public Try<Void> load() {
migrateFromOldConfigFile();
return configHandle.load();
return Try.run(this::migrateFromOldConfigFile)
.flatMap(ignore -> configHandle.load())
.onFailure(e -> {
Logging.severe("Failed to load Multiverse-Core config.yml!");
e.printStackTrace();
});
}
@Override
@ -297,4 +305,12 @@ public class MVCoreConfig implements MVConfig {
public boolean isShowingDonateMessage() {
return configHandle.get(configNodes.SHOW_DONATION_MESSAGE);
}
/**
* Gets the underlying config file object
* @return The config file
*/
public FileConfiguration getConfig() {
return configHandle.getConfig();
}
}

View File

@ -2,6 +2,7 @@ package org.mvplugins.multiverse.core.config;
import com.dumptruckman.minecraft.util.Logging;
import io.vavr.control.Try;
import jakarta.inject.Provider;
import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.NotNull;
@ -19,9 +20,9 @@ class MVCoreConfigNodes {
private final NodeGroup nodes = new NodeGroup();
private PluginManager pluginManager;
private MVCommandManager commandManager;
private Provider<MVCommandManager> commandManager;
MVCoreConfigNodes(@NotNull PluginManager pluginManager, @NotNull MVCommandManager commandManager) {
MVCoreConfigNodes(@NotNull PluginManager pluginManager, @NotNull Provider<MVCommandManager> commandManager) {
this.pluginManager = pluginManager;
this.commandManager = commandManager;
}
@ -199,7 +200,7 @@ class MVCoreConfigNodes {
.defaultValue(Locale.ENGLISH)
.name("default-locale")
.onSetValue((oldValue, newValue) -> {
commandManager.getLocales().setDefaultLocale(newValue);
commandManager.get().getLocales().setDefaultLocale(newValue);
})
.build());
@ -211,7 +212,7 @@ class MVCoreConfigNodes {
.name("per-player-locale")
.onSetValue((oldValue, newValue) -> {
// autoDetectFromClient will be done by MVLocalesListener instead
commandManager.usePerIssuerLocale(newValue, false);
commandManager.get().usePerIssuerLocale(newValue, false);
})
.build());

View File

@ -3,14 +3,18 @@ multiverse-configuration:
enforceaccess: 'true'
prefixchat: 'false'
prefixchatformat: '[%world%]>>%chat%'
useasyncchat: 'true'
teleportintercept: 'false'
firstspawnoverride: 'true'
displaypermerrors: 'false'
enablebuscript: 'false'
globaldebug: '2'
silentstart: 'false'
messagecooldown: '5000'
version: '2.9'
firstspawnworld: world
teleportcooldown: '1000'
defaultportalsearch: 'true'
portalsearchradius: '128'
autopurge: 'true'
idonotwanttodonate: 'true'
idonotwanttodonate: 'true'