mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-02-28 18:21:03 +01:00
(BROKEN) Swapped PlanConfig to use the new Config
This commit is contained in:
parent
2fcb9590e5
commit
224003029f
@ -16,8 +16,8 @@
|
||||
*/
|
||||
package com.djrapitops.plan.data.plugin;
|
||||
|
||||
import com.djrapitops.plan.system.settings.config.ConfigNode;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
package com.djrapitops.plan.system.info.server;
|
||||
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
import com.djrapitops.plugin.config.Config;
|
||||
import com.djrapitops.plan.system.settings.config.Config;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
@ -16,11 +16,8 @@
|
||||
*/
|
||||
package com.djrapitops.plan.system.settings.changes;
|
||||
|
||||
import com.djrapitops.plugin.config.Config;
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collection;
|
||||
import com.djrapitops.plan.system.settings.config.Config;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigNode;
|
||||
|
||||
/**
|
||||
* Represents a change made to the config structure.
|
||||
@ -46,11 +43,7 @@ public interface ConfigChange {
|
||||
|
||||
@Override
|
||||
public void apply(Config config) {
|
||||
ConfigNode newNode = config.getConfigNode(newPath);
|
||||
ConfigNode oldNode = config.getConfigNode(oldPath);
|
||||
newNode.copyDefaults(oldNode);
|
||||
newNode.set(oldNode.getString());
|
||||
removeNode(oldNode);
|
||||
config.moveChild(oldPath, newPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -72,7 +65,7 @@ public interface ConfigChange {
|
||||
public void apply(Config config) {
|
||||
ConfigNode newNode = config.getConfigNode(newPath);
|
||||
ConfigNode oldNode = config.getConfigNode(oldPath);
|
||||
newNode.copyDefaults(oldNode);
|
||||
newNode.copyMissing(oldNode);
|
||||
newNode.set(oldNode.getString());
|
||||
}
|
||||
|
||||
@ -96,26 +89,7 @@ public interface ConfigChange {
|
||||
|
||||
@Override
|
||||
public synchronized void apply(Config config) {
|
||||
ConfigNode node = config.getConfigNode(oldPath);
|
||||
removeNode(node);
|
||||
}
|
||||
|
||||
void removeNode(ConfigNode node) {
|
||||
ConfigNode parent = node.getParent();
|
||||
String key = node.getKey(false);
|
||||
parent.getChildren().remove(key);
|
||||
|
||||
// Remove the child from child order, since it is otherwise trying to save null.
|
||||
try {
|
||||
Field field = ConfigNode.class.getDeclaredField("childOrder");
|
||||
field.setAccessible(true);
|
||||
Collection<String> childOrder = (Collection<String>) field.get(parent);
|
||||
childOrder.remove(key);
|
||||
field.set(parent, childOrder);
|
||||
field.setAccessible(false);
|
||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||
throw new IllegalStateException("Error removing config node", e);
|
||||
}
|
||||
config.removeNode(oldPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
package com.djrapitops.plan.system.settings.changes;
|
||||
|
||||
import com.djrapitops.plugin.config.Config;
|
||||
import com.djrapitops.plan.system.settings.config.Config;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
|
@ -24,12 +24,14 @@
|
||||
package com.djrapitops.plan.system.settings.config;
|
||||
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
import org.apache.commons.text.TextStringBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Configuration utility for storing settings in a .yml file.
|
||||
@ -60,6 +62,11 @@ public class Config extends ConfigNode {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Config(File configFile, Iterable<String> defaults) {
|
||||
this(configFile, new ConfigReader(new Scanner(new TextStringBuilder().appendWithSeparators(defaults, "\n").toString())).readAndClose());
|
||||
}
|
||||
|
||||
public Config(File configFile, ConfigNode defaults) {
|
||||
this(configFile);
|
||||
copyMissing(defaults);
|
||||
@ -76,7 +83,9 @@ public class Config extends ConfigNode {
|
||||
}
|
||||
|
||||
public void read() throws IOException {
|
||||
copyMissing(new ConfigReader(Files.newInputStream(configFilePath)).read());
|
||||
try (ConfigReader reader = new ConfigReader(Files.newInputStream(configFilePath))) {
|
||||
copyMissing(reader.read());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -23,6 +23,8 @@
|
||||
*/
|
||||
package com.djrapitops.plan.system.settings.config;
|
||||
|
||||
import org.apache.commons.text.TextStringBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
@ -60,7 +62,7 @@ public class ConfigNode {
|
||||
}
|
||||
|
||||
public Optional<ConfigNode> getNode(String path) {
|
||||
String[] parts = path.split("\\.", 2);
|
||||
String[] parts = splitPathInTwo(path);
|
||||
String key = parts[0];
|
||||
String leftover = parts[1];
|
||||
|
||||
@ -71,10 +73,28 @@ public class ConfigNode {
|
||||
}
|
||||
}
|
||||
|
||||
private String[] splitPathInTwo(String path) {
|
||||
String[] split = path.split("\\.", 2);
|
||||
if (split.length <= 1) {
|
||||
return new String[]{split[0], ""};
|
||||
}
|
||||
return split;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public ConfigNode getConfigNode(String path) {
|
||||
return getNode(path).orElse(null);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public boolean contains(String path) {
|
||||
return getNode(path).isPresent();
|
||||
}
|
||||
|
||||
protected void addNode(String path) {
|
||||
ConfigNode newParent = this;
|
||||
if (!path.isEmpty()) {
|
||||
String[] parts = path.split("\\.", 2);
|
||||
String[] parts = splitPathInTwo(path);
|
||||
String key = parts[0];
|
||||
String leftover = parts[1];
|
||||
|
||||
@ -207,6 +227,11 @@ public class ConfigNode {
|
||||
return new ConfigValueParser.IntegerParser().compose(value);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Integer getInt() {
|
||||
return getInteger();
|
||||
}
|
||||
|
||||
public Long getLong() {
|
||||
return new ConfigValueParser.LongParser().compose(value);
|
||||
}
|
||||
@ -215,18 +240,23 @@ public class ConfigNode {
|
||||
return new ConfigValueParser.StringParser().compose(value);
|
||||
}
|
||||
|
||||
public boolean isTrue() {
|
||||
public boolean getBoolean() {
|
||||
return new ConfigValueParser.BooleanParser().compose(value);
|
||||
}
|
||||
|
||||
public List<String> getStringList(String path) {
|
||||
return getNode(path).map(ConfigNode::getStringList).orElse(new ArrayList<>());
|
||||
return getNode(path).map(ConfigNode::getStringList).orElse(Collections.emptyList());
|
||||
}
|
||||
|
||||
public Integer getInteger(String path) {
|
||||
return getNode(path).map(ConfigNode::getInteger).orElse(null);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Integer getInt(String path) {
|
||||
return getInteger(path);
|
||||
}
|
||||
|
||||
public Long getLong(String path) {
|
||||
return getNode(path).map(ConfigNode::getLong).orElse(null);
|
||||
}
|
||||
@ -235,8 +265,8 @@ public class ConfigNode {
|
||||
return getNode(path).map(ConfigNode::getString).orElse(null);
|
||||
}
|
||||
|
||||
public boolean isTrue(String path) {
|
||||
return getNode(path).map(ConfigNode::isTrue).orElse(false);
|
||||
public boolean getBoolean(String path) {
|
||||
return getNode(path).map(ConfigNode::getBoolean).orElse(false);
|
||||
}
|
||||
|
||||
public void copyMissing(ConfigNode from) {
|
||||
@ -263,4 +293,31 @@ public class ConfigNode {
|
||||
protected int getNodeDepth() {
|
||||
return parent != null ? parent.getNodeDepth() + 1 : 0;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Map<String, ConfigNode> getChildren() {
|
||||
return childNodes;
|
||||
}
|
||||
|
||||
public ConfigNode getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void copyDefaults(ConfigNode from) {
|
||||
copyMissing(from);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void copyDefaults(List<String> from) throws IOException {
|
||||
Scanner linesScanner = new Scanner(new TextStringBuilder().appendWithSeparators(from, "\n").toString());
|
||||
try (ConfigReader reader = new ConfigReader(linesScanner)) {
|
||||
copyMissing(reader.read());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ import java.util.Scanner;
|
||||
public class ConfigReader implements Closeable {
|
||||
|
||||
private static final ConfigValueParser.StringParser STRING_PARSER = new ConfigValueParser.StringParser();
|
||||
private final InputStream in;
|
||||
private InputStream in;
|
||||
private final Scanner scanner;
|
||||
private ConfigNode previousNode;
|
||||
private ConfigNode parent;
|
||||
@ -41,6 +41,23 @@ public class ConfigReader implements Closeable {
|
||||
this.scanner = new Scanner(in);
|
||||
}
|
||||
|
||||
public ConfigReader(Scanner scanner) {
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Config readAndClose() {
|
||||
try {
|
||||
return read();
|
||||
} finally {
|
||||
try {
|
||||
close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Config read() {
|
||||
Config config = new Config();
|
||||
|
||||
@ -166,6 +183,8 @@ public class ConfigReader implements Closeable {
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
scanner.close();
|
||||
in.close();
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,8 +20,6 @@ import com.djrapitops.plan.data.plugin.PluginsConfigSection;
|
||||
import com.djrapitops.plan.system.settings.network.NetworkSettings;
|
||||
import com.djrapitops.plan.system.settings.paths.TimeSettings;
|
||||
import com.djrapitops.plan.system.settings.paths.key.Setting;
|
||||
import com.djrapitops.plugin.config.Config;
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
@ -24,7 +24,6 @@ import com.djrapitops.plan.system.processing.Processing;
|
||||
import com.djrapitops.plan.system.settings.paths.DisplaySettings;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatters;
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.djrapitops.plan.system.settings.paths;
|
||||
|
||||
import com.djrapitops.plan.system.settings.config.ConfigNode;
|
||||
import com.djrapitops.plan.system.settings.paths.key.BooleanSetting;
|
||||
import com.djrapitops.plan.system.settings.paths.key.IntegerSetting;
|
||||
import com.djrapitops.plan.system.settings.paths.key.Setting;
|
||||
import com.djrapitops.plan.system.settings.paths.key.StringSetting;
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
|
||||
/**
|
||||
* {@link Setting} values that are in "Display_options" section.
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.djrapitops.plan.system.settings.paths.key;
|
||||
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigNode;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@ -21,6 +21,6 @@ public class BooleanSetting extends Setting<Boolean> {
|
||||
|
||||
@Override
|
||||
public Boolean getValueFrom(ConfigNode node) {
|
||||
return node.contains(path) && node.getBoolean(path);
|
||||
return node.getNode(path).map(ConfigNode::getBoolean).orElse(false);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package com.djrapitops.plan.system.settings.paths.key;
|
||||
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigNode;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@ -21,6 +21,6 @@ public class IntegerSetting extends Setting<Integer> {
|
||||
|
||||
@Override
|
||||
public Integer getValueFrom(ConfigNode node) {
|
||||
return node.contains(path) ? node.getInt(path) : null;
|
||||
return node.getInteger(path);
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@
|
||||
package com.djrapitops.plan.system.settings.paths.key;
|
||||
|
||||
import com.djrapitops.plan.data.store.Type;
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigNode;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
|
@ -1,9 +1,8 @@
|
||||
package com.djrapitops.plan.system.settings.paths.key;
|
||||
|
||||
import com.djrapitops.plan.data.store.Type;
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigNode;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@ -24,6 +23,6 @@ public class StringListSetting extends Setting<List<String>> {
|
||||
|
||||
@Override
|
||||
public List<String> getValueFrom(ConfigNode node) {
|
||||
return node.contains(path) ? node.getStringList(path) : Collections.emptyList();
|
||||
return node.getStringList(path);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package com.djrapitops.plan.system.settings.paths.key;
|
||||
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigNode;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@ -21,6 +21,6 @@ public class StringSetting extends Setting<String> {
|
||||
|
||||
@Override
|
||||
public String getValueFrom(ConfigNode node) {
|
||||
return node.contains(path) ? node.getString(path) : null;
|
||||
return node.getString(path);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package com.djrapitops.plan.system.settings.paths.key;
|
||||
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigNode;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Predicate;
|
||||
|
@ -17,10 +17,10 @@
|
||||
package com.djrapitops.plan.system.settings.theme;
|
||||
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
import com.djrapitops.plan.system.settings.config.Config;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigNode;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.system.settings.paths.DisplaySettings;
|
||||
import com.djrapitops.plugin.config.Config;
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -16,8 +16,9 @@
|
||||
*/
|
||||
package com.djrapitops.plan.system.settings.changes;
|
||||
|
||||
import com.djrapitops.plugin.config.Config;
|
||||
import com.djrapitops.plugin.config.ConfigNode;
|
||||
import com.djrapitops.plan.system.settings.config.Config;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigNode;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigReader;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@ -26,8 +27,10 @@ import org.junit.runner.RunWith;
|
||||
import org.junitpioneer.jupiter.TempDirectory;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@ -46,7 +49,11 @@ class ConfigChangeTest {
|
||||
}
|
||||
|
||||
private Config prepareConfig(String withValue) {
|
||||
return new Config(temporaryFolder.resolve("config.yml").toFile(), Collections.singletonList(withValue));
|
||||
try (ConfigReader reader = new ConfigReader(new Scanner(withValue))) {
|
||||
return new Config(temporaryFolder.resolve("config.yml").toFile(), reader.read());
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.system.settings.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
class ConfigNodeTest {
|
||||
|
||||
@Test
|
||||
void rootNodeReturnsDepthZero() {
|
||||
assertEquals(0, new ConfigNode(null, null, null).getNodeDepth());
|
||||
}
|
||||
|
||||
}
|
@ -317,7 +317,10 @@
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.1</version>
|
||||
<version>3.0.0-M2</version>
|
||||
<configuration>
|
||||
<trimStackTrace>false</trimStackTrace>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
Loading…
Reference in New Issue
Block a user