mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2025-01-25 09:41:23 +01:00
Use DI for new config.
This commit is contained in:
parent
a45e95c7fd
commit
7f9c45d8e1
@ -21,11 +21,9 @@ import com.onarandombox.MultiverseCore.api.Destination;
|
||||
import com.onarandombox.MultiverseCore.api.MVCore;
|
||||
import com.onarandombox.MultiverseCore.api.MVWorld;
|
||||
import com.onarandombox.MultiverseCore.api.MVWorldManager;
|
||||
import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
|
||||
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
|
||||
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
|
||||
import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider;
|
||||
import com.onarandombox.MultiverseCore.configuration.DefaultMVConfig;
|
||||
import com.onarandombox.MultiverseCore.destination.DestinationsProvider;
|
||||
import com.onarandombox.MultiverseCore.economy.MVEconomist;
|
||||
import com.onarandombox.MultiverseCore.inject.InjectableListener;
|
||||
@ -347,16 +345,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
|
||||
@Override
|
||||
public void loadConfigs() {
|
||||
getConfigProvider().loadConfigs();
|
||||
// TODO move to config provider
|
||||
// config = DefaultMVConfig.init(this);
|
||||
//
|
||||
// this.worldManager.loadWorldConfig(new File(getDataFolder(), "worlds.yml"));
|
||||
//
|
||||
// int level = Logging.getDebugLevel();
|
||||
// Logging.setDebugLevel(getMVConfig().getGlobalDebug());
|
||||
// if (level != Logging.getDebugLevel()) {
|
||||
// getServer().getPluginManager().callEvent(new MVDebugModeEvent(level));
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -9,18 +9,29 @@ import co.aikar.commands.annotation.Optional;
|
||||
import co.aikar.commands.annotation.Single;
|
||||
import co.aikar.commands.annotation.Subcommand;
|
||||
import co.aikar.commands.annotation.Syntax;
|
||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||
import com.onarandombox.MultiverseCore.api.MVConfig;
|
||||
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
|
||||
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
|
||||
import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue;
|
||||
import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jvnet.hk2.annotations.Service;
|
||||
|
||||
@Service
|
||||
@CommandAlias("mv")
|
||||
public class ConfigCommand extends MultiverseCoreCommand {
|
||||
private final MVConfig config;
|
||||
public class ConfigCommand extends MultiverseCommand {
|
||||
|
||||
public ConfigCommand(@NotNull MultiverseCore plugin) {
|
||||
super(plugin);
|
||||
this.config = plugin.getMVConfig();
|
||||
private final MVCoreConfigProvider configProvider;
|
||||
|
||||
@Inject
|
||||
public ConfigCommand(@NotNull MVCommandManager commandManager, @NotNull MVCoreConfigProvider configProvider) {
|
||||
super(commandManager);
|
||||
this.configProvider = configProvider;
|
||||
}
|
||||
|
||||
private MVConfig getConfig() {
|
||||
return configProvider.getConfig();
|
||||
}
|
||||
|
||||
@Subcommand("config")
|
||||
@ -48,20 +59,20 @@ public class ConfigCommand extends MultiverseCoreCommand {
|
||||
}
|
||||
|
||||
private void showConfigValue(BukkitCommandIssuer issuer, String name) {
|
||||
Object currentValue = config.getProperty(name);
|
||||
Object currentValue = getConfig().getProperty(name);
|
||||
if (currentValue == null) {
|
||||
issuer.sendMessage("No such config option: " + name);
|
||||
return;
|
||||
}
|
||||
issuer.sendMessage(name + "is currently set to " + config.getProperty(name));
|
||||
issuer.sendMessage(name + "is currently set to " + getConfig().getProperty(name));
|
||||
}
|
||||
|
||||
private void updateConfigValue(BukkitCommandIssuer issuer, String name, Object value) {
|
||||
if (!config.setProperty(name, value)) {
|
||||
if (!getConfig().setProperty(name, value)) {
|
||||
issuer.sendMessage("Unable to set " + name + " to " + value);
|
||||
return;
|
||||
}
|
||||
config.save();
|
||||
getConfig().save();
|
||||
issuer.sendMessage("Successfully set " + name + " to " + value);
|
||||
}
|
||||
}
|
||||
|
@ -8,11 +8,11 @@ import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Description;
|
||||
import co.aikar.commands.annotation.Subcommand;
|
||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||
import com.onarandombox.MultiverseCore.anchor.AnchorManager;
|
||||
import com.onarandombox.MultiverseCore.api.MVWorldManager;
|
||||
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
|
||||
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
|
||||
import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider;
|
||||
import com.onarandombox.MultiverseCore.event.MVConfigReloadEvent;
|
||||
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
|
||||
import jakarta.inject.Inject;
|
||||
@ -24,7 +24,7 @@ import org.jvnet.hk2.annotations.Service;
|
||||
@CommandAlias("mv")
|
||||
public class ReloadCommand extends MultiverseCommand {
|
||||
|
||||
private final MultiverseCore plugin;
|
||||
private final MVCoreConfigProvider configProvider;
|
||||
private final AnchorManager anchorManager;
|
||||
private final MVWorldManager worldManager;
|
||||
private final PluginManager pluginManager;
|
||||
@ -32,13 +32,13 @@ public class ReloadCommand extends MultiverseCommand {
|
||||
@Inject
|
||||
public ReloadCommand(
|
||||
@NotNull MVCommandManager commandManager,
|
||||
@NotNull MultiverseCore plugin,
|
||||
@NotNull MVCoreConfigProvider configProvider,
|
||||
@NotNull AnchorManager anchorManager,
|
||||
@NotNull MVWorldManager worldManager,
|
||||
@NotNull PluginManager pluginManager
|
||||
) {
|
||||
super(commandManager);
|
||||
this.plugin = plugin;
|
||||
this.configProvider = configProvider;
|
||||
this.anchorManager = anchorManager;
|
||||
this.worldManager = worldManager;
|
||||
this.pluginManager = pluginManager;
|
||||
@ -49,7 +49,7 @@ public class ReloadCommand extends MultiverseCommand {
|
||||
@Description("{@@mv-core.reload.description}")
|
||||
public void onReloadCommand(@NotNull BukkitCommandIssuer issuer) {
|
||||
issuer.sendInfo(MVCorei18n.RELOAD_RELOADING);
|
||||
this.plugin.loadConfigs();
|
||||
this.configProvider.loadConfigs();
|
||||
this.anchorManager.loadAnchors();
|
||||
this.worldManager.loadWorlds(true);
|
||||
|
||||
|
@ -2,39 +2,27 @@ package com.onarandombox.MultiverseCore.config;
|
||||
|
||||
import com.dumptruckman.minecraft.util.Logging;
|
||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||
import com.onarandombox.MultiverseCore.MultiverseCoreConfiguration;
|
||||
import com.onarandombox.MultiverseCore.api.MVConfig;
|
||||
import com.onarandombox.MultiverseCore.api.MVWorldManager;
|
||||
import com.onarandombox.MultiverseCore.configuration.DefaultMVConfig;
|
||||
import com.onarandombox.MultiverseCore.event.MVDebugModeEvent;
|
||||
import io.vavr.control.Option;
|
||||
import io.vavr.control.Try;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Provider;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jvnet.hk2.annotations.Service;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
@Service
|
||||
public final class MVCoreConfigProvider {
|
||||
|
||||
private static final String CONFIG_FILE = "config.yml";
|
||||
private static final String DEFAULTS_CONFIG_FILE = "/defaults/config.yml";
|
||||
private static final String WORLDS_CONFIG_FILE = "worlds.yml";
|
||||
private static final String CHARACTER_ENCODING = "UTF-8";
|
||||
private static final String CONFIG_KEY = "multiverse-configuration";
|
||||
|
||||
private FileConfiguration multiverseConfig;
|
||||
private volatile MultiverseCoreConfiguration config;
|
||||
private volatile DefaultMVConfig config;
|
||||
|
||||
private final Plugin plugin;
|
||||
private final MultiverseCore plugin;
|
||||
private final PluginManager pluginManager;
|
||||
|
||||
private final Provider<MVWorldManager> worldManagerProvider; // TODO remove this dependency
|
||||
@ -60,60 +48,26 @@ public final class MVCoreConfigProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Core configuration instance.
|
||||
* Gets the Core configuration instance. If the config has not been loaded yet, it will first be loaded.
|
||||
*
|
||||
* @return The config
|
||||
* @throws IllegalStateException If the config is not loaded
|
||||
*/
|
||||
@NotNull
|
||||
public MVConfig getConfig() throws IllegalStateException {
|
||||
public MVConfig getConfig() {
|
||||
if (config == null) {
|
||||
throw new IllegalStateException("Config is not loaded");
|
||||
loadConfigs();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
public void loadConfigs() {
|
||||
multiverseConfig = loadConfigWithDefaults();
|
||||
setConfigOptions(multiverseConfig);
|
||||
config = getOrCreateConfigObject(multiverseConfig);
|
||||
config = DefaultMVConfig.init(plugin);
|
||||
|
||||
loadWorldConfigs();
|
||||
|
||||
setDebugLevelFromConfig(config);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private FileConfiguration loadConfigWithDefaults() {
|
||||
var config = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), CONFIG_FILE));
|
||||
|
||||
Option.of(plugin.getClass().getResourceAsStream(DEFAULTS_CONFIG_FILE))
|
||||
.toTry()
|
||||
.mapTry(defaultsStream -> YamlConfiguration.loadConfiguration(
|
||||
new BufferedReader(new InputStreamReader(defaultsStream, CHARACTER_ENCODING))))
|
||||
.andThen(config::setDefaults)
|
||||
.onFailure(e -> {
|
||||
Logging.severe("Couldn't load default config with UTF-8 encoding. Details follow:");
|
||||
e.printStackTrace();
|
||||
Logging.severe("Default configs NOT loaded.");
|
||||
});
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
private void setConfigOptions(@NotNull FileConfiguration config) {
|
||||
config.options().copyDefaults(false);
|
||||
config.options().copyHeader(true);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private MultiverseCoreConfiguration getOrCreateConfigObject(@NotNull FileConfiguration config) {
|
||||
return Try.of(() -> (MultiverseCoreConfiguration) config.get(CONFIG_KEY))
|
||||
.toOption() // Ignore exceptions
|
||||
.map(c -> c == null ? new MultiverseCoreConfiguration() : c)
|
||||
.getOrElse(new MultiverseCoreConfiguration());
|
||||
}
|
||||
|
||||
private void loadWorldConfigs() {
|
||||
worldManagerProvider.get().loadWorldConfig(new File(plugin.getDataFolder(), WORLDS_CONFIG_FILE));
|
||||
}
|
||||
@ -128,13 +82,9 @@ public final class MVCoreConfigProvider {
|
||||
|
||||
@NotNull
|
||||
public Try<Void> saveConfig() {
|
||||
try {
|
||||
this.multiverseConfig.set(CONFIG_KEY, config);
|
||||
this.multiverseConfig.save(new File(plugin.getDataFolder(), CONFIG_FILE));
|
||||
return Try.success(null);
|
||||
} catch (IOException e) {
|
||||
return Try.failure(new MultiverseConfigurationException(
|
||||
"Could not save Multiverse config.yml config. Please check your file permissions.", e));
|
||||
if (config != null) {
|
||||
return Try.run(() -> config.save());
|
||||
}
|
||||
return Try.failure(new IllegalStateException("Config not loaded yet"));
|
||||
}
|
||||
}
|
||||
|
@ -7,13 +7,11 @@ import java.util.stream.Collectors;
|
||||
|
||||
import co.aikar.commands.BukkitCommandIssuer;
|
||||
import co.aikar.commands.CommandIssuer;
|
||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||
import com.onarandombox.MultiverseCore.api.Destination;
|
||||
import com.onarandombox.MultiverseCore.api.DestinationInstance;
|
||||
import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
|
||||
import com.onarandombox.MultiverseCore.api.Teleporter;
|
||||
import jakarta.inject.Inject;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.Permission;
|
||||
|
@ -13,13 +13,13 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.dumptruckman.minecraft.util.Logging;
|
||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||
import com.onarandombox.MultiverseCore.api.BlockSafety;
|
||||
import com.onarandombox.MultiverseCore.api.LocationManipulation;
|
||||
import com.onarandombox.MultiverseCore.api.MVWorld;
|
||||
import com.onarandombox.MultiverseCore.api.MVWorldManager;
|
||||
import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
|
||||
import com.onarandombox.MultiverseCore.api.WorldPurger;
|
||||
import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider;
|
||||
import com.onarandombox.MultiverseCore.exceptions.PropertyDoesNotExistException;
|
||||
import com.onarandombox.MultiverseCore.listeners.MVPlayerListener;
|
||||
import com.onarandombox.MultiverseCore.world.configuration.AllowedPortalType;
|
||||
@ -55,6 +55,7 @@ public class SimpleMVWorld implements MVWorld {
|
||||
private static final int SPAWN_LOCATION_SEARCH_RADIUS = 16;
|
||||
|
||||
private final MVWorldManager worldManager;
|
||||
private final MVCoreConfigProvider configProvider;
|
||||
private final WorldPurger worldPurger;
|
||||
private final MVPlayerListener playerListener;
|
||||
private final BlockSafety blockSafety;
|
||||
@ -67,6 +68,7 @@ public class SimpleMVWorld implements MVWorld {
|
||||
|
||||
public SimpleMVWorld(
|
||||
MVWorldManager worldManager,
|
||||
MVCoreConfigProvider configProvider,
|
||||
WorldPurger worldPurger,
|
||||
MVPlayerListener playerListener,
|
||||
BlockSafety blockSafety,
|
||||
@ -76,7 +78,7 @@ public class SimpleMVWorld implements MVWorld {
|
||||
World world,
|
||||
WorldProperties properties
|
||||
) {
|
||||
this(worldManager, worldPurger, playerListener, blockSafety, safeTTeleporter,
|
||||
this(worldManager, configProvider, worldPurger, playerListener, blockSafety, safeTTeleporter,
|
||||
locationManipulation, server, world, properties, true);
|
||||
}
|
||||
|
||||
@ -85,6 +87,7 @@ public class SimpleMVWorld implements MVWorld {
|
||||
*/
|
||||
public SimpleMVWorld(
|
||||
MVWorldManager worldManager,
|
||||
MVCoreConfigProvider configProvider,
|
||||
WorldPurger worldPurger,
|
||||
MVPlayerListener playerListener,
|
||||
BlockSafety blockSafety,
|
||||
@ -96,6 +99,7 @@ public class SimpleMVWorld implements MVWorld {
|
||||
boolean fixSpawn
|
||||
) {
|
||||
this.worldManager = worldManager;
|
||||
this.configProvider = configProvider;
|
||||
this.worldPurger = worldPurger;
|
||||
this.playerListener = playerListener;
|
||||
this.blockSafety = blockSafety;
|
||||
@ -321,7 +325,7 @@ public class SimpleMVWorld implements MVWorld {
|
||||
}
|
||||
world.setSpawnFlags(allowMonsters, allowAnimals);
|
||||
}
|
||||
if (MultiverseCoreConfiguration.getInstance().isAutoPurgeEntities()) {
|
||||
if (configProvider.getConfig().isAutoPurgeEntities()) {
|
||||
worldPurger.purgeWorld(SimpleMVWorld.this);
|
||||
}
|
||||
return super.validateChange(property, newValue, oldValue, object);
|
||||
|
@ -32,6 +32,7 @@ import com.onarandombox.MultiverseCore.api.MVWorldManager;
|
||||
import com.onarandombox.MultiverseCore.api.MVWorld;
|
||||
import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
|
||||
import com.onarandombox.MultiverseCore.api.WorldPurger;
|
||||
import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider;
|
||||
import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent;
|
||||
import com.onarandombox.MultiverseCore.listeners.MVPlayerListener;
|
||||
import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper;
|
||||
@ -61,6 +62,7 @@ import org.jvnet.hk2.annotations.Service;
|
||||
@Service
|
||||
public class SimpleMVWorldManager implements MVWorldManager {
|
||||
private final MultiverseCore plugin;
|
||||
private final MVCoreConfigProvider configProvider;
|
||||
private final MVPlayerListener playerListener;
|
||||
private final BlockSafety blockSafety;
|
||||
private final SafeTTeleporter safeTTeleporter;
|
||||
@ -77,6 +79,7 @@ public class SimpleMVWorldManager implements MVWorldManager {
|
||||
@Inject
|
||||
public SimpleMVWorldManager(
|
||||
MultiverseCore plugin,
|
||||
MVCoreConfigProvider configProvider,
|
||||
MVPlayerListener playerListener,
|
||||
BlockSafety blockSafety,
|
||||
SafeTTeleporter safeTTeleporter,
|
||||
@ -86,6 +89,7 @@ public class SimpleMVWorldManager implements MVWorldManager {
|
||||
Server server
|
||||
) {
|
||||
this.plugin = plugin;
|
||||
this.configProvider = configProvider;
|
||||
this.playerListener = playerListener;
|
||||
this.blockSafety = blockSafety;
|
||||
this.safeTTeleporter = safeTTeleporter;
|
||||
@ -511,9 +515,9 @@ public class SimpleMVWorldManager implements MVWorldManager {
|
||||
nullWorld(worldName);
|
||||
return false;
|
||||
}
|
||||
SimpleMVWorld world = new SimpleMVWorld(this, worldPurger, playerListener, blockSafety, safeTTeleporter,
|
||||
locationManipulation, server, cbworld, mvworld);
|
||||
if (MultiverseCoreConfiguration.getInstance().isAutoPurgeEntities()) {
|
||||
SimpleMVWorld world = new SimpleMVWorld(this, configProvider, worldPurger, playerListener, blockSafety,
|
||||
safeTTeleporter, locationManipulation, server, cbworld, mvworld);
|
||||
if (configProvider.getConfig().isAutoPurgeEntities()) {
|
||||
this.worldPurger.purgeWorld(world);
|
||||
}
|
||||
this.worlds.put(worldName, world);
|
||||
|
@ -120,7 +120,7 @@ class InjectionTest : TestWithMockBukkit() {
|
||||
fun `Commands are available as services`() {
|
||||
val commands = multiverseCore.getAllServices(MultiverseCommand::class.java)
|
||||
// TODO come up with a better way to test this like via actually testing the effect of calling each command
|
||||
assertEquals(16, commands.size)
|
||||
assertEquals(17, commands.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user