From 15173c83695dda477c2d50a6d8a42458d323cb44 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Thu, 3 Feb 2022 13:38:14 +0000 Subject: [PATCH] Finally added eco.yml --- .../java/com/willfp/eco/core/EcoPlugin.java | 39 +++++++++++--- .../com/willfp/eco/core/EcoPluginProps.java | 52 +++++++++++++++++++ .../eco/core/config/TransientConfig.java | 12 +++++ .../com/willfp/eco/internal/EcoPropsParser.kt | 22 ++++++++ .../eco/internal/config/EcoConfigFactory.kt | 2 +- .../willfp/eco/internal/spigot/EcoHandler.kt | 10 +++- .../eco/internal/spigot/EcoSpigotPlugin.kt | 7 +-- .../core-plugin/src/main/resources/eco.yml | 5 ++ 8 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 eco-api/src/main/java/com/willfp/eco/core/EcoPluginProps.java create mode 100644 eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/EcoPropsParser.kt create mode 100644 eco-core/core-plugin/src/main/resources/eco.yml diff --git a/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java b/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java index 512752ef..034946fc 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java +++ b/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java @@ -1,6 +1,7 @@ package com.willfp.eco.core; import com.willfp.eco.core.command.impl.PluginCommand; +import com.willfp.eco.core.config.TransientConfig; import com.willfp.eco.core.config.base.ConfigYml; import com.willfp.eco.core.config.base.LangYml; import com.willfp.eco.core.config.updating.ConfigHandler; @@ -152,10 +153,13 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike { private final ProxyFactory proxyFactory; /** - * Create a new plugin without a specified color, proxy support, polymart, or bStats. + * Create a new plugin. + *

+ * Will read from eco.yml (like plugin.yml) to fetch values that would otherwise be passed + * into the constructor. */ protected EcoPlugin() { - this("&f"); + this((EcoPluginProps) null); } /** @@ -236,6 +240,23 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike { @NotNull final String proxyPackage, @NotNull final String color, final boolean supportingExtensions) { + this( + new EcoPluginProps( + resourceId, + bStatsId, + proxyPackage, + color, + supportingExtensions + ) + ); + } + + /** + * Create a new plugin. + * + * @param pluginProps The props. If left null, it will read from eco.yml. + */ + protected EcoPlugin(@Nullable final EcoPluginProps pluginProps) { /* The handler must be initialized before any plugin's constructors are called, as the constructors call Eco#getHandler(). @@ -272,11 +293,15 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike { assert Eco.getHandler() != null; - this.resourceId = resourceId; - this.bStatsId = bStatsId; - this.proxyPackage = proxyPackage; - this.color = color; - this.supportingExtensions = supportingExtensions; + EcoPluginProps props = pluginProps != null ? pluginProps : EcoPluginProps.fromConfig( + new TransientConfig(this.getClass().getResourceAsStream("eco.yml")) + ); + + this.resourceId = props.resourceId(); + this.bStatsId = props.bStatsId(); + this.proxyPackage = props.proxyPackage(); + this.color = props.color(); + this.supportingExtensions = props.supportingExtensions(); this.scheduler = Eco.getHandler().createScheduler(this); this.eventManager = Eco.getHandler().createEventManager(this); diff --git a/eco-api/src/main/java/com/willfp/eco/core/EcoPluginProps.java b/eco-api/src/main/java/com/willfp/eco/core/EcoPluginProps.java new file mode 100644 index 00000000..18334a8c --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/EcoPluginProps.java @@ -0,0 +1,52 @@ +package com.willfp.eco.core; + +import com.willfp.eco.core.config.interfaces.Config; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +@ApiStatus.Internal +public record EcoPluginProps(int resourceId, + int bStatsId, + @NotNull String proxyPackage, + @NotNull String color, + boolean supportingExtensions) { + /** + * The parser for config props. + */ + private static PropsParser configParser = null; + + /** + * Load props from config. + * + * @param config The config. + * @return The props. + */ + public static EcoPluginProps fromConfig(@NotNull final Config config) { + return configParser.parseFrom(config); + } + + /** + * Initialize the parser for eco.yml. + * + * @param parser The parser. + */ + @ApiStatus.Internal + public static void setConfigParser(@NotNull final PropsParser parser) { + configParser = parser; + } + + /** + * Parse arguments into props for a plugin. + * + * @param The type of source. + */ + public interface PropsParser { + /** + * Parse props from a given source. + * + * @param source The source. + * @return The props. + */ + EcoPluginProps parseFrom(@NotNull T source); + } +} diff --git a/eco-api/src/main/java/com/willfp/eco/core/config/TransientConfig.java b/eco-api/src/main/java/com/willfp/eco/core/config/TransientConfig.java index dc11610b..fb70d740 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/config/TransientConfig.java +++ b/eco-api/src/main/java/com/willfp/eco/core/config/TransientConfig.java @@ -5,7 +5,10 @@ import com.willfp.eco.core.config.interfaces.Config; import com.willfp.eco.core.config.wrapper.ConfigWrapper; import org.bukkit.configuration.file.YamlConfiguration; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.Map; /** @@ -21,6 +24,15 @@ public class TransientConfig extends ConfigWrapper { super(Eco.getHandler().getConfigFactory().createConfig(config)); } + /** + * @param stream The InputStream. + */ + public TransientConfig(@Nullable final InputStream stream) { + super(stream != null ? Eco.getHandler().getConfigFactory().createConfig(YamlConfiguration.loadConfiguration( + new InputStreamReader(stream) + )) : new TransientConfig()); + } + /** * Create a new empty transient config. * diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/EcoPropsParser.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/EcoPropsParser.kt new file mode 100644 index 00000000..7ce4f776 --- /dev/null +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/EcoPropsParser.kt @@ -0,0 +1,22 @@ +package com.willfp.eco.internal + +import com.willfp.eco.core.EcoPluginProps +import com.willfp.eco.core.config.interfaces.Config + +class EcoPropsParser : EcoPluginProps.PropsParser { + override fun parseFrom(config: Config): EcoPluginProps { + val resourceId = config.getIntOrNull("resource-id") ?: 0 + val bstatsId = config.getIntOrNull("bstats-id") ?: 0 + val proxyPackage = config.getStringOrNull("proxy-package") ?: "" + val color = config.getStringOrNull("color") ?: "&f" + val supportsExtensions = config.getBoolOrNull("supports-extensions") ?: false + + return EcoPluginProps( + resourceId, + bstatsId, + proxyPackage, + color, + supportsExtensions + ) + } +} \ No newline at end of file diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/EcoConfigFactory.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/EcoConfigFactory.kt index 500de65f..55ca8374 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/EcoConfigFactory.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/EcoConfigFactory.kt @@ -15,7 +15,7 @@ import com.willfp.eco.internal.config.yaml.EcoYamlConfigSection import org.bukkit.configuration.file.YamlConfiguration import java.io.StringReader -class EcoConfigFactory : ConfigFactory { +object EcoConfigFactory : ConfigFactory { override fun createConfig(config: YamlConfiguration): Config { return EcoYamlConfigSection(config) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt index 4a0fdf83..ed4e2084 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt @@ -1,10 +1,12 @@ package com.willfp.eco.internal.spigot import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.EcoPluginProps import com.willfp.eco.core.Handler import com.willfp.eco.core.fast.FastItemStack import com.willfp.eco.core.integrations.placeholder.PlaceholderIntegration import com.willfp.eco.internal.EcoCleaner +import com.willfp.eco.internal.EcoPropsParser import com.willfp.eco.internal.Plugins import com.willfp.eco.internal.config.EcoConfigFactory import com.willfp.eco.internal.config.updating.EcoConfigHandler @@ -95,7 +97,7 @@ class EcoHandler : EcoSpigotPlugin(), Handler { } override fun getConfigFactory(): EcoConfigFactory { - return EcoConfigFactory() + return EcoConfigFactory } override fun getDropQueueFactory(): EcoDropQueueFactory { @@ -163,4 +165,10 @@ class EcoHandler : EcoSpigotPlugin(), Handler { @Suppress("DEPRECATION") return keyFactory?.create(namespace, key) ?: NamespacedKey(namespace, key) } + + companion object { + init { + EcoPluginProps.setConfigParser(EcoPropsParser()) + } + } } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt index 7345bdfe..a1647c05 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt @@ -120,12 +120,7 @@ import org.bukkit.Material import org.bukkit.event.Listener import org.bukkit.inventory.ItemStack -abstract class EcoSpigotPlugin : EcoPlugin( - 773, - 10043, - "com.willfp.eco.internal.spigot.proxy", - "&a" -) { +abstract class EcoSpigotPlugin : EcoPlugin() { init { Items.registerArgParser(ArgParserEnchantment()) Items.registerArgParser(ArgParserColor()) diff --git a/eco-core/core-plugin/src/main/resources/eco.yml b/eco-core/core-plugin/src/main/resources/eco.yml new file mode 100644 index 00000000..388bae5f --- /dev/null +++ b/eco-core/core-plugin/src/main/resources/eco.yml @@ -0,0 +1,5 @@ +resource-id: 773 +bstats-id: 10043 +proxy-package: com.willfp.eco.internal.spigot.proxy +color: '&a' +supports-extensions: false \ No newline at end of file