Implemented SettingsService

This commit is contained in:
Rsl1122 2019-09-02 16:40:18 +03:00
parent 0f63ffe859
commit 5d3bc4c95d
5 changed files with 195 additions and 3 deletions

View File

@ -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')

View File

@ -56,9 +56,13 @@ enum Capability {
*/
DATA_EXTENSION_SHOW_IN_PLAYER_TABLE,
/**
*
* QueryService and CommonQueries
*/
QUERY_API;
QUERY_API,
/**
* SettingsService
*/
SETTINGS_API;
static Optional<Capability> getByName(String name) {
if (name == null) {

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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.
* <p>
* 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 "<Plugin_name>.Some_setting"}
* <p>
* 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<String> 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<Integer> 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<String> getStringList(String path, Supplier<List<String>> defaultValue);
class SettingsServiceHolder {
static SettingsService service;
private SettingsServiceHolder() {
/* Static variable holder */
}
static void set(SettingsService service) {
SettingsServiceHolder.service = service;
}
}
}

View File

@ -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;
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String> defaultValue) {
String pluginPath = getPluginPath(path);
Optional<ConfigNode> node = config.getNode(pluginPath);
if (node.isPresent()) {
return node.get().getString();
} else {
set(pluginPath, defaultValue);
return config.getString(pluginPath);
}
}
public <T> void set(String pluginPath, Supplier<T> 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<Integer> defaultValue) {
String pluginPath = getPluginPath(path);
Optional<ConfigNode> node = config.getNode(pluginPath);
if (node.isPresent()) {
return node.get().getInteger();
} else {
set(pluginPath, defaultValue);
return config.getInteger(pluginPath);
}
}
@Override
public List<String> getStringList(String path, Supplier<List<String>> defaultValue) {
String pluginPath = getPluginPath(path);
Optional<ConfigNode> 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;
}
}