refactor: Move MVCoreConfigNodes to a non-static context

This commit is contained in:
Ben Woo 2023-03-27 11:39:11 +08:00
parent ce9d53c6e9
commit a8c8ef7a6b
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
5 changed files with 95 additions and 66 deletions

View File

@ -1,5 +1,8 @@
package com.onarandombox.MultiverseCore.api;
import java.util.Collection;
import com.onarandombox.MultiverseCore.configuration.node.NodeGroup;
import com.onarandombox.MultiverseCore.placeholders.MultiverseCorePlaceholders;
public interface MVConfig {
@ -15,6 +18,13 @@ public interface MVConfig {
*/
void save();
/**
* Gets the nodes for the config.
*
* @return The nodes for the config.
*/
NodeGroup getNodes();
/**
* Gets a property from the config.
*

View File

@ -18,7 +18,7 @@ import co.aikar.commands.RootCommand;
import com.google.common.collect.Sets;
import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.config.MVCoreConfigNodes;
import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider;
import com.onarandombox.MultiverseCore.destination.DestinationsProvider;
import com.onarandombox.MultiverseCore.destination.ParsedDestination;
import jakarta.inject.Inject;
@ -37,8 +37,9 @@ public class MVCommandCompletions extends PaperCommandCompletions {
public MVCommandCompletions(
@NotNull MVCommandManager mvCommandManager,
@NotNull MVWorldManager worldManager,
@NotNull DestinationsProvider destinationsProvider
) {
@NotNull DestinationsProvider destinationsProvider,
@NotNull MVCoreConfigProvider configProvider
) {
super(mvCommandManager);
this.commandManager = mvCommandManager;
this.worldManager = worldManager;
@ -48,7 +49,7 @@ public class MVCommandCompletions extends PaperCommandCompletions {
registerAsyncCompletion("destinations", this::suggestDestinations);
registerAsyncCompletion("flags", this::suggestFlags);
registerStaticCompletion("gamerules", this::suggestGamerules);
registerStaticCompletion("mvconfigs", MVCoreConfigNodes.getNodes().getNames());
registerStaticCompletion("mvconfigs", configProvider.getConfig().getNodes().getNames());
registerAsyncCompletion("mvworlds", this::suggestMVWorlds);
setDefaultCompletion("destinations", ParsedDestination.class);

View File

@ -14,7 +14,7 @@ import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.commandtools.context.GameRuleValue;
import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue;
import com.onarandombox.MultiverseCore.config.MVCoreConfigNodes;
import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider;
import com.onarandombox.MultiverseCore.destination.DestinationsProvider;
import com.onarandombox.MultiverseCore.destination.ParsedDestination;
import com.onarandombox.MultiverseCore.display.filters.ContentFilter;
@ -33,16 +33,19 @@ public class MVCommandContexts extends PaperCommandContexts {
private final DestinationsProvider destinationsProvider;
private final MVWorldManager worldManager;
private final MVCoreConfigProvider configProvider;
@Inject
public MVCommandContexts(
MVCommandManager mvCommandManager,
DestinationsProvider destinationsProvider,
MVWorldManager worldManager
MVWorldManager worldManager,
MVCoreConfigProvider configProvider
) {
super(mvCommandManager);
this.destinationsProvider = destinationsProvider;
this.worldManager = worldManager;
this.configProvider = configProvider;
registerIssuerOnlyContext(BukkitCommandIssuer.class, BukkitCommandExecutionContext::getIssuer);
registerOptionalContext(ContentFilter.class, this::parseContentFilter);
@ -120,7 +123,7 @@ public class MVCommandContexts extends PaperCommandContexts {
if (Strings.isNullOrEmpty(configName)) {
throw new InvalidCommandArgument("No config name specified.");
}
Optional<CommentedNode> node = MVCoreConfigNodes.getNodes().findNode(configName);
Optional<CommentedNode> node = configProvider.getConfig().getNodes().findNode(configName);
if (node.isEmpty()) {
throw new InvalidCommandArgument("The config " + configName + " is not valid.");
}

View File

@ -14,6 +14,8 @@ import com.onarandombox.MultiverseCore.configuration.migration.IntegerMigratorAc
import com.onarandombox.MultiverseCore.configuration.migration.InvertBoolMigratorAction;
import com.onarandombox.MultiverseCore.configuration.migration.MoveMigratorAction;
import com.onarandombox.MultiverseCore.configuration.migration.VersionMigrator;
import com.onarandombox.MultiverseCore.configuration.node.NodeGroup;
import org.bukkit.plugin.PluginManager;
public class MVCoreConfig implements MVConfig {
public static final String CONFIG_FILENAME = "config.yml";
@ -33,17 +35,16 @@ public class MVCoreConfig implements MVConfig {
}
private final Path configPath;
private final MVCoreConfigNodes configNodes;
private final ConfigHandle configHandle;
public MVCoreConfig(MultiverseCore core) {
configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME);
migrateFromOldConfigFile();
configNodes = new MVCoreConfigNodes(core.getService(PluginManager.class));
configHandle = ConfigHandle.builder(configPath)
.logger(Logging.getLogger())
.nodes(MVCoreConfigNodes.getNodes())
.migrator(ConfigMigrator.builder(MVCoreConfigNodes.VERSION)
.nodes(configNodes.getNodes())
.migrator(ConfigMigrator.builder(configNodes.VERSION)
.addVersionMigrator(VersionMigrator.builder(5.0)
.addAction(MoveMigratorAction.of("multiverse-configuration.enforceaccess", "world.enforce-access"))
.addAction(BooleanMigratorAction.of("world.enforce-access"))
@ -73,6 +74,8 @@ public class MVCoreConfig implements MVConfig {
.build())
.build())
.build();
migrateFromOldConfigFile();
}
private void migrateFromOldConfigFile() {
@ -101,6 +104,11 @@ public class MVCoreConfig implements MVConfig {
configHandle.save();
}
@Override
public NodeGroup getNodes() {
return configNodes.getNodes();
}
@Override
public Object getProperty(String name) {
return configHandle.get(name);
@ -113,141 +121,141 @@ public class MVCoreConfig implements MVConfig {
@Override
public void setEnforceAccess(boolean enforceAccess) {
configHandle.set(MVCoreConfigNodes.ENFORCE_ACCESS, enforceAccess);
configHandle.set(configNodes.ENFORCE_ACCESS, enforceAccess);
}
@Override
public boolean getEnforceAccess() {
return configHandle.get(MVCoreConfigNodes.ENFORCE_ACCESS);
return configHandle.get(configNodes.ENFORCE_ACCESS);
}
@Override
public void setEnforceGameMode(boolean enforceGameMode) {
configHandle.set(MVCoreConfigNodes.ENFORCE_GAMEMODE, enforceGameMode);
configHandle.set(configNodes.ENFORCE_GAMEMODE, enforceGameMode);
}
@Override
public boolean getEnforceGameMode() {
return configHandle.get(MVCoreConfigNodes.ENFORCE_GAMEMODE);
return configHandle.get(configNodes.ENFORCE_GAMEMODE);
}
@Override
public void setAutoPurgeEntities(boolean autopurge) {
configHandle.set(MVCoreConfigNodes.AUTO_PURGE_ENTITIES, autopurge);
configHandle.set(configNodes.AUTO_PURGE_ENTITIES, autopurge);
}
@Override
public boolean isAutoPurgeEntities() {
return configHandle.get(MVCoreConfigNodes.AUTO_PURGE_ENTITIES);
return configHandle.get(configNodes.AUTO_PURGE_ENTITIES);
}
@Override
public void setTeleportIntercept(boolean teleportIntercept) {
configHandle.set(MVCoreConfigNodes.TELEPORT_INTERCEPT, teleportIntercept);
configHandle.set(configNodes.TELEPORT_INTERCEPT, teleportIntercept);
}
@Override
public boolean getTeleportIntercept() {
return configHandle.get(MVCoreConfigNodes.TELEPORT_INTERCEPT);
return configHandle.get(configNodes.TELEPORT_INTERCEPT);
}
@Override
public void setFirstSpawnOverride(boolean firstSpawnOverride) {
configHandle.set(MVCoreConfigNodes.FIRST_SPAWN_OVERRIDE, firstSpawnOverride);
configHandle.set(configNodes.FIRST_SPAWN_OVERRIDE, firstSpawnOverride);
}
@Override
public boolean getFirstSpawnOverride() {
return configHandle.get(MVCoreConfigNodes.FIRST_SPAWN_OVERRIDE);
return configHandle.get(configNodes.FIRST_SPAWN_OVERRIDE);
}
@Override
public void setFirstSpawnLocation(String firstSpawnWorld) {
configHandle.set(MVCoreConfigNodes.FIRST_SPAWN_LOCATION, firstSpawnWorld);
configHandle.set(configNodes.FIRST_SPAWN_LOCATION, firstSpawnWorld);
}
@Override
public String getFirstSpawnLocation() {
return configHandle.get(MVCoreConfigNodes.FIRST_SPAWN_LOCATION);
return configHandle.get(configNodes.FIRST_SPAWN_LOCATION);
}
@Override
public void setUseCustomPortalSearch(boolean useDefaultPortalSearch) {
configHandle.set(MVCoreConfigNodes.USE_CUSTOM_PORTAL_SEARCH, useDefaultPortalSearch);
configHandle.set(configNodes.USE_CUSTOM_PORTAL_SEARCH, useDefaultPortalSearch);
}
@Override
public boolean isUsingCustomPortalSearch() {
return configHandle.get(MVCoreConfigNodes.USE_CUSTOM_PORTAL_SEARCH);
return configHandle.get(configNodes.USE_CUSTOM_PORTAL_SEARCH);
}
@Override
public void setCustomPortalSearchRadius(int searchRadius) {
configHandle.set(MVCoreConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS, searchRadius);
configHandle.set(configNodes.CUSTOM_PORTAL_SEARCH_RADIUS, searchRadius);
}
@Override
public int getCustomPortalSearchRadius() {
return configHandle.get(MVCoreConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS);
return configHandle.get(configNodes.CUSTOM_PORTAL_SEARCH_RADIUS);
}
@Override
public void setEnablePrefixChat(boolean prefixChat) {
configHandle.set(MVCoreConfigNodes.ENABLE_CHAT_PREFIX, prefixChat);
configHandle.set(configNodes.ENABLE_CHAT_PREFIX, prefixChat);
}
@Override
public boolean isEnablePrefixChat() {
return configHandle.get(MVCoreConfigNodes.ENABLE_CHAT_PREFIX);
return configHandle.get(configNodes.ENABLE_CHAT_PREFIX);
}
@Override
public void setPrefixChatFormat(String prefixChatFormat) {
configHandle.set(MVCoreConfigNodes.CHAT_PREFIX_FORMAT, prefixChatFormat);
configHandle.set(configNodes.CHAT_PREFIX_FORMAT, prefixChatFormat);
}
@Override
public String getPrefixChatFormat() {
return configHandle.get(MVCoreConfigNodes.CHAT_PREFIX_FORMAT);
return configHandle.get(configNodes.CHAT_PREFIX_FORMAT);
}
@Override
public void setRegisterPapiHook(boolean registerPapiHook) {
configHandle.set(MVCoreConfigNodes.REGISTER_PAPI_HOOK, registerPapiHook);
configHandle.set(configNodes.REGISTER_PAPI_HOOK, registerPapiHook);
}
@Override
public boolean isRegisterPapiHook() {
return configHandle.get(MVCoreConfigNodes.REGISTER_PAPI_HOOK);
return configHandle.get(configNodes.REGISTER_PAPI_HOOK);
}
@Override
public void setGlobalDebug(int globalDebug) {
configHandle.set(MVCoreConfigNodes.GLOBAL_DEBUG, globalDebug);
configHandle.set(configNodes.GLOBAL_DEBUG, globalDebug);
}
@Override
public int getGlobalDebug() {
return configHandle.get(MVCoreConfigNodes.GLOBAL_DEBUG);
return configHandle.get(configNodes.GLOBAL_DEBUG);
}
@Override
public void setSilentStart(boolean silentStart) {
configHandle.set(MVCoreConfigNodes.SILENT_START, silentStart);
configHandle.set(configNodes.SILENT_START, silentStart);
}
@Override
public boolean getSilentStart() {
return configHandle.get(MVCoreConfigNodes.SILENT_START);
return configHandle.get(configNodes.SILENT_START);
}
@Override
public void setShowDonateMessage(boolean showDonateMessage) {
configHandle.set(MVCoreConfigNodes.SHOW_DONATION_MESSAGE, showDonateMessage);
configHandle.set(configNodes.SHOW_DONATION_MESSAGE, showDonateMessage);
}
@Override
public boolean isShowingDonateMessage() {
return configHandle.get(MVCoreConfigNodes.SHOW_DONATION_MESSAGE);
return configHandle.get(configNodes.SHOW_DONATION_MESSAGE);
}
}

View File

@ -5,20 +5,27 @@ import com.onarandombox.MultiverseCore.configuration.node.MVCommentedNode;
import com.onarandombox.MultiverseCore.configuration.node.MVValueNode;
import com.onarandombox.MultiverseCore.configuration.node.NodeGroup;
import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode;
import org.bukkit.plugin.PluginManager;
public class MVCoreConfigNodes {
private static final NodeGroup nodes = new NodeGroup();
class MVCoreConfigNodes {
public static NodeGroup getNodes() {
private final NodeGroup nodes = new NodeGroup();
private final PluginManager pluginManager;
MVCoreConfigNodes(PluginManager pluginManager) {
this.pluginManager = pluginManager;
}
public NodeGroup getNodes() {
return nodes;
}
private static <N extends CommentedNode> N node(N node) {
private <N extends CommentedNode> N node(N node) {
nodes.add(node);
return node;
}
private static final MVCommentedNode HEADER = node(MVCommentedNode.builder("world") // TODO hacky way to get the header to the top of the file
private final MVCommentedNode HEADER = node(MVCommentedNode.builder("world") // TODO hacky way to get the header to the top of the file
.comment("####################################################################################################")
.comment("# #")
.comment("# █▀▄▀█ █░█ █░░ ▀█▀ █ █░█ █▀▀ █▀█ █▀ █▀▀   █▀▀ █▀█ █▀█ █▀▀ #")
@ -42,12 +49,12 @@ public class MVCoreConfigNodes {
.comment("")
.build());
// private static final MVCommentedNode WORLD_HEADER = node(MVCommentedNode.builder("world")
// private final MVCommentedNode WORLD_HEADER = node(MVCommentedNode.builder("world")
// .comment("")
// .comment("")
// .build());
public static final MVValueNode<Boolean> ENFORCE_ACCESS = node(MVValueNode.builder("world.enforce-access", Boolean.class)
public final MVValueNode<Boolean> ENFORCE_ACCESS = node(MVValueNode.builder("world.enforce-access", Boolean.class)
.comment("This setting will prevent players from entering worlds they don't have access to.")
.comment("If this is set to false, players will be able to enter any world they want.")
.comment("If this is set to true, players will only be able to enter worlds they have")
@ -56,7 +63,7 @@ public class MVCoreConfigNodes {
.name("enforce-access")
.build());
public static final MVValueNode<Boolean> ENFORCE_GAMEMODE = node(MVValueNode.builder("world.enforce-gamemode", Boolean.class)
public final MVValueNode<Boolean> ENFORCE_GAMEMODE = node(MVValueNode.builder("world.enforce-gamemode", Boolean.class)
.comment("")
.comment("Sets whether Multiverse will should enforce gamemode on world change.")
.comment("If enabled, players will be forced into the gamemode of the world they are entering, unless they have")
@ -65,14 +72,14 @@ public class MVCoreConfigNodes {
.name("enforce-gamemode")
.build());
public static final MVValueNode<Boolean> AUTO_PURGE_ENTITIES = node(MVValueNode.builder("world.auto-purge-entities", Boolean.class)
public final MVValueNode<Boolean> AUTO_PURGE_ENTITIES = node(MVValueNode.builder("world.auto-purge-entities", Boolean.class)
.comment("")
.comment("Sets whether Multiverse will purge mobs and entities with be automatically.")
.defaultValue(false)
.name("auto-purge-entities")
.build());
public static final MVValueNode<Boolean> TELEPORT_INTERCEPT = node(MVValueNode.builder("world.teleport-intercept", Boolean.class)
public final MVValueNode<Boolean> TELEPORT_INTERCEPT = node(MVValueNode.builder("world.teleport-intercept", Boolean.class)
.comment("")
.comment("If this is set to true, Multiverse will enforce access permissions for all teleportation,")
.comment("including teleportation from other plugins.")
@ -80,12 +87,12 @@ public class MVCoreConfigNodes {
.name("teleport-intercept")
.build());
private static final MVCommentedNode SPAWN_HEADER = node(MVCommentedNode.builder("spawn")
private final MVCommentedNode SPAWN_HEADER = node(MVCommentedNode.builder("spawn")
.comment("")
.comment("")
.build());
public static final MVValueNode<Boolean> FIRST_SPAWN_OVERRIDE = node(MVValueNode.builder("spawn.first-spawn-override", Boolean.class)
public final MVValueNode<Boolean> FIRST_SPAWN_OVERRIDE = node(MVValueNode.builder("spawn.first-spawn-override", Boolean.class)
.comment("Sets whether Multiverse will override the first spawn location of a world.")
.comment("If enabled, Multiverse will set the first spawn location of a world to the spawn location of the world.")
.comment("If disabled, it will default to server.properties settings.")
@ -93,7 +100,7 @@ public class MVCoreConfigNodes {
.name("first-spawn-override")
.build());
public static final MVValueNode<String> FIRST_SPAWN_LOCATION = node(MVValueNode.builder("spawn.first-spawn-location", String.class)
public final MVValueNode<String> FIRST_SPAWN_LOCATION = node(MVValueNode.builder("spawn.first-spawn-location", String.class)
.comment("")
.comment("Sets the world that Multiverse will use as the location for players that first join the server.")
.comment("This only applies if first-spawn-override is set to true.")
@ -101,19 +108,19 @@ public class MVCoreConfigNodes {
.name("first-spawn-location")
.build());
private static final MVCommentedNode PORTAL_HEADER = node(MVCommentedNode.builder("portal")
private final MVCommentedNode PORTAL_HEADER = node(MVCommentedNode.builder("portal")
.comment("")
.comment("")
.build());
public static final MVValueNode<Boolean> USE_CUSTOM_PORTAL_SEARCH = node(MVValueNode.builder("portal.use-custom-portal-search", Boolean.class)
public final MVValueNode<Boolean> USE_CUSTOM_PORTAL_SEARCH = node(MVValueNode.builder("portal.use-custom-portal-search", Boolean.class)
.comment("This config option defines whether or not Multiverse should interfere with's Bukkit's default portal search radius.")
.comment("Setting it to false would mean you want to simply let Bukkit decides the search radius itself.")
.defaultValue(false)
.name("use-custom-portal-search")
.build());
public static final MVValueNode<Integer> CUSTOM_PORTAL_SEARCH_RADIUS = node(MVValueNode.builder("portal.custom-portal-search-radius", Integer.class)
public final MVValueNode<Integer> CUSTOM_PORTAL_SEARCH_RADIUS = node(MVValueNode.builder("portal.custom-portal-search-radius", Integer.class)
.comment("")
.comment("This config option defines the search radius Multiverse should use when searching for a portal.")
.comment("This only applies if use-custom-portal-search is set to true.")
@ -122,19 +129,19 @@ public class MVCoreConfigNodes {
.validator(value -> value >= 0)
.build());
private static final MVCommentedNode MESSAGING_HEADER = node(MVCommentedNode.builder("messaging")
private final MVCommentedNode MESSAGING_HEADER = node(MVCommentedNode.builder("messaging")
.comment("")
.comment("")
.build());
public static final MVValueNode<Boolean> ENABLE_CHAT_PREFIX = node(MVValueNode.builder("messaging.enable-chat-prefix", Boolean.class)
public final MVValueNode<Boolean> ENABLE_CHAT_PREFIX = node(MVValueNode.builder("messaging.enable-chat-prefix", Boolean.class)
.comment("This config option defines whether or not Multiverse should prefix the chat with the world name.")
.comment("This only applies if use-custom-portal-search is set to true.")
.defaultValue(false)
.name("enable-chat-prefix")
.build());
public static final MVValueNode<String> CHAT_PREFIX_FORMAT = node(MVValueNode.builder("messaging.chat-prefix-format", String.class)
public final MVValueNode<String> CHAT_PREFIX_FORMAT = node(MVValueNode.builder("messaging.chat-prefix-format", String.class)
.comment("")
.comment("This config option defines the format Multiverse should use when prefixing the chat with the world name.")
.comment("This only applies if enable-chat-prefix is set to true.")
@ -142,7 +149,7 @@ public class MVCoreConfigNodes {
.name("chat-prefix-format")
.build());
public static final MVValueNode<Boolean> REGISTER_PAPI_HOOK = node(MVValueNode.builder("messaging.register-papi-hook", Boolean.class)
public final MVValueNode<Boolean> REGISTER_PAPI_HOOK = node(MVValueNode.builder("messaging.register-papi-hook", Boolean.class)
.comment("")
.comment("This config option defines whether or not Multiverse should register the PlaceholderAPI hook.")
.comment("This only applies if PlaceholderAPI is installed.")
@ -150,12 +157,12 @@ public class MVCoreConfigNodes {
.name("register-papi-hook")
.build());
private static final MVCommentedNode MISC_HEADER = node(MVCommentedNode.builder("misc")
private final MVCommentedNode MISC_HEADER = node(MVCommentedNode.builder("misc")
.comment("")
.comment("")
.build());
public static final MVValueNode<Integer> GLOBAL_DEBUG = node(MVValueNode.builder("misc.global-debug", Integer.class)
public final MVValueNode<Integer> GLOBAL_DEBUG = node(MVValueNode.builder("misc.global-debug", Integer.class)
.comment("This is our debug flag to help identify issues with Multiverse.")
.comment("If you are having issues with Multiverse, please set this to 3 and then post your log to pastebin.com")
.comment("Otherwise, there's no need to touch this. If not instructed by a wiki page or developer.")
@ -166,10 +173,10 @@ public class MVCoreConfigNodes {
.defaultValue(0)
.name("global-debug")
.validator(value -> value >= 0 && value <= 3)
.onSetValue((oldValue, newValue) -> Logging.setDebugLevel(newValue))
.onSetValue((oldValue, newValue) -> Logging.setDebugLevel(newValue)) //TODO Debug event
.build());
public static final MVValueNode<Boolean> SILENT_START = node(MVValueNode.builder("misc.silent-start", Boolean.class)
public final MVValueNode<Boolean> SILENT_START = node(MVValueNode.builder("misc.silent-start", Boolean.class)
.comment("")
.comment("If true, the startup console messages will no longer show.")
.defaultValue(false)
@ -177,14 +184,14 @@ public class MVCoreConfigNodes {
.onSetValue((oldValue, newValue) -> Logging.setShowingConfig(!newValue))
.build());
public static final MVValueNode<Boolean> SHOW_DONATION_MESSAGE = node(MVValueNode.builder("misc.show-donation-message", Boolean.class)
public final MVValueNode<Boolean> SHOW_DONATION_MESSAGE = node(MVValueNode.builder("misc.show-donation-message", Boolean.class)
.comment("")
.comment("If you don't want to donate, you can set this to false and Multiverse will stop nagging you.")
.defaultValue(true)
.name("show-donation-message")
.build());
public static final MVValueNode<Double> VERSION = node(MVValueNode.builder("version", Double.class)
public final MVValueNode<Double> VERSION = node(MVValueNode.builder("version", Double.class)
.comment("")
.comment("")
.comment("This just signifies the version number so we can see what version of config you have.")