From 5d3bc4c95d8da6589da55b37c8aabe2f589b0e2b Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 2 Sep 2019 16:40:18 +0300 Subject: [PATCH] Implemented SettingsService --- Plan/api/build.gradle | 2 +- .../plan/capability/Capability.java | 8 +- .../plan/settings/SettingsService.java | 79 +++++++++++++ .../java/com/djrapitops/plan/PlanSystem.java | 5 + .../SettingsServiceImplementation.java | 104 ++++++++++++++++++ 5 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 Plan/api/src/main/java/com/djrapitops/plan/settings/SettingsService.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/settings/SettingsServiceImplementation.java diff --git a/Plan/api/build.gradle b/Plan/api/build.gradle index f3808b767..a9f124074 100644 --- a/Plan/api/build.gradle +++ b/Plan/api/build.gradle @@ -2,7 +2,7 @@ plugins { id "com.jfrog.bintray" version "1.8.4" } -ext.apiVersion = '5.0-R0.2' +ext.apiVersion = '5.0-R0.3' bintray { user = System.getenv('BINTRAY_USER') diff --git a/Plan/api/src/main/java/com/djrapitops/plan/capability/Capability.java b/Plan/api/src/main/java/com/djrapitops/plan/capability/Capability.java index 1d5aa0936..0ee8c5d7f 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/capability/Capability.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/capability/Capability.java @@ -56,9 +56,13 @@ enum Capability { */ DATA_EXTENSION_SHOW_IN_PLAYER_TABLE, /** - * + * QueryService and CommonQueries */ - QUERY_API; + QUERY_API, + /** + * SettingsService + */ + SETTINGS_API; static Optional getByName(String name) { if (name == null) { diff --git a/Plan/api/src/main/java/com/djrapitops/plan/settings/SettingsService.java b/Plan/api/src/main/java/com/djrapitops/plan/settings/SettingsService.java new file mode 100644 index 000000000..136ed9729 --- /dev/null +++ b/Plan/api/src/main/java/com/djrapitops/plan/settings/SettingsService.java @@ -0,0 +1,79 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.settings; + +import java.util.List; +import java.util.Optional; +import java.util.function.Supplier; + +/** + * Service for defining plugin specific settings to the Plan config. + *

+ * All given paths will be prepended with "Plugins." to place the settings in the plugins config section. + * It is recommended to use setting paths {@code ".Some_setting"} + *

+ * Requires Capability SETTINGS_API + * + * @author Rsl1122 + */ +public interface SettingsService { + + static SettingsService getInstance() { + return Optional.ofNullable(SettingsServiceHolder.service) + .orElseThrow(() -> new IllegalStateException("SettingsService has not been initialised yet.")); + } + + /** + * Get a String from the config or the default value. + * + * @param path Path in the config + * @param defaultValue Supplier for the default value, {@code () -> "Example"}. + * @return value in the config + */ + String getString(String path, Supplier defaultValue); + + /** + * Get a Integer from the config or the default value. + * + * @param path Path in the config + * @param defaultValue Supplier for the default value, {@code () -> 500}. + * @return value in the config + */ + Integer getInteger(String path, Supplier defaultValue); + + /** + * Get a String list from the config or the default value. + * + * @param path Path in the config + * @param defaultValue Supplier for the default value, {@code () -> Arrays.asList("Example", "Another")}. + * @return value in the config + */ + List getStringList(String path, Supplier> defaultValue); + + class SettingsServiceHolder { + static SettingsService service; + + private SettingsServiceHolder() { + /* Static variable holder */ + } + + static void set(SettingsService service) { + SettingsServiceHolder.service = service; + } + } + +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/PlanSystem.java b/Plan/common/src/main/java/com/djrapitops/plan/PlanSystem.java index b0bdf20bc..a9bcb1d66 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/PlanSystem.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/PlanSystem.java @@ -32,6 +32,7 @@ import com.djrapitops.plan.identification.ServerInfo; import com.djrapitops.plan.processing.Processing; import com.djrapitops.plan.query.QueryServiceImplementation; import com.djrapitops.plan.settings.ConfigSystem; +import com.djrapitops.plan.settings.SettingsServiceImplementation; import com.djrapitops.plan.settings.locale.LocaleSystem; import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.queries.objects.ServerQueries; @@ -73,6 +74,7 @@ public class PlanSystem implements SubSystem { private final DeliveryUtilities deliveryUtilities; private final ExtensionServiceImplementation extensionService; private final QueryServiceImplementation queryService; + private final SettingsServiceImplementation settingsService; private final ErrorHandler errorHandler; @Inject @@ -93,6 +95,7 @@ public class PlanSystem implements SubSystem { DeliveryUtilities deliveryUtilities, ExtensionServiceImplementation extensionService, QueryServiceImplementation queryService, + SettingsServiceImplementation settingsService, ErrorHandler errorHandler ) { this.files = files; @@ -111,6 +114,7 @@ public class PlanSystem implements SubSystem { this.deliveryUtilities = deliveryUtilities; this.extensionService = extensionService; this.queryService = queryService; + this.settingsService = settingsService; this.errorHandler = errorHandler; } @@ -145,6 +149,7 @@ public class PlanSystem implements SubSystem { ); queryService.register(); extensionService.register(); + settingsService.register(); enabled = true; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/settings/SettingsServiceImplementation.java b/Plan/common/src/main/java/com/djrapitops/plan/settings/SettingsServiceImplementation.java new file mode 100644 index 000000000..fad43ef5f --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/settings/SettingsServiceImplementation.java @@ -0,0 +1,104 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.settings; + +import com.djrapitops.plan.settings.config.ConfigNode; +import com.djrapitops.plan.settings.config.PlanConfig; +import com.djrapitops.plugin.logging.L; +import com.djrapitops.plugin.logging.error.ErrorHandler; + +import javax.inject.Inject; +import javax.inject.Singleton; +import java.io.IOException; +import java.util.List; +import java.util.Optional; +import java.util.function.Supplier; + +/** + * Implementation for {@link SettingsService}. + * + * @author Rsl1122 + */ +@Singleton +public class SettingsServiceImplementation implements SettingsService { + + private final PlanConfig config; + private final ErrorHandler errorHandler; + + @Inject + public SettingsServiceImplementation( + PlanConfig config, + ErrorHandler errorHandler + ) { + this.config = config; + this.errorHandler = errorHandler; + } + + public void register() { + SettingsService.SettingsServiceHolder.set(this); + } + + @Override + public String getString(String path, Supplier defaultValue) { + String pluginPath = getPluginPath(path); + Optional node = config.getNode(pluginPath); + if (node.isPresent()) { + return node.get().getString(); + } else { + set(pluginPath, defaultValue); + return config.getString(pluginPath); + } + } + + public void set(String pluginPath, Supplier defaultValue) { + config.set(pluginPath, defaultValue.get()); + + try { + config.save(); + } catch (IOException e) { + errorHandler.log(L.ERROR, this.getClass(), e); + } + } + + @Override + public Integer getInteger(String path, Supplier defaultValue) { + String pluginPath = getPluginPath(path); + Optional node = config.getNode(pluginPath); + if (node.isPresent()) { + return node.get().getInteger(); + } else { + set(pluginPath, defaultValue); + return config.getInteger(pluginPath); + } + } + + @Override + public List getStringList(String path, Supplier> defaultValue) { + String pluginPath = getPluginPath(path); + Optional node = config.getNode(pluginPath); + if (node.isPresent()) { + return node.get().getStringList(); + } else { + set(pluginPath, defaultValue); + return config.getStringList(pluginPath); + } + } + + private String getPluginPath(String path) { + return "Plugin." + path; + } +} \ No newline at end of file