Added recursive JSON Get/Set and tests
This commit is contained in:
@@ -13,6 +13,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@SuppressWarnings({"unchecked", "unused"})
|
||||
public abstract class JSONConfigWrapper implements Config {
|
||||
@@ -28,6 +29,12 @@ public abstract class JSONConfigWrapper implements Config {
|
||||
@Getter
|
||||
private final Map<String, Object> values = new HashMap<>();
|
||||
|
||||
/**
|
||||
* All cached values.
|
||||
*/
|
||||
@Getter
|
||||
private final Map<String, Object> cache = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Abstract config.
|
||||
*/
|
||||
@@ -42,12 +49,45 @@ public abstract class JSONConfigWrapper implements Config {
|
||||
|
||||
@Override
|
||||
public final void clearCache() {
|
||||
// Do nothing.
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean has(@NotNull final String path) {
|
||||
return values.containsKey(path);
|
||||
return getOfKnownType(path, Object.class) != null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected <T> T getOfKnownType(@NotNull final String path,
|
||||
@NotNull final Class<T> clazz) {
|
||||
return getOfKnownType(path, clazz, true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected <T> T getOfKnownType(@NotNull final String path,
|
||||
@NotNull final Class<T> clazz,
|
||||
final boolean isBase) {
|
||||
String closestPath = path;
|
||||
|
||||
if (cache.containsKey(path) && isBase) {
|
||||
return (T) cache.get(path);
|
||||
}
|
||||
|
||||
if (path.contains(".")) {
|
||||
String[] split = path.split("\\.");
|
||||
closestPath = split[0];
|
||||
}
|
||||
|
||||
if (values.get(closestPath) instanceof Map && !path.equals(closestPath)) {
|
||||
JSONConfigSection section = new JSONConfigSection((Map<String, Object>) values.get(closestPath));
|
||||
return section.getOfKnownType(path.substring(closestPath.length() + 1), clazz, false);
|
||||
} else {
|
||||
if (values.containsKey(closestPath)) {
|
||||
return (T) values.get(closestPath);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -59,13 +99,33 @@ public abstract class JSONConfigWrapper implements Config {
|
||||
@Override
|
||||
@Nullable
|
||||
public Object get(@NotNull final String path) {
|
||||
return values.get(path);
|
||||
return getOfKnownType(path, Object.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@NotNull final String path,
|
||||
@Nullable final Object object) {
|
||||
values.put(path, object);
|
||||
setRecursively(path, object, true);
|
||||
clearCache();
|
||||
}
|
||||
|
||||
protected void setRecursively(@NotNull final String path,
|
||||
@Nullable final Object object,
|
||||
final boolean isBase) {
|
||||
String closestPath = path;
|
||||
|
||||
if (path.contains(".")) {
|
||||
String[] split = path.split("\\.");
|
||||
closestPath = split[0];
|
||||
}
|
||||
|
||||
if (values.get(closestPath) instanceof Map && !path.equals(closestPath)) {
|
||||
JSONConfigSection section = new JSONConfigSection((Map<String, Object>) values.get(closestPath));
|
||||
section.setRecursively(path.substring(closestPath.length() + 1), object, false);
|
||||
values.put(closestPath, section.getValues());
|
||||
} else {
|
||||
values.put(path, object);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -89,12 +149,7 @@ public abstract class JSONConfigWrapper implements Config {
|
||||
|
||||
@Override
|
||||
public int getInt(@NotNull final String path) {
|
||||
if (values.containsKey(path)) {
|
||||
return (int) values.get(path);
|
||||
} else {
|
||||
values.put(path, 0);
|
||||
return getInt(path);
|
||||
}
|
||||
return Objects.requireNonNullElse(getOfKnownType(path, Integer.class), 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -110,23 +165,13 @@ public abstract class JSONConfigWrapper implements Config {
|
||||
@Override
|
||||
public int getInt(@NotNull final String path,
|
||||
final int def) {
|
||||
if (values.containsKey(path)) {
|
||||
return (int) values.get(path);
|
||||
} else {
|
||||
values.put(path, def);
|
||||
return getInt(path);
|
||||
}
|
||||
return Objects.requireNonNullElse(getOfKnownType(path, Integer.class), def);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<Integer> getInts(@NotNull final String path) {
|
||||
if (values.containsKey(path)) {
|
||||
return (List<Integer>) values.get(path);
|
||||
} else {
|
||||
values.put(path, 0);
|
||||
return getInts(path);
|
||||
}
|
||||
return (List<Integer>) Objects.requireNonNullElse(getOfKnownType(path, Object.class), new ArrayList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -141,12 +186,7 @@ public abstract class JSONConfigWrapper implements Config {
|
||||
|
||||
@Override
|
||||
public boolean getBool(@NotNull final String path) {
|
||||
if (values.containsKey(path)) {
|
||||
return (Boolean) values.get(path);
|
||||
} else {
|
||||
values.put(path, 0);
|
||||
return getBool(path);
|
||||
}
|
||||
return Objects.requireNonNullElse(getOfKnownType(path, Boolean.class), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -162,12 +202,7 @@ public abstract class JSONConfigWrapper implements Config {
|
||||
@Override
|
||||
@NotNull
|
||||
public List<Boolean> getBools(@NotNull final String path) {
|
||||
if (values.containsKey(path)) {
|
||||
return (List<Boolean>) values.get(path);
|
||||
} else {
|
||||
values.put(path, 0);
|
||||
return getBools(path);
|
||||
}
|
||||
return (List<Boolean>) Objects.requireNonNullElse(getOfKnownType(path, Object.class), new ArrayList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -183,12 +218,7 @@ public abstract class JSONConfigWrapper implements Config {
|
||||
@Override
|
||||
@NotNull
|
||||
public String getString(@NotNull final String path) {
|
||||
if (values.containsKey(path)) {
|
||||
return (String) values.get(path);
|
||||
} else {
|
||||
values.put(path, 0);
|
||||
return getString(path);
|
||||
}
|
||||
return Objects.requireNonNullElse(getOfKnownType(path, String.class), "");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -204,12 +234,7 @@ public abstract class JSONConfigWrapper implements Config {
|
||||
@Override
|
||||
@NotNull
|
||||
public List<String> getStrings(@NotNull final String path) {
|
||||
if (values.containsKey(path)) {
|
||||
return (List<String>) values.get(path);
|
||||
} else {
|
||||
values.put(path, 0);
|
||||
return getStrings(path);
|
||||
}
|
||||
return (List<String>) Objects.requireNonNullElse(getOfKnownType(path, Object.class), new ArrayList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -224,12 +249,7 @@ public abstract class JSONConfigWrapper implements Config {
|
||||
|
||||
@Override
|
||||
public double getDouble(@NotNull final String path) {
|
||||
if (values.containsKey(path)) {
|
||||
return (double) values.get(path);
|
||||
} else {
|
||||
values.put(path, 0);
|
||||
return getDouble(path);
|
||||
}
|
||||
return Objects.requireNonNullElse(getOfKnownType(path, Double.class), 0D);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -245,12 +265,7 @@ public abstract class JSONConfigWrapper implements Config {
|
||||
@Override
|
||||
@NotNull
|
||||
public List<Double> getDoubles(@NotNull final String path) {
|
||||
if (values.containsKey(path)) {
|
||||
return (List<Double>) values.get(path);
|
||||
} else {
|
||||
values.put(path, 0);
|
||||
return getDoubles(path);
|
||||
}
|
||||
return (List<Double>) Objects.requireNonNullElse(getOfKnownType(path, Object.class), new ArrayList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.willfp.eco.internal.config;
|
||||
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
@@ -40,31 +39,10 @@ public interface LoadableConfig {
|
||||
*/
|
||||
File getConfigFile();
|
||||
|
||||
/**
|
||||
* Get the plugin.
|
||||
*
|
||||
* @return The plugin.
|
||||
*/
|
||||
EcoPlugin getPlugin();
|
||||
|
||||
/**
|
||||
* Get the config name (including extension)
|
||||
*
|
||||
* @return The name.
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* Get the subdirectory path.
|
||||
*
|
||||
* @return The path.
|
||||
*/
|
||||
String getSubDirectoryPath();
|
||||
|
||||
/**
|
||||
* Get the source/provider.
|
||||
*
|
||||
* @return The source.
|
||||
*/
|
||||
Class<?> getSource();
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public abstract class LoadableJsonConfig extends JSONConfigWrapper implements Lo
|
||||
/**
|
||||
* The physical config file, as stored on disk.
|
||||
*/
|
||||
@Getter(AccessLevel.PROTECTED)
|
||||
@Getter
|
||||
private final File configFile;
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,7 +20,7 @@ public abstract class LoadableYamlConfig extends ConfigWrapper<YamlConfiguration
|
||||
/**
|
||||
* The physical config file, as stored on disk.
|
||||
*/
|
||||
@Getter(AccessLevel.PROTECTED)
|
||||
@Getter
|
||||
private final File configFile;
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,7 @@ import com.willfp.eco.proxy.proxies.BlockBreakProxy;
|
||||
import com.willfp.eco.proxy.proxies.SkullProxy;
|
||||
import com.willfp.eco.proxy.proxies.TridentStackProxy;
|
||||
import com.willfp.eco.spigot.config.DataYml;
|
||||
import com.willfp.eco.spigot.config.TestJson;
|
||||
import com.willfp.eco.spigot.display.PacketAutoRecipe;
|
||||
import com.willfp.eco.spigot.display.PacketChat;
|
||||
import com.willfp.eco.spigot.display.PacketOpenWindowMerchant;
|
||||
@@ -26,7 +27,11 @@ import com.willfp.eco.spigot.eventlisteners.DispenserArmorListener;
|
||||
import com.willfp.eco.spigot.eventlisteners.EntityDeathByEntityListeners;
|
||||
import com.willfp.eco.spigot.eventlisteners.NaturalExpGainListeners;
|
||||
import com.willfp.eco.spigot.gui.GUIListener;
|
||||
import com.willfp.eco.spigot.integrations.anticheat.*;
|
||||
import com.willfp.eco.spigot.integrations.anticheat.AnticheatAAC;
|
||||
import com.willfp.eco.spigot.integrations.anticheat.AnticheatMatrix;
|
||||
import com.willfp.eco.spigot.integrations.anticheat.AnticheatNCP;
|
||||
import com.willfp.eco.spigot.integrations.anticheat.AnticheatSpartan;
|
||||
import com.willfp.eco.spigot.integrations.anticheat.AnticheatVulcan;
|
||||
import com.willfp.eco.spigot.integrations.antigrief.AntigriefCombatLogX;
|
||||
import com.willfp.eco.spigot.integrations.antigrief.AntigriefFactionsUUID;
|
||||
import com.willfp.eco.spigot.integrations.antigrief.AntigriefGriefPrevention;
|
||||
@@ -60,6 +65,8 @@ public class EcoSpigotPlugin extends EcoPlugin {
|
||||
*/
|
||||
private final DataYml dataYml;
|
||||
|
||||
private final TestJson testJson;
|
||||
|
||||
/**
|
||||
* Create a new instance of eco.
|
||||
*/
|
||||
@@ -79,6 +86,8 @@ public class EcoSpigotPlugin extends EcoPlugin {
|
||||
|
||||
this.dataYml = new DataYml(this);
|
||||
Data.init(this.dataYml);
|
||||
|
||||
this.testJson = new TestJson(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -90,6 +99,7 @@ public class EcoSpigotPlugin extends EcoPlugin {
|
||||
public void disable() {
|
||||
try {
|
||||
Data.save(this.dataYml);
|
||||
testJson.save();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -107,7 +117,7 @@ public class EcoSpigotPlugin extends EcoPlugin {
|
||||
|
||||
@Override
|
||||
public void postLoad() {
|
||||
|
||||
this.getLogger().info(testJson.get("quiz.sport.q1.options").toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.willfp.eco.spigot.config;
|
||||
|
||||
import com.willfp.eco.core.config.JsonStaticBaseConfig;
|
||||
import com.willfp.eco.spigot.EcoSpigotPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TestJson extends JsonStaticBaseConfig {
|
||||
/**
|
||||
* Init data.yml.
|
||||
*
|
||||
* @param plugin EcoSpigotPlugin.
|
||||
*/
|
||||
public TestJson(@NotNull final EcoSpigotPlugin plugin) {
|
||||
super("test", plugin);
|
||||
}
|
||||
}
|
||||
38
eco-core/core-plugin/src/main/resources/test.json
Normal file
38
eco-core/core-plugin/src/main/resources/test.json
Normal file
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"quiz": {
|
||||
"sport": {
|
||||
"q1": {
|
||||
"question": "Which one is correct team name in NBA?",
|
||||
"options": [
|
||||
"New York Bulls",
|
||||
"Los Angeles Kings",
|
||||
"Golden State Warriros",
|
||||
"Huston Rocket"
|
||||
],
|
||||
"answer": "Huston Rocket"
|
||||
}
|
||||
},
|
||||
"maths": {
|
||||
"q1": {
|
||||
"question": "5 + 7 = ?",
|
||||
"options": [
|
||||
"10",
|
||||
"11",
|
||||
"12",
|
||||
"13"
|
||||
],
|
||||
"answer": "12"
|
||||
},
|
||||
"q2": {
|
||||
"question": "12 - 8 = ?",
|
||||
"options": [
|
||||
"1",
|
||||
"2",
|
||||
"3",
|
||||
"4"
|
||||
],
|
||||
"answer": "4"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
version = 5.4.1
|
||||
version = 5.5.0
|
||||
plugin-name = eco
|
||||
Reference in New Issue
Block a user